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