1 /* crypto/engine/enginetest.c */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/e_os2.h>
64 #ifdef OPENSSL_NO_ENGINE
65 int main(int argc, char *argv[])
67 printf("No ENGINE support\n");
71 # include <openssl/buffer.h>
72 # include <openssl/crypto.h>
73 # include <openssl/engine.h>
74 # include <openssl/err.h>
76 static void display_engine_list(void)
81 h = ENGINE_get_first();
83 printf("listing available engine types\n");
85 printf("engine %i, id = \"%s\", name = \"%s\"\n",
86 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
87 h = ENGINE_get_next(h);
89 printf("end of list\n");
91 * ENGINE_get_first() increases the struct_ref counter, so we must call
92 * ENGINE_free() to decrease it again
97 int main(int argc, char *argv[])
101 const char *id, *name, *p;
105 ENGINE *new_h1 = NULL;
106 ENGINE *new_h2 = NULL;
107 ENGINE *new_h3 = NULL;
108 ENGINE *new_h4 = NULL;
110 p = getenv("OPENSSL_DEBUG_MEMORY");
111 if (p != NULL && strcmp(p, "on") == 0)
112 CRYPTO_set_mem_debug(1);
113 ERR_load_crypto_strings();
115 memset(block, 0, sizeof(block));
116 if (((new_h1 = ENGINE_new()) == NULL) ||
117 !ENGINE_set_id(new_h1, "test_id0") ||
118 !ENGINE_set_name(new_h1, "First test item") ||
119 ((new_h2 = ENGINE_new()) == NULL) ||
120 !ENGINE_set_id(new_h2, "test_id1") ||
121 !ENGINE_set_name(new_h2, "Second test item") ||
122 ((new_h3 = ENGINE_new()) == NULL) ||
123 !ENGINE_set_id(new_h3, "test_id2") ||
124 !ENGINE_set_name(new_h3, "Third test item") ||
125 ((new_h4 = ENGINE_new()) == NULL) ||
126 !ENGINE_set_id(new_h4, "test_id3") ||
127 !ENGINE_set_name(new_h4, "Fourth test item")) {
128 printf("Couldn't set up test ENGINE structures\n");
131 printf("\nenginetest beginning\n\n");
132 display_engine_list();
133 if (!ENGINE_add(new_h1)) {
134 printf("Add failed!\n");
137 display_engine_list();
138 ptr = ENGINE_get_first();
139 if (!ENGINE_remove(ptr)) {
140 printf("Remove failed!\n");
144 display_engine_list();
145 if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) {
146 printf("Add failed!\n");
149 display_engine_list();
150 if (!ENGINE_remove(new_h2)) {
151 printf("Remove failed!\n");
154 display_engine_list();
155 if (!ENGINE_add(new_h4)) {
156 printf("Add failed!\n");
159 display_engine_list();
160 if (ENGINE_add(new_h3)) {
161 printf("Add *should* have failed but didn't!\n");
164 printf("Add that should fail did.\n");
166 if (ENGINE_remove(new_h2)) {
167 printf("Remove *should* have failed but didn't!\n");
170 printf("Remove that should fail did.\n");
172 if (!ENGINE_remove(new_h3)) {
173 printf("Remove failed!\n");
176 display_engine_list();
177 if (!ENGINE_remove(new_h4)) {
178 printf("Remove failed!\n");
181 display_engine_list();
183 * Depending on whether there's any hardware support compiled in, this
184 * remove may be destined to fail.
186 ptr = ENGINE_get_first();
188 if (!ENGINE_remove(ptr))
189 printf("Remove failed!i - probably no hardware "
190 "support present.\n");
192 display_engine_list();
193 if (!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) {
194 printf("Couldn't add and remove to an empty list!\n");
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 sprintf(buf, "id%i", loop);
201 id = OPENSSL_strdup(buf);
202 sprintf(buf, "Fake engine type %i", loop);
203 name = OPENSSL_strdup(buf);
204 if (((block[loop] = ENGINE_new()) == NULL) ||
205 !ENGINE_set_id(block[loop], id) ||
206 !ENGINE_set_name(block[loop], name)) {
207 printf("Couldn't create block of ENGINE structures.\n"
208 "I'll probably also core-dump now, damn.\n");
212 for (loop = 0; loop < 512; loop++) {
213 if (!ENGINE_add(block[loop])) {
214 printf("\nAdding stopped at %i, (%s,%s)\n",
215 loop, ENGINE_get_id(block[loop]),
216 ENGINE_get_name(block[loop]));
223 printf("\nAbout to empty the engine-type list\n");
224 while ((ptr = ENGINE_get_first()) != NULL) {
225 if (!ENGINE_remove(ptr)) {
226 printf("\nRemove failed!\n");
233 for (loop = 0; loop < 512; loop++) {
234 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
235 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
237 printf("\nTests completed happily\n");
241 ERR_print_errors_fp(stderr);
246 for (loop = 0; loop < 512; loop++)
247 ENGINE_free(block[loop]);
249 CRYPTO_cleanup_all_ex_data();
251 ERR_remove_thread_state(NULL);
252 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
253 CRYPTO_mem_leaks_fp(stderr);