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