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");
118 for (i = 0; i != num_tests; ++i) {
119 if (all_tests[i].num == -1) {
120 int ret = all_tests[i].test_fn();
123 printf("** %s failed **\n--------\n",
124 all_tests[i].test_case_name);
129 for (j = 0; j < all_tests[i].num; j++) {
130 int ret = all_tests[i].param_test_fn(j);
133 printf("** %s failed test %d\n--------\n",
134 all_tests[i].test_case_name, j);
142 if (num_failed != 0) {
143 printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
144 num_failed, num_failed != 1 ? "s" : "", num_test_cases);
147 printf(" All tests passed.\n");
152 * A common routine to output test failure messages. Generally this should not
153 * be called directly, rather it should be called by the following functions.
155 * |desc| is a printf formatted description with arguments |args| that is
156 * supplied by the user and |desc| can be NULL. |type| is the data type
157 * that was tested (int, char, ptr, ...). |fmt| is a system provided
158 * printf format with following arguments that spell out the failure
159 * details i.e. the actual values compared and the operator used.
161 * The typical use for this is from an utility test function:
163 * int test6(const char *file, int line, int n) {
165 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
171 * calling test6(3, "oops") will return 0 and produce out along the lines of:
172 * FAIL oops: (int) value 3 is not 6\n
174 * It general, test_fail_message should not be called directly.
176 static void test_fail_message(const char *prefix, const char *file, int line,
177 const char *type, const char *fmt, ...)
180 static void test_fail_message_va(const char *prefix, const char *file, int line,
181 const char *type, const char *fmt, va_list ap)
183 fputs(prefix != NULL ? prefix : "ERROR", stderr);
186 fprintf(stderr, " (%s)", type);
189 vfprintf(stderr, fmt, ap);
192 fprintf(stderr, " @ %s:%d", file, line);
197 static void test_fail_message(const char *prefix, const char *file, int line,
198 const char *type, const char *fmt, ...)
202 test_fail_message_va(prefix, file, line, type, fmt, ap);
206 void test_info_c90(const char *desc, ...)
211 test_fail_message_va("INFO", NULL, -1, NULL, desc, ap);
215 void test_info(const char *file, int line, const char *desc, ...)
220 test_fail_message_va("INFO", file, line, NULL, desc, ap);
224 void test_error_c90(const char *desc, ...)
229 test_fail_message(NULL, NULL, -1, NULL, desc, ap);
233 void test_error(const char *file, int line, const char *desc, ...)
238 test_fail_message_va(NULL, file, line, NULL, desc, ap);
243 * Define some comparisons between pairs of various types.
244 * These functions return 1 if the test is true.
245 * Otherwise, they return 0 and pretty-print diagnostics.
247 * In each case the functions produced are:
248 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
249 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
250 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
251 * int test_name_le(const type t1, const type t2, const char *desc, ...);
252 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
253 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
255 * The t1 and t2 arguments are to be compared for equality, inequality,
256 * less than, less than or equal to, greater than and greater than or
257 * equal to respectively. If the specified condition holds, the functions
258 * return 1. If the condition does not hold, the functions print a diagnostic
259 * message and return 0.
261 * The desc argument is a printf format string followed by its arguments and
262 * this is included in the output if the condition being tested for is false.
264 #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
265 int test_ ## name ## _ ## opname(const char *file, int line, \
266 const char *s1, const char *s2, \
267 const type t1, const type t2) \
271 test_fail_message(NULL, file, line, #type, \
272 "%s [" fmt "] " #op " %s [" fmt "]", \
277 #define DEFINE_COMPARISONS(type, name, fmt) \
278 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
279 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
280 DEFINE_COMPARISON(type, name, lt, <, fmt) \
281 DEFINE_COMPARISON(type, name, le, <=, fmt) \
282 DEFINE_COMPARISON(type, name, gt, >, fmt) \
283 DEFINE_COMPARISON(type, name, ge, >=, fmt)
285 DEFINE_COMPARISONS(int, int, "%d")
286 DEFINE_COMPARISONS(unsigned int, uint, "%u")
287 DEFINE_COMPARISONS(char, char, "%c")
288 DEFINE_COMPARISONS(unsigned char, uchar, "%u")
289 DEFINE_COMPARISONS(long, long, "%ld")
290 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
291 DEFINE_COMPARISONS(size_t, size_t, "%" OSSLzu)
293 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
294 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
296 int test_ptr_null(const char *file, int line, const char *s, const void *p)
300 test_fail_message(NULL, file, line, "ptr", "%s [%p] == NULL", s, p);
304 int test_ptr(const char *file, int line, const char *s, const void *p)
308 test_fail_message(NULL, file, line, "ptr", "%s [%p] != NULL", s, p);
312 int test_true(const char *file, int line, const char *s, int b)
316 test_fail_message(NULL, file, line, "bool", "%s [false] == true", s);
320 int test_false(const char *file, int line, const char *s, int b)
324 test_fail_message(NULL, file, line, "bool", "%s [true] == false", s);
328 static const char *print_string_maybe_null(const char *s)
330 return s == NULL ? "(NULL)" : s;
333 int test_str_eq(const char *file, int line, const char *st1, const char *st2,
334 const char *s1, const char *s2)
336 if (s1 == NULL && s2 == NULL)
338 if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
339 test_fail_message(NULL, file, line, "string", "%s [%s] == %s [%s]",
340 st1, print_string_maybe_null(s1),
341 st2, print_string_maybe_null(s2));
347 int test_str_ne(const char *file, int line, const char *st1, const char *st2,
348 const char *s1, const char *s2)
350 if ((s1 == NULL) ^ (s2 == NULL))
352 if (s1 == NULL || strcmp(s1, s2) == 0) {
353 test_fail_message(NULL, file, line, "string", "%s [%s] != %s [%s]",
354 st1, print_string_maybe_null(s1),
355 st2, print_string_maybe_null(s2));
362 * We could use OPENSSL_buf2hexstr() to do this but trying to allocate memory
363 * in a failure state isn't generally a great idea.
365 static const char *print_mem_maybe_null(const void *s, size_t n,
366 char out[MEM_BUFFER_SIZE])
369 const unsigned char *p = (const unsigned char *)s;
370 int pad = 2*n >= MEM_BUFFER_SIZE;
375 n = MEM_BUFFER_SIZE-4;
377 for (i=0; i<2*n; i++) {
378 unsigned char c = (i & 1) != 0 ? p[i / 2] & 15 : p[i / 2] >> 4;
379 out[i] = "0123456789abcdef"[c];
391 int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
392 const void *s1, size_t n1, const void *s2, size_t n2)
394 char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
396 if (s1 == NULL && s2 == NULL)
399 test_fail_message(NULL, file, line, "memory",
400 "size mismatch %s %s [%"OSSLzu"] != %s %s [%"OSSLzu"]",
401 st1, print_mem_maybe_null(s1, n1, b1), n1,
402 st2, print_mem_maybe_null(s2, n2, b2), n2);
405 if (s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
406 test_fail_message(NULL, file, line, "memory",
407 "%s %s [%"OSSLzu"] != %s %s [%"OSSLzu"]",
408 st1, print_mem_maybe_null(s1, n1, b1), n1,
409 st2, print_mem_maybe_null(s2, n2, b2), n2);
415 int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
416 const void *s1, size_t n1, const void *s2, size_t n2)
418 char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];
420 if ((s1 == NULL) ^ (s2 == NULL))
424 if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
425 test_fail_message(NULL, file, line, "memory",
426 "%s %s [%"OSSLzu"] != %s %s [%"OSSLzu"]",
427 st1, print_mem_maybe_null(s1, n1, b1), n1,
428 st2, print_mem_maybe_null(s2, n2, b2), n2);