Update the test framework so that the need for test_main is removed. Everything
[openssl.git] / test / enginetest.c
1 /*
2  * Copyright 2000-2017 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 <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/e_os2.h>
14
15 #ifndef OPENSSL_NO_ENGINE
16 # include <openssl/buffer.h>
17 # include <openssl/crypto.h>
18 # include <openssl/engine.h>
19 # include <openssl/err.h>
20 # include "testutil.h"
21
22 static void display_engine_list(void)
23 {
24     ENGINE *h;
25     int loop;
26
27     loop = 0;
28     for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
29         TEST_info("#%d: id = \"%s\", name = \"%s\"",
30                loop++, ENGINE_get_id(h), ENGINE_get_name(h));
31     }
32
33     /*
34      * ENGINE_get_first() increases the struct_ref counter, so we must call
35      * ENGINE_free() to decrease it again
36      */
37     ENGINE_free(h);
38 }
39
40 #define NUMTOADD 512
41
42 static int test_engines(void)
43 {
44     ENGINE *block[NUMTOADD];
45     char buf[256];
46     const char *id, *name;
47     ENGINE *ptr;
48     int loop;
49     int to_return = 0;
50     ENGINE *new_h1 = NULL;
51     ENGINE *new_h2 = NULL;
52     ENGINE *new_h3 = NULL;
53     ENGINE *new_h4 = NULL;
54
55     memset(block, 0, sizeof(block));
56     if (!TEST_ptr(new_h1 = ENGINE_new())
57             || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
58             || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
59             || !TEST_ptr(new_h2 = ENGINE_new())
60             || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
61             || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
62             || !TEST_ptr(new_h3 = ENGINE_new())
63             || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
64             || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
65             || !TEST_ptr(new_h4 = ENGINE_new())
66             || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
67             || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
68         goto end;
69     TEST_info("Engines:");
70     display_engine_list();
71
72     if (!TEST_true(ENGINE_add(new_h1)))
73         goto end;
74     TEST_info("Engines:");
75     display_engine_list();
76
77     ptr = ENGINE_get_first();
78     if (!TEST_true(ENGINE_remove(ptr)))
79         goto end;
80     ENGINE_free(ptr);
81     TEST_info("Engines:");
82     display_engine_list();
83
84     if (!TEST_true(ENGINE_add(new_h3))
85             || !TEST_true(ENGINE_add(new_h2)))
86         goto end;
87     TEST_info("Engines:");
88     display_engine_list();
89
90     if (!TEST_true(ENGINE_remove(new_h2)))
91         goto end;
92     TEST_info("Engines:");
93     display_engine_list();
94
95     if (!TEST_true(ENGINE_add(new_h4)))
96         goto end;
97     TEST_info("Engines:");
98     display_engine_list();
99
100     /* Should fail. */
101     if (!TEST_false(ENGINE_add(new_h3)))
102         goto end;
103     ERR_clear_error();
104
105     /* Should fail. */
106     if (!TEST_false(ENGINE_remove(new_h2)))
107         goto end;
108     ERR_clear_error();
109
110     if (!TEST_true(ENGINE_remove(new_h3)))
111         goto end;
112     TEST_info("Engines:");
113     display_engine_list();
114
115     if (!TEST_true(ENGINE_remove(new_h4)))
116         goto end;
117     TEST_info("Engines:");
118     display_engine_list();
119
120     /*
121      * Depending on whether there's any hardware support compiled in, this
122      * remove may be destined to fail.
123      */
124     if ((ptr = ENGINE_get_first()) != NULL) {
125         if (!ENGINE_remove(ptr))
126             TEST_info("Remove failed - probably no hardware support present");
127     }
128     ENGINE_free(ptr);
129     TEST_info("Engines:");
130     display_engine_list();
131
132     if (!TEST_true(ENGINE_add(new_h1))
133             || !TEST_true(ENGINE_remove(new_h1)))
134         goto end;
135
136     TEST_info("About to beef up the engine-type list");
137     for (loop = 0; loop < NUMTOADD; loop++) {
138         sprintf(buf, "id%d", loop);
139         id = OPENSSL_strdup(buf);
140         sprintf(buf, "Fake engine type %d", loop);
141         name = OPENSSL_strdup(buf);
142         if (!TEST_ptr(block[loop] = ENGINE_new())
143                 || !TEST_true(ENGINE_set_id(block[loop], id))
144                 || !TEST_true(ENGINE_set_name(block[loop], name)))
145             goto end;
146     }
147     for (loop = 0; loop < NUMTOADD; loop++) {
148         if (!TEST_true(ENGINE_add(block[loop]))) {
149             test_note("Adding stopped at %d, (%s,%s)",
150                       loop, ENGINE_get_id(block[loop]),
151                       ENGINE_get_name(block[loop]));
152             goto cleanup_loop;
153         }
154     }
155  cleanup_loop:
156     TEST_info("About to empty the engine-type list");
157     while ((ptr = ENGINE_get_first()) != NULL) {
158         if (!TEST_true(ENGINE_remove(ptr)))
159             goto end;
160         ENGINE_free(ptr);
161     }
162     for (loop = 0; loop < NUMTOADD; loop++) {
163         OPENSSL_free((void *)ENGINE_get_id(block[loop]));
164         OPENSSL_free((void *)ENGINE_get_name(block[loop]));
165     }
166     to_return = 1;
167
168  end:
169     ENGINE_free(new_h1);
170     ENGINE_free(new_h2);
171     ENGINE_free(new_h3);
172     ENGINE_free(new_h4);
173     for (loop = 0; loop < NUMTOADD; loop++)
174         ENGINE_free(block[loop]);
175     return to_return;
176 }
177 #endif
178
179 int setup_tests(void)
180 {
181 #ifdef OPENSSL_NO_ENGINE
182     TEST_note("No ENGINE support");
183 #else
184     ADD_TEST(test_engines);
185 #endif
186     return 1;
187 }