51eb091e60c96c52574da18747a81fcb35c83b8e
[openssl.git] / crypto / engine / enginetest.c
1 /* crypto/engine/enginetest.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
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
59 #include <openssl/e_os2.h>
60 #include <stdio.h>
61 #ifdef OPENSSL_SYS_WINCE
62 #include <stdlib_extras.h>
63 #endif
64 #include <string.h>
65 #include <openssl/buffer.h>
66 #include <openssl/crypto.h>
67 #include <openssl/engine.h>
68 #include <openssl/err.h>
69
70 static void display_engine_list()
71         {
72         ENGINE *h;
73         int loop;
74
75         h = ENGINE_get_first();
76         loop = 0;
77         printf("listing available engine types\n");
78         while(h)
79                 {
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);
83                 }
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 */
87         ENGINE_free(h);
88         }
89
90 int main(int argc, char *argv[])
91         {
92         ENGINE *block[512];
93         char buf[256];
94         const char *id, *name;
95         ENGINE *ptr;
96         int loop;
97         int to_return = 1;
98         ENGINE *new_h1 = NULL;
99         ENGINE *new_h2 = NULL;
100         ENGINE *new_h3 = NULL;
101         ENGINE *new_h4 = NULL;
102
103         /* enable memory leak checking unless explicitly disabled */
104         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
105                 {
106                 CRYPTO_malloc_debug_init();
107                 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
108                 }
109         else
110                 {
111                 /* OPENSSL_DEBUG_MEMORY=off */
112                 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
113                 }
114         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
115         ERR_load_crypto_strings();
116
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"))
130                 {
131                 printf("Couldn't set up test ENGINE structures\n");
132                 goto end;
133                 }
134         printf("\nenginetest beginning\n\n");
135         display_engine_list();
136         if(!ENGINE_add(new_h1))
137                 {
138                 printf("Add failed!\n");
139                 goto end;
140                 }
141         display_engine_list();
142         ptr = ENGINE_get_first();
143         if(!ENGINE_remove(ptr))
144                 {
145                 printf("Remove failed!\n");
146                 goto end;
147                 }
148         if (ptr)
149                 ENGINE_free(ptr);
150         display_engine_list();
151         if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
152                 {
153                 printf("Add failed!\n");
154                 goto end;
155                 }
156         display_engine_list();
157         if(!ENGINE_remove(new_h2))
158                 {
159                 printf("Remove failed!\n");
160                 goto end;
161                 }
162         display_engine_list();
163         if(!ENGINE_add(new_h4))
164                 {
165                 printf("Add failed!\n");
166                 goto end;
167                 }
168         display_engine_list();
169         if(ENGINE_add(new_h3))
170                 {
171                 printf("Add *should* have failed but didn't!\n");
172                 goto end;
173                 }
174         else
175                 printf("Add that should fail did.\n");
176         ERR_clear_error();
177         if(ENGINE_remove(new_h2))
178                 {
179                 printf("Remove *should* have failed but didn't!\n");
180                 goto end;
181                 }
182         else
183                 printf("Remove that should fail did.\n");
184         ERR_clear_error();
185         if(!ENGINE_remove(new_h3))
186                 {
187                 printf("Remove failed!\n");
188                 goto end;
189                 }
190         display_engine_list();
191         if(!ENGINE_remove(new_h4))
192                 {
193                 printf("Remove failed!\n");
194                 goto end;
195                 }
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();
200         if(ptr)
201                 if(!ENGINE_remove(ptr))
202                         printf("Remove failed!i - probably no hardware "
203                                 "support present.\n");
204         if (ptr)
205                 ENGINE_free(ptr);
206         display_engine_list();
207         if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
208                 {
209                 printf("Couldn't add and remove to an empty list!\n");
210                 goto end;
211                 }
212         else
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++)
216                 {
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))
224                         {
225                         printf("Couldn't create block of ENGINE structures.\n"
226                                 "I'll probably also core-dump now, damn.\n");
227                         goto end;
228                         }
229                 }
230         for(loop = 0; loop < 512; loop++)
231                 {
232                 if(!ENGINE_add(block[loop]))
233                         {
234                         printf("\nAdding stopped at %i, (%s,%s)\n",
235                                 loop, ENGINE_get_id(block[loop]),
236                                 ENGINE_get_name(block[loop]));
237                         goto cleanup_loop;
238                         }
239                 else
240                         printf("."); fflush(stdout);
241                 }
242 cleanup_loop:
243         printf("\nAbout to empty the engine-type list\n");
244         while((ptr = ENGINE_get_first()) != NULL)
245                 {
246                 if(!ENGINE_remove(ptr))
247                         {
248                         printf("\nRemove failed!\n");
249                         goto end;
250                         }
251                 ENGINE_free(ptr);
252                 printf("."); fflush(stdout);
253                 }
254         for(loop = 0; loop < 512; loop++)
255                 {
256                 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
257                 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
258                 }
259         printf("\nTests completed happily\n");
260         to_return = 0;
261 end:
262         if(to_return)
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++)
269                 if(block[loop])
270                         ENGINE_free(block[loop]);
271         ENGINE_cleanup();
272         CRYPTO_cleanup_all_ex_data();
273         ERR_free_strings();
274         ERR_remove_state(0);
275         CRYPTO_mem_leaks_fp(stderr);
276         return to_return;
277         }