forked from dsprenkels/randombytes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandombytes_test.c
More file actions
155 lines (135 loc) · 4.37 KB
/
randombytes_test.c
File metadata and controls
155 lines (135 loc) · 4.37 KB
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
#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;
// ======== 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));
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_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_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_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);
}
// ======== Mock OS functions to simulate uncommon behavior ========
#if defined(__linux__) && defined(SYS_getrandom)
int __wrap_syscall(int n, char *buf, size_t buflen, int flags) {
syscall_called++;
if (current_test == test_getrandom_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_interrupted) {
if (syscall_called < 5) {
errno = EINTR;
return -1;
}
}
return __real_syscall(n, buf, buflen, flags);
}
#endif /* defined(__linux__) && defined(SYS_getrandom) */
#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(SYS_getrandom)
RUN_TEST(test_getrandom_partial)
RUN_TEST(test_getrandom_interrupted)
#else
SKIP_TEST(test_getrandom_partial)
SKIP_TEST(test_getrandom_interrupted)
#endif /* defined(__linux__) && defined(SYS_getrandom) */
#if defined(__linux__) && !defined(SYS_getrandom)
RUN_TEST(test_issue_17)
RUN_TEST(test_issue_22)
#else
SKIP_TEST(test_issue_17)
SKIP_TEST(test_issue_22)
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
return 0;
}