Split out DHE CKE construction into a separate function
[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 "e_os.h"
16
17 /*
18  * Declares the structures needed to register each test case function.
19  */
20 typedef struct test_info {
21     const char *test_case_name;
22     int (*test_fn) ();
23     int (*param_test_fn)(int idx);
24     int num;
25 } TEST_INFO;
26
27 static TEST_INFO all_tests[1024];
28 static int num_tests = 0;
29 /*
30  * A parameterised tests runs a loop of test cases.
31  * |num_test_cases| counts the total number of test cases
32  * across all tests.
33  */
34 static int num_test_cases = 0;
35
36 void add_test(const char *test_case_name, int (*test_fn) ())
37 {
38     assert(num_tests != OSSL_NELEM(all_tests));
39     all_tests[num_tests].test_case_name = test_case_name;
40     all_tests[num_tests].test_fn = test_fn;
41     all_tests[num_tests].num = -1;
42     ++num_test_cases;
43     ++num_tests;
44 }
45
46 void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
47                    int num)
48 {
49     assert(num_tests != OSSL_NELEM(all_tests));
50     all_tests[num_tests].test_case_name = test_case_name;
51     all_tests[num_tests].param_test_fn = test_fn;
52     all_tests[num_tests].num = num;
53     ++num_tests;
54     num_test_cases += num;
55 }
56
57 int run_tests(const char *test_prog_name)
58 {
59     int num_failed = 0;
60
61     int i, j;
62
63     printf("%s: %d test case%s\n", test_prog_name, num_test_cases,
64            num_test_cases == 1 ? "" : "s");
65
66     for (i = 0; i != num_tests; ++i) {
67         if (all_tests[i].num == -1) {
68             if (!all_tests[i].test_fn()) {
69                 printf("** %s failed **\n--------\n",
70                        all_tests[i].test_case_name);
71                 ++num_failed;
72             }
73         } else {
74             for (j = 0; j < all_tests[i].num; j++) {
75                 if (!all_tests[i].param_test_fn(j)) {
76                     printf("** %s failed test %d\n--------\n",
77                            all_tests[i].test_case_name, j);
78                     ++num_failed;
79                 }
80             }
81         }
82     }
83
84     if (num_failed != 0) {
85         printf("%s: %d test%s failed (out of %d)\n", test_prog_name,
86                num_failed, num_failed != 1 ? "s" : "", num_test_cases);
87         return EXIT_FAILURE;
88     }
89     printf("  All tests passed.\n");
90     return EXIT_SUCCESS;
91 }