#include "../src/random.c" #include #include "../src/testing.h" static int test_random_bytes(void) { int rc = -1; test_start("random_bytes()"); { testing("we get to pick the size that comes out"); enum { LEN = 256, }; u8 arr[LEN] = { 0 }; for (size_t n = 0; n < LEN; n++) { if (random_bytes(n, &arr)) { logerr("random_bytes()"); goto out; } for (size_t i = n; i < LEN; i++) { assert(arr[i] == 0); } } test_ok(); } { testing("we always get a new value as a result"); enum { LEN = 64, }; u8 arr1[LEN] = { 0 }; u8 arr2[LEN] = { 0 }; if (random_bytes(LEN, &arr1)) { logerr("random_bytes()"); goto out; } const size_t attempts = 10; for (size_t n = 0; n < attempts; n++) { if (random_bytes(LEN, &arr2)) { logerr("random_bytes()"); goto out; } assert(memcmp(arr1, arr2, LEN) != 0); } test_ok(); } rc = 0; out: return rc; } int main(void) { int rc = EXIT_FAILURE; if (random_init()) { logerr("random_init()"); goto out; } if (test_random_bytes()) { logerr("test_random_bytes()"); goto out; } rc = EXIT_SUCCESS; out: if (random_end()) { if (rc == EXIT_SUCCESS) { rc = EXIT_FAILURE; } } return rc; }