test_helper.c (294328) | test_helper.c (294332) |
---|---|
1/* $OpenBSD: test_helper.c,v 1.2 2014/05/02 09:41:32 andre Exp $ */ | 1/* $OpenBSD: test_helper.c,v 1.6 2015/03/03 20:42:49 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 --- 6 unchanged lines hidden (view full) --- 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> | 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 --- 6 unchanged lines hidden (view full) --- 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> |
|
24 25#include <fcntl.h> 26#include <stdio.h> 27#ifdef HAVE_STDINT_H 28# include <stdint.h> 29#endif 30#include <stdlib.h> 31#include <string.h> 32#include <assert.h> 33#include <unistd.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> |
|
34 35#include <openssl/bn.h> 36 37#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) 38# include <vis.h> 39#endif 40 41#include "test_helper.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" |
|
42 43#define TEST_CHECK_INT(r, pred) do { \ 44 switch (pred) { \ 45 case TEST_EQ: \ 46 if (r == 0) \ 47 return; \ 48 break; \ 49 case TEST_NE: \ --- 56 unchanged lines hidden (view full) --- 106 107static int verbose_mode = 0; 108static int quiet_mode = 0; 109static char *active_test_name = NULL; 110static u_int test_number = 0; 111static test_onerror_func_t *test_onerror = NULL; 112static void *onerror_ctx = NULL; 113static const char *data_dir = NULL; | 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: \ --- 56 unchanged lines hidden (view full) --- 109 110static int verbose_mode = 0; 111static int quiet_mode = 0; 112static char *active_test_name = NULL; 113static u_int test_number = 0; 114static test_onerror_func_t *test_onerror = NULL; 115static void *onerror_ctx = NULL; 116static const char *data_dir = NULL; |
117static char subtest_info[512]; |
|
114 115int 116main(int argc, char **argv) 117{ 118 int ch; 119 120 /* Handle systems without __progname */ 121 if (__progname == NULL) { --- 53 unchanged lines hidden (view full) --- 175 fprintf(stderr, "Cannot access data file %s: %s\n", 176 ret, strerror(errno)); 177 exit(1); 178 } 179 return ret; 180} 181 182void | 118 119int 120main(int argc, char **argv) 121{ 122 int ch; 123 124 /* Handle systems without __progname */ 125 if (__progname == NULL) { --- 53 unchanged lines hidden (view full) --- 179 fprintf(stderr, "Cannot access data file %s: %s\n", 180 ret, strerror(errno)); 181 exit(1); 182 } 183 return ret; 184} 185 186void |
187test_info(char *s, size_t len) 188{ 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); 192} 193 194#ifdef SIGINFO 195static void 196siginfo(int unused __attribute__((__unused__))) 197{ 198 char buf[256]; 199 200 test_info(buf, sizeof(buf)); 201 atomicio(vwrite, STDERR_FILENO, buf, strlen(buf)); 202} 203#endif 204 205void |
|
183test_start(const char *n) 184{ 185 assert(active_test_name == NULL); 186 assert((active_test_name = strdup(n)) != NULL); | 206test_start(const char *n) 207{ 208 assert(active_test_name == NULL); 209 assert((active_test_name = strdup(n)) != NULL); |
210 *subtest_info = '\0'; |
|
187 if (verbose_mode) 188 printf("test %u - \"%s\": ", test_number, active_test_name); 189 test_number++; | 211 if (verbose_mode) 212 printf("test %u - \"%s\": ", test_number, active_test_name); 213 test_number++; |
214#ifdef SIGINFO 215 signal(SIGINFO, siginfo); 216#endif |
|
190} 191 192void 193set_onerror_func(test_onerror_func_t *f, void *ctx) 194{ 195 test_onerror = f; 196 onerror_ctx = ctx; 197} 198 199void 200test_done(void) 201{ | 217} 218 219void 220set_onerror_func(test_onerror_func_t *f, void *ctx) 221{ 222 test_onerror = f; 223 onerror_ctx = ctx; 224} 225 226void 227test_done(void) 228{ |
229 *subtest_info = '\0'; |
|
202 assert(active_test_name != NULL); 203 free(active_test_name); 204 active_test_name = NULL; 205 if (verbose_mode) 206 printf("OK\n"); 207 else if (!quiet_mode) { 208 printf("."); 209 fflush(stdout); 210 } 211} 212 213void | 230 assert(active_test_name != NULL); 231 free(active_test_name); 232 active_test_name = NULL; 233 if (verbose_mode) 234 printf("OK\n"); 235 else if (!quiet_mode) { 236 printf("."); 237 fflush(stdout); 238 } 239} 240 241void |
242test_subtest_info(const char *fmt, ...) 243{ 244 va_list ap; 245 246 va_start(ap, fmt); 247 vsnprintf(subtest_info, sizeof(subtest_info), fmt, ap); 248 va_end(ap); 249} 250 251void |
|
214ssl_err_check(const char *file, int line) 215{ 216 long openssl_error = ERR_get_error(); 217 218 if (openssl_error == 0) 219 return; 220 221 fprintf(stderr, "\n%s:%d: uncaught OpenSSL error: %s", --- 29 unchanged lines hidden (view full) --- 251 test_onerror(onerror_ctx); 252 abort(); 253} 254 255static void 256test_header(const char *file, int line, const char *a1, const char *a2, 257 const char *name, enum test_predicate pred) 258{ | 252ssl_err_check(const char *file, int line) 253{ 254 long openssl_error = ERR_get_error(); 255 256 if (openssl_error == 0) 257 return; 258 259 fprintf(stderr, "\n%s:%d: uncaught OpenSSL error: %s", --- 29 unchanged lines hidden (view full) --- 289 test_onerror(onerror_ctx); 290 abort(); 291} 292 293static void 294test_header(const char *file, int line, const char *a1, const char *a2, 295 const char *name, enum test_predicate pred) 296{ |
259 fprintf(stderr, "\n%s:%d test #%u \"%s\"\n", 260 file, line, test_number, active_test_name); | 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); |
261 fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n", 262 name, pred_name(pred), a1, 263 a2 != NULL ? ", " : "", a2 != NULL ? a2 : ""); 264} 265 266void 267assert_bignum(const char *file, int line, const char *a1, const char *a2, 268 const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred) --- 6 unchanged lines hidden (view full) --- 275 fprintf(stderr, "%12s = 0x%s\n", a2, BN_bn2hex(aa2)); 276 test_die(); 277} 278 279void 280assert_string(const char *file, int line, const char *a1, const char *a2, 281 const char *aa1, const char *aa2, enum test_predicate pred) 282{ | 300 fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n", 301 name, pred_name(pred), a1, 302 a2 != NULL ? ", " : "", a2 != NULL ? a2 : ""); 303} 304 305void 306assert_bignum(const char *file, int line, const char *a1, const char *a2, 307 const BIGNUM *aa1, const BIGNUM *aa2, enum test_predicate pred) --- 6 unchanged lines hidden (view full) --- 314 fprintf(stderr, "%12s = 0x%s\n", a2, BN_bn2hex(aa2)); 315 test_die(); 316} 317 318void 319assert_string(const char *file, int line, const char *a1, const char *a2, 320 const char *aa1, const char *aa2, enum test_predicate pred) 321{ |
283 int r = strcmp(aa1, aa2); | 322 int r; |
284 | 323 |
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); 327 328 r = strcmp(aa1, aa2); |
|
285 TEST_CHECK_INT(r, pred); 286 test_header(file, line, a1, a2, "STRING", pred); 287 fprintf(stderr, "%12s = %s (len %zu)\n", a1, aa1, strlen(aa1)); 288 fprintf(stderr, "%12s = %s (len %zu)\n", a2, aa2, strlen(aa2)); 289 test_die(); 290} 291 292static char * --- 12 unchanged lines hidden (view full) --- 305 r[j] = '\0'; 306 return r; 307} 308 309void 310assert_mem(const char *file, int line, const char *a1, const char *a2, 311 const void *aa1, const void *aa2, size_t l, enum test_predicate pred) 312{ | 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)); 333 test_die(); 334} 335 336static char * --- 12 unchanged lines hidden (view full) --- 349 r[j] = '\0'; 350 return r; 351} 352 353void 354assert_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) 356{ |
313 int r = memcmp(aa1, aa2, l); | 357 int r; |
314 | 358 |
359 if (l == 0) 360 return; 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); 364 365 r = memcmp(aa1, aa2, l); |
|
315 TEST_CHECK_INT(r, pred); 316 test_header(file, line, a1, a2, "STRING", pred); 317 fprintf(stderr, "%12s = %s (len %zu)\n", a1, tohex(aa1, MIN(l, 256)), l); 318 fprintf(stderr, "%12s = %s (len %zu)\n", a2, tohex(aa2, MIN(l, 256)), l); 319 test_die(); 320} 321 322static int --- 10 unchanged lines hidden (view full) --- 333 return 0; 334} 335 336void 337assert_mem_filled(const char *file, int line, const char *a1, 338 const void *aa1, u_char v, size_t l, enum test_predicate pred) 339{ 340 size_t where = -1; | 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); 370 test_die(); 371} 372 373static int --- 10 unchanged lines hidden (view full) --- 384 return 0; 385} 386 387void 388assert_mem_filled(const char *file, int line, const char *a1, 389 const void *aa1, u_char v, size_t l, enum test_predicate pred) 390{ 391 size_t where = -1; |
341 int r = memvalcmp(aa1, v, l, &where); | 392 int r; |
342 char tmp[64]; 343 344 if (l == 0) 345 return; | 393 char tmp[64]; 394 395 if (l == 0) 396 return; |
397 /* If length is >0, then verify the pointer is not NULL */ 398 assert_ptr(file, line, a1, "NULL", aa1, NULL, TEST_NE); 399 400 r = memvalcmp(aa1, v, l, &where); |
|
346 TEST_CHECK_INT(r, pred); 347 test_header(file, line, a1, NULL, "MEM_ZERO", pred); 348 fprintf(stderr, "%20s = %s%s (len %zu)\n", a1, 349 tohex(aa1, MIN(l, 20)), l > 20 ? "..." : "", l); 350 snprintf(tmp, sizeof(tmp), "(%s)[%zu]", a1, where); 351 fprintf(stderr, "%20s = 0x%02x (expected 0x%02x)\n", tmp, 352 ((u_char *)aa1)[where], v); 353 test_die(); --- 118 unchanged lines hidden --- | 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); 408 test_die(); --- 118 unchanged lines hidden --- |