strdup() is a X/Open extension.
[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 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 <stdio.h>
60 #include <openssl/e_os2.h>
61 #ifdef OPENSSL_SYS_VMS_DECC
62 # define _XOPEN_SOURCE_EXTENDED
63 #endif
64 #include <string.h>
65 #include <openssl/engine.h>
66 #include <openssl/err.h>
67
68 static void display_engine_list()
69         {
70         ENGINE *h;
71         int loop;
72
73         h = ENGINE_get_first();
74         loop = 0;
75         printf("listing available engine types\n");
76         while(h)
77                 {
78                 printf("engine %i, id = \"%s\", name = \"%s\"\n",
79                         loop++, ENGINE_get_id(h), ENGINE_get_name(h));
80                 h = ENGINE_get_next(h);
81                 }
82         printf("end of list\n");
83         }
84
85 int main(int argc, char *argv[])
86         {
87         ENGINE *block[512];
88         char buf[256];
89         const char *id, *name;
90         ENGINE *ptr;
91         int loop;
92         int to_return = 1;
93         ENGINE *new_h1 = NULL;
94         ENGINE *new_h2 = NULL;
95         ENGINE *new_h3 = NULL;
96         ENGINE *new_h4 = NULL;
97
98         ERR_load_crypto_strings();
99
100         memset(block, 0, 512 * sizeof(ENGINE *));
101         if(((new_h1 = ENGINE_new()) == NULL) ||
102                         !ENGINE_set_id(new_h1, "test_id0") ||
103                         !ENGINE_set_name(new_h1, "First test item") ||
104                         ((new_h2 = ENGINE_new()) == NULL) ||
105                         !ENGINE_set_id(new_h2, "test_id1") ||
106                         !ENGINE_set_name(new_h2, "Second test item") ||
107                         ((new_h3 = ENGINE_new()) == NULL) ||
108                         !ENGINE_set_id(new_h3, "test_id2") ||
109                         !ENGINE_set_name(new_h3, "Third test item") ||
110                         ((new_h4 = ENGINE_new()) == NULL) ||
111                         !ENGINE_set_id(new_h4, "test_id3") ||
112                         !ENGINE_set_name(new_h4, "Fourth test item"))
113                 {
114                 printf("Couldn't set up test ENGINE structures\n");
115                 goto end;
116                 }
117         printf("\nenginetest beginning\n\n");
118         display_engine_list();
119         if(!ENGINE_add(new_h1))
120                 {
121                 printf("Add failed!\n");
122                 goto end;
123                 }
124         display_engine_list();
125         ptr = ENGINE_get_first();
126         if(!ENGINE_remove(ptr))
127                 {
128                 printf("Remove failed!\n");
129                 goto end;
130                 }
131         display_engine_list();
132         if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
133                 {
134                 printf("Add failed!\n");
135                 goto end;
136                 }
137         display_engine_list();
138         if(!ENGINE_remove(new_h2))
139                 {
140                 printf("Remove failed!\n");
141                 goto end;
142                 }
143         display_engine_list();
144         if(!ENGINE_add(new_h4))
145                 {
146                 printf("Add failed!\n");
147                 goto end;
148                 }
149         display_engine_list();
150         if(ENGINE_add(new_h3))
151                 {
152                 printf("Add *should* have failed but didn't!\n");
153                 goto end;
154                 }
155         else
156                 printf("Add that should fail did.\n");
157         ERR_clear_error();
158         if(ENGINE_remove(new_h2))
159                 {
160                 printf("Remove *should* have failed but didn't!\n");
161                 goto end;
162                 }
163         else
164                 printf("Remove that should fail did.\n");
165         if(!ENGINE_remove(new_h1))
166                 {
167                 printf("Remove failed!\n");
168                 goto end;
169                 }
170         display_engine_list();
171         if(!ENGINE_remove(new_h3))
172                 {
173                 printf("Remove failed!\n");
174                 goto end;
175                 }
176         display_engine_list();
177         if(!ENGINE_remove(new_h4))
178                 {
179                 printf("Remove failed!\n");
180                 goto end;
181                 }
182         display_engine_list();
183         /* Depending on whether there's any hardware support compiled
184          * in, this remove may be destined to fail. */
185         ptr = ENGINE_get_first();
186         if(ptr)
187                 if(!ENGINE_remove(ptr))
188                         printf("Remove failed!i - probably no hardware "
189                                 "support present.\n");
190         display_engine_list();
191         if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
192                 {
193                 printf("Couldn't add and remove to an empty list!\n");
194                 goto end;
195                 }
196         else
197                 printf("Successfully added and removed to an empty list!\n");
198         printf("About to beef up the engine-type list\n");
199         for(loop = 0; loop < 512; loop++)
200                 {
201                 sprintf(buf, "id%i", loop);
202                 id = strdup(buf);
203                 sprintf(buf, "Fake engine type %i", loop);
204                 name = strdup(buf);
205                 if(((block[loop] = ENGINE_new()) == NULL) ||
206                                 !ENGINE_set_id(block[loop], id) ||
207                                 !ENGINE_set_name(block[loop], name))
208                         {
209                         printf("Couldn't create block of ENGINE structures.\n"
210                                 "I'll probably also core-dump now, damn.\n");
211                         goto end;
212                         }
213                 }
214         for(loop = 0; loop < 512; loop++)
215                 {
216                 if(!ENGINE_add(block[loop]))
217                         {
218                         printf("\nAdding stopped at %i, (%s,%s)\n",
219                                 loop, ENGINE_get_id(block[loop]),
220                                 ENGINE_get_name(block[loop]));
221                         goto cleanup_loop;
222                         }
223                 else
224                         printf("."); fflush(stdout);
225                 }
226 cleanup_loop:
227         printf("\nAbout to empty the engine-type list\n");
228         while((ptr = ENGINE_get_first()) != NULL)
229                 {
230                 if(!ENGINE_remove(ptr))
231                         {
232                         printf("\nRemove failed!\n");
233                         goto end;
234                         }
235                 printf("."); fflush(stdout);
236                 }
237         for(loop = 0; loop < 512; loop++)
238                 {
239                 free((char *)(ENGINE_get_id(block[loop])));
240                 free((char *)(ENGINE_get_name(block[loop])));
241                 }
242         printf("\nTests completed happily\n");
243         to_return = 0;
244 end:
245         if(to_return)
246                 ERR_print_errors_fp(stderr);
247         if(new_h1) ENGINE_free(new_h1);
248         if(new_h2) ENGINE_free(new_h2);
249         if(new_h3) ENGINE_free(new_h3);
250         if(new_h4) ENGINE_free(new_h4);
251         for(loop = 0; loop < 512; loop++)
252                 if(block[loop])
253                         ENGINE_free(block[loop]);
254         return to_return;
255         }