testutil: always print errors on failure
[oweals/openssl.git] / test / testutil.c
1 /*
2  * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "testutil.h"
11
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include "e_os.h"
17
18 #include <openssl/err.h>
19
20 /*
21  * Declares the structures needed to register each test case function.
22  */
23 typedef struct test_info {
24     const char *test_case_name;
25     int (*test_fn) ();
26     int (*param_test_fn)(int idx);
27     int num;
28 } TEST_INFO;
29
30 static TEST_INFO all_tests[1024];
31 static int num_tests = 0;
32 /*
33  * A parameterised tests runs a loop of test cases.
34  * |num_test_cases| counts the total number of test cases
35  * across all tests.
36  */
37 static int num_test_cases = 0;
38
39 void add_test(const char *test_case_name, int (*test_fn) ())
40 {
41     assert(num_tests != OSSL_NELEM(all_tests));
42     all_tests[num_tests].test_case_name = test_case_name;
43     all_tests[num_tests].test_fn = test_fn;
44     all_tests[num_tests].num = -1;
45     ++num_test_cases;
46     ++num_tests;
47 }
48
49 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
50                    int num)
51 {
52     assert(num_tests != OSSL_NELEM(all_tests));
53     all_tests[num_tests].test_case_name = test_case_name;
54     all_tests[num_tests].param_test_fn = test_fn;
55     all_tests[num_tests].num = num;
56     ++num_tests;
57     num_test_cases += num;
58 }
59
60 static void finalize(int success)
61 {
62     if (success)
63         ERR_clear_error();
64     else
65         ERR_print_errors_fp(stderr);
66 }
67
68 int run_tests(const char *test_prog_name)
69 {
70     int num_failed = 0;
71
72     int i, j;
73
74     printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
75            num_test_cases == 1 ? "" : "s");
76
77     for (i = 0; i != num_tests; ++i) {
78         if (all_tests[i].num == -1) {
79             int ret = all_tests[i].test_fn();
80             if (!ret) {
81                 printf("** %s failed **\n--------\n",
82                        all_tests[i].test_case_name);
83                 ++num_failed;
84             }
85             finalize(ret);
86         } else {
87             for (j = 0; j < all_tests[i].num; j++) {
88                 int ret = all_tests[i].param_test_fn(j);
89                 if (!ret) {
90                     printf("** %s failed test %d\n--------\n",
91                            all_tests[i].test_case_name, j);
92                     ++num_failed;
93                 }
94                 finalize(ret);
95             }
96         }
97     }
98
99     if (num_failed != 0) {
100         printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
101                num_failed, num_failed != 1 ? "s" : "", num_test_cases);
102         return EXIT_FAILURE;
103     }
104     printf("  All tests passed.\n");
105     return EXIT_SUCCESS;
106 }
107
108 static const char *print_string_maybe_null(const char *s)
109 {
110     return s == NULL ? "(NULL)" : s;
111 }
112
113 int strings_equal(const char *desc, const char *s1, const char *s2)
114 {
115     if (s1 == NULL && s2 == NULL)
116       return 1;
117     if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
118         fprintf(stderr, "%s mismatch: %s vs %s\n", desc, print_string_maybe_null(s1),
119                 print_string_maybe_null(s2));
120         return 0;
121     }
122     return 1;
123 }