diff options
Diffstat (limited to 'regress/unittests/test_helper')
-rw-r--r-- | regress/unittests/test_helper/Makefile | 13 | ||||
-rw-r--r-- | regress/unittests/test_helper/fuzz.c | 374 | ||||
-rw-r--r-- | regress/unittests/test_helper/test_helper.c | 452 | ||||
-rw-r--r-- | regress/unittests/test_helper/test_helper.h | 288 |
4 files changed, 1127 insertions, 0 deletions
diff --git a/regress/unittests/test_helper/Makefile b/regress/unittests/test_helper/Makefile new file mode 100644 index 000000000..3e90903ef --- /dev/null +++ b/regress/unittests/test_helper/Makefile | |||
@@ -0,0 +1,13 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2014/04/30 05:32:00 djm Exp $ | ||
2 | |||
3 | LIB= test_helper | ||
4 | SRCS= test_helper.c fuzz.c | ||
5 | |||
6 | DEBUGLIBS= no | ||
7 | NOPROFILE= yes | ||
8 | NOPIC= yes | ||
9 | |||
10 | install: | ||
11 | @echo -n | ||
12 | |||
13 | .include <bsd.lib.mk> | ||
diff --git a/regress/unittests/test_helper/fuzz.c b/regress/unittests/test_helper/fuzz.c new file mode 100644 index 000000000..b64af24ed --- /dev/null +++ b/regress/unittests/test_helper/fuzz.c | |||
@@ -0,0 +1,374 @@ | |||
1 | /* $OpenBSD: fuzz.c,v 1.3 2014/05/02 09:41:32 andre Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2011 Damien Miller <djm@mindrot.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | /* Utility functions/framework for fuzz tests */ | ||
19 | |||
20 | #include <sys/types.h> | ||
21 | |||
22 | #include <assert.h> | ||
23 | #include <ctype.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdint.h> | ||
26 | #include <stdlib.h> | ||
27 | #include <string.h> | ||
28 | #include <assert.h> | ||
29 | |||
30 | #include "test_helper.h" | ||
31 | |||
32 | /* #define FUZZ_DEBUG */ | ||
33 | |||
34 | #ifdef FUZZ_DEBUG | ||
35 | # define FUZZ_DBG(x) do { \ | ||
36 | printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \ | ||
37 | printf x; \ | ||
38 | printf("\n"); \ | ||
39 | fflush(stdout); \ | ||
40 | } while (0) | ||
41 | #else | ||
42 | # define FUZZ_DBG(x) | ||
43 | #endif | ||
44 | |||
45 | /* For brevity later */ | ||
46 | typedef unsigned long long fuzz_ullong; | ||
47 | |||
48 | /* For base-64 fuzzing */ | ||
49 | static const char fuzz_b64chars[] = | ||
50 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
51 | |||
52 | struct fuzz { | ||
53 | /* Fuzz method currently in use */ | ||
54 | int strategy; | ||
55 | |||
56 | /* Fuzz methods remaining */ | ||
57 | int strategies; | ||
58 | |||
59 | /* Original seed data blob */ | ||
60 | void *seed; | ||
61 | size_t slen; | ||
62 | |||
63 | /* Current working copy of seed with fuzz mutations applied */ | ||
64 | u_char *fuzzed; | ||
65 | |||
66 | /* Used by fuzz methods */ | ||
67 | size_t o1, o2; | ||
68 | }; | ||
69 | |||
70 | static const char * | ||
71 | fuzz_ntop(u_int n) | ||
72 | { | ||
73 | switch (n) { | ||
74 | case 0: | ||
75 | return "NONE"; | ||
76 | case FUZZ_1_BIT_FLIP: | ||
77 | return "FUZZ_1_BIT_FLIP"; | ||
78 | case FUZZ_2_BIT_FLIP: | ||
79 | return "FUZZ_2_BIT_FLIP"; | ||
80 | case FUZZ_1_BYTE_FLIP: | ||
81 | return "FUZZ_1_BYTE_FLIP"; | ||
82 | case FUZZ_2_BYTE_FLIP: | ||
83 | return "FUZZ_2_BYTE_FLIP"; | ||
84 | case FUZZ_TRUNCATE_START: | ||
85 | return "FUZZ_TRUNCATE_START"; | ||
86 | case FUZZ_TRUNCATE_END: | ||
87 | return "FUZZ_TRUNCATE_END"; | ||
88 | case FUZZ_BASE64: | ||
89 | return "FUZZ_BASE64"; | ||
90 | default: | ||
91 | abort(); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | void | ||
96 | fuzz_dump(struct fuzz *fuzz) | ||
97 | { | ||
98 | u_char *p = fuzz_ptr(fuzz); | ||
99 | size_t i, j, len = fuzz_len(fuzz); | ||
100 | |||
101 | switch (fuzz->strategy) { | ||
102 | case FUZZ_1_BIT_FLIP: | ||
103 | fprintf(stderr, "%s case %zu of %zu (bit: %zu)\n", | ||
104 | fuzz_ntop(fuzz->strategy), | ||
105 | fuzz->o1, fuzz->slen * 8, fuzz->o1); | ||
106 | break; | ||
107 | case FUZZ_2_BIT_FLIP: | ||
108 | fprintf(stderr, "%s case %llu of %llu (bits: %zu, %zu)\n", | ||
109 | fuzz_ntop(fuzz->strategy), | ||
110 | (((fuzz_ullong)fuzz->o2) * fuzz->slen * 8) + fuzz->o1, | ||
111 | ((fuzz_ullong)fuzz->slen * 8) * fuzz->slen * 8, | ||
112 | fuzz->o1, fuzz->o2); | ||
113 | break; | ||
114 | case FUZZ_1_BYTE_FLIP: | ||
115 | fprintf(stderr, "%s case %zu of %zu (byte: %zu)\n", | ||
116 | fuzz_ntop(fuzz->strategy), | ||
117 | fuzz->o1, fuzz->slen, fuzz->o1); | ||
118 | break; | ||
119 | case FUZZ_2_BYTE_FLIP: | ||
120 | fprintf(stderr, "%s case %llu of %llu (bytes: %zu, %zu)\n", | ||
121 | fuzz_ntop(fuzz->strategy), | ||
122 | (((fuzz_ullong)fuzz->o2) * fuzz->slen) + fuzz->o1, | ||
123 | ((fuzz_ullong)fuzz->slen) * fuzz->slen, | ||
124 | fuzz->o1, fuzz->o2); | ||
125 | break; | ||
126 | case FUZZ_TRUNCATE_START: | ||
127 | fprintf(stderr, "%s case %zu of %zu (offset: %zu)\n", | ||
128 | fuzz_ntop(fuzz->strategy), | ||
129 | fuzz->o1, fuzz->slen, fuzz->o1); | ||
130 | break; | ||
131 | case FUZZ_TRUNCATE_END: | ||
132 | fprintf(stderr, "%s case %zu of %zu (offset: %zu)\n", | ||
133 | fuzz_ntop(fuzz->strategy), | ||
134 | fuzz->o1, fuzz->slen, fuzz->o1); | ||
135 | break; | ||
136 | case FUZZ_BASE64: | ||
137 | assert(fuzz->o2 < sizeof(fuzz_b64chars) - 1); | ||
138 | fprintf(stderr, "%s case %llu of %llu (offset: %zu char: %c)\n", | ||
139 | fuzz_ntop(fuzz->strategy), | ||
140 | (fuzz->o1 * (fuzz_ullong)64) + fuzz->o2, | ||
141 | fuzz->slen * (fuzz_ullong)64, fuzz->o1, | ||
142 | fuzz_b64chars[fuzz->o2]); | ||
143 | break; | ||
144 | default: | ||
145 | abort(); | ||
146 | } | ||
147 | |||
148 | fprintf(stderr, "fuzz context %p len = %zu\n", fuzz, len); | ||
149 | for (i = 0; i < len; i += 16) { | ||
150 | fprintf(stderr, "%.4zd: ", i); | ||
151 | for (j = i; j < i + 16; j++) { | ||
152 | if (j < len) | ||
153 | fprintf(stderr, "%02x ", p[j]); | ||
154 | else | ||
155 | fprintf(stderr, " "); | ||
156 | } | ||
157 | fprintf(stderr, " "); | ||
158 | for (j = i; j < i + 16; j++) { | ||
159 | if (j < len) { | ||
160 | if (isascii(p[j]) && isprint(p[j])) | ||
161 | fprintf(stderr, "%c", p[j]); | ||
162 | else | ||
163 | fprintf(stderr, "."); | ||
164 | } | ||
165 | } | ||
166 | fprintf(stderr, "\n"); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | struct fuzz * | ||
171 | fuzz_begin(u_int strategies, const void *p, size_t l) | ||
172 | { | ||
173 | struct fuzz *ret = calloc(sizeof(*ret), 1); | ||
174 | |||
175 | assert(p != NULL); | ||
176 | assert(ret != NULL); | ||
177 | ret->seed = malloc(l); | ||
178 | assert(ret->seed != NULL); | ||
179 | memcpy(ret->seed, p, l); | ||
180 | ret->slen = l; | ||
181 | ret->strategies = strategies; | ||
182 | |||
183 | assert(ret->slen < SIZE_MAX / 8); | ||
184 | assert(ret->strategies <= (FUZZ_MAX|(FUZZ_MAX-1))); | ||
185 | |||
186 | FUZZ_DBG(("begin, ret = %p", ret)); | ||
187 | |||
188 | fuzz_next(ret); | ||
189 | return ret; | ||
190 | } | ||
191 | |||
192 | void | ||
193 | fuzz_cleanup(struct fuzz *fuzz) | ||
194 | { | ||
195 | FUZZ_DBG(("cleanup, fuzz = %p", fuzz)); | ||
196 | assert(fuzz != NULL); | ||
197 | assert(fuzz->seed != NULL); | ||
198 | assert(fuzz->fuzzed != NULL); | ||
199 | free(fuzz->seed); | ||
200 | free(fuzz->fuzzed); | ||
201 | free(fuzz); | ||
202 | } | ||
203 | |||
204 | static int | ||
205 | fuzz_strategy_done(struct fuzz *fuzz) | ||
206 | { | ||
207 | FUZZ_DBG(("fuzz = %p, strategy = %s, o1 = %zu, o2 = %zu, slen = %zu", | ||
208 | fuzz, fuzz_ntop(fuzz->strategy), fuzz->o1, fuzz->o2, fuzz->slen)); | ||
209 | |||
210 | switch (fuzz->strategy) { | ||
211 | case FUZZ_1_BIT_FLIP: | ||
212 | return fuzz->o1 >= fuzz->slen * 8; | ||
213 | case FUZZ_2_BIT_FLIP: | ||
214 | return fuzz->o2 >= fuzz->slen * 8; | ||
215 | case FUZZ_2_BYTE_FLIP: | ||
216 | return fuzz->o2 >= fuzz->slen; | ||
217 | case FUZZ_1_BYTE_FLIP: | ||
218 | case FUZZ_TRUNCATE_START: | ||
219 | case FUZZ_TRUNCATE_END: | ||
220 | case FUZZ_BASE64: | ||
221 | return fuzz->o1 >= fuzz->slen; | ||
222 | default: | ||
223 | abort(); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | void | ||
228 | fuzz_next(struct fuzz *fuzz) | ||
229 | { | ||
230 | u_int i; | ||
231 | |||
232 | FUZZ_DBG(("start, fuzz = %p, strategy = %s, strategies = 0x%lx, " | ||
233 | "o1 = %zu, o2 = %zu, slen = %zu", fuzz, fuzz_ntop(fuzz->strategy), | ||
234 | (u_long)fuzz->strategies, fuzz->o1, fuzz->o2, fuzz->slen)); | ||
235 | |||
236 | if (fuzz->strategy == 0 || fuzz_strategy_done(fuzz)) { | ||
237 | /* If we are just starting out, we need to allocate too */ | ||
238 | if (fuzz->fuzzed == NULL) { | ||
239 | FUZZ_DBG(("alloc")); | ||
240 | fuzz->fuzzed = calloc(fuzz->slen, 1); | ||
241 | } | ||
242 | /* Pick next strategy */ | ||
243 | FUZZ_DBG(("advance")); | ||
244 | for (i = 1; i <= FUZZ_MAX; i <<= 1) { | ||
245 | if ((fuzz->strategies & i) != 0) { | ||
246 | fuzz->strategy = i; | ||
247 | break; | ||
248 | } | ||
249 | } | ||
250 | FUZZ_DBG(("selected = %u", fuzz->strategy)); | ||
251 | if (fuzz->strategy == 0) { | ||
252 | FUZZ_DBG(("done, no more strategies")); | ||
253 | return; | ||
254 | } | ||
255 | fuzz->strategies &= ~(fuzz->strategy); | ||
256 | fuzz->o1 = fuzz->o2 = 0; | ||
257 | } | ||
258 | |||
259 | assert(fuzz->fuzzed != NULL); | ||
260 | |||
261 | switch (fuzz->strategy) { | ||
262 | case FUZZ_1_BIT_FLIP: | ||
263 | assert(fuzz->o1 / 8 < fuzz->slen); | ||
264 | memcpy(fuzz->fuzzed, fuzz->seed, fuzz->slen); | ||
265 | fuzz->fuzzed[fuzz->o1 / 8] ^= 1 << (fuzz->o1 % 8); | ||
266 | fuzz->o1++; | ||
267 | break; | ||
268 | case FUZZ_2_BIT_FLIP: | ||
269 | assert(fuzz->o1 / 8 < fuzz->slen); | ||
270 | assert(fuzz->o2 / 8 < fuzz->slen); | ||
271 | memcpy(fuzz->fuzzed, fuzz->seed, fuzz->slen); | ||
272 | fuzz->fuzzed[fuzz->o1 / 8] ^= 1 << (fuzz->o1 % 8); | ||
273 | fuzz->fuzzed[fuzz->o2 / 8] ^= 1 << (fuzz->o2 % 8); | ||
274 | fuzz->o1++; | ||
275 | if (fuzz->o1 >= fuzz->slen * 8) { | ||
276 | fuzz->o1 = 0; | ||
277 | fuzz->o2++; | ||
278 | } | ||
279 | break; | ||
280 | case FUZZ_1_BYTE_FLIP: | ||
281 | assert(fuzz->o1 < fuzz->slen); | ||
282 | memcpy(fuzz->fuzzed, fuzz->seed, fuzz->slen); | ||
283 | fuzz->fuzzed[fuzz->o1] ^= 0xff; | ||
284 | fuzz->o1++; | ||
285 | break; | ||
286 | case FUZZ_2_BYTE_FLIP: | ||
287 | assert(fuzz->o1 < fuzz->slen); | ||
288 | assert(fuzz->o2 < fuzz->slen); | ||
289 | memcpy(fuzz->fuzzed, fuzz->seed, fuzz->slen); | ||
290 | fuzz->fuzzed[fuzz->o1] ^= 0xff; | ||
291 | fuzz->fuzzed[fuzz->o2] ^= 0xff; | ||
292 | fuzz->o1++; | ||
293 | if (fuzz->o1 >= fuzz->slen) { | ||
294 | fuzz->o1 = 0; | ||
295 | fuzz->o2++; | ||
296 | } | ||
297 | break; | ||
298 | case FUZZ_TRUNCATE_START: | ||
299 | case FUZZ_TRUNCATE_END: | ||
300 | assert(fuzz->o1 < fuzz->slen); | ||
301 | memcpy(fuzz->fuzzed, fuzz->seed, fuzz->slen); | ||
302 | fuzz->o1++; | ||
303 | break; | ||
304 | case FUZZ_BASE64: | ||
305 | assert(fuzz->o1 < fuzz->slen); | ||
306 | assert(fuzz->o2 < sizeof(fuzz_b64chars) - 1); | ||
307 | memcpy(fuzz->fuzzed, fuzz->seed, fuzz->slen); | ||
308 | fuzz->fuzzed[fuzz->o1] = fuzz_b64chars[fuzz->o2]; | ||
309 | fuzz->o2++; | ||
310 | if (fuzz->o2 >= sizeof(fuzz_b64chars) - 1) { | ||
311 | fuzz->o2 = 0; | ||
312 | fuzz->o1++; | ||
313 | } | ||
314 | break; | ||
315 | default: | ||
316 | abort(); | ||
317 | } | ||
318 | |||
319 | FUZZ_DBG(("done, fuzz = %p, strategy = %s, strategies = 0x%lx, " | ||
320 | "o1 = %zu, o2 = %zu, slen = %zu", fuzz, fuzz_ntop(fuzz->strategy), | ||
321 | (u_long)fuzz->strategies, fuzz->o1, fuzz->o2, fuzz->slen)); | ||
322 | } | ||
323 | |||
324 | int | ||
325 | fuzz_done(struct fuzz *fuzz) | ||
326 | { | ||
327 | FUZZ_DBG(("fuzz = %p, strategies = 0x%lx", fuzz, | ||
328 | (u_long)fuzz->strategies)); | ||
329 | |||
330 | return fuzz_strategy_done(fuzz) && fuzz->strategies == 0; | ||
331 | } | ||
332 | |||
333 | size_t | ||
334 | fuzz_len(struct fuzz *fuzz) | ||
335 | { | ||
336 | assert(fuzz->fuzzed != NULL); | ||
337 | switch (fuzz->strategy) { | ||
338 | case FUZZ_1_BIT_FLIP: | ||
339 | case FUZZ_2_BIT_FLIP: | ||
340 | case FUZZ_1_BYTE_FLIP: | ||
341 | case FUZZ_2_BYTE_FLIP: | ||
342 | case FUZZ_BASE64: | ||
343 | return fuzz->slen; | ||
344 | case FUZZ_TRUNCATE_START: | ||
345 | case FUZZ_TRUNCATE_END: | ||
346 | assert(fuzz->o1 <= fuzz->slen); | ||
347 | return fuzz->slen - fuzz->o1; | ||
348 | default: | ||
349 | abort(); | ||
350 | } | ||
351 | } | ||
352 | |||
353 | u_char * | ||
354 | fuzz_ptr(struct fuzz *fuzz) | ||
355 | { | ||
356 | assert(fuzz->fuzzed != NULL); | ||
357 | switch (fuzz->strategy) { | ||
358 | case FUZZ_1_BIT_FLIP: | ||
359 | case FUZZ_2_BIT_FLIP: | ||
360 | case FUZZ_1_BYTE_FLIP: | ||
361 | case FUZZ_2_BYTE_FLIP: | ||
362 | case FUZZ_BASE64: | ||
363 | return fuzz->fuzzed; | ||
364 | case FUZZ_TRUNCATE_START: | ||
365 | assert(fuzz->o1 <= fuzz->slen); | ||
366 | return fuzz->fuzzed + fuzz->o1; | ||
367 | case FUZZ_TRUNCATE_END: | ||
368 | assert(fuzz->o1 <= fuzz->slen); | ||
369 | return fuzz->fuzzed; | ||
370 | default: | ||
371 | abort(); | ||
372 | } | ||
373 | } | ||
374 | |||
diff --git a/regress/unittests/test_helper/test_helper.c b/regress/unittests/test_helper/test_helper.c new file mode 100644 index 000000000..8f0bbdec9 --- /dev/null +++ b/regress/unittests/test_helper/test_helper.c | |||
@@ -0,0 +1,452 @@ | |||
1 | /* $OpenBSD: test_helper.c,v 1.2 2014/05/02 09:41:32 andre Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2011 Damien Miller <djm@mindrot.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | /* Utility functions/framework for regress tests */ | ||
19 | |||
20 | #include <sys/types.h> | ||
21 | #include <sys/param.h> | ||
22 | |||
23 | #include <fcntl.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdint.h> | ||
26 | #include <stdlib.h> | ||
27 | #include <string.h> | ||
28 | #include <assert.h> | ||
29 | #include <unistd.h> | ||
30 | |||
31 | #include <openssl/bn.h> | ||
32 | |||
33 | #include <vis.h> | ||
34 | |||
35 | #include "test_helper.h" | ||
36 | |||
37 | #define TEST_CHECK_INT(r, pred) do { \ | ||
38 | switch (pred) { \ | ||
39 | case TEST_EQ: \ | ||
40 | if (r == 0) \ | ||
41 | return; \ | ||
42 | break; \ | ||
43 | case TEST_NE: \ | ||
44 | if (r != 0) \ | ||
45 | return; \ | ||
46 | break; \ | ||
47 | case TEST_LT: \ | ||
48 | if (r < 0) \ | ||
49 | return; \ | ||
50 | break; \ | ||
51 | case TEST_LE: \ | ||
52 | if (r <= 0) \ | ||
53 | return; \ | ||
54 | break; \ | ||
55 | case TEST_GT: \ | ||
56 | if (r > 0) \ | ||
57 | return; \ | ||
58 | break; \ | ||
59 | case TEST_GE: \ | ||
60 | if (r >= 0) \ | ||
61 | return; \ | ||
62 | break; \ | ||
63 | default: \ | ||
64 | abort(); \ | ||
65 | } \ | ||
66 | } while (0) | ||
67 | |||
68 | #define TEST_CHECK(x1, x2, pred) do { \ | ||
69 | switch (pred) { \ | ||
70 | case TEST_EQ: \ | ||
71 | if (x1 == x2) \ | ||
72 | return; \ | ||
73 | break; \ | ||
74 | case TEST_NE: \ | ||
75 | if (x1 != x2) \ | ||
76 | return; \ | ||
77 | break; \ | ||
78 | case TEST_LT: \ | ||
79 | if (x1 < x2) \ | ||
80 | return; \ | ||
81 | break; \ | ||
82 | case TEST_LE: \ | ||
83 | if (x1 <= x2) \ | ||
84 | return; \ | ||
85 | break; \ | ||
86 | case TEST_GT: \ | ||
87 | if (x1 > x2) \ | ||
88 | return; \ | ||
89 | break; \ | ||
90 | case TEST_GE: \ | ||
91 | if (x1 >= x2) \ | ||
92 | return; \ | ||
93 | break; \ | ||
94 | default: \ | ||
95 | abort(); \ | ||
96 | } \ | ||
97 | } while (0) | ||
98 | |||
99 | extern char *__progname; | ||
100 | |||
101 | static int verbose_mode = 0; | ||
102 | static int quiet_mode = 0; | ||
103 | static char *active_test_name = NULL; | ||
104 | static u_int test_number = 0; | ||
105 | static test_onerror_func_t *test_onerror = NULL; | ||
106 | static void *onerror_ctx = NULL; | ||
107 | static const char *data_dir = NULL; | ||
108 | |||
109 | int | ||
110 | main(int argc, char **argv) | ||
111 | { | ||
112 | int ch; | ||
113 | |||
114 | while ((ch = getopt(argc, argv, "vqd:")) != -1) { | ||
115 | switch (ch) { | ||
116 | case 'd': | ||
117 | data_dir = optarg; | ||
118 | break; | ||
119 | case 'q': | ||
120 | verbose_mode = 0; | ||
121 | quiet_mode = 1; | ||
122 | break; | ||
123 | case 'v': | ||
124 | verbose_mode = 1; | ||
125 | quiet_mode = 0; | ||
126 | break; | ||
127 | default: | ||
128 | fprintf(stderr, "Unrecognised command line option\n"); | ||
129 | fprintf(stderr, "Usage: %s [-v]\n", __progname); | ||
130 | exit(1); | ||
131 | } | ||
132 | } | ||
133 | setvbuf(stdout, NULL, _IONBF, 0); | ||
134 | if (!quiet_mode) | ||
135 | printf("%s: ", __progname); | ||
136 | if (verbose_mode) | ||
137 | printf("\n"); | ||
138 | |||
139 | tests(); | ||
140 | |||
141 | if (!quiet_mode) | ||
142 | printf(" %u tests ok\n", test_number); | ||
143 | return 0; | ||
144 | } | ||
145 | |||
146 | const char * | ||
147 | test_data_file(const char *name) | ||
148 | { | ||
149 | static char ret[PATH_MAX]; | ||
150 | |||
151 | if (data_dir != NULL) | ||
152 | snprintf(ret, sizeof(ret), "%s/%s", data_dir, name); | ||
153 | else | ||
154 | strlcpy(ret, name, sizeof(ret)); | ||
155 | if (access(ret, F_OK) != 0) { | ||
156 | fprintf(stderr, "Cannot access data file %s: %s\n", | ||
157 | ret, strerror(errno)); | ||
158 | exit(1); | ||
159 | } | ||
160 | return ret; | ||
161 | } | ||
162 | |||
163 | void | ||
164 | test_start(const char *n) | ||
165 | { | ||
166 | assert(active_test_name == NULL); | ||
167 | assert((active_test_name = strdup(n)) != NULL); | ||
168 | if (verbose_mode) | ||
169 | printf("test %u - \"%s\": ", test_number, active_test_name); | ||
170 | test_number++; | ||
171 | } | ||
172 | |||
173 | void | ||
174 | set_onerror_func(test_onerror_func_t *f, void *ctx) | ||
175 | { | ||
176 | test_onerror = f; | ||
177 | onerror_ctx = ctx; | ||
178 | } | ||
179 | |||
180 | void | ||
181 | test_done(void) | ||
182 | { | ||
183 | assert(active_test_name != NULL); | ||
184 | free(active_test_name); | ||
185 | active_test_name = NULL; | ||
186 | if (verbose_mode) | ||
187 | printf("OK\n"); | ||
188 | else if (!quiet_mode) { | ||
189 | printf("."); | ||
190 | fflush(stdout); | ||
191 | } | ||
192 | } | ||
193 | |||
194 | void | ||
195 | ssl_err_check(const char *file, int line) | ||
196 | { | ||
197 | long openssl_error = ERR_get_error(); | ||
198 | |||
199 | if (openssl_error == 0) | ||
200 | return; | ||
201 | |||
202 | fprintf(stderr, "\n%s:%d: uncaught OpenSSL error: %s", | ||
203 | file, line, ERR_error_string(openssl_error, NULL)); | ||
204 | abort(); | ||
205 | } | ||
206 | |||
207 | static const char * | ||
208 | pred_name(enum test_predicate p) | ||
209 | { | ||
210 | switch (p) { | ||
211 | case TEST_EQ: | ||
212 | return "EQ"; | ||
213 | case TEST_NE: | ||
214 | return "NE"; | ||
215 | case TEST_LT: | ||
216 | return "LT"; | ||
217 | case TEST_LE: | ||
218 | return "LE"; | ||
219 | case TEST_GT: | ||
220 | return "GT"; | ||
221 | case TEST_GE: | ||
222 | return "GE"; | ||
223 | default: | ||
224 | return "UNKNOWN"; | ||
225 | } | ||
226 | } | ||
227 | |||
228 | static void | ||
229 | test_die(void) | ||
230 | { | ||
231 | if (test_onerror != NULL) | ||
232 | test_onerror(onerror_ctx); | ||
233 | abort(); | ||
234 | } | ||
235 | |||
236 | static void | ||
237 | test_header(const char *file, int line, const char *a1, const char *a2, | ||
238 | const char *name, enum test_predicate pred) | ||
239 | { | ||
240 | fprintf(stderr, "\n%s:%d test #%u \"%s\"\n", | ||
241 | file, line, test_number, active_test_name); | ||
242 | fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n", | ||
243 | name, pred_name(pred), a1, | ||
244 | a2 != NULL ? ", " : "", a2 != NULL ? a2 : ""); | ||
245 | } | ||
246 | |||
247 | void | ||
248 | assert_bignum(const char *file, int line, const char *a1, const char *a2, | ||
249 | const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred) | ||
250 | { | ||
251 | int r = BN_cmp(aa1, aa2); | ||
252 | |||
253 | TEST_CHECK_INT(r, pred); | ||
254 | test_header(file, line, a1, a2, "BIGNUM", pred); | ||
255 | fprintf(stderr, "%12s = 0x%s\n", a1, BN_bn2hex(aa1)); | ||
256 | fprintf(stderr, "%12s = 0x%s\n", a2, BN_bn2hex(aa2)); | ||
257 | test_die(); | ||
258 | } | ||
259 | |||
260 | void | ||
261 | assert_string(const char *file, int line, const char *a1, const char *a2, | ||
262 | const char *aa1, const char *aa2, enum test_predicate pred) | ||
263 | { | ||
264 | int r = strcmp(aa1, aa2); | ||
265 | |||
266 | TEST_CHECK_INT(r, pred); | ||
267 | test_header(file, line, a1, a2, "STRING", pred); | ||
268 | fprintf(stderr, "%12s = %s (len %zu)\n", a1, aa1, strlen(aa1)); | ||
269 | fprintf(stderr, "%12s = %s (len %zu)\n", a2, aa2, strlen(aa2)); | ||
270 | test_die(); | ||
271 | } | ||
272 | |||
273 | static char * | ||
274 | tohex(const void *_s, size_t l) | ||
275 | { | ||
276 | u_int8_t *s = (u_int8_t *)_s; | ||
277 | size_t i, j; | ||
278 | const char *hex = "0123456789abcdef"; | ||
279 | char *r = malloc((l * 2) + 1); | ||
280 | |||
281 | assert(r != NULL); | ||
282 | for (i = j = 0; i < l; i++) { | ||
283 | r[j++] = hex[(s[i] >> 4) & 0xf]; | ||
284 | r[j++] = hex[s[i] & 0xf]; | ||
285 | } | ||
286 | r[j] = '\0'; | ||
287 | return r; | ||
288 | } | ||
289 | |||
290 | void | ||
291 | assert_mem(const char *file, int line, const char *a1, const char *a2, | ||
292 | const void *aa1, const void *aa2, size_t l, enum test_predicate pred) | ||
293 | { | ||
294 | int r = memcmp(aa1, aa2, l); | ||
295 | |||
296 | TEST_CHECK_INT(r, pred); | ||
297 | test_header(file, line, a1, a2, "STRING", pred); | ||
298 | fprintf(stderr, "%12s = %s (len %zu)\n", a1, tohex(aa1, MIN(l, 256)), l); | ||
299 | fprintf(stderr, "%12s = %s (len %zu)\n", a2, tohex(aa2, MIN(l, 256)), l); | ||
300 | test_die(); | ||
301 | } | ||
302 | |||
303 | static int | ||
304 | memvalcmp(const u_int8_t *s, u_char v, size_t l, size_t *where) | ||
305 | { | ||
306 | size_t i; | ||
307 | |||
308 | for (i = 0; i < l; i++) { | ||
309 | if (s[i] != v) { | ||
310 | *where = i; | ||
311 | return 1; | ||
312 | } | ||
313 | } | ||
314 | return 0; | ||
315 | } | ||
316 | |||
317 | void | ||
318 | assert_mem_filled(const char *file, int line, const char *a1, | ||
319 | const void *aa1, u_char v, size_t l, enum test_predicate pred) | ||
320 | { | ||
321 | size_t where = -1; | ||
322 | int r = memvalcmp(aa1, v, l, &where); | ||
323 | char tmp[64]; | ||
324 | |||
325 | if (l == 0) | ||
326 | return; | ||
327 | TEST_CHECK_INT(r, pred); | ||
328 | test_header(file, line, a1, NULL, "MEM_ZERO", pred); | ||
329 | fprintf(stderr, "%20s = %s%s (len %zu)\n", a1, | ||
330 | tohex(aa1, MIN(l, 20)), l > 20 ? "..." : "", l); | ||
331 | snprintf(tmp, sizeof(tmp), "(%s)[%zu]", a1, where); | ||
332 | fprintf(stderr, "%20s = 0x%02x (expected 0x%02x)\n", tmp, | ||
333 | ((u_char *)aa1)[where], v); | ||
334 | test_die(); | ||
335 | } | ||
336 | |||
337 | void | ||
338 | assert_int(const char *file, int line, const char *a1, const char *a2, | ||
339 | int aa1, int aa2, enum test_predicate pred) | ||
340 | { | ||
341 | TEST_CHECK(aa1, aa2, pred); | ||
342 | test_header(file, line, a1, a2, "INT", pred); | ||
343 | fprintf(stderr, "%12s = %d\n", a1, aa1); | ||
344 | fprintf(stderr, "%12s = %d\n", a2, aa2); | ||
345 | test_die(); | ||
346 | } | ||
347 | |||
348 | void | ||
349 | assert_size_t(const char *file, int line, const char *a1, const char *a2, | ||
350 | size_t aa1, size_t aa2, enum test_predicate pred) | ||
351 | { | ||
352 | TEST_CHECK(aa1, aa2, pred); | ||
353 | test_header(file, line, a1, a2, "SIZE_T", pred); | ||
354 | fprintf(stderr, "%12s = %zu\n", a1, aa1); | ||
355 | fprintf(stderr, "%12s = %zu\n", a2, aa2); | ||
356 | test_die(); | ||
357 | } | ||
358 | |||
359 | void | ||
360 | assert_u_int(const char *file, int line, const char *a1, const char *a2, | ||
361 | u_int aa1, u_int aa2, enum test_predicate pred) | ||
362 | { | ||
363 | TEST_CHECK(aa1, aa2, pred); | ||
364 | test_header(file, line, a1, a2, "U_INT", pred); | ||
365 | fprintf(stderr, "%12s = %u / 0x%x\n", a1, aa1, aa1); | ||
366 | fprintf(stderr, "%12s = %u / 0x%x\n", a2, aa2, aa2); | ||
367 | test_die(); | ||
368 | } | ||
369 | |||
370 | void | ||
371 | assert_long_long(const char *file, int line, const char *a1, const char *a2, | ||
372 | long long aa1, long long aa2, enum test_predicate pred) | ||
373 | { | ||
374 | TEST_CHECK(aa1, aa2, pred); | ||
375 | test_header(file, line, a1, a2, "LONG LONG", pred); | ||
376 | fprintf(stderr, "%12s = %lld / 0x%llx\n", a1, aa1, aa1); | ||
377 | fprintf(stderr, "%12s = %lld / 0x%llx\n", a2, aa2, aa2); | ||
378 | test_die(); | ||
379 | } | ||
380 | |||
381 | void | ||
382 | assert_char(const char *file, int line, const char *a1, const char *a2, | ||
383 | char aa1, char aa2, enum test_predicate pred) | ||
384 | { | ||
385 | char buf[8]; | ||
386 | |||
387 | TEST_CHECK(aa1, aa2, pred); | ||
388 | test_header(file, line, a1, a2, "CHAR", pred); | ||
389 | fprintf(stderr, "%12s = '%s' / 0x02%x\n", a1, | ||
390 | vis(buf, aa1, VIS_SAFE|VIS_NL|VIS_TAB|VIS_OCTAL, 0), aa1); | ||
391 | fprintf(stderr, "%12s = '%s' / 0x02%x\n", a1, | ||
392 | vis(buf, aa2, VIS_SAFE|VIS_NL|VIS_TAB|VIS_OCTAL, 0), aa2); | ||
393 | test_die(); | ||
394 | } | ||
395 | |||
396 | void | ||
397 | assert_u8(const char *file, int line, const char *a1, const char *a2, | ||
398 | u_int8_t aa1, u_int8_t aa2, enum test_predicate pred) | ||
399 | { | ||
400 | TEST_CHECK(aa1, aa2, pred); | ||
401 | test_header(file, line, a1, a2, "U8", pred); | ||
402 | fprintf(stderr, "%12s = 0x%02x %u\n", a1, aa1, aa1); | ||
403 | fprintf(stderr, "%12s = 0x%02x %u\n", a2, aa2, aa2); | ||
404 | test_die(); | ||
405 | } | ||
406 | |||
407 | void | ||
408 | assert_u16(const char *file, int line, const char *a1, const char *a2, | ||
409 | u_int16_t aa1, u_int16_t aa2, enum test_predicate pred) | ||
410 | { | ||
411 | TEST_CHECK(aa1, aa2, pred); | ||
412 | test_header(file, line, a1, a2, "U16", pred); | ||
413 | fprintf(stderr, "%12s = 0x%04x %u\n", a1, aa1, aa1); | ||
414 | fprintf(stderr, "%12s = 0x%04x %u\n", a2, aa2, aa2); | ||
415 | test_die(); | ||
416 | } | ||
417 | |||
418 | void | ||
419 | assert_u32(const char *file, int line, const char *a1, const char *a2, | ||
420 | u_int32_t aa1, u_int32_t aa2, enum test_predicate pred) | ||
421 | { | ||
422 | TEST_CHECK(aa1, aa2, pred); | ||
423 | test_header(file, line, a1, a2, "U32", pred); | ||
424 | fprintf(stderr, "%12s = 0x%08x %u\n", a1, aa1, aa1); | ||
425 | fprintf(stderr, "%12s = 0x%08x %u\n", a2, aa2, aa2); | ||
426 | test_die(); | ||
427 | } | ||
428 | |||
429 | void | ||
430 | assert_u64(const char *file, int line, const char *a1, const char *a2, | ||
431 | u_int64_t aa1, u_int64_t aa2, enum test_predicate pred) | ||
432 | { | ||
433 | TEST_CHECK(aa1, aa2, pred); | ||
434 | test_header(file, line, a1, a2, "U64", pred); | ||
435 | fprintf(stderr, "%12s = 0x%016llx %llu\n", a1, | ||
436 | (unsigned long long)aa1, (unsigned long long)aa1); | ||
437 | fprintf(stderr, "%12s = 0x%016llx %llu\n", a2, | ||
438 | (unsigned long long)aa2, (unsigned long long)aa2); | ||
439 | test_die(); | ||
440 | } | ||
441 | |||
442 | void | ||
443 | assert_ptr(const char *file, int line, const char *a1, const char *a2, | ||
444 | const void *aa1, const void *aa2, enum test_predicate pred) | ||
445 | { | ||
446 | TEST_CHECK(aa1, aa2, pred); | ||
447 | test_header(file, line, a1, a2, "PTR", pred); | ||
448 | fprintf(stderr, "%12s = %p\n", a1, aa1); | ||
449 | fprintf(stderr, "%12s = %p\n", a2, aa2); | ||
450 | test_die(); | ||
451 | } | ||
452 | |||
diff --git a/regress/unittests/test_helper/test_helper.h b/regress/unittests/test_helper/test_helper.h new file mode 100644 index 000000000..6ead92a1c --- /dev/null +++ b/regress/unittests/test_helper/test_helper.h | |||
@@ -0,0 +1,288 @@ | |||
1 | /* $OpenBSD: test_helper.h,v 1.3 2014/05/02 09:41:32 andre Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2011 Damien Miller <djm@mindrot.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | /* Utility functions/framework for regress tests */ | ||
19 | |||
20 | #ifndef _TEST_HELPER_H | ||
21 | #define _TEST_HELPER_H | ||
22 | |||
23 | #include <sys/types.h> | ||
24 | #include <stdint.h> | ||
25 | |||
26 | #include <openssl/bn.h> | ||
27 | #include <openssl/err.h> | ||
28 | |||
29 | enum test_predicate { | ||
30 | TEST_EQ, TEST_NE, TEST_LT, TEST_LE, TEST_GT, TEST_GE | ||
31 | }; | ||
32 | typedef void (test_onerror_func_t)(void *); | ||
33 | |||
34 | /* Supplied by test suite */ | ||
35 | void tests(void); | ||
36 | |||
37 | const char *test_data_file(const char *name); | ||
38 | void test_start(const char *n); | ||
39 | void set_onerror_func(test_onerror_func_t *f, void *ctx); | ||
40 | void test_done(void); | ||
41 | void ssl_err_check(const char *file, int line); | ||
42 | void assert_bignum(const char *file, int line, | ||
43 | const char *a1, const char *a2, | ||
44 | const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred); | ||
45 | void assert_string(const char *file, int line, | ||
46 | const char *a1, const char *a2, | ||
47 | const char *aa1, const char *aa2, enum test_predicate pred); | ||
48 | void assert_mem(const char *file, int line, | ||
49 | const char *a1, const char *a2, | ||
50 | const void *aa1, const void *aa2, size_t l, enum test_predicate pred); | ||
51 | void assert_mem_filled(const char *file, int line, | ||
52 | const char *a1, | ||
53 | const void *aa1, u_char v, size_t l, enum test_predicate pred); | ||
54 | void assert_int(const char *file, int line, | ||
55 | const char *a1, const char *a2, | ||
56 | int aa1, int aa2, enum test_predicate pred); | ||
57 | void assert_size_t(const char *file, int line, | ||
58 | const char *a1, const char *a2, | ||
59 | size_t aa1, size_t aa2, enum test_predicate pred); | ||
60 | void assert_u_int(const char *file, int line, | ||
61 | const char *a1, const char *a2, | ||
62 | u_int aa1, u_int aa2, enum test_predicate pred); | ||
63 | void assert_long_long(const char *file, int line, | ||
64 | const char *a1, const char *a2, | ||
65 | long long aa1, long long aa2, enum test_predicate pred); | ||
66 | void assert_char(const char *file, int line, | ||
67 | const char *a1, const char *a2, | ||
68 | char aa1, char aa2, enum test_predicate pred); | ||
69 | void assert_ptr(const char *file, int line, | ||
70 | const char *a1, const char *a2, | ||
71 | const void *aa1, const void *aa2, enum test_predicate pred); | ||
72 | void assert_u8(const char *file, int line, | ||
73 | const char *a1, const char *a2, | ||
74 | u_int8_t aa1, u_int8_t aa2, enum test_predicate pred); | ||
75 | void assert_u16(const char *file, int line, | ||
76 | const char *a1, const char *a2, | ||
77 | u_int16_t aa1, u_int16_t aa2, enum test_predicate pred); | ||
78 | void assert_u32(const char *file, int line, | ||
79 | const char *a1, const char *a2, | ||
80 | u_int32_t aa1, u_int32_t aa2, enum test_predicate pred); | ||
81 | void assert_u64(const char *file, int line, | ||
82 | const char *a1, const char *a2, | ||
83 | u_int64_t aa1, u_int64_t aa2, enum test_predicate pred); | ||
84 | |||
85 | #define TEST_START(n) test_start(n) | ||
86 | #define TEST_DONE() test_done() | ||
87 | #define TEST_ONERROR(f, c) set_onerror_func(f, c) | ||
88 | #define SSL_ERR_CHECK() ssl_err_check(__FILE__, __LINE__) | ||
89 | |||
90 | #define ASSERT_BIGNUM_EQ(a1, a2) \ | ||
91 | assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
92 | #define ASSERT_STRING_EQ(a1, a2) \ | ||
93 | assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
94 | #define ASSERT_MEM_EQ(a1, a2, l) \ | ||
95 | assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_EQ) | ||
96 | #define ASSERT_MEM_FILLED_EQ(a1, c, l) \ | ||
97 | assert_mem_filled(__FILE__, __LINE__, #a1, a1, c, l, TEST_EQ) | ||
98 | #define ASSERT_MEM_ZERO_EQ(a1, l) \ | ||
99 | assert_mem_filled(__FILE__, __LINE__, #a1, a1, '\0', l, TEST_EQ) | ||
100 | #define ASSERT_INT_EQ(a1, a2) \ | ||
101 | assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
102 | #define ASSERT_SIZE_T_EQ(a1, a2) \ | ||
103 | assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
104 | #define ASSERT_U_INT_EQ(a1, a2) \ | ||
105 | assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
106 | #define ASSERT_LONG_LONG_EQ(a1, a2) \ | ||
107 | assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
108 | #define ASSERT_CHAR_EQ(a1, a2) \ | ||
109 | assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
110 | #define ASSERT_PTR_EQ(a1, a2) \ | ||
111 | assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
112 | #define ASSERT_U8_EQ(a1, a2) \ | ||
113 | assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
114 | #define ASSERT_U16_EQ(a1, a2) \ | ||
115 | assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
116 | #define ASSERT_U32_EQ(a1, a2) \ | ||
117 | assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
118 | #define ASSERT_U64_EQ(a1, a2) \ | ||
119 | assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_EQ) | ||
120 | |||
121 | #define ASSERT_BIGNUM_NE(a1, a2) \ | ||
122 | assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
123 | #define ASSERT_STRING_NE(a1, a2) \ | ||
124 | assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
125 | #define ASSERT_MEM_NE(a1, a2, l) \ | ||
126 | assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_NE) | ||
127 | #define ASSERT_MEM_ZERO_NE(a1, l) \ | ||
128 | assert_mem_filled(__FILE__, __LINE__, #a1, a1, '\0', l, TEST_NE) | ||
129 | #define ASSERT_INT_NE(a1, a2) \ | ||
130 | assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
131 | #define ASSERT_SIZE_T_NE(a1, a2) \ | ||
132 | assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
133 | #define ASSERT_U_INT_NE(a1, a2) \ | ||
134 | assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
135 | #define ASSERT_LONG_LONG_NE(a1, a2) \ | ||
136 | assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
137 | #define ASSERT_CHAR_NE(a1, a2) \ | ||
138 | assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
139 | #define ASSERT_PTR_NE(a1, a2) \ | ||
140 | assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
141 | #define ASSERT_U8_NE(a1, a2) \ | ||
142 | assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
143 | #define ASSERT_U16_NE(a1, a2) \ | ||
144 | assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
145 | #define ASSERT_U32_NE(a1, a2) \ | ||
146 | assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
147 | #define ASSERT_U64_NE(a1, a2) \ | ||
148 | assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_NE) | ||
149 | |||
150 | #define ASSERT_BIGNUM_LT(a1, a2) \ | ||
151 | assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
152 | #define ASSERT_STRING_LT(a1, a2) \ | ||
153 | assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
154 | #define ASSERT_MEM_LT(a1, a2, l) \ | ||
155 | assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_LT) | ||
156 | #define ASSERT_INT_LT(a1, a2) \ | ||
157 | assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
158 | #define ASSERT_SIZE_T_LT(a1, a2) \ | ||
159 | assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
160 | #define ASSERT_U_INT_LT(a1, a2) \ | ||
161 | assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
162 | #define ASSERT_LONG_LONG_LT(a1, a2) \ | ||
163 | assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
164 | #define ASSERT_CHAR_LT(a1, a2) \ | ||
165 | assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
166 | #define ASSERT_PTR_LT(a1, a2) \ | ||
167 | assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
168 | #define ASSERT_U8_LT(a1, a2) \ | ||
169 | assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
170 | #define ASSERT_U16_LT(a1, a2) \ | ||
171 | assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
172 | #define ASSERT_U32_LT(a1, a2) \ | ||
173 | assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
174 | #define ASSERT_U64_LT(a1, a2) \ | ||
175 | assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LT) | ||
176 | |||
177 | #define ASSERT_BIGNUM_LE(a1, a2) \ | ||
178 | assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
179 | #define ASSERT_STRING_LE(a1, a2) \ | ||
180 | assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
181 | #define ASSERT_MEM_LE(a1, a2, l) \ | ||
182 | assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_LE) | ||
183 | #define ASSERT_INT_LE(a1, a2) \ | ||
184 | assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
185 | #define ASSERT_SIZE_T_LE(a1, a2) \ | ||
186 | assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
187 | #define ASSERT_U_INT_LE(a1, a2) \ | ||
188 | assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
189 | #define ASSERT_LONG_LONG_LE(a1, a2) \ | ||
190 | assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
191 | #define ASSERT_CHAR_LE(a1, a2) \ | ||
192 | assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
193 | #define ASSERT_PTR_LE(a1, a2) \ | ||
194 | assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
195 | #define ASSERT_U8_LE(a1, a2) \ | ||
196 | assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
197 | #define ASSERT_U16_LE(a1, a2) \ | ||
198 | assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
199 | #define ASSERT_U32_LE(a1, a2) \ | ||
200 | assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
201 | #define ASSERT_U64_LE(a1, a2) \ | ||
202 | assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_LE) | ||
203 | |||
204 | #define ASSERT_BIGNUM_GT(a1, a2) \ | ||
205 | assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
206 | #define ASSERT_STRING_GT(a1, a2) \ | ||
207 | assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
208 | #define ASSERT_MEM_GT(a1, a2, l) \ | ||
209 | assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_GT) | ||
210 | #define ASSERT_INT_GT(a1, a2) \ | ||
211 | assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
212 | #define ASSERT_SIZE_T_GT(a1, a2) \ | ||
213 | assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
214 | #define ASSERT_U_INT_GT(a1, a2) \ | ||
215 | assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
216 | #define ASSERT_LONG_LONG_GT(a1, a2) \ | ||
217 | assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
218 | #define ASSERT_CHAR_GT(a1, a2) \ | ||
219 | assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
220 | #define ASSERT_PTR_GT(a1, a2) \ | ||
221 | assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
222 | #define ASSERT_U8_GT(a1, a2) \ | ||
223 | assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
224 | #define ASSERT_U16_GT(a1, a2) \ | ||
225 | assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
226 | #define ASSERT_U32_GT(a1, a2) \ | ||
227 | assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
228 | #define ASSERT_U64_GT(a1, a2) \ | ||
229 | assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GT) | ||
230 | |||
231 | #define ASSERT_BIGNUM_GE(a1, a2) \ | ||
232 | assert_bignum(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
233 | #define ASSERT_STRING_GE(a1, a2) \ | ||
234 | assert_string(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
235 | #define ASSERT_MEM_GE(a1, a2, l) \ | ||
236 | assert_mem(__FILE__, __LINE__, #a1, #a2, a1, a2, l, TEST_GE) | ||
237 | #define ASSERT_INT_GE(a1, a2) \ | ||
238 | assert_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
239 | #define ASSERT_SIZE_T_GE(a1, a2) \ | ||
240 | assert_size_t(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
241 | #define ASSERT_U_INT_GE(a1, a2) \ | ||
242 | assert_u_int(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
243 | #define ASSERT_LONG_LONG_GE(a1, a2) \ | ||
244 | assert_long_long(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
245 | #define ASSERT_CHAR_GE(a1, a2) \ | ||
246 | assert_char(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
247 | #define ASSERT_PTR_GE(a1, a2) \ | ||
248 | assert_ptr(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
249 | #define ASSERT_U8_GE(a1, a2) \ | ||
250 | assert_u8(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
251 | #define ASSERT_U16_GE(a1, a2) \ | ||
252 | assert_u16(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
253 | #define ASSERT_U32_GE(a1, a2) \ | ||
254 | assert_u32(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
255 | #define ASSERT_U64_GE(a1, a2) \ | ||
256 | assert_u64(__FILE__, __LINE__, #a1, #a2, a1, a2, TEST_GE) | ||
257 | |||
258 | /* Fuzzing support */ | ||
259 | |||
260 | struct fuzz; | ||
261 | #define FUZZ_1_BIT_FLIP 0x00000001 /* Flip one bit at a time */ | ||
262 | #define FUZZ_2_BIT_FLIP 0x00000002 /* Flip two bits at a time */ | ||
263 | #define FUZZ_1_BYTE_FLIP 0x00000004 /* Flip one byte at a time */ | ||
264 | #define FUZZ_2_BYTE_FLIP 0x00000008 /* Flip two bytes at a time */ | ||
265 | #define FUZZ_TRUNCATE_START 0x00000010 /* Truncate from beginning */ | ||
266 | #define FUZZ_TRUNCATE_END 0x00000020 /* Truncate from end */ | ||
267 | #define FUZZ_BASE64 0x00000040 /* Try all base64 chars */ | ||
268 | #define FUZZ_MAX FUZZ_BASE64 | ||
269 | |||
270 | /* Start fuzzing a blob of data with selected strategies (bitmask) */ | ||
271 | struct fuzz *fuzz_begin(u_int strategies, const void *p, size_t l); | ||
272 | |||
273 | /* Free a fuzz context */ | ||
274 | void fuzz_cleanup(struct fuzz *fuzz); | ||
275 | |||
276 | /* Prepare the next fuzz case in the series */ | ||
277 | void fuzz_next(struct fuzz *fuzz); | ||
278 | |||
279 | /* Determine whether the current fuzz sequence is exhausted (nonzero = yes) */ | ||
280 | int fuzz_done(struct fuzz *fuzz); | ||
281 | |||
282 | /* Return the length and a pointer to the current fuzzed case */ | ||
283 | size_t fuzz_len(struct fuzz *fuzz); | ||
284 | u_char *fuzz_ptr(struct fuzz *fuzz); | ||
285 | |||
286 | /* Dump the current fuzz case to stderr */ | ||
287 | void fuzz_dump(struct fuzz *fuzz); | ||
288 | #endif /* _TEST_HELPER_H */ | ||