summaryrefslogtreecommitdiff
path: root/tests/t_random.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/t_random.c')
-rw-r--r--tests/t_random.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/t_random.c b/tests/t_random.c
new file mode 100644
index 0000000..b070c3f
--- /dev/null
+++ b/tests/t_random.c
@@ -0,0 +1,112 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <stdint.h>
6#include <string.h>
7
8/* Opentracker */
9#include "ot_random.h"
10#include "test.h"
11
12/* Nothing here can test that the bytes are unpredictable, which is a property
13 of the generator the build selected. What these cases do pin is that the
14 wrapper hands the whole buffer to it, writes nothing beyond the length it
15 was given, and is actually wired to something that varies. The first of
16 those matters: the fallback this module replaced filled only the first
17 sixteen bytes of the sixty-four byte key buffer in ot_udp.c. */
18
19#define DRAWS 32
20#define BUFLEN 64
21
22static void case_every_byte_varies(void) {
23 unsigned char first[BUFLEN], draw[BUFLEN];
24 unsigned char varied[BUFLEN];
25 size_t i, n;
26
27 test_case("every byte of the buffer comes from the generator");
28 memset(varied, 0, sizeof(varied));
29 random_bytes(first, sizeof(first));
30
31 for (n = 0; n < DRAWS; ++n) {
32 random_bytes(draw, sizeof(draw));
33 for (i = 0; i < BUFLEN; ++i)
34 if (draw[i] != first[i])
35 varied[i] = 1;
36 }
37
38 /* A byte position that never moved across 32 draws is not being written.
39 A false alarm needs all 32 draws to agree by chance, around 256^-31. */
40 for (i = 0; i < BUFLEN; ++i)
41 if (!varied[i]) {
42 test_failf(__FILE__, __LINE__, "byte %zu never changed over %d draws", i, DRAWS);
43 return;
44 }
45 ++test_checks;
46}
47
48static void case_respects_length(void) {
49 unsigned char buf[32];
50 size_t i;
51
52 test_case("nothing is written past the requested length");
53 memset(buf, 0xA5, sizeof(buf));
54 random_bytes(buf, 8);
55 for (i = 8; i < sizeof(buf); ++i)
56 if (buf[i] != 0xA5) {
57 test_failf(__FILE__, __LINE__, "byte %zu past the end was overwritten", i);
58 return;
59 }
60 ++test_checks;
61}
62
63static void case_zero_length(void) {
64 unsigned char buf[4];
65
66 test_case("a zero length request writes nothing");
67 memset(buf, 0x5A, sizeof(buf));
68 random_bytes(buf, 0);
69 CHECK_MEM(buf, "\x5a\x5a\x5a\x5a", 4);
70}
71
72static void case_successive_draws_differ(void) {
73 unsigned char a[32], b[32];
74
75 test_case("two draws differ");
76 random_bytes(a, sizeof(a));
77 random_bytes(b, sizeof(b));
78 CHECK(memcmp(a, b, sizeof(a)) != 0);
79}
80
81static void case_uint32_varies(void) {
82 uint32_t first = random_uint32();
83 int differs = 0;
84 size_t n;
85
86 test_case("random_uint32 does not return a constant");
87 for (n = 0; n < DRAWS; ++n)
88 if (random_uint32() != first)
89 differs = 1;
90 CHECK(differs);
91}
92
93static void case_uint32_uses_the_top_bit(void) {
94 uint32_t seen = 0;
95 size_t n;
96
97 test_case("random_uint32 spans the full 32 bits");
98 /* random() never sets bit 31, so a build that returned it unmixed would
99 fail this. Over 64 draws the chance of a false alarm is 2^-64. */
100 for (n = 0; n < 64; ++n)
101 seen |= random_uint32();
102 CHECK_INT(seen & 0x80000000u, 0x80000000u);
103}
104
105void test_random(void) {
106 case_every_byte_varies();
107 case_respects_length();
108 case_zero_length();
109 case_successive_draws_differ();
110 case_uint32_varies();
111 case_uint32_uses_the_top_bit();
112}