forked from dsprenkels/randombytes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randombytes_test.c
217 lines (193 loc) · 6.35 KB
/
randombytes_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "randombytes.c"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
static void *current_test = NULL;
static int syscall_called = 0;
static int glib_getrandom_called = 0;
// ======== Helper macros and functions ========
#define RUN_TEST(name) \
printf("%s ... ", #name); \
padto(' ', sizeof(#name) + sizeof(" ... "), 32); \
current_test = name; \
name(); \
printf("ok\n");
#define SKIP_TEST(name) \
printf("%s ... ", #name); \
padto(' ', sizeof(#name) + sizeof(" ... "), 32); \
printf("skipped\n");
static void padto(const char c, const size_t curlen, const size_t len) {
for (size_t i = curlen; i < len; i++) {
putchar(c);
}
}
// ======== Forward declarations needed for mocked functions ========
#if defined(__linux__) && defined(SYS_getrandom)
int __wrap_syscall(int n, char *buf, size_t buflen, int flags);
int __real_syscall(int n, char *buf, size_t buflen, int flags);
#endif /* defined(__linux__) && defined(SYS_getrandom) */
#if defined(__linux__) && !defined(SYS_getrandom)
int __wrap_ioctl(int fd, int code, int* ret);
int __real_ioctl(int fd, int code, int* ret);
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
// ======== Test definitions ========
static void test_functional(void) {
uint8_t buf1[20] = {}, buf2[sizeof(buf1)] = {};
const int ret1 = randombytes(buf1, sizeof(buf1));
const int ret2 = randombytes(buf2, sizeof(buf2));
if (ret1 != 0 || ret2 != 0) {
printf("error: %s\n", strerror(errno));
}
assert(ret1 == 0);
assert(ret2 == 0);
assert(memcmp(buf1, buf2, sizeof(buf1)) != 0);
}
static void test_empty(void) {
const uint8_t zero[20] = {};
uint8_t buf[sizeof(zero)] = {};
const int ret = randombytes(buf, 0);
assert(ret == 0);
assert(memcmp(buf, zero, sizeof(zero)) == 0);
}
static void test_getrandom_syscall_partial(void) {
syscall_called = 0;
uint8_t buf[100] = {};
const int ret = randombytes(buf, sizeof(buf));
assert(ret == 0);
assert(syscall_called >= 5);
for (int i = 1; i < 5; i++) {
assert(memcmp(&buf[0], &buf[20*i], 20) != 0);
}
}
static void test_getrandom_syscall_interrupted(void) {
syscall_called = 0;
uint8_t zero[20] = {};
uint8_t buf[sizeof(zero)] = {};
const int ret = randombytes(buf, sizeof(buf));
assert(ret == 0);
assert(memcmp(buf, zero, 20) != 0);
}
static void test_getrandom_glib_partial(void) {
glib_getrandom_called = 0;
uint8_t buf[100] = {};
const int ret = randombytes(buf, sizeof(buf));
assert(ret == 0);
assert(glib_getrandom_called >= 5);
for (int i = 1; i < 5; i++) {
assert(memcmp(&buf[0], &buf[20*i], 20) != 0);
}
}
static void test_getrandom_glib_interrupted(void) {
glib_getrandom_called = 0;
uint8_t zero[20] = {};
uint8_t buf[sizeof(zero)] = {};
const int ret = randombytes(buf, sizeof(buf));
assert(ret == 0);
assert(memcmp(buf, zero, 20) != 0);
}
static void test_issue_17(void) {
uint8_t buf1[20] = {}, buf2[sizeof(buf1)] = {};
const int ret1 = randombytes(buf1, sizeof(buf1));
const int ret2 = randombytes(buf2, sizeof(buf2));
assert(ret1 == 0);
assert(ret2 == 0);
assert(memcmp(buf1, buf2, sizeof(buf1)) != 0);
}
static void test_issue_22(void) {
uint8_t buf1[20] = {}, buf2[sizeof(buf1)] = {};
const int ret1 = randombytes(buf1, sizeof(buf1));
const int ret2 = randombytes(buf2, sizeof(buf2));
assert(ret1 == 0);
assert(ret2 == 0);
assert(memcmp(buf1, buf2, sizeof(buf1)) != 0);
}
static void test_issue_33(void) {
for (size_t idx = 0; idx < 100000; idx++) {
uint8_t buf[20] = {};
const int ret = randombytes(&buf, sizeof(buf));
if (ret != 0) {
printf("error: %s\n", strerror(errno));
}
assert(ret == 0);
}
}
// ======== Mock OS functions to simulate uncommon behavior ========
#if defined(__linux__) && defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC_MINOR__ > 24))
int __wrap_getrandom(char *buf, size_t buflen, int flags) {
glib_getrandom_called++;
if (current_test == test_getrandom_glib_partial) {
// Fill only 16 bytes, the caller should retry
const size_t current_buflen = buflen <= 16 ? buflen : 16;
return __real_getrandom(buf, current_buflen, flags);
} else if (current_test == test_getrandom_glib_interrupted) {
if (glib_getrandom_called < 5) {
errno = EINTR;
return -1;
}
}
return __real_getrandom(buf, buflen, flags);
}
#elif defined(__linux__) && defined(SYS_getrandom)
int __wrap_syscall(int n, char *buf, size_t buflen, int flags) {
syscall_called++;
if (current_test == test_getrandom_syscall_partial) {
// Fill only 16 bytes, the caller should retry
const size_t current_buflen = buflen <= 16 ? buflen : 16;
return __real_syscall(n, buf, current_buflen, flags);
} else if (current_test == test_getrandom_syscall_interrupted) {
if (syscall_called < 5) {
errno = EINTR;
return -1;
}
}
return __real_syscall(n, buf, buflen, flags);
}
#endif /* defined(__linux__) && (defined(SYS_getrandom) or glibc version > 2.24) */
#if defined(__linux__) && !defined(SYS_getrandom)
int __wrap_ioctl(int fd, int code, int* ret) {
if (current_test == test_issue_17) {
errno = ENOTTY;
return -1;
}
if (current_test == test_issue_17) {
errno = ENOSYS;
return -1;
}
return __real_ioctl(fd, code, ret);
}
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
// ======== Main function ========
int main(void) {
// Use `#if defined()` to enable/disable tests on a platform. If disabled,
// please still call `SKIP_TEST` to make sure no tests are skipped silently.
RUN_TEST(test_functional)
RUN_TEST(test_empty)
#if defined(__linux__) && defined(USE_GLIBC)
RUN_TEST(test_getrandom_glib_partial)
RUN_TEST(test_getrandom_glib_interrupted)
SKIP_TEST(test_getrandom_syscall_partial)
SKIP_TEST(test_getrandom_syscall_interrupted)
#elif defined(__linux__) && defined(SYS_getrandom)
SKIP_TEST(test_getrandom_glib_partial)
SKIP_TEST(test_getrandom_glib_interrupted)
RUN_TEST(test_getrandom_syscall_partial)
RUN_TEST(test_getrandom_syscall_interrupted)
#else
SKIP_TEST(test_getrandom_glib_partial)
SKIP_TEST(test_getrandom_glib_interrupted)
SKIP_TEST(test_getrandom_syscall_partial)
SKIP_TEST(test_getrandom_syscall_interrupted)
#endif /* defined(__linux__) && (defined(SYS_getrandom) or glibc version > 2.24) */
#if defined(__linux__) && !defined(SYS_getrandom)
RUN_TEST(test_issue_17)
RUN_TEST(test_issue_22)
RUN_TEST(test_issue_33)
#else
SKIP_TEST(test_issue_17)
SKIP_TEST(test_issue_22)
SKIP_TEST(test_issue_33)
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
return 0;
}