blob: 21cd20af050f19469525ffe984731761e370b582 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Rich Salz440e5d82016-05-17 14:20:24 -04002 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
Richard Levitte5270e702000-10-26 21:07:28 +00003 *
Rich Salz440e5d82016-05-17 14:20:24 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Richard Levitte5270e702000-10-26 21:07:28 +00008 */
9
Richard Levitte1ae6dda2001-06-23 16:44:15 +000010#include <stdio.h>
Geoff Thorpe1023cfe2001-09-10 21:02:06 +000011#include <string.h>
Dr. Stephen Henson70531c12008-12-20 17:04:40 +000012#include <openssl/e_os2.h>
Richard Levitte0b13e9f2003-01-30 17:39:26 +000013
14#ifdef OPENSSL_NO_ENGINE
15int main(int argc, char *argv[])
16{
17 printf("No ENGINE support\n");
Matt Caswell0f113f32015-01-22 03:40:55 +000018 return (0);
Richard Levitte0b13e9f2003-01-30 17:39:26 +000019}
20#else
Matt Caswell0f113f32015-01-22 03:40:55 +000021# include <openssl/buffer.h>
22# include <openssl/crypto.h>
23# include <openssl/engine.h>
24# include <openssl/err.h>
Richard Levitte5270e702000-10-26 21:07:28 +000025
Ben Laurie41a15c42005-03-31 09:26:39 +000026static void display_engine_list(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000027{
28 ENGINE *h;
29 int loop;
Richard Levitte5270e702000-10-26 21:07:28 +000030
Matt Caswell0f113f32015-01-22 03:40:55 +000031 h = ENGINE_get_first();
32 loop = 0;
33 printf("listing available engine types\n");
34 while (h) {
35 printf("engine %i, id = \"%s\", name = \"%s\"\n",
36 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
37 h = ENGINE_get_next(h);
38 }
39 printf("end of list\n");
40 /*
41 * ENGINE_get_first() increases the struct_ref counter, so we must call
42 * ENGINE_free() to decrease it again
43 */
44 ENGINE_free(h);
45}
Richard Levitte5270e702000-10-26 21:07:28 +000046
47int main(int argc, char *argv[])
Matt Caswell0f113f32015-01-22 03:40:55 +000048{
49 ENGINE *block[512];
50 char buf[256];
Rich Salzbbd86bf2016-01-07 15:06:38 -050051 const char *id, *name, *p;
Matt Caswell0f113f32015-01-22 03:40:55 +000052 ENGINE *ptr;
53 int loop;
54 int to_return = 1;
55 ENGINE *new_h1 = NULL;
56 ENGINE *new_h2 = NULL;
57 ENGINE *new_h3 = NULL;
58 ENGINE *new_h4 = NULL;
Richard Levitte5270e702000-10-26 21:07:28 +000059
Rich Salzbbd86bf2016-01-07 15:06:38 -050060 p = getenv("OPENSSL_DEBUG_MEMORY");
61 if (p != NULL && strcmp(p, "on") == 0)
62 CRYPTO_set_mem_debug(1);
Richard Levitte5270e702000-10-26 21:07:28 +000063
Rich Salz16f8d4e2015-05-04 18:00:15 -040064 memset(block, 0, sizeof(block));
Matt Caswell0f113f32015-01-22 03:40:55 +000065 if (((new_h1 = ENGINE_new()) == NULL) ||
66 !ENGINE_set_id(new_h1, "test_id0") ||
67 !ENGINE_set_name(new_h1, "First test item") ||
68 ((new_h2 = ENGINE_new()) == NULL) ||
69 !ENGINE_set_id(new_h2, "test_id1") ||
70 !ENGINE_set_name(new_h2, "Second test item") ||
71 ((new_h3 = ENGINE_new()) == NULL) ||
72 !ENGINE_set_id(new_h3, "test_id2") ||
73 !ENGINE_set_name(new_h3, "Third test item") ||
74 ((new_h4 = ENGINE_new()) == NULL) ||
75 !ENGINE_set_id(new_h4, "test_id3") ||
76 !ENGINE_set_name(new_h4, "Fourth test item")) {
77 printf("Couldn't set up test ENGINE structures\n");
78 goto end;
79 }
80 printf("\nenginetest beginning\n\n");
81 display_engine_list();
82 if (!ENGINE_add(new_h1)) {
83 printf("Add failed!\n");
84 goto end;
85 }
86 display_engine_list();
87 ptr = ENGINE_get_first();
88 if (!ENGINE_remove(ptr)) {
89 printf("Remove failed!\n");
90 goto end;
91 }
Rich Salzefa7dd62015-05-01 10:15:18 -040092 ENGINE_free(ptr);
Matt Caswell0f113f32015-01-22 03:40:55 +000093 display_engine_list();
94 if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) {
95 printf("Add failed!\n");
96 goto end;
97 }
98 display_engine_list();
99 if (!ENGINE_remove(new_h2)) {
100 printf("Remove failed!\n");
101 goto end;
102 }
103 display_engine_list();
104 if (!ENGINE_add(new_h4)) {
105 printf("Add failed!\n");
106 goto end;
107 }
108 display_engine_list();
109 if (ENGINE_add(new_h3)) {
110 printf("Add *should* have failed but didn't!\n");
111 goto end;
112 } else
113 printf("Add that should fail did.\n");
114 ERR_clear_error();
115 if (ENGINE_remove(new_h2)) {
116 printf("Remove *should* have failed but didn't!\n");
117 goto end;
118 } else
119 printf("Remove that should fail did.\n");
120 ERR_clear_error();
121 if (!ENGINE_remove(new_h3)) {
122 printf("Remove failed!\n");
123 goto end;
124 }
125 display_engine_list();
126 if (!ENGINE_remove(new_h4)) {
127 printf("Remove failed!\n");
128 goto end;
129 }
130 display_engine_list();
131 /*
132 * Depending on whether there's any hardware support compiled in, this
133 * remove may be destined to fail.
134 */
135 ptr = ENGINE_get_first();
136 if (ptr)
137 if (!ENGINE_remove(ptr))
138 printf("Remove failed!i - probably no hardware "
139 "support present.\n");
Rich Salzefa7dd62015-05-01 10:15:18 -0400140 ENGINE_free(ptr);
Matt Caswell0f113f32015-01-22 03:40:55 +0000141 display_engine_list();
142 if (!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) {
143 printf("Couldn't add and remove to an empty list!\n");
144 goto end;
145 } else
146 printf("Successfully added and removed to an empty list!\n");
147 printf("About to beef up the engine-type list\n");
148 for (loop = 0; loop < 512; loop++) {
149 sprintf(buf, "id%i", loop);
Rich Salz7644a9a2015-12-16 16:12:24 -0500150 id = OPENSSL_strdup(buf);
Matt Caswell0f113f32015-01-22 03:40:55 +0000151 sprintf(buf, "Fake engine type %i", loop);
Rich Salz7644a9a2015-12-16 16:12:24 -0500152 name = OPENSSL_strdup(buf);
Matt Caswell0f113f32015-01-22 03:40:55 +0000153 if (((block[loop] = ENGINE_new()) == NULL) ||
154 !ENGINE_set_id(block[loop], id) ||
155 !ENGINE_set_name(block[loop], name)) {
156 printf("Couldn't create block of ENGINE structures.\n"
157 "I'll probably also core-dump now, damn.\n");
158 goto end;
159 }
160 }
161 for (loop = 0; loop < 512; loop++) {
162 if (!ENGINE_add(block[loop])) {
163 printf("\nAdding stopped at %i, (%s,%s)\n",
164 loop, ENGINE_get_id(block[loop]),
165 ENGINE_get_name(block[loop]));
166 goto cleanup_loop;
167 } else
168 printf(".");
169 fflush(stdout);
170 }
171 cleanup_loop:
172 printf("\nAbout to empty the engine-type list\n");
173 while ((ptr = ENGINE_get_first()) != NULL) {
174 if (!ENGINE_remove(ptr)) {
175 printf("\nRemove failed!\n");
176 goto end;
177 }
178 ENGINE_free(ptr);
179 printf(".");
180 fflush(stdout);
181 }
182 for (loop = 0; loop < 512; loop++) {
183 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
184 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
185 }
186 printf("\nTests completed happily\n");
187 to_return = 0;
188 end:
189 if (to_return)
190 ERR_print_errors_fp(stderr);
Rich Salzefa7dd62015-05-01 10:15:18 -0400191 ENGINE_free(new_h1);
192 ENGINE_free(new_h2);
193 ENGINE_free(new_h3);
194 ENGINE_free(new_h4);
Matt Caswell0f113f32015-01-22 03:40:55 +0000195 for (loop = 0; loop < 512; loop++)
Rich Salzefa7dd62015-05-01 10:15:18 -0400196 ENGINE_free(block[loop]);
Matt Caswell8793f012016-02-08 16:45:35 +0000197
Viktor Dukhovnic2e27312016-01-10 14:42:10 -0500198#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Dr. Stephen Henson541e9562016-01-14 22:00:03 +0000199 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
200 to_return = 1;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500201#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000202 return to_return;
203}
Richard Levitte0b13e9f2003-01-30 17:39:26 +0000204#endif