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