From 9510a538c2f13ca67fa5967288c4eda3cce99367 Mon Sep 17 00:00:00 2001 From: Jan-Niklas Burfeind Date: Thu, 9 Aug 2018 11:00:00 +0200 Subject: [PATCH] tests: Add torture_hashes for pubkey hashes Signed-off-by: Jan-Niklas Burfeind Reviewed-by: Andreas Schneider --- tests/unittests/CMakeLists.txt | 2 + tests/unittests/torture_hashes.c | 118 +++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tests/unittests/torture_hashes.c diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index 264bc4be..d585cada 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -32,6 +32,8 @@ target_compile_options(torture_isipaddr PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) add_cmocka_test(torture_knownhosts_parsing torture_knownhosts_parsing.c ${TORTURE_LIBRARY}) target_compile_options(torture_knownhosts_parsing PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) +add_cmocka_test(torture_hashes torture_hashes.c ${TORTURE_LIBRARY}) +target_compile_options(torture_hashes PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) if (CMAKE_USE_PTHREADS_INIT) add_cmocka_test(torture_rand torture_rand.c ${TORTURE_LIBRARY}) diff --git a/tests/unittests/torture_hashes.c b/tests/unittests/torture_hashes.c new file mode 100644 index 00000000..ed84342b --- /dev/null +++ b/tests/unittests/torture_hashes.c @@ -0,0 +1,118 @@ +#include "config.h" + +#define LIBSSH_STATIC + +#include "torture.h" +#include "torture_key.h" +#include "legacy.c" +#include "dh.c" + +static int setup_rsa_key(void **state) +{ + int rc=0; + enum ssh_keytypes_e type; + char *b64_key, *p; + ssh_key key; + + const char *q; + + b64_key = strdup(torture_get_testkey_pub(SSH_KEYTYPE_RSA, 0)); + assert_true(b64_key != NULL); + + q = p = b64_key; + while (*p != ' ') p++; + *p = '\0'; + + type = ssh_key_type_from_name(q); + assert_true(type == SSH_KEYTYPE_RSA); + + q = ++p; + while (*p != ' ') p++; + *p = '\0'; + + rc = ssh_pki_import_pubkey_base64(q, type, &key); + assert_true(rc == 0); + + free(b64_key); + *state = key; + + return 0; +} + +static int teardown(void **state) +{ + ssh_key_free(*state); + return 0; +} + +static void torture_md5_hash(void **state) +{ + ssh_key pubkey = *state; + unsigned char *hash = NULL; + char *hexa = NULL; + size_t hlen; + int rc = 0; + + rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5, &hash, &hlen); + assert_true(rc == 0); + + hexa = ssh_get_hexa(hash, hlen); + assert_string_equal(hexa, + "50:15:a0:9b:92:bf:33:1c:01:c5:8c:fe:18:fa:ce:78"); + + ssh_string_free_char(hexa); +} + +static void torture_sha1_hash(void **state) +{ + ssh_key pubkey = *state; + unsigned char *hash = NULL; + char *sha1 = NULL; + int rc = 0; + size_t hlen; + + rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen); + assert_true(rc == 0); + + sha1 = ssh_get_b64_unpadded(hash, hlen); + assert_string_equal(sha1, "6wP+houujQmxLBiFugTcoeoODCM"); + + ssh_string_free_char(sha1); +} + +static void torture_sha256_hash(void **state) +{ + ssh_key pubkey = *state; + unsigned char *hash = NULL; + char *sha256 = NULL; + int rc = 0; + size_t hlen; + + rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256, &hash, &hlen); + assert_true(rc == 0); + + sha256 = ssh_get_b64_unpadded(hash, hlen); + assert_string_equal(sha256, "jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII"); + + ssh_string_free_char(sha256); + +} + +int torture_run_tests(void) { + int rc; + struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(torture_md5_hash, + setup_rsa_key, + teardown), + cmocka_unit_test_setup_teardown(torture_sha1_hash, + setup_rsa_key, + teardown), + cmocka_unit_test_setup_teardown(torture_sha256_hash, + setup_rsa_key, + teardown), + }; + + torture_filter_tests(tests); + rc = cmocka_run_group_tests(tests, NULL, NULL); + return rc; +}