appveyor: make tests verbose
[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 /*
19  * Declares the structures needed to register each test case function.
20  */
21 typedef struct test_info {
22     const char *test_case_name;
23     int (*test_fn) ();
24     int (*param_test_fn)(int idx);
25     int num;
26 } TEST_INFO;
27
28 static TEST_INFO all_tests[1024];
29 static int num_tests = 0;
30 /*
31  * A parameterised tests runs a loop of test cases.
32  * |num_test_cases| counts the total number of test cases
33  * across all tests.
34  */
35 static int num_test_cases = 0;
36
37 void add_test(const char *test_case_name, int (*test_fn) ())
38 {
39     assert(num_tests != OSSL_NELEM(all_tests));
40     all_tests[num_tests].test_case_name = test_case_name;
41     all_tests[num_tests].test_fn = test_fn;
42     all_tests[num_tests].num = -1;
43     ++num_test_cases;
44     ++num_tests;
45 }
46
47 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
48                    int num)
49 {
50     assert(num_tests != OSSL_NELEM(all_tests));
51     all_tests[num_tests].test_case_name = test_case_name;
52     all_tests[num_tests].param_test_fn = test_fn;
53     all_tests[num_tests].num = num;
54     ++num_tests;
55     num_test_cases += num;
56 }
57
58 int run_tests(const char *test_prog_name)
59 {
60     int num_failed = 0;
61
62     int i, j;
63
64     printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
65            num_test_cases == 1 ? "" : "s");
66
67     for (i = 0; i != num_tests; ++i) {
68         if (all_tests[i].num == -1) {
69             if (!all_tests[i].test_fn()) {
70                 printf("** %s failed **\n--------\n",
71                        all_tests[i].test_case_name);
72                 ++num_failed;
73             }
74         } else {
75             for (j = 0; j < all_tests[i].num; j++) {
76                 if (!all_tests[i].param_test_fn(j)) {
77                     printf("** %s failed test %d\n--------\n",
78                            all_tests[i].test_case_name, j);
79                     ++num_failed;
80                 }
81             }
82         }
83     }
84
85     if (num_failed != 0) {
86         printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
87                num_failed, num_failed != 1 ? "s" : "", num_test_cases);
88         return EXIT_FAILURE;
89     }
90     printf("  All tests passed.\n");
91     return EXIT_SUCCESS;
92 }
93
94 static const char *print_string_maybe_null(const char *s)
95 {
96     return s == NULL ? "(NULL)" : s;
97 }
98
99 int strings_equal(const char *desc, const char *s1, const char *s2)
100 {
101     if (s1 == NULL && s2 == NULL)
102       return 1;
103     if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
104         fprintf(stderr, "%s mismatch: %s vs %s\n", desc, print_string_maybe_null(s1),
105                 print_string_maybe_null(s2));
106         return 0;
107     }
108     return 1;
109 }