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 # include "testutil.h"
17 #ifndef OPENSSL_NO_ENGINE
18 # include <openssl/buffer.h>
19 # include <openssl/crypto.h>
20 # include <openssl/engine.h>
21 # include <openssl/err.h>
23 static void display_engine_list(void)
29 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
30 TEST_info("#%d: id = \"%s\", name = \"%s\"",
31 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
35 * ENGINE_get_first() increases the struct_ref counter, so we must call
36 * ENGINE_free() to decrease it again
43 static int test_engines(void)
45 ENGINE *block[NUMTOADD];
47 const char *id, *name;
51 ENGINE *new_h1 = NULL;
52 ENGINE *new_h2 = NULL;
53 ENGINE *new_h3 = NULL;
54 ENGINE *new_h4 = NULL;
56 memset(block, 0, sizeof(block));
57 if (!TEST_ptr(new_h1 = ENGINE_new())
58 || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
59 || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
60 || !TEST_ptr(new_h2 = ENGINE_new())
61 || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
62 || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
63 || !TEST_ptr(new_h3 = ENGINE_new())
64 || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
65 || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
66 || !TEST_ptr(new_h4 = ENGINE_new())
67 || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
68 || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
70 TEST_info("Engines:");
71 display_engine_list();
73 if (!TEST_true(ENGINE_add(new_h1)))
75 TEST_info("Engines:");
76 display_engine_list();
78 ptr = ENGINE_get_first();
79 if (!TEST_true(ENGINE_remove(ptr)))
82 TEST_info("Engines:");
83 display_engine_list();
85 if (!TEST_true(ENGINE_add(new_h3))
86 || !TEST_true(ENGINE_add(new_h2)))
88 TEST_info("Engines:");
89 display_engine_list();
91 if (!TEST_true(ENGINE_remove(new_h2)))
93 TEST_info("Engines:");
94 display_engine_list();
96 if (!TEST_true(ENGINE_add(new_h4)))
98 TEST_info("Engines:");
99 display_engine_list();
102 if (!TEST_false(ENGINE_add(new_h3)))
107 if (!TEST_false(ENGINE_remove(new_h2)))
111 if (!TEST_true(ENGINE_remove(new_h3)))
113 TEST_info("Engines:");
114 display_engine_list();
116 if (!TEST_true(ENGINE_remove(new_h4)))
118 TEST_info("Engines:");
119 display_engine_list();
122 * Depending on whether there's any hardware support compiled in, this
123 * remove may be destined to fail.
125 if ((ptr = ENGINE_get_first()) != NULL) {
126 if (!ENGINE_remove(ptr))
127 TEST_info("Remove failed - probably no hardware support present");
130 TEST_info("Engines:");
131 display_engine_list();
133 if (!TEST_true(ENGINE_add(new_h1))
134 || !TEST_true(ENGINE_remove(new_h1)))
137 TEST_info("About to beef up the engine-type list");
138 for (loop = 0; loop < NUMTOADD; loop++) {
139 sprintf(buf, "id%d", loop);
140 id = OPENSSL_strdup(buf);
141 sprintf(buf, "Fake engine type %d", loop);
142 name = OPENSSL_strdup(buf);
143 if (!TEST_ptr(block[loop] = ENGINE_new())
144 || !TEST_true(ENGINE_set_id(block[loop], id))
145 || !TEST_true(ENGINE_set_name(block[loop], name)))
148 for (loop = 0; loop < NUMTOADD; loop++) {
149 if (!TEST_true(ENGINE_add(block[loop]))) {
150 test_note("Adding stopped at %d, (%s,%s)",
151 loop, ENGINE_get_id(block[loop]),
152 ENGINE_get_name(block[loop]));
157 TEST_info("About to empty the engine-type list");
158 while ((ptr = ENGINE_get_first()) != NULL) {
159 if (!TEST_true(ENGINE_remove(ptr)))
163 for (loop = 0; loop < NUMTOADD; loop++) {
164 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
165 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
174 for (loop = 0; loop < NUMTOADD; loop++)
175 ENGINE_free(block[loop]);
180 int setup_tests(void)
182 #ifdef OPENSSL_NO_ENGINE
183 TEST_note("No ENGINE support");
185 ADD_TEST(test_engines);