diff options
author | EuAndreh <eu@euandre.org> | 2023-12-31 08:49:16 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-06-04 20:11:50 -0300 |
commit | bf54d3bc7be151ee3de15e3cc9fabea68369008e (patch) | |
tree | 6ee57bf4e6d57f3aaba9155a3088323a446d5df8 /tests/lib.c | |
parent | Add complete "Makefile" for standard packaging (diff) | |
download | siphash-main.tar.gz siphash-main.tar.xz |
- assert that the generated `siphash.o` code is identical to the
original code, available at `tests/assert-identical.sh`;
- remove unneeded `#define`s;
- rewrite code with the correct indentation, spacing and formatting;
- use C99 constructs over C89 (for loop variable declarations inside
the parentheses);
- fix the public API.
Diffstat (limited to '')
-rw-r--r-- | tests/lib.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/lib.c b/tests/lib.c new file mode 100644 index 0000000..acae834 --- /dev/null +++ b/tests/lib.c @@ -0,0 +1,101 @@ +#include "../src/lib.c" + +#include <assert.h> +#include <time.h> + + +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; +} |