summaryrefslogtreecommitdiff
path: root/tests/unit/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2022-08-16 21:25:30 +0200
committerSébastien Helleu <flashcode@flashtux.org>2022-08-16 21:33:50 +0200
commit0090695f7d79efa4f50398b55bc59317d1de1d75 (patch)
tree9c1adbf669c3cdca080bd8aea4cbb9a119bb2d18 /tests/unit/core
parente61441081520bcada6de699229ce9af241302665 (diff)
downloadweechat-0090695f7d79efa4f50398b55bc59317d1de1d75.zip
api: add function crypto_hash_file
Diffstat (limited to 'tests/unit/core')
-rw-r--r--tests/unit/core/test-core-crypto.cpp69
1 files changed, 67 insertions, 2 deletions
diff --git a/tests/unit/core/test-core-crypto.cpp b/tests/unit/core/test-core-crypto.cpp
index acda37669..4f7833ec6 100644
--- a/tests/unit/core/test-core-crypto.cpp
+++ b/tests/unit/core/test-core-crypto.cpp
@@ -23,6 +23,8 @@
extern "C"
{
+#include <unistd.h>
+#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <gcrypt.h>
@@ -111,6 +113,27 @@ extern "C"
} \
LONGS_EQUAL(hash_size_expected, hash_size);
+#define WEE_CHECK_HASH_FILE(__result_code, __result_hash, \
+ __filename, __hash_algo) \
+ if (__result_hash) \
+ { \
+ hash_size_expected = string_base16_decode (__result_hash, \
+ hash_expected); \
+ } \
+ else \
+ { \
+ hash_size_expected = 0; \
+ } \
+ hash_size = -1; \
+ LONGS_EQUAL(__result_code, \
+ weecrypto_hash_file (__filename, __hash_algo, \
+ hash, &hash_size)); \
+ if (__result_hash) \
+ { \
+ MEMCMP_EQUAL(hash_expected, hash, hash_size); \
+ } \
+ LONGS_EQUAL(hash_size_expected, hash_size);
+
#define WEE_CHECK_HASH_PBKDF2(__result_code, __result_hash, \
__data, __data_size, \
__hash_algo, __salt, __salt_size, \
@@ -223,8 +246,8 @@ TEST(CoreCrypto, Hash)
data_size = strlen (data);
- WEE_CHECK_HASH(0, NULL, NULL, 0, 0);
- WEE_CHECK_HASH(0, NULL, "test", 0, 0);
+ WEE_CHECK_HASH(0, NULL, NULL, 0, GCRY_MD_SHA256);
+ WEE_CHECK_HASH(0, NULL, "test", 0, GCRY_MD_SHA256);
LONGS_EQUAL (0, weecrypto_hash (data, data_size, GCRY_MD_SHA256,
NULL, NULL));
@@ -246,6 +269,48 @@ TEST(CoreCrypto, Hash)
/*
* Tests functions:
+ * weecrypto_hash_file
+ */
+
+TEST(CoreCrypto, HashFile)
+{
+ const char *data = DATA_HASH_MSG;
+ char *filename, hash_expected[4096], hash[4096];
+ FILE *file;
+ int hash_size_expected, hash_size;
+
+ filename = string_eval_path_home ("${weechat_data_dir}/test_file.txt",
+ NULL, NULL, NULL);
+ file = fopen (filename, "w");
+ fwrite (data, 1, strlen (data), file);
+ fflush (file);
+ fclose (file);
+
+ WEE_CHECK_HASH_FILE(0, NULL, NULL, GCRY_MD_SHA256);
+
+ LONGS_EQUAL (0, weecrypto_hash_file (filename, GCRY_MD_SHA256,
+ NULL, NULL));
+
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_CRC32, filename, GCRY_MD_CRC32);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_MD5, filename, GCRY_MD_MD5);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA1, filename, GCRY_MD_SHA1);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA224, filename, GCRY_MD_SHA224);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA256, filename, GCRY_MD_SHA256);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA384, filename, GCRY_MD_SHA384);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA512, filename, GCRY_MD_SHA512);
+#if GCRYPT_VERSION_NUMBER >= 0x010700
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA3_224, filename, GCRY_MD_SHA3_224);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA3_256, filename, GCRY_MD_SHA3_256);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA3_384, filename, GCRY_MD_SHA3_384);
+ WEE_CHECK_HASH_FILE(1, DATA_HASH_SHA3_512, filename, GCRY_MD_SHA3_512);
+#endif
+
+ unlink (filename);
+ free (filename);
+}
+
+/*
+ * Tests functions:
* weecrypto_hash_pbkdf2
*/