1 /* $OpenBSD: test_helper.c,v 1.6 2015/03/03 20:42:49 djm Exp $ */
3 * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
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.
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.
18 /* Utility functions/framework for regress tests */
22 #include <sys/types.h>
23 #include <sys/param.h>
37 #include <openssl/bn.h>
39 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
43 #include "test_helper.h"
46 #define TEST_CHECK_INT(r, pred) do { \
77 #define TEST_CHECK(x1, x2, pred) do { \
108 extern char *__progname;
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];
120 main(int argc, char **argv)
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];
131 if ((__progname = strdup(__progname)) == NULL) {
132 fprintf(stderr, "strdup failed\n");
137 while ((ch = getopt(argc, argv, "vqd:")) != -1) {
151 fprintf(stderr, "Unrecognised command line option\n");
152 fprintf(stderr, "Usage: %s [-v]\n", __progname);
156 setvbuf(stdout, NULL, _IONBF, 0);
158 printf("%s: ", __progname);
165 printf(" %u tests ok\n", test_number);
170 test_data_file(const char *name)
172 static char ret[PATH_MAX];
174 if (data_dir != NULL)
175 snprintf(ret, sizeof(ret), "%s/%s", data_dir, name);
177 strlcpy(ret, name, sizeof(ret));
178 if (access(ret, F_OK) != 0) {
179 fprintf(stderr, "Cannot access data file %s: %s\n",
180 ret, strerror(errno));
187 test_info(char *s, size_t len)
189 snprintf(s, len, "In test %u: \"%s\"%s%s\n", test_number,
190 active_test_name == NULL ? "<none>" : active_test_name,
191 *subtest_info != '\0' ? " - " : "", subtest_info);
196 siginfo(int unused __attribute__((__unused__)))
200 test_info(buf, sizeof(buf));
201 atomicio(vwrite, STDERR_FILENO, buf, strlen(buf));
206 test_start(const char *n)
208 assert(active_test_name == NULL);
209 assert((active_test_name = strdup(n)) != NULL);
210 *subtest_info = '\0';
212 printf("test %u - \"%s\": ", test_number, active_test_name);
215 signal(SIGINFO, siginfo);
220 set_onerror_func(test_onerror_func_t *f, void *ctx)
229 *subtest_info = '\0';
230 assert(active_test_name != NULL);
231 free(active_test_name);
232 active_test_name = NULL;
235 else if (!quiet_mode) {
242 test_subtest_info(const char *fmt, ...)
247 vsnprintf(subtest_info, sizeof(subtest_info), fmt, ap);
252 ssl_err_check(const char *file, int line)
254 long openssl_error = ERR_get_error();
256 if (openssl_error == 0)
259 fprintf(stderr, "\n%s:%d: uncaught OpenSSL error: %s",
260 file, line, ERR_error_string(openssl_error, NULL));
265 pred_name(enum test_predicate p)
288 if (test_onerror != NULL)
289 test_onerror(onerror_ctx);
294 test_header(const char *file, int line, const char *a1, const char *a2,
295 const char *name, enum test_predicate pred)
297 fprintf(stderr, "\n%s:%d test #%u \"%s\"%s%s\n",
298 file, line, test_number, active_test_name,
299 *subtest_info != '\0' ? " - " : "", subtest_info);
300 fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n",
301 name, pred_name(pred), a1,
302 a2 != NULL ? ", " : "", a2 != NULL ? a2 : "");
306 assert_bignum(const char *file, int line, const char *a1, const char *a2,
307 const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred)
309 int r = BN_cmp(aa1, aa2);
311 TEST_CHECK_INT(r, pred);
312 test_header(file, line, a1, a2, "BIGNUM", pred);
313 fprintf(stderr, "%12s = 0x%s\n", a1, BN_bn2hex(aa1));
314 fprintf(stderr, "%12s = 0x%s\n", a2, BN_bn2hex(aa2));
319 assert_string(const char *file, int line, const char *a1, const char *a2,
320 const char *aa1, const char *aa2, enum test_predicate pred)
324 /* Verify pointers are not NULL */
325 assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE);
326 assert_ptr(file, line, a2, "NULL", aa2, NULL, TEST_NE);
328 r = strcmp(aa1, aa2);
329 TEST_CHECK_INT(r, pred);
330 test_header(file, line, a1, a2, "STRING", pred);
331 fprintf(stderr, "%12s = %s (len %zu)\n", a1, aa1, strlen(aa1));
332 fprintf(stderr, "%12s = %s (len %zu)\n", a2, aa2, strlen(aa2));
337 tohex(const void *_s, size_t l)
339 u_int8_t *s = (u_int8_t *)_s;
341 const char *hex = "0123456789abcdef";
342 char *r = malloc((l * 2) + 1);
345 for (i = j = 0; i < l; i++) {
346 r[j++] = hex[(s[i] >> 4) & 0xf];
347 r[j++] = hex[s[i] & 0xf];
354 assert_mem(const char *file, int line, const char *a1, const char *a2,
355 const void *aa1, const void *aa2, size_t l, enum test_predicate pred)
361 /* If length is >0, then verify pointers are not NULL */
362 assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE);
363 assert_ptr(file, line, a2, "NULL", aa2, NULL, TEST_NE);
365 r = memcmp(aa1, aa2, l);
366 TEST_CHECK_INT(r, pred);
367 test_header(file, line, a1, a2, "STRING", pred);
368 fprintf(stderr, "%12s = %s (len %zu)\n", a1, tohex(aa1, MIN(l, 256)), l);
369 fprintf(stderr, "%12s = %s (len %zu)\n", a2, tohex(aa2, MIN(l, 256)), l);
374 memvalcmp(const u_int8_t *s, u_char v, size_t l, size_t *where)
378 for (i = 0; i < l; i++) {
388 assert_mem_filled(const char *file, int line, const char *a1,
389 const void *aa1, u_char v, size_t l, enum test_predicate pred)
397 /* If length is >0, then verify the pointer is not NULL */
398 assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE);
400 r = memvalcmp(aa1, v, l, &where);
401 TEST_CHECK_INT(r, pred);
402 test_header(file, line, a1, NULL, "MEM_ZERO", pred);
403 fprintf(stderr, "%20s = %s%s (len %zu)\n", a1,
404 tohex(aa1, MIN(l, 20)), l > 20 ? "..." : "", l);
405 snprintf(tmp, sizeof(tmp), "(%s)[%zu]", a1, where);
406 fprintf(stderr, "%20s = 0x%02x (expected 0x%02x)\n", tmp,
407 ((u_char *)aa1)[where], v);
412 assert_int(const char *file, int line, const char *a1, const char *a2,
413 int aa1, int aa2, enum test_predicate pred)
415 TEST_CHECK(aa1, aa2, pred);
416 test_header(file, line, a1, a2, "INT", pred);
417 fprintf(stderr, "%12s = %d\n", a1, aa1);
418 fprintf(stderr, "%12s = %d\n", a2, aa2);
423 assert_size_t(const char *file, int line, const char *a1, const char *a2,
424 size_t aa1, size_t aa2, enum test_predicate pred)
426 TEST_CHECK(aa1, aa2, pred);
427 test_header(file, line, a1, a2, "SIZE_T", pred);
428 fprintf(stderr, "%12s = %zu\n", a1, aa1);
429 fprintf(stderr, "%12s = %zu\n", a2, aa2);
434 assert_u_int(const char *file, int line, const char *a1, const char *a2,
435 u_int aa1, u_int aa2, enum test_predicate pred)
437 TEST_CHECK(aa1, aa2, pred);
438 test_header(file, line, a1, a2, "U_INT", pred);
439 fprintf(stderr, "%12s = %u / 0x%x\n", a1, aa1, aa1);
440 fprintf(stderr, "%12s = %u / 0x%x\n", a2, aa2, aa2);
445 assert_long_long(const char *file, int line, const char *a1, const char *a2,
446 long long aa1, long long aa2, enum test_predicate pred)
448 TEST_CHECK(aa1, aa2, pred);
449 test_header(file, line, a1, a2, "LONG LONG", pred);
450 fprintf(stderr, "%12s = %lld / 0x%llx\n", a1, aa1, aa1);
451 fprintf(stderr, "%12s = %lld / 0x%llx\n", a2, aa2, aa2);
456 assert_char(const char *file, int line, const char *a1, const char *a2,
457 char aa1, char aa2, enum test_predicate pred)
461 TEST_CHECK(aa1, aa2, pred);
462 test_header(file, line, a1, a2, "CHAR", pred);
463 fprintf(stderr, "%12s = '%s' / 0x02%x\n", a1,
464 vis(buf, aa1, VIS_SAFE|VIS_NL|VIS_TAB|VIS_OCTAL, 0), aa1);
465 fprintf(stderr, "%12s = '%s' / 0x02%x\n", a1,
466 vis(buf, aa2, VIS_SAFE|VIS_NL|VIS_TAB|VIS_OCTAL, 0), aa2);
471 assert_u8(const char *file, int line, const char *a1, const char *a2,
472 u_int8_t aa1, u_int8_t aa2, enum test_predicate pred)
474 TEST_CHECK(aa1, aa2, pred);
475 test_header(file, line, a1, a2, "U8", pred);
476 fprintf(stderr, "%12s = 0x%02x %u\n", a1, aa1, aa1);
477 fprintf(stderr, "%12s = 0x%02x %u\n", a2, aa2, aa2);
482 assert_u16(const char *file, int line, const char *a1, const char *a2,
483 u_int16_t aa1, u_int16_t aa2, enum test_predicate pred)
485 TEST_CHECK(aa1, aa2, pred);
486 test_header(file, line, a1, a2, "U16", pred);
487 fprintf(stderr, "%12s = 0x%04x %u\n", a1, aa1, aa1);
488 fprintf(stderr, "%12s = 0x%04x %u\n", a2, aa2, aa2);
493 assert_u32(const char *file, int line, const char *a1, const char *a2,
494 u_int32_t aa1, u_int32_t aa2, enum test_predicate pred)
496 TEST_CHECK(aa1, aa2, pred);
497 test_header(file, line, a1, a2, "U32", pred);
498 fprintf(stderr, "%12s = 0x%08x %u\n", a1, aa1, aa1);
499 fprintf(stderr, "%12s = 0x%08x %u\n", a2, aa2, aa2);
504 assert_u64(const char *file, int line, const char *a1, const char *a2,
505 u_int64_t aa1, u_int64_t aa2, enum test_predicate pred)
507 TEST_CHECK(aa1, aa2, pred);
508 test_header(file, line, a1, a2, "U64", pred);
509 fprintf(stderr, "%12s = 0x%016llx %llu\n", a1,
510 (unsigned long long)aa1, (unsigned long long)aa1);
511 fprintf(stderr, "%12s = 0x%016llx %llu\n", a2,
512 (unsigned long long)aa2, (unsigned long long)aa2);
517 assert_ptr(const char *file, int line, const char *a1, const char *a2,
518 const void *aa1, const void *aa2, enum test_predicate pred)
520 TEST_CHECK(aa1, aa2, pred);
521 test_header(file, line, a1, a2, "PTR", pred);
522 fprintf(stderr, "%12s = %p\n", a1, aa1);
523 fprintf(stderr, "%12s = %p\n", a2, aa2);