2 #include "../../src/random.h"
3 #include "../../src/xalloc.h"
5 static int setup(void **state) {
11 static int teardown(void **state) {
18 static const uint8_t zero[zerolen] = {0};
20 static void test_randomize_zero_must_not_change_memory(void **state) {
23 uint8_t buf[zerolen] = {0};
26 assert_memory_equal(zero, buf, sizeof(buf));
29 static void test_randomize_does_not_overflow(void **state) {
32 uint8_t buf[zerolen] = {0};
33 const size_t half = sizeof(buf) / 2;
36 assert_memory_not_equal(zero, buf, half);
37 assert_memory_equal(zero, &buf[half], half);
40 static void test_randomize_full_changes_memory(void **state) {
43 uint8_t buf[zerolen] = {0};
44 randomize(buf, sizeof(buf));
46 assert_memory_not_equal(zero, buf, sizeof(buf));
49 static void test_randomize_does_not_repeat(void **state) {
52 // Ask randomize() for small chunks so there's more
53 // chance for it to repeat itself (within reason).
56 const size_t chunks = 1024;
57 uint8_t (*buffers)[chunklen] = xzalloc(chunks * chunklen);
59 // Fill buffers with (hopefully) random data
60 for(size_t i = 0; i < chunks; ++i) {
61 randomize(buffers[i], chunklen);
63 // Check there was no overflow to the right
65 assert_memory_equal(zero, buffers[i + 1], chunklen);
69 // Check there were no repetitions (with 128-bit buffers collisions are very unlikely)
70 for(size_t i = 0; i < chunks - 1; ++i) {
71 for(size_t j = i + 1; j < chunks; ++j) {
72 assert_memory_not_equal(buffers[i], buffers[j], chunklen);
81 const struct CMUnitTest tests[] = {
82 cmocka_unit_test(test_randomize_zero_must_not_change_memory),
83 cmocka_unit_test(test_randomize_does_not_overflow),
84 cmocka_unit_test(test_randomize_full_changes_memory),
85 cmocka_unit_test(test_randomize_does_not_repeat),
87 return cmocka_run_group_tests(tests, setup, teardown);