From: Arne Schwabe Date: Fri, 5 Jun 2020 11:25:19 +0000 (+0200) Subject: Add unit test for cipher name translations X-Git-Tag: v2.5_beta1~110 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bc12aefd571ab24543cff4cba2f0e25d3268941;p=thirdparty%2Fopenvpn.git Add unit test for cipher name translations The unit test duplicates some part of the test for the ncp-cipher list but that is not a bad thing. Signed-off-by: Arne Schwabe Acked-by: Gert Doering Message-Id: <20200605112519.22714-3-arne@rfc2549.org> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg19968.html Signed-off-by: Gert Doering --- diff --git a/tests/unit_tests/openvpn/test_crypto.c b/tests/unit_tests/openvpn/test_crypto.c index fdf814de2..ea9b99b2c 100644 --- a/tests/unit_tests/openvpn/test_crypto.c +++ b/tests/unit_tests/openvpn/test_crypto.c @@ -69,11 +69,79 @@ crypto_pem_encode_decode_loopback(void **state) gc_free(&gc); } +static void +test_translate_cipher(const char *ciphername, const char *openvpn_name) +{ + const cipher_kt_t *cipher = cipher_kt_get(ciphername); + + /* Empty cipher is fine */ + if (!cipher) + { + return; + } + + const char *kt_name = cipher_kt_name(cipher); + + assert_string_equal(kt_name, openvpn_name); +} + +static void +test_cipher_names(const char *ciphername, const char *openvpn_name) +{ + struct gc_arena gc = gc_new(); + /* Go through some variants, if the cipher library accepts these, they + * should be normalised to the openvpn name */ + char *upper = string_alloc(ciphername, &gc); + char *lower = string_alloc(ciphername, &gc); + char *random_case = string_alloc(ciphername, &gc); + + for (int i = 0; i < strlen(ciphername); i++) + { + upper[i] = toupper(ciphername[i]); + lower[i] = tolower(ciphername[i]); + if (rand() & 0x1) + { + random_case[i] = upper[i]; + } + else + { + random_case[i] = lower[i]; + } + } + + if (!openvpn_name) + { + openvpn_name = upper; + } + + test_translate_cipher(upper, openvpn_name); + test_translate_cipher(lower, openvpn_name); + test_translate_cipher(random_case, openvpn_name); + test_translate_cipher(ciphername, openvpn_name); + + + gc_free(&gc); +} + +static void +crypto_translate_cipher_names(void **state) +{ + /* Test that a number of ciphers to see that they turn out correctly */ + test_cipher_names("BF-CBC", NULL); + test_cipher_names("BLOWFISH-CBC", "BF-CBC"); + test_cipher_names("Chacha20-Poly1305", NULL); + test_cipher_names("AES-128-GCM", NULL); + test_cipher_names("AES-128-CBC", NULL); + test_cipher_names("CAMELLIA-128-CFB128", "CAMELLIA-128-CFB"); + test_cipher_names("id-aes256-GCM", "AES-256-GCM"); +} + int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(crypto_pem_encode_decode_loopback), + cmocka_unit_test(crypto_translate_cipher_names), }; #if defined(ENABLE_CRYPTO_OPENSSL)