From 410a5b341f4b6246fcebdeab18251f85f3f1bda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 29 Feb 2020 21:02:42 +0100 Subject: api: add functions string_hash_binary and string_hash --- tests/unit/core/test-core-secure.cpp | 22 +------- tests/unit/core/test-core-string.cpp | 99 ++++++++++++++++++++++++++++++++++++ tests/unit/core/test-core.h | 45 ++++++++++++++++ 3 files changed, 145 insertions(+), 21 deletions(-) create mode 100644 tests/unit/core/test-core.h (limited to 'tests/unit/core') diff --git a/tests/unit/core/test-core-secure.cpp b/tests/unit/core/test-core-secure.cpp index cd8891515..2fe178b44 100644 --- a/tests/unit/core/test-core-secure.cpp +++ b/tests/unit/core/test-core-secure.cpp @@ -25,30 +25,10 @@ extern "C" { #include #include +#include "tests/unit/core/test-core.h" #include "src/core/wee-secure.h" #include "src/core/wee-string.h" -#define DATA_HASH "this is a test of hash function" -#define DATA_HASH_MD5 "1197d121af621ac6a63cb8ef6b5dfa30" -#define DATA_HASH_SHA1 "799d818061175b400dc5aaeb14b8d32cdef32ff0" -#define DATA_HASH_SHA224 "637d21f3ba3f4e9fa9fb889dc990b31a658cb37b4aefb5144" \ - "70b016d" -#define DATA_HASH_SHA256 "b9a4c3393dfac4330736684510378851e581c68add8eca841" \ - "10c31a33e694676" -#define DATA_HASH_SHA384 "42853280be9b8409eed265f272bd580e2fbd448b7c7e236c7" \ - "f37dafec7906d51d982dc84ec70a4733eca49d86ac19455" -#define DATA_HASH_SHA512 "4469190d4e0d1fdc0afb6f408d9873c89b8ce89cc4db79fe0" \ - "58255c55ad6821fa5e9bb068f9e578c8ae7cc825d85ff99c439d59e439bc589d95620a" \ - "1e6b8ae6e" -#define DATA_HASH_SHA3_224 "26432a3a4ea998790be43386b1de417f88be43146a4af98" \ - "2a9627d10" -#define DATA_HASH_SHA3_256 "226e3830306711cf653c1661765c304b37038e7457c35dd" \ - "14fca0f6a8ba1d2e3" -#define DATA_HASH_SHA3_384 "77bc16f89c102efc783ddeccc71862fe919b66e1aaa88bd" \ - "2ba5f0bbe604fcb86c68f0e401d5d553597366cdd400595ba" -#define DATA_HASH_SHA3_512 "31dfb5fc8f30ac7007acddc4fce562d408706833d0d2af2" \ - "e5f61a179099592927ff7d100e278406c7f98d42575001e26e153b135c21f7ef5b00c8" \ - "cef93ca048d" #define SECURE_PASSPHRASE "this_is_a_secret_passphrase" #define SECURE_PASSWORD "this_is_a_secret_password" #define TOTP_SECRET "secretpasswordbase32" diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index 24801e29c..0c39e5204 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -32,6 +32,7 @@ extern "C" #include #include #include "tests/tests.h" +#include "tests/unit/core/test-core.h" #include "src/core/weechat.h" #include "src/core/wee-string.h" #include "src/core/wee-hashtable.h" @@ -107,6 +108,47 @@ extern "C" STRCMP_EQUAL(__result, str); \ free (str); +#define WEE_CHECK_HASH_BIN(__result, __buffer, __length, __hash_algo) \ + if (__result) \ + { \ + result_bin = (char *)malloc (4096); \ + length_bin = string_base16_decode (__result, \ + (char *)result_bin); \ + } \ + else \ + { \ + result_bin = NULL; \ + length_bin = 0; \ + } \ + string_hash_binary (__buffer, __length, __hash_algo, \ + &hash_bin, &length_hash_bin); \ + if (__result == NULL) \ + { \ + POINTERS_EQUAL(NULL, hash_bin); \ + } \ + else \ + { \ + MEMCMP_EQUAL(result_bin, hash_bin, length_hash_bin); \ + } \ + LONGS_EQUAL(length_bin, length_hash_bin); \ + if (result_bin) \ + free (result_bin); \ + if (hash_bin) \ + free (hash_bin); + +#define WEE_CHECK_HASH_HEX(__result, __buffer, __length, __hash_algo) \ + hash = string_hash (__buffer, __length, __hash_algo); \ + if (__result == NULL) \ + { \ + POINTERS_EQUAL(NULL, hash); \ + } \ + else \ + { \ + STRCMP_EQUAL(__result, hash); \ + } \ + if (hash) \ + free (hash); + extern struct t_hashtable *string_hashtable_shared; TEST_GROUP(CoreString) @@ -1910,6 +1952,63 @@ TEST(CoreString, Hex_dump) str); } +/* + * Tests functions: + * string_hash_binary + * string_hash + */ + +TEST(CoreString, Hash) +{ + const char *data = DATA_HASH; + char *result_bin, *hash_bin, *hash; + int length, length_bin, length_hash_bin; + + length = strlen (data); + + WEE_CHECK_HASH_BIN(NULL, NULL, 0, NULL); + WEE_CHECK_HASH_HEX(NULL, NULL, 0, NULL); + + WEE_CHECK_HASH_BIN(NULL, DATA_HASH, 0, NULL); + WEE_CHECK_HASH_HEX(NULL, DATA_HASH, 0, NULL); + + WEE_CHECK_HASH_BIN(NULL, DATA_HASH, length, NULL); + WEE_CHECK_HASH_HEX(NULL, DATA_HASH, length, NULL); + + WEE_CHECK_HASH_BIN(NULL, DATA_HASH, length, "not_an_algo"); + WEE_CHECK_HASH_HEX(NULL, DATA_HASH, length, "not_an_algo"); + + WEE_CHECK_HASH_BIN(DATA_HASH_MD5, data, length, "md5"); + WEE_CHECK_HASH_HEX(DATA_HASH_MD5, data, length, "md5"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA1, data, length, "sha1"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA1, data, length, "sha1"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA224, data, length, "sha224"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA224, data, length, "sha224"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA256, data, length, "sha256"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA256, data, length, "sha256"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA384, data, length, "sha384"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA384, data, length, "sha384"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA512, data, length, "sha512"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA512, data, length, "sha512"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_224, data, length, "sha3-224"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_224, data, length, "sha3-224"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_256, data, length, "sha3-256"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_256, data, length, "sha3-256"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_384, data, length, "sha3-384"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_384, data, length, "sha3-384"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_512, data, length, "sha3-512"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_512, data, length, "sha3-512"); +} + /* * Tests functions: * string_is_command_char diff --git a/tests/unit/core/test-core.h b/tests/unit/core/test-core.h new file mode 100644 index 000000000..ace1e55aa --- /dev/null +++ b/tests/unit/core/test-core.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#ifndef WEECHAT_TEST_UNIT_CORE_H +#define WEECHAT_TEST_UNIT_CORE_H + +#define DATA_HASH "this is a test of hash function" +#define DATA_HASH_MD5 "1197d121af621ac6a63cb8ef6b5dfa30" +#define DATA_HASH_SHA1 "799d818061175b400dc5aaeb14b8d32cdef32ff0" +#define DATA_HASH_SHA224 "637d21f3ba3f4e9fa9fb889dc990b31a658cb37b4aefb5144" \ + "70b016d" +#define DATA_HASH_SHA256 "b9a4c3393dfac4330736684510378851e581c68add8eca841" \ + "10c31a33e694676" +#define DATA_HASH_SHA384 "42853280be9b8409eed265f272bd580e2fbd448b7c7e236c7" \ + "f37dafec7906d51d982dc84ec70a4733eca49d86ac19455" +#define DATA_HASH_SHA512 "4469190d4e0d1fdc0afb6f408d9873c89b8ce89cc4db79fe0" \ + "58255c55ad6821fa5e9bb068f9e578c8ae7cc825d85ff99c439d59e439bc589d95620a" \ + "1e6b8ae6e" +#define DATA_HASH_SHA3_224 "26432a3a4ea998790be43386b1de417f88be43146a4af98" \ + "2a9627d10" +#define DATA_HASH_SHA3_256 "226e3830306711cf653c1661765c304b37038e7457c35dd" \ + "14fca0f6a8ba1d2e3" +#define DATA_HASH_SHA3_384 "77bc16f89c102efc783ddeccc71862fe919b66e1aaa88bd" \ + "2ba5f0bbe604fcb86c68f0e401d5d553597366cdd400595ba" +#define DATA_HASH_SHA3_512 "31dfb5fc8f30ac7007acddc4fce562d408706833d0d2af2" \ + "e5f61a179099592927ff7d100e278406c7f98d42575001e26e153b135c21f7ef5b00c8" \ + "cef93ca048d" + +#endif /* WEECHAT_TEST_UNIT_CORE_H */ -- cgit v1.2.3