1 /*	$OpenBSD: test_helper.c,v 1.8 2018/02/08 08:46:20 djm 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 "includes.h"
21 
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/uio.h>
25 
26 #include <fcntl.h>
27 #include <stdio.h>
28 #ifdef HAVE_STDINT_H
29 # include <stdint.h>
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <unistd.h>
35 #include <signal.h>
36 
37 #include <openssl/bn.h>
38 
39 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
40 # include <vis.h>
41 #endif
42 
43 #include "test_helper.h"
44 #include "atomicio.h"
45 
46 #define TEST_CHECK_INT(r, pred) do {		\
47 		switch (pred) {			\
48 		case TEST_EQ:			\
49 			if (r == 0)		\
50 				return;		\
51 			break;			\
52 		case TEST_NE:			\
53 			if (r != 0)		\
54 				return;		\
55 			break;			\
56 		case TEST_LT:			\
57 			if (r < 0)		\
58 				return;		\
59 			break;			\
60 		case TEST_LE:			\
61 			if (r <= 0)		\
62 				return;		\
63 			break;			\
64 		case TEST_GT:			\
65 			if (r > 0)		\
66 				return;		\
67 			break;			\
68 		case TEST_GE:			\
69 			if (r >= 0)		\
70 				return;		\
71 			break;			\
72 		default:			\
73 			abort();		\
74 		}				\
75 	} while (0)
76 
77 #define TEST_CHECK(x1, x2, pred) do {		\
78 		switch (pred) {			\
79 		case TEST_EQ:			\
80 			if (x1 == x2)		\
81 				return;		\
82 			break;			\
83 		case TEST_NE:			\
84 			if (x1 != x2)		\
85 				return;		\
86 			break;			\
87 		case TEST_LT:			\
88 			if (x1 < x2)		\
89 				return;		\
90 			break;			\
91 		case TEST_LE:			\
92 			if (x1 <= x2)		\
93 				return;		\
94 			break;			\
95 		case TEST_GT:			\
96 			if (x1 > x2)		\
97 				return;		\
98 			break;			\
99 		case TEST_GE:			\
100 			if (x1 >= x2)		\
101 				return;		\
102 			break;			\
103 		default:			\
104 			abort();		\
105 		}				\
106 	} while (0)
107 
108 extern char *__progname;
109 
110 static int verbose_mode = 0;
111 static int quiet_mode = 0;
112 static char *active_test_name = NULL;
113 static u_int test_number = 0;
114 static test_onerror_func_t *test_onerror = NULL;
115 static void *onerror_ctx = NULL;
116 static const char *data_dir = NULL;
117 static char subtest_info[512];
118 
119 int
120 main(int argc, char **argv)
121 {
122 	int ch;
123 
124 	/* Handle systems without __progname */
125 	if (__progname == NULL) {
126 		__progname = strrchr(argv[0], '/');
127 		if (__progname == NULL || __progname[1] == '\0')
128 			__progname = argv[0];
129 		else
130 			__progname++;
131 		if ((__progname = strdup(__progname)) == NULL) {
132 			fprintf(stderr, "strdup failed\n");
133 			exit(1);
134 		}
135 	}
136 
137 	while ((ch = getopt(argc, argv, "vqd:")) != -1) {
138 		switch (ch) {
139 		case 'd':
140 			data_dir = optarg;
141 			break;
142 		case 'q':
143 			verbose_mode = 0;
144 			quiet_mode = 1;
145 			break;
146 		case 'v':
147 			verbose_mode = 1;
148 			quiet_mode = 0;
149 			break;
150 		default:
151 			fprintf(stderr, "Unrecognised command line option\n");
152 			fprintf(stderr, "Usage: %s [-v]\n", __progname);
153 			exit(1);
154 		}
155 	}
156 	setvbuf(stdout, NULL, _IONBF, 0);
157 	if (!quiet_mode)
158 		printf("%s: ", __progname);
159 	if (verbose_mode)
160 		printf("\n");
161 
162 	tests();
163 
164 	if (!quiet_mode)
165 		printf(" %u tests ok\n", test_number);
166 	return 0;
167 }
168 
169 int
170 test_is_verbose()
171 {
172 	return verbose_mode;
173 }
174 
175 int
176 test_is_quiet()
177 {
178 	return quiet_mode;
179 }
180 
181 const char *
182 test_data_file(const char *name)
183 {
184 	static char ret[PATH_MAX];
185 
186 	if (data_dir != NULL)
187 		snprintf(ret, sizeof(ret), "%s/%s", data_dir, name);
188 	else
189 		strlcpy(ret, name, sizeof(ret));
190 	if (access(ret, F_OK) != 0) {
191 		fprintf(stderr, "Cannot access data file %s: %s\n",
192 		    ret, strerror(errno));
193 		exit(1);
194 	}
195 	return ret;
196 }
197 
198 void
199 test_info(char *s, size_t len)
200 {
201 	snprintf(s, len, "In test %u: \"%s\"%s%s\n", test_number,
202 	    active_test_name == NULL ? "<none>" : active_test_name,
203 	    *subtest_info != '\0' ? " - " : "", subtest_info);
204 }
205 
206 #ifdef SIGINFO
207 static void
208 siginfo(int unused __attribute__((__unused__)))
209 {
210 	char buf[256];
211 
212 	test_info(buf, sizeof(buf));
213 	atomicio(vwrite, STDERR_FILENO, buf, strlen(buf));
214 }
215 #endif
216 
217 void
218 test_start(const char *n)
219 {
220 	assert(active_test_name == NULL);
221 	assert((active_test_name = strdup(n)) != NULL);
222 	*subtest_info = '\0';
223 	if (verbose_mode)
224 		printf("test %u - \"%s\": ", test_number, active_test_name);
225 	test_number++;
226 #ifdef SIGINFO
227 	signal(SIGINFO, siginfo);
228 #endif
229 }
230 
231 void
232 set_onerror_func(test_onerror_func_t *f, void *ctx)
233 {
234 	test_onerror = f;
235 	onerror_ctx = ctx;
236 }
237 
238 void
239 test_done(void)
240 {
241 	*subtest_info = '\0';
242 	assert(active_test_name != NULL);
243 	free(active_test_name);
244 	active_test_name = NULL;
245 	if (verbose_mode)
246 		printf("OK\n");
247 	else if (!quiet_mode) {
248 		printf(".");
249 		fflush(stdout);
250 	}
251 }
252 
253 void
254 test_subtest_info(const char *fmt, ...)
255 {
256 	va_list ap;
257 
258 	va_start(ap, fmt);
259 	vsnprintf(subtest_info, sizeof(subtest_info), fmt, ap);
260 	va_end(ap);
261 }
262 
263 void
264 ssl_err_check(const char *file, int line)
265 {
266 	long openssl_error = ERR_get_error();
267 
268 	if (openssl_error == 0)
269 		return;
270 
271 	fprintf(stderr, "\n%s:%d: uncaught OpenSSL error: %s",
272 	    file, line, ERR_error_string(openssl_error, NULL));
273 	abort();
274 }
275 
276 static const char *
277 pred_name(enum test_predicate p)
278 {
279 	switch (p) {
280 	case TEST_EQ:
281 		return "EQ";
282 	case TEST_NE:
283 		return "NE";
284 	case TEST_LT:
285 		return "LT";
286 	case TEST_LE:
287 		return "LE";
288 	case TEST_GT:
289 		return "GT";
290 	case TEST_GE:
291 		return "GE";
292 	default:
293 		return "UNKNOWN";
294 	}
295 }
296 
297 static void
298 test_die(void)
299 {
300 	if (test_onerror != NULL)
301 		test_onerror(onerror_ctx);
302 	abort();
303 }
304 
305 static void
306 test_header(const char *file, int line, const char *a1, const char *a2,
307     const char *name, enum test_predicate pred)
308 {
309 	fprintf(stderr, "\n%s:%d test #%u \"%s\"%s%s\n",
310 	    file, line, test_number, active_test_name,
311 	    *subtest_info != '\0' ? " - " : "", subtest_info);
312 	fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n",
313 	    name, pred_name(pred), a1,
314 	    a2 != NULL ? ", " : "", a2 != NULL ? a2 : "");
315 }
316 
317 void
318 assert_bignum(const char *file, int line, const char *a1, const char *a2,
319     const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred)
320 {
321 	int r = BN_cmp(aa1, aa2);
322 
323 	TEST_CHECK_INT(r, pred);
324 	test_header(file, line, a1, a2, "BIGNUM", pred);
325 	fprintf(stderr, "%12s = 0x%s\n", a1, BN_bn2hex(aa1));
326 	fprintf(stderr, "%12s = 0x%s\n", a2, BN_bn2hex(aa2));
327 	test_die();
328 }
329 
330 void
331 assert_string(const char *file, int line, const char *a1, const char *a2,
332     const char *aa1, const char *aa2, enum test_predicate pred)
333 {
334 	int r;
335 
336 	/* Verify pointers are not NULL */
337 	assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE);
338 	assert_ptr(file, line, a2, "NULL", aa2, NULL, TEST_NE);
339 
340 	r = strcmp(aa1, aa2);
341 	TEST_CHECK_INT(r, pred);
342 	test_header(file, line, a1, a2, "STRING", pred);
343 	fprintf(stderr, "%12s = %s (len %zu)\n", a1, aa1, strlen(aa1));
344 	fprintf(stderr, "%12s = %s (len %zu)\n", a2, aa2, strlen(aa2));
345 	test_die();
346 }
347 
348 static char *
349 tohex(const void *_s, size_t l)
350 {
351 	u_int8_t *s = (u_int8_t *)_s;
352 	size_t i, j;
353 	const char *hex = "0123456789abcdef";
354 	char *r = malloc((l * 2) + 1);
355 
356 	assert(r != NULL);
357 	for (i = j = 0; i < l; i++) {
358 		r[j++] = hex[(s[i] >> 4) & 0xf];
359 		r[j++] = hex[s[i] & 0xf];
360 	}
361 	r[j] = '\0';
362 	return r;
363 }
364 
365 void
366 assert_mem(const char *file, int line, const char *a1, const char *a2,
367     const void *aa1, const void *aa2, size_t l, enum test_predicate pred)
368 {
369 	int r;
370 
371 	if (l == 0)
372 		return;
373 	/* If length is >0, then verify pointers are not NULL */
374 	assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE);
375 	assert_ptr(file, line, a2, "NULL", aa2, NULL, TEST_NE);
376 
377 	r = memcmp(aa1, aa2, l);
378 	TEST_CHECK_INT(r, pred);
379 	test_header(file, line, a1, a2, "STRING", pred);
380 	fprintf(stderr, "%12s = %s (len %zu)\n", a1, tohex(aa1, MIN(l, 256)), l);
381 	fprintf(stderr, "%12s = %s (len %zu)\n", a2, tohex(aa2, MIN(l, 256)), l);
382 	test_die();
383 }
384 
385 static int
386 memvalcmp(const u_int8_t *s, u_char v, size_t l, size_t *where)
387 {
388 	size_t i;
389 
390 	for (i = 0; i < l; i++) {
391 		if (s[i] != v) {
392 			*where = i;
393 			return 1;
394 		}
395 	}
396 	return 0;
397 }
398 
399 void
400 assert_mem_filled(const char *file, int line, const char *a1,
401     const void *aa1, u_char v, size_t l, enum test_predicate pred)
402 {
403 	size_t where = -1;
404 	int r;
405 	char tmp[64];
406 
407 	if (l == 0)
408 		return;
409 	/* If length is >0, then verify the pointer is not NULL */
410 	assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE);
411 
412 	r = memvalcmp(aa1, v, l, &where);
413 	TEST_CHECK_INT(r, pred);
414 	test_header(file, line, a1, NULL, "MEM_ZERO", pred);
415 	fprintf(stderr, "%20s = %s%s (len %zu)\n", a1,
416 	    tohex(aa1, MIN(l, 20)), l > 20 ? "..." : "", l);
417 	snprintf(tmp, sizeof(tmp), "(%s)[%zu]", a1, where);
418 	fprintf(stderr, "%20s = 0x%02x (expected 0x%02x)\n", tmp,
419 	    ((u_char *)aa1)[where], v);
420 	test_die();
421 }
422 
423 void
424 assert_int(const char *file, int line, const char *a1, const char *a2,
425     int aa1, int aa2, enum test_predicate pred)
426 {
427 	TEST_CHECK(aa1, aa2, pred);
428 	test_header(file, line, a1, a2, "INT", pred);
429 	fprintf(stderr, "%12s = %d\n", a1, aa1);
430 	fprintf(stderr, "%12s = %d\n", a2, aa2);
431 	test_die();
432 }
433 
434 void
435 assert_size_t(const char *file, int line, const char *a1, const char *a2,
436     size_t aa1, size_t aa2, enum test_predicate pred)
437 {
438 	TEST_CHECK(aa1, aa2, pred);
439 	test_header(file, line, a1, a2, "SIZE_T", pred);
440 	fprintf(stderr, "%12s = %zu\n", a1, aa1);
441 	fprintf(stderr, "%12s = %zu\n", a2, aa2);
442 	test_die();
443 }
444 
445 void
446 assert_u_int(const char *file, int line, const char *a1, const char *a2,
447     u_int aa1, u_int aa2, enum test_predicate pred)
448 {
449 	TEST_CHECK(aa1, aa2, pred);
450 	test_header(file, line, a1, a2, "U_INT", pred);
451 	fprintf(stderr, "%12s = %u / 0x%x\n", a1, aa1, aa1);
452 	fprintf(stderr, "%12s = %u / 0x%x\n", a2, aa2, aa2);
453 	test_die();
454 }
455 
456 void
457 assert_long(const char *file, int line, const char *a1, const char *a2,
458     long aa1, long aa2, enum test_predicate pred)
459 {
460 	TEST_CHECK(aa1, aa2, pred);
461 	test_header(file, line, a1, a2, "LONG", pred);
462 	fprintf(stderr, "%12s = %ld / 0x%lx\n", a1, aa1, aa1);
463 	fprintf(stderr, "%12s = %ld / 0x%lx\n", a2, aa2, aa2);
464 	test_die();
465 }
466 
467 void
468 assert_long_long(const char *file, int line, const char *a1, const char *a2,
469     long long aa1, long long aa2, enum test_predicate pred)
470 {
471 	TEST_CHECK(aa1, aa2, pred);
472 	test_header(file, line, a1, a2, "LONG LONG", pred);
473 	fprintf(stderr, "%12s = %lld / 0x%llx\n", a1, aa1, aa1);
474 	fprintf(stderr, "%12s = %lld / 0x%llx\n", a2, aa2, aa2);
475 	test_die();
476 }
477 
478 void
479 assert_char(const char *file, int line, const char *a1, const char *a2,
480     char aa1, char aa2, enum test_predicate pred)
481 {
482 	char buf[8];
483 
484 	TEST_CHECK(aa1, aa2, pred);
485 	test_header(file, line, a1, a2, "CHAR", pred);
486 	fprintf(stderr, "%12s = '%s' / 0x02%x\n", a1,
487 	    vis(buf, aa1, VIS_SAFE|VIS_NL|VIS_TAB|VIS_OCTAL, 0), aa1);
488 	fprintf(stderr, "%12s = '%s' / 0x02%x\n", a1,
489 	    vis(buf, aa2, VIS_SAFE|VIS_NL|VIS_TAB|VIS_OCTAL, 0), aa2);
490 	test_die();
491 }
492 
493 void
494 assert_u8(const char *file, int line, const char *a1, const char *a2,
495     u_int8_t aa1, u_int8_t aa2, enum test_predicate pred)
496 {
497 	TEST_CHECK(aa1, aa2, pred);
498 	test_header(file, line, a1, a2, "U8", pred);
499 	fprintf(stderr, "%12s = 0x%02x %u\n", a1, aa1, aa1);
500 	fprintf(stderr, "%12s = 0x%02x %u\n", a2, aa2, aa2);
501 	test_die();
502 }
503 
504 void
505 assert_u16(const char *file, int line, const char *a1, const char *a2,
506     u_int16_t aa1, u_int16_t aa2, enum test_predicate pred)
507 {
508 	TEST_CHECK(aa1, aa2, pred);
509 	test_header(file, line, a1, a2, "U16", pred);
510 	fprintf(stderr, "%12s = 0x%04x %u\n", a1, aa1, aa1);
511 	fprintf(stderr, "%12s = 0x%04x %u\n", a2, aa2, aa2);
512 	test_die();
513 }
514 
515 void
516 assert_u32(const char *file, int line, const char *a1, const char *a2,
517     u_int32_t aa1, u_int32_t aa2, enum test_predicate pred)
518 {
519 	TEST_CHECK(aa1, aa2, pred);
520 	test_header(file, line, a1, a2, "U32", pred);
521 	fprintf(stderr, "%12s = 0x%08x %u\n", a1, aa1, aa1);
522 	fprintf(stderr, "%12s = 0x%08x %u\n", a2, aa2, aa2);
523 	test_die();
524 }
525 
526 void
527 assert_u64(const char *file, int line, const char *a1, const char *a2,
528     u_int64_t aa1, u_int64_t aa2, enum test_predicate pred)
529 {
530 	TEST_CHECK(aa1, aa2, pred);
531 	test_header(file, line, a1, a2, "U64", pred);
532 	fprintf(stderr, "%12s = 0x%016llx %llu\n", a1,
533 	    (unsigned long long)aa1, (unsigned long long)aa1);
534 	fprintf(stderr, "%12s = 0x%016llx %llu\n", a2,
535 	    (unsigned long long)aa2, (unsigned long long)aa2);
536 	test_die();
537 }
538 
539 void
540 assert_ptr(const char *file, int line, const char *a1, const char *a2,
541     const void *aa1, const void *aa2, enum test_predicate pred)
542 {
543 	TEST_CHECK(aa1, aa2, pred);
544 	test_header(file, line, a1, a2, "PTR", pred);
545 	fprintf(stderr, "%12s = %p\n", a1, aa1);
546 	fprintf(stderr, "%12s = %p\n", a2, aa2);
547 	test_die();
548 }
549 
550