]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/enginetest.c
Summarise the last couple of commits.
[thirdparty/openssl.git] / crypto / engine / enginetest.c
CommitLineData
5270e702
RL
1/* crypto/engine/enginetest.c */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
2b671586 6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
5270e702
RL
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
35618bf6 59#include <openssl/e_os2.h>
1ae6ddac 60#include <stdio.h>
1023cfe7 61#include <string.h>
619b2c03
BM
62#include <openssl/buffer.h>
63#include <openssl/crypto.h>
5270e702
RL
64#include <openssl/engine.h>
65#include <openssl/err.h>
66
67static void display_engine_list()
68 {
69 ENGINE *h;
70 int loop;
71
72 h = ENGINE_get_first();
73 loop = 0;
74 printf("listing available engine types\n");
75 while(h)
76 {
77 printf("engine %i, id = \"%s\", name = \"%s\"\n",
78 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
79 h = ENGINE_get_next(h);
80 }
81 printf("end of list\n");
e8e7fbdb
BM
82 /* ENGINE_get_first() increases the struct_ref counter, so we
83 must call ENGINE_free() to decrease it again */
84 ENGINE_free(h);
5270e702
RL
85 }
86
87int main(int argc, char *argv[])
88 {
89 ENGINE *block[512];
90 char buf[256];
91 const char *id, *name;
92 ENGINE *ptr;
93 int loop;
94 int to_return = 1;
95 ENGINE *new_h1 = NULL;
96 ENGINE *new_h2 = NULL;
97 ENGINE *new_h3 = NULL;
98 ENGINE *new_h4 = NULL;
99
e8e7fbdb
BM
100 /* enable memory leak checking unless explicitly disabled */
101 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
102 {
103 CRYPTO_malloc_debug_init();
104 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
105 }
106 else
107 {
108 /* OPENSSL_DEBUG_MEMORY=off */
109 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
110 }
111 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
5270e702
RL
112 ERR_load_crypto_strings();
113
114 memset(block, 0, 512 * sizeof(ENGINE *));
115 if(((new_h1 = ENGINE_new()) == NULL) ||
116 !ENGINE_set_id(new_h1, "test_id0") ||
117 !ENGINE_set_name(new_h1, "First test item") ||
118 ((new_h2 = ENGINE_new()) == NULL) ||
119 !ENGINE_set_id(new_h2, "test_id1") ||
120 !ENGINE_set_name(new_h2, "Second test item") ||
121 ((new_h3 = ENGINE_new()) == NULL) ||
122 !ENGINE_set_id(new_h3, "test_id2") ||
123 !ENGINE_set_name(new_h3, "Third test item") ||
124 ((new_h4 = ENGINE_new()) == NULL) ||
125 !ENGINE_set_id(new_h4, "test_id3") ||
126 !ENGINE_set_name(new_h4, "Fourth test item"))
127 {
128 printf("Couldn't set up test ENGINE structures\n");
129 goto end;
130 }
131 printf("\nenginetest beginning\n\n");
132 display_engine_list();
133 if(!ENGINE_add(new_h1))
134 {
135 printf("Add failed!\n");
136 goto end;
137 }
138 display_engine_list();
139 ptr = ENGINE_get_first();
140 if(!ENGINE_remove(ptr))
141 {
142 printf("Remove failed!\n");
143 goto end;
144 }
e8e7fbdb
BM
145 if (ptr)
146 ENGINE_free(ptr);
5270e702
RL
147 display_engine_list();
148 if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
149 {
150 printf("Add failed!\n");
151 goto end;
152 }
153 display_engine_list();
154 if(!ENGINE_remove(new_h2))
155 {
156 printf("Remove failed!\n");
157 goto end;
158 }
159 display_engine_list();
160 if(!ENGINE_add(new_h4))
161 {
162 printf("Add failed!\n");
163 goto end;
164 }
165 display_engine_list();
166 if(ENGINE_add(new_h3))
167 {
168 printf("Add *should* have failed but didn't!\n");
169 goto end;
170 }
171 else
172 printf("Add that should fail did.\n");
173 ERR_clear_error();
174 if(ENGINE_remove(new_h2))
175 {
176 printf("Remove *should* have failed but didn't!\n");
177 goto end;
178 }
179 else
180 printf("Remove that should fail did.\n");
b6d1e52d 181 ERR_clear_error();
5270e702
RL
182 if(!ENGINE_remove(new_h3))
183 {
184 printf("Remove failed!\n");
185 goto end;
186 }
187 display_engine_list();
188 if(!ENGINE_remove(new_h4))
189 {
190 printf("Remove failed!\n");
191 goto end;
192 }
193 display_engine_list();
194 /* Depending on whether there's any hardware support compiled
195 * in, this remove may be destined to fail. */
196 ptr = ENGINE_get_first();
197 if(ptr)
198 if(!ENGINE_remove(ptr))
199 printf("Remove failed!i - probably no hardware "
200 "support present.\n");
e8e7fbdb
BM
201 if (ptr)
202 ENGINE_free(ptr);
5270e702
RL
203 display_engine_list();
204 if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
205 {
206 printf("Couldn't add and remove to an empty list!\n");
207 goto end;
208 }
209 else
210 printf("Successfully added and removed to an empty list!\n");
211 printf("About to beef up the engine-type list\n");
212 for(loop = 0; loop < 512; loop++)
213 {
214 sprintf(buf, "id%i", loop);
619b2c03 215 id = BUF_strdup(buf);
5270e702 216 sprintf(buf, "Fake engine type %i", loop);
619b2c03 217 name = BUF_strdup(buf);
5270e702
RL
218 if(((block[loop] = ENGINE_new()) == NULL) ||
219 !ENGINE_set_id(block[loop], id) ||
220 !ENGINE_set_name(block[loop], name))
221 {
222 printf("Couldn't create block of ENGINE structures.\n"
223 "I'll probably also core-dump now, damn.\n");
224 goto end;
225 }
226 }
227 for(loop = 0; loop < 512; loop++)
228 {
229 if(!ENGINE_add(block[loop]))
230 {
231 printf("\nAdding stopped at %i, (%s,%s)\n",
232 loop, ENGINE_get_id(block[loop]),
233 ENGINE_get_name(block[loop]));
234 goto cleanup_loop;
235 }
236 else
237 printf("."); fflush(stdout);
238 }
239cleanup_loop:
240 printf("\nAbout to empty the engine-type list\n");
241 while((ptr = ENGINE_get_first()) != NULL)
242 {
243 if(!ENGINE_remove(ptr))
244 {
245 printf("\nRemove failed!\n");
246 goto end;
247 }
e8e7fbdb 248 ENGINE_free(ptr);
5270e702
RL
249 printf("."); fflush(stdout);
250 }
251 for(loop = 0; loop < 512; loop++)
252 {
8573fa18
BM
253 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
254 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
5270e702
RL
255 }
256 printf("\nTests completed happily\n");
257 to_return = 0;
258end:
259 if(to_return)
260 ERR_print_errors_fp(stderr);
261 if(new_h1) ENGINE_free(new_h1);
262 if(new_h2) ENGINE_free(new_h2);
263 if(new_h3) ENGINE_free(new_h3);
264 if(new_h4) ENGINE_free(new_h4);
265 for(loop = 0; loop < 512; loop++)
266 if(block[loop])
267 ENGINE_free(block[loop]);
e8e7fbdb
BM
268 ENGINE_cleanup();
269 CRYPTO_cleanup_all_ex_data();
270 ERR_free_strings();
271 ERR_remove_state(0);
272 CRYPTO_mem_leaks_fp(stderr);
5270e702
RL
273 return to_return;
274 }