2 * Copyright 2000-2017 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
13 #include <openssl/e_os2.h>
15 #ifdef OPENSSL_NO_ENGINE
16 int main(int argc, char *argv[])
18 printf("No ENGINE support\n");
22 # include <openssl/buffer.h>
23 # include <openssl/crypto.h>
24 # include <openssl/engine.h>
25 # include <openssl/err.h>
26 # include "testutil.h"
28 static void display_engine_list(void)
34 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
35 TEST_info("#%d: id = \"%s\", name = \"%s\"",
36 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
40 * ENGINE_get_first() increases the struct_ref counter, so we must call
41 * ENGINE_free() to decrease it again
48 static int test_engines(void)
50 ENGINE *block[NUMTOADD];
52 const char *id, *name;
56 ENGINE *new_h1 = NULL;
57 ENGINE *new_h2 = NULL;
58 ENGINE *new_h3 = NULL;
59 ENGINE *new_h4 = NULL;
61 memset(block, 0, sizeof(block));
62 if (!TEST_ptr(new_h1 = ENGINE_new())
63 || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
64 || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
65 || !TEST_ptr(new_h2 = ENGINE_new())
66 || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
67 || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
68 || !TEST_ptr(new_h3 = ENGINE_new())
69 || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
70 || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
71 || !TEST_ptr(new_h4 = ENGINE_new())
72 || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
73 || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
75 TEST_info("Engines:");
76 display_engine_list();
78 if (!TEST_true(ENGINE_add(new_h1)))
80 TEST_info("Engines:");
81 display_engine_list();
83 ptr = ENGINE_get_first();
84 if (!TEST_true(ENGINE_remove(ptr)))
87 TEST_info("Engines:");
88 display_engine_list();
90 if (!TEST_true(ENGINE_add(new_h3))
91 || !TEST_true(ENGINE_add(new_h2)))
93 TEST_info("Engines:");
94 display_engine_list();
96 if (!TEST_true(ENGINE_remove(new_h2)))
98 TEST_info("Engines:");
99 display_engine_list();
101 if (!TEST_true(ENGINE_add(new_h4)))
103 TEST_info("Engines:");
104 display_engine_list();
107 if (!TEST_false(ENGINE_add(new_h3)))
112 if (!TEST_false(ENGINE_remove(new_h2)))
116 if (!TEST_true(ENGINE_remove(new_h3)))
118 TEST_info("Engines:");
119 display_engine_list();
121 if (!TEST_true(ENGINE_remove(new_h4)))
123 TEST_info("Engines:");
124 display_engine_list();
127 * Depending on whether there's any hardware support compiled in, this
128 * remove may be destined to fail.
130 if ((ptr = ENGINE_get_first()) != NULL) {
131 if (!ENGINE_remove(ptr))
132 TEST_info("Remove failed - probably no hardware support present");
135 TEST_info("Engines:");
136 display_engine_list();
138 if (!TEST_true(ENGINE_add(new_h1))
139 || !TEST_true(ENGINE_remove(new_h1)))
142 TEST_info("About to beef up the engine-type list");
143 for (loop = 0; loop < NUMTOADD; loop++) {
144 sprintf(buf, "id%d", loop);
145 id = OPENSSL_strdup(buf);
146 sprintf(buf, "Fake engine type %d", loop);
147 name = OPENSSL_strdup(buf);
148 if (!TEST_ptr(block[loop] = ENGINE_new())
149 || !TEST_true(ENGINE_set_id(block[loop], id))
150 || !TEST_true(ENGINE_set_name(block[loop], name)))
153 for (loop = 0; loop < NUMTOADD; loop++) {
154 if (!TEST_true(ENGINE_add(block[loop]))) {
155 test_note("Adding stopped at %d, (%s,%s)",
156 loop, ENGINE_get_id(block[loop]),
157 ENGINE_get_name(block[loop]));
162 TEST_info("About to empty the engine-type list");
163 while ((ptr = ENGINE_get_first()) != NULL) {
164 if (!TEST_true(ENGINE_remove(ptr)))
168 for (loop = 0; loop < NUMTOADD; loop++) {
169 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
170 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
179 for (loop = 0; loop < NUMTOADD; loop++)
180 ENGINE_free(block[loop]);
184 void register_tests(void)
186 ADD_TEST(test_engines);