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 #ifndef OPENSSL_NO_ENGINE
16 # include <openssl/buffer.h>
17 # include <openssl/crypto.h>
18 # include <openssl/engine.h>
19 # include <openssl/err.h>
20 # include "testutil.h"
22 static void display_engine_list(void)
28 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
29 TEST_info("#%d: id = \"%s\", name = \"%s\"",
30 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
34 * ENGINE_get_first() increases the struct_ref counter, so we must call
35 * ENGINE_free() to decrease it again
42 static int test_engines(void)
44 ENGINE *block[NUMTOADD];
46 const char *id, *name;
50 ENGINE *new_h1 = NULL;
51 ENGINE *new_h2 = NULL;
52 ENGINE *new_h3 = NULL;
53 ENGINE *new_h4 = NULL;
55 memset(block, 0, sizeof(block));
56 if (!TEST_ptr(new_h1 = ENGINE_new())
57 || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
58 || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
59 || !TEST_ptr(new_h2 = ENGINE_new())
60 || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
61 || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
62 || !TEST_ptr(new_h3 = ENGINE_new())
63 || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
64 || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
65 || !TEST_ptr(new_h4 = ENGINE_new())
66 || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
67 || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
69 TEST_info("Engines:");
70 display_engine_list();
72 if (!TEST_true(ENGINE_add(new_h1)))
74 TEST_info("Engines:");
75 display_engine_list();
77 ptr = ENGINE_get_first();
78 if (!TEST_true(ENGINE_remove(ptr)))
81 TEST_info("Engines:");
82 display_engine_list();
84 if (!TEST_true(ENGINE_add(new_h3))
85 || !TEST_true(ENGINE_add(new_h2)))
87 TEST_info("Engines:");
88 display_engine_list();
90 if (!TEST_true(ENGINE_remove(new_h2)))
92 TEST_info("Engines:");
93 display_engine_list();
95 if (!TEST_true(ENGINE_add(new_h4)))
97 TEST_info("Engines:");
98 display_engine_list();
101 if (!TEST_false(ENGINE_add(new_h3)))
106 if (!TEST_false(ENGINE_remove(new_h2)))
110 if (!TEST_true(ENGINE_remove(new_h3)))
112 TEST_info("Engines:");
113 display_engine_list();
115 if (!TEST_true(ENGINE_remove(new_h4)))
117 TEST_info("Engines:");
118 display_engine_list();
121 * Depending on whether there's any hardware support compiled in, this
122 * remove may be destined to fail.
124 if ((ptr = ENGINE_get_first()) != NULL) {
125 if (!ENGINE_remove(ptr))
126 TEST_info("Remove failed - probably no hardware support present");
129 TEST_info("Engines:");
130 display_engine_list();
132 if (!TEST_true(ENGINE_add(new_h1))
133 || !TEST_true(ENGINE_remove(new_h1)))
136 TEST_info("About to beef up the engine-type list");
137 for (loop = 0; loop < NUMTOADD; loop++) {
138 sprintf(buf, "id%d", loop);
139 id = OPENSSL_strdup(buf);
140 sprintf(buf, "Fake engine type %d", loop);
141 name = OPENSSL_strdup(buf);
142 if (!TEST_ptr(block[loop] = ENGINE_new())
143 || !TEST_true(ENGINE_set_id(block[loop], id))
144 || !TEST_true(ENGINE_set_name(block[loop], name)))
147 for (loop = 0; loop < NUMTOADD; loop++) {
148 if (!TEST_true(ENGINE_add(block[loop]))) {
149 test_note("Adding stopped at %d, (%s,%s)",
150 loop, ENGINE_get_id(block[loop]),
151 ENGINE_get_name(block[loop]));
156 TEST_info("About to empty the engine-type list");
157 while ((ptr = ENGINE_get_first()) != NULL) {
158 if (!TEST_true(ENGINE_remove(ptr)))
162 for (loop = 0; loop < NUMTOADD; loop++) {
163 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
164 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
173 for (loop = 0; loop < NUMTOADD; loop++)
174 ENGINE_free(block[loop]);
179 int setup_tests(void)
181 #ifdef OPENSSL_NO_ENGINE
182 TEST_note("No ENGINE support");
184 ADD_TEST(test_engines);