2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * 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
12 #include <openssl/e_os2.h>
14 #ifdef OPENSSL_NO_ENGINE
15 int main(int argc, char *argv[])
17 printf("No ENGINE support\n");
21 # include <openssl/buffer.h>
22 # include <openssl/crypto.h>
23 # include <openssl/engine.h>
24 # include <openssl/err.h>
26 static void display_engine_list(void)
31 h = ENGINE_get_first();
33 printf("listing available engine types\n");
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);
39 printf("end of list\n");
41 * ENGINE_get_first() increases the struct_ref counter, so we must call
42 * ENGINE_free() to decrease it again
47 int main(int argc, char *argv[])
51 const char *id, *name, *p;
55 ENGINE *new_h1 = NULL;
56 ENGINE *new_h2 = NULL;
57 ENGINE *new_h3 = NULL;
58 ENGINE *new_h4 = NULL;
60 p = getenv("OPENSSL_DEBUG_MEMORY");
61 if (p != NULL && strcmp(p, "on") == 0)
62 CRYPTO_set_mem_debug(1);
64 memset(block, 0, sizeof(block));
65 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");
80 printf("\nenginetest beginning\n\n");
81 display_engine_list();
82 if (!ENGINE_add(new_h1)) {
83 printf("Add failed!\n");
86 display_engine_list();
87 ptr = ENGINE_get_first();
88 if (!ENGINE_remove(ptr)) {
89 printf("Remove failed!\n");
93 display_engine_list();
94 if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) {
95 printf("Add failed!\n");
98 display_engine_list();
99 if (!ENGINE_remove(new_h2)) {
100 printf("Remove failed!\n");
103 display_engine_list();
104 if (!ENGINE_add(new_h4)) {
105 printf("Add failed!\n");
108 display_engine_list();
109 if (ENGINE_add(new_h3)) {
110 printf("Add *should* have failed but didn't!\n");
113 printf("Add that should fail did.\n");
115 if (ENGINE_remove(new_h2)) {
116 printf("Remove *should* have failed but didn't!\n");
119 printf("Remove that should fail did.\n");
121 if (!ENGINE_remove(new_h3)) {
122 printf("Remove failed!\n");
125 display_engine_list();
126 if (!ENGINE_remove(new_h4)) {
127 printf("Remove failed!\n");
130 display_engine_list();
132 * Depending on whether there's any hardware support compiled in, this
133 * remove may be destined to fail.
135 ptr = ENGINE_get_first();
137 if (!ENGINE_remove(ptr))
138 printf("Remove failed!i - probably no hardware "
139 "support present.\n");
141 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");
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);
150 id = OPENSSL_strdup(buf);
151 sprintf(buf, "Fake engine type %i", loop);
152 name = OPENSSL_strdup(buf);
153 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");
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]));
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");
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]));
186 printf("\nTests completed happily\n");
190 ERR_print_errors_fp(stderr);
195 for (loop = 0; loop < 512; loop++)
196 ENGINE_free(block[loop]);
198 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
199 if (CRYPTO_mem_leaks_fp(stderr) <= 0)