1 /* crypto/engine/enginetest.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 #include <openssl/e_os2.h>
61 #ifdef OPENSSL_SYS_WINCE
62 #include <stdlib_extras.h>
65 #include <openssl/buffer.h>
66 #include <openssl/crypto.h>
67 #include <openssl/engine.h>
68 #include <openssl/err.h>
70 static void display_engine_list()
75 h = ENGINE_get_first();
77 printf("listing available engine types\n");
80 printf("engine %i, id = \"%s\", name = \"%s\"\n",
81 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
82 h = ENGINE_get_next(h);
84 printf("end of list\n");
85 /* ENGINE_get_first() increases the struct_ref counter, so we
86 must call ENGINE_free() to decrease it again */
90 int main(int argc, char *argv[])
94 const char *id, *name;
98 ENGINE *new_h1 = NULL;
99 ENGINE *new_h2 = NULL;
100 ENGINE *new_h3 = NULL;
101 ENGINE *new_h4 = NULL;
103 /* enable memory leak checking unless explicitly disabled */
104 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
106 CRYPTO_malloc_debug_init();
107 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
111 /* OPENSSL_DEBUG_MEMORY=off */
112 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
114 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
115 ERR_load_crypto_strings();
117 memset(block, 0, 512 * sizeof(ENGINE *));
118 if(((new_h1 = ENGINE_new()) == NULL) ||
119 !ENGINE_set_id(new_h1, "test_id0") ||
120 !ENGINE_set_name(new_h1, "First test item") ||
121 ((new_h2 = ENGINE_new()) == NULL) ||
122 !ENGINE_set_id(new_h2, "test_id1") ||
123 !ENGINE_set_name(new_h2, "Second test item") ||
124 ((new_h3 = ENGINE_new()) == NULL) ||
125 !ENGINE_set_id(new_h3, "test_id2") ||
126 !ENGINE_set_name(new_h3, "Third test item") ||
127 ((new_h4 = ENGINE_new()) == NULL) ||
128 !ENGINE_set_id(new_h4, "test_id3") ||
129 !ENGINE_set_name(new_h4, "Fourth test item"))
131 printf("Couldn't set up test ENGINE structures\n");
134 printf("\nenginetest beginning\n\n");
135 display_engine_list();
136 if(!ENGINE_add(new_h1))
138 printf("Add failed!\n");
141 display_engine_list();
142 ptr = ENGINE_get_first();
143 if(!ENGINE_remove(ptr))
145 printf("Remove failed!\n");
150 display_engine_list();
151 if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
153 printf("Add failed!\n");
156 display_engine_list();
157 if(!ENGINE_remove(new_h2))
159 printf("Remove failed!\n");
162 display_engine_list();
163 if(!ENGINE_add(new_h4))
165 printf("Add failed!\n");
168 display_engine_list();
169 if(ENGINE_add(new_h3))
171 printf("Add *should* have failed but didn't!\n");
175 printf("Add that should fail did.\n");
177 if(ENGINE_remove(new_h2))
179 printf("Remove *should* have failed but didn't!\n");
183 printf("Remove that should fail did.\n");
185 if(!ENGINE_remove(new_h3))
187 printf("Remove failed!\n");
190 display_engine_list();
191 if(!ENGINE_remove(new_h4))
193 printf("Remove failed!\n");
196 display_engine_list();
197 /* Depending on whether there's any hardware support compiled
198 * in, this remove may be destined to fail. */
199 ptr = ENGINE_get_first();
201 if(!ENGINE_remove(ptr))
202 printf("Remove failed!i - probably no hardware "
203 "support present.\n");
206 display_engine_list();
207 if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
209 printf("Couldn't add and remove to an empty list!\n");
213 printf("Successfully added and removed to an empty list!\n");
214 printf("About to beef up the engine-type list\n");
215 for(loop = 0; loop < 512; loop++)
217 sprintf(buf, "id%i", loop);
218 id = BUF_strdup(buf);
219 sprintf(buf, "Fake engine type %i", loop);
220 name = BUF_strdup(buf);
221 if(((block[loop] = ENGINE_new()) == NULL) ||
222 !ENGINE_set_id(block[loop], id) ||
223 !ENGINE_set_name(block[loop], name))
225 printf("Couldn't create block of ENGINE structures.\n"
226 "I'll probably also core-dump now, damn.\n");
230 for(loop = 0; loop < 512; loop++)
232 if(!ENGINE_add(block[loop]))
234 printf("\nAdding stopped at %i, (%s,%s)\n",
235 loop, ENGINE_get_id(block[loop]),
236 ENGINE_get_name(block[loop]));
240 printf("."); fflush(stdout);
243 printf("\nAbout to empty the engine-type list\n");
244 while((ptr = ENGINE_get_first()) != NULL)
246 if(!ENGINE_remove(ptr))
248 printf("\nRemove failed!\n");
252 printf("."); fflush(stdout);
254 for(loop = 0; loop < 512; loop++)
256 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
257 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
259 printf("\nTests completed happily\n");
263 ERR_print_errors_fp(stderr);
264 if(new_h1) ENGINE_free(new_h1);
265 if(new_h2) ENGINE_free(new_h2);
266 if(new_h3) ENGINE_free(new_h3);
267 if(new_h4) ENGINE_free(new_h4);
268 for(loop = 0; loop < 512; loop++)
270 ENGINE_free(block[loop]);
272 CRYPTO_cleanup_all_ex_data();
275 CRYPTO_mem_leaks_fp(stderr);