2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
18 #include <openssl/opensslconf.h>
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
22 /* The size of memory buffers to display on failure */
23 #define MEM_BUFFER_SIZE (21)
26 * Declares the structures needed to register each test case function.
28 typedef struct test_info {
29 const char *test_case_name;
31 int (*param_test_fn)(int idx);
35 static TEST_INFO all_tests[1024];
36 static int num_tests = 0;
38 * A parameterised tests runs a loop of test cases.
39 * |num_test_cases| counts the total number of test cases
42 static int num_test_cases = 0;
44 void add_test(const char *test_case_name, int (*test_fn) ())
46 assert(num_tests != OSSL_NELEM(all_tests));
47 all_tests[num_tests].test_case_name = test_case_name;
48 all_tests[num_tests].test_fn = test_fn;
49 all_tests[num_tests].num = -1;
54 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
57 assert(num_tests != OSSL_NELEM(all_tests));
58 all_tests[num_tests].test_case_name = test_case_name;
59 all_tests[num_tests].param_test_fn = test_fn;
60 all_tests[num_tests].num = num;
62 num_test_cases += num;
65 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
66 static int should_report_leaks()
69 * When compiled with enable-crypto-mdebug, OPENSSL_DEBUG_MEMORY=0
70 * can be used to disable leak checking at runtime.
71 * Note this only works when running the test binary manually;
72 * the test harness always enables OPENSSL_DEBUG_MEMORY.
74 char *mem_debug_env = getenv("OPENSSL_DEBUG_MEMORY");
76 return mem_debug_env == NULL
77 || (strcmp(mem_debug_env, "0") && strcmp(mem_debug_env, ""));
84 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
85 if (should_report_leaks()) {
86 CRYPTO_set_mem_debug(1);
87 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
92 int finish_test(int ret)
94 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
95 if (should_report_leaks() && CRYPTO_mem_leaks_fp(stderr) <= 0)
101 static void finalize(int success)
106 ERR_print_errors_fp(stderr);
109 int run_tests(const char *test_prog_name)
115 printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
116 num_test_cases == 1 ? "" : "s");
119 for (i = 0; i != num_tests; ++i) {
120 if (all_tests[i].num == -1) {
121 int ret = all_tests[i].test_fn();
124 printf("** %s failed **\n--------\n",
125 all_tests[i].test_case_name);
131 for (j = 0; j < all_tests[i].num; j++) {
132 int ret = all_tests[i].param_test_fn(j);
135 printf("** %s failed test %d\n--------\n",
136 all_tests[i].test_case_name, j);
145 if (num_failed != 0) {
146 printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
147 num_failed, num_failed != 1 ? "s" : "", num_test_cases);
151 printf(" All tests passed.\n");
157 * A common routine to output test failure messages. Generally this should not
158 * be called directly, rather it should be called by the following functions.
160 * |desc| is a printf formatted description with arguments |args| that is
161 * supplied by the user and |desc| can be NULL. |type| is the data type
162 * that was tested (int, char, ptr, ...). |fmt| is a system provided
163 * printf format with following arguments that spell out the failure
164 * details i.e. the actual values compared and the operator used.
166 * The typical use for this is from an utility test function:
168 * int test6(const char *file, int line, int n) {
170 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
176 * calling test6(3, "oops") will return 0 and produce out along the lines of:
177 * FAIL oops: (int) value 3 is not 6\n
179 * It general, test_fail_message should not be called directly.
181 static void test_fail_message(const char *prefix, const char *file, int line,
182 const char *type, const char *fmt, ...)
185 static void test_fail_message_va(const char *prefix, const char *file, int line,
186 const char *type, const char *fmt, va_list ap)
188 fputs(prefix != NULL ? prefix : "ERROR", stderr);
191 fprintf(stderr, " (%s)", type);
194 vfprintf(stderr, fmt, ap);
197 fprintf(stderr, " @ %s:%d", file, line);
202 static void test_fail_message(const char *prefix, const char *file, int line,
203 const char *type, const char *fmt, ...)
207 test_fail_message_va(prefix, file, line, type, fmt, ap);
211 void test_info_c90(const char *desc, ...)
216 test_fail_message_va("INFO", NULL, -1, NULL, desc, ap);
220 void test_info(const char *file, int line, const char *desc, ...)
225 test_fail_message_va("INFO", file, line, NULL, desc, ap);
229 void test_error_c90(const char *desc, ...)
234 test_fail_message(NULL, NULL, -1, NULL, desc, ap);
238 void test_error(const char *file, int line, const char *desc, ...)
243 test_fail_message_va(NULL, file, line, NULL, desc, ap);
248 * Define some comparisons between pairs of various types.
249 * These functions return 1 if the test is true.
250 * Otherwise, they return 0 and pretty-print diagnostics.
252 * In each case the functions produced are:
253 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
254 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
255 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
256 * int test_name_le(const type t1, const type t2, const char *desc, ...);
257 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
258 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
260 * The t1 and t2 arguments are to be compared for equality, inequality,
261 * less than, less than or equal to, greater than and greater than or
262 * equal to respectively. If the specified condition holds, the functions
263 * return 1. If the condition does not hold, the functions print a diagnostic
264 * message and return 0.
266 * The desc argument is a printf format string followed by its arguments and
267 * this is included in the output if the condition being tested for is false.
269 #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
270 int test_ ## name ## _ ## opname(const char *file, int line, \
271 const char *s1, const char *s2, \
272 const type t1, const type t2) \
276 test_fail_message(NULL, file, line, #type, \
277 "%s [" fmt "] " #op " %s [" fmt "]", \
282 #define DEFINE_COMPARISONS(type, name, fmt) \
283 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
284 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
285 DEFINE_COMPARISON(type, name, lt, <, fmt) \
286 DEFINE_COMPARISON(type, name, le, <=, fmt) \
287 DEFINE_COMPARISON(type, name, gt, >, fmt) \
288 DEFINE_COMPARISON(type, name, ge, >=, fmt)
290 DEFINE_COMPARISONS(int, int, "%d")
291 DEFINE_COMPARISONS(unsigned int, uint, "%u")
292 DEFINE_COMPARISONS(char, char, "%c")
293 DEFINE_COMPARISONS(unsigned char, uchar, "%u")
294 DEFINE_COMPARISONS(long, long, "%ld")
295 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
296 DEFINE_COMPARISONS(size_t, size_t, "%" OSSLzu)
298 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
299 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
301 int test_ptr_null(const char *file, int line, const char *s, const void *p)
305 test_fail_message(NULL, file, line, "ptr", "%s [%p] == NULL", s, p);
309 int test_ptr(const char *file, int line, const char *s, const void *p)
313 test_fail_message(NULL, file, line, "ptr", "%s [%p] != NULL", s, p);
317 int test_true(const char *file, int line, const char *s, int b)
321 test_fail_message(NULL, file, line, "bool", "%s [false] == true", s);
325 int test_false(const char *file, int line, const char *s, int b)
329 test_fail_message(NULL, file, line, "bool", "%s [true] == false", s);
333 static const char *print_string_maybe_null(const char *s)
335 return s == NULL ? "(NULL)" : s;
338 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
339 const char *s1, const char *s2)
341 if (s1 == NULL && s2 == NULL)
343 if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
344 test_fail_message(NULL, file, line, "string", "%s [%s] == %s [%s]",
345 st1, print_string_maybe_null(s1),
346 st2, print_string_maybe_null(s2));
352 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
353 const char *s1, const char *s2)
355 if ((s1 == NULL) ^ (s2 == NULL))
357 if (s1 == NULL || strcmp(s1, s2) == 0) {
358 test_fail_message(NULL, file, line, "string", "%s [%s] != %s [%s]",
359 st1, print_string_maybe_null(s1),
360 st2, print_string_maybe_null(s2));
366 int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
367 const char *s1, const char *s2, size_t len)
371 if (s1 == NULL && s2 == NULL)
373 if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
374 test_fail_message(NULL, file, line, "string", "%.s [%.*s] == %s [%.*s]",
375 st1, prec, print_string_maybe_null(s1),
376 st2, prec, print_string_maybe_null(s2));
382 int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
383 const char *s1, const char *s2, size_t len)
387 if ((s1 == NULL) ^ (s2 == NULL))
389 if (s1 == NULL || strncmp(s1, s2, len) == 0) {
390 test_fail_message(NULL, file, line, "string", "%s [%.*s] != %s [%.*s]",
391 st1, prec, print_string_maybe_null(s1),
392 st2, prec, print_string_maybe_null(s2));
399 * We could use OPENSSL_buf2hexstr() to do this but trying to allocate memory
400 * in a failure state isn't generally a great idea.
402 static const char *print_mem_maybe_null(const void *s, size_t n,
403 char out[MEM_BUFFER_SIZE])
406 const unsigned char *p = (const unsigned char *)s;
407 int pad = 2*n >= MEM_BUFFER_SIZE;
412 n = MEM_BUFFER_SIZE-4;
414 for (i=0; i<2*n; i++) {
415 unsigned char c = (i & 1) != 0 ? p[i / 2] & 15 : p[i / 2] >> 4;
416 out[i] = "0123456789abcdef"[c];
428 int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
429 const void *s1, size_t n1, const void *s2, size_t n2)
431 char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
433 if (s1 == NULL && s2 == NULL)
436 test_fail_message(NULL, file, line, "memory",
437 "size mismatch %s %s [%"OSSLzu"] != %s %s [%"OSSLzu"]",
438 st1, print_mem_maybe_null(s1, n1, b1), n1,
439 st2, print_mem_maybe_null(s2, n2, b2), n2);
442 if (s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
443 test_fail_message(NULL, file, line, "memory",
444 "%s %s [%"OSSLzu"] != %s %s [%"OSSLzu"]",
445 st1, print_mem_maybe_null(s1, n1, b1), n1,
446 st2, print_mem_maybe_null(s2, n2, b2), n2);
452 int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
453 const void *s1, size_t n1, const void *s2, size_t n2)
455 char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
457 if ((s1 == NULL) ^ (s2 == NULL))
461 if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
462 test_fail_message(NULL, file, line, "memory",
463 "%s %s [%"OSSLzu"] != %s %s [%"OSSLzu"]",
464 st1, print_mem_maybe_null(s1, n1, b1), n1,
465 st2, print_mem_maybe_null(s2, n2, b2), n2);