]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil.c
ccb62482341f1148153d76bb81c82352bd1a4ae0
[thirdparty/openssl.git] / test / testutil.c
1 /*-
2 * Utilities for writing OpenSSL unit tests.
3 *
4 * More information:
5 * http://wiki.openssl.org/index.php/How_To_Write_Unit_Tests_For_OpenSSL
6 *
7 * Author: Mike Bland (mbland@acm.org)
8 * Date: 2014-07-15
9 * ====================================================================
10 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. All advertising materials mentioning features or use of this
25 * software must display the following acknowledgment:
26 * "This product includes software developed by the OpenSSL Project
27 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
28 *
29 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
30 * endorse or promote products derived from this software without
31 * prior written permission. For written permission, please contact
32 * licensing@OpenSSL.org.
33 *
34 * 5. Products derived from this software may not be called "OpenSSL"
35 * nor may "OpenSSL" appear in their names without prior written
36 * permission of the OpenSSL Project.
37 *
38 * 6. Redistributions of any form whatsoever must retain the following
39 * acknowledgment:
40 * "This product includes software developed by the OpenSSL Project
41 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
44 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
47 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 * ====================================================================
56 */
57
58 #include "testutil.h"
59
60 #include <assert.h>
61 #include <stdlib.h>
62 #include <stdio.h>
63 #include "e_os.h"
64
65 /*
66 * Declares the structures needed to register each test case function.
67 */
68 typedef struct test_info {
69 const char *test_case_name;
70 int (*test_fn) ();
71 int (*param_test_fn)(int idx);
72 int num;
73 } TEST_INFO;
74
75 static TEST_INFO all_tests[1024];
76 static int num_tests = 0;
77 /*
78 * A parameterised tests runs a loop of test cases.
79 * |num_test_cases| counts the total number of test cases
80 * across all tests.
81 */
82 static int num_test_cases = 0;
83
84 void add_test(const char *test_case_name, int (*test_fn) ())
85 {
86 assert(num_tests != OSSL_NELEM(all_tests));
87 all_tests[num_tests].test_case_name = test_case_name;
88 all_tests[num_tests].test_fn = test_fn;
89 all_tests[num_tests].num = -1;
90 ++num_test_cases;
91 ++num_tests;
92 }
93
94 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
95 int num)
96 {
97 assert(num_tests != OSSL_NELEM(all_tests));
98 all_tests[num_tests].test_case_name = test_case_name;
99 all_tests[num_tests].param_test_fn = test_fn;
100 all_tests[num_tests].num = num;
101 ++num_tests;
102 num_test_cases += num;
103 }
104
105 int run_tests(const char *test_prog_name)
106 {
107 int num_failed = 0;
108
109 int i, j;
110
111 printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
112 num_test_cases == 1 ? "" : "s");
113
114 for (i = 0; i != num_tests; ++i) {
115 if (all_tests[i].num == -1) {
116 if (all_tests[i].test_fn()) {
117 printf("** %s failed **\n--------\n",
118 all_tests[i].test_case_name);
119 ++num_failed;
120 }
121 } else {
122 for (j = 0; j < all_tests[i].num; j++) {
123 if (all_tests[i].param_test_fn(j)) {
124 printf("** %s failed test %d\n--------\n",
125 all_tests[i].test_case_name, j);
126 ++num_failed;
127 }
128 }
129 }
130 }
131
132 if (num_failed != 0) {
133 printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
134 num_failed, num_failed != 1 ? "s" : "", num_test_cases);
135 return EXIT_FAILURE;
136 }
137 printf(" All tests passed.\n");
138 return EXIT_SUCCESS;
139 }