#include "../src/lib.c" #include #include static int test_hash(void) { int rc = -1; fprintf(stderr, "hash():\n"); FILE *f = NULL; size_t read_count; const size_t iterations = 100U; u8 *input = NULL; f = fopen("/dev/urandom", "r"); if (f == NULL) { perror("fopen()"); goto out; } u8 key[SIPHASH_KEY_LENGTH]; read_count = fread(key, sizeof(u8), sizeof(key), f); if (ferror(f)) { perror("fread()"); goto out; } assert(read_count == sizeof(key)); time_t t = time(NULL); if (t == (time_t)-1) { perror("time(NULL)"); } srand((unsigned int)t); { fprintf( stderr, "\033[0;33mtesting\033[0m: hash() === SipHash-2-4-128 %ld times...", iterations ); u8 given[SIPHASH_OUTPUT_LENGTH]; u8 expected[SIPHASH_OUTPUT_LENGTH]; for (u8 i = 0; i < iterations; i++) { const size_t input_size = ((unsigned int)abs(rand())) % 256; input = malloc(input_size); if (input == NULL) { perror("malloc()"); goto out; } read_count = fread(input, sizeof(u8), input_size, f); if (ferror(f)) { perror("fread()"); goto out; } assert(read_count == input_size); siphash(key, input_size, input, given); siphash_impl(input, input_size, key, expected, SIPHASH_OUTPUT_LENGTH); assert(memcmp(given, expected, SIPHASH_OUTPUT_LENGTH) == 0); free(input); input = NULL; } fprintf(stderr, " \033[0;32mOK\033[0m\n"); } rc = 0; out: if (f != NULL) { if (fclose(f)) { perror("fclose(f)"); rc = -1; } } if (input != NULL) { free(input); } return rc; } int main(void) { int rc = -1; if (test_hash()) { fprintf(stderr, "test_hash()\n"); goto out; } rc = 0; out: return !!rc; }