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>
25 # include "testutil.h"
27 static void display_engine_list(void)
33 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
34 TEST_info("#%d: id = \"%s\", name = \"%s\"",
35 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
39 * ENGINE_get_first() increases the struct_ref counter, so we must call
40 * ENGINE_free() to decrease it again
47 static int test_engines(void)
49 ENGINE *block[NUMTOADD];
51 const char *id, *name;
55 ENGINE *new_h1 = NULL;
56 ENGINE *new_h2 = NULL;
57 ENGINE *new_h3 = NULL;
58 ENGINE *new_h4 = NULL;
60 memset(block, 0, sizeof(block));
61 if (!TEST_ptr(new_h1 = ENGINE_new())
62 || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
63 || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
64 || !TEST_ptr(new_h2 = ENGINE_new())
65 || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
66 || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
67 || !TEST_ptr(new_h3 = ENGINE_new())
68 || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
69 || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
70 || !TEST_ptr(new_h4 = ENGINE_new())
71 || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
72 || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
74 TEST_info("Engines:");
75 display_engine_list();
77 if (!TEST_true(ENGINE_add(new_h1)))
79 TEST_info("Engines:");
80 display_engine_list();
82 ptr = ENGINE_get_first();
83 if (!TEST_true(ENGINE_remove(ptr)))
86 TEST_info("Engines:");
87 display_engine_list();
89 if (!TEST_true(ENGINE_add(new_h3))
90 || !TEST_true(ENGINE_add(new_h2)))
92 TEST_info("Engines:");
93 display_engine_list();
95 if (!TEST_true(ENGINE_remove(new_h2)))
97 TEST_info("Engines:");
98 display_engine_list();
100 if (!TEST_true(ENGINE_add(new_h4)))
102 TEST_info("Engines:");
103 display_engine_list();
106 if (!TEST_false(ENGINE_add(new_h3)))
111 if (!TEST_false(ENGINE_remove(new_h2)))
115 if (!TEST_true(ENGINE_remove(new_h3)))
117 TEST_info("Engines:");
118 display_engine_list();
120 if (!TEST_true(ENGINE_remove(new_h4)))
122 TEST_info("Engines:");
123 display_engine_list();
126 * Depending on whether there's any hardware support compiled in, this
127 * remove may be destined to fail.
129 if ((ptr = ENGINE_get_first()) != NULL) {
130 if (!ENGINE_remove(ptr))
131 TEST_info("Remove failed - probably no hardware support present");
134 TEST_info("Engines:");
135 display_engine_list();
137 if (!TEST_true(ENGINE_add(new_h1))
138 || !TEST_true(ENGINE_remove(new_h1)))
141 TEST_info("About to beef up the engine-type list");
142 for (loop = 0; loop < NUMTOADD; loop++) {
143 sprintf(buf, "id%d", loop);
144 id = OPENSSL_strdup(buf);
145 sprintf(buf, "Fake engine type %d", loop);
146 name = OPENSSL_strdup(buf);
147 if (!TEST_ptr(block[loop] = ENGINE_new())
148 || !TEST_true(ENGINE_set_id(block[loop], id))
149 || !TEST_true(ENGINE_set_name(block[loop], name)))
152 for (loop = 0; loop < NUMTOADD; loop++) {
153 if (!TEST_true(ENGINE_add(block[loop]))) {
154 printf("Adding stopped at %d, (%s,%s)",
155 loop, ENGINE_get_id(block[loop]),
156 ENGINE_get_name(block[loop]));
161 TEST_info("About to empty the engine-type list");
162 while ((ptr = ENGINE_get_first()) != NULL) {
163 if (!TEST_true(ENGINE_remove(ptr)))
167 for (loop = 0; loop < NUMTOADD; loop++) {
168 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
169 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
178 for (loop = 0; loop < NUMTOADD; loop++)
179 ENGINE_free(block[loop]);
183 void register_tests(void)
185 ADD_TEST(test_engines);