diff options
Diffstat (limited to 'common.h')
-rw-r--r-- | common.h | 53 |
1 files changed, 47 insertions, 6 deletions
@@ -108,13 +108,16 @@ static int run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_size, struct testcase *testcases, size_t ntestcases, char hexsum[]) { - struct libhashsum_hasher hasher; + struct libhashsum_hasher hasher, hasher2; + unsigned char custom_hashbuffer[2048]; + unsigned char custom_hashbuffer2[sizeof(custom_hashbuffer)]; + int use_custom_hashbuffer = 0; char *input, *p, bitstr[sizeof(" + b\"1234567\"")]; unsigned char extra_bit; int ok = 1, caseok; size_t i, j, input_string_len, input_total_len, input_size; size_t bits; - for (i = 0; i < ntestcases; i++) { + for (i = 0; i < ntestcases; i++, use_custom_hashbuffer ^= 1) { if (libhashsum_init_hasher(&hasher, algorithm)) { perror("libhashsum_init_hasher"); return 2; @@ -177,17 +180,47 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz } if (bits) *p = testcases[i].input[input_string_len]; - if (hasher.finalise(&hasher, input, input_total_len, (unsigned)bits, input_size)) { - perror("hasher.finalise"); + hasher.hash_output = use_custom_hashbuffer ? custom_hashbuffer : NULL; + if (hasher.finalise_const(&hasher, input, input_total_len, (unsigned)bits)) { + perror("hasher.finalise_const"); return 2; } if (!hasher.hash_output) { - fprintf(stderr, "hasher.finalise did not set hasher.hash_output\n"); + fprintf(stderr, "hasher.finalise_const did not set hasher.hash_output\n"); return 2; } - free(input); +#ifndef NO_CUSTOM_HASH_BUFFER_SUPPORT + if (use_custom_hashbuffer && hasher.hash_output != custom_hashbuffer) { + fprintf(stderr, "hasher.finalise_const did keep custom hash buffer\n"); + return 2; + } +#endif hex(hexsum, hasher.hash_output, hasher.hash_size); ok &= caseok = !testcases[i].output || !strcmp(hexsum, testcases[i].output); + if (libhashsum_init_hasher(&hasher2, algorithm)) { + perror("libhashsum_init_hasher"); + return 2; + } + hasher2.hash_output = use_custom_hashbuffer ? custom_hashbuffer2 : NULL; + if (hasher2.finalise(&hasher2, input, input_total_len, (unsigned)bits, input_size)) { + perror("hasher.finalise"); + return 2; + } + if (!hasher2.hash_output) { + fprintf(stderr, "hasher.finalise did not set hasher.hash_output\n"); + return 2; + } +#ifndef NO_CUSTOM_HASH_BUFFER_SUPPORT + if (use_custom_hashbuffer && hasher2.hash_output != custom_hashbuffer2) { + fprintf(stderr, "hasher.finalise did keep custom hash buffer\n"); + return 2; + } +#endif + if (memcmp(hasher2.hash_output, hasher.hash_output, hasher.hash_size)) { + fprintf(stderr, "hasher.finalise and hasher.finalise_const produced different hashes\n"); + return 2; + } + free(input); input = escape(testcases[i].input, input_string_len); bitstr[0] = '\0'; if (bits) { @@ -229,6 +262,14 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz return run_tests(NAME, LIBHASHSUM_##ID, LIBHASHSUM_##ID##_HASH_SIZE,\ testcases, sizeof(testcases) / sizeof(*testcases), hexsum) +# define ASSERT(ASSERTION)\ + do {\ + if ((ASSERTION))\ + break;\ + fprintf(stderr, "assertion `%s` at line %i failed\n", #ASSERTION, __LINE__);\ + exit(2);\ + } while (0) + #endif |