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