]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
8fe3127c | 2 | * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved. |
5270e702 | 3 | * |
440e5d80 RS |
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 | |
5270e702 RL |
8 | */ |
9 | ||
1ae6ddac | 10 | #include <stdio.h> |
1023cfe7 | 11 | #include <string.h> |
c2500f65 | 12 | #include <stdlib.h> |
70531c14 | 13 | #include <openssl/e_os2.h> |
0b13e9f0 | 14 | |
ad887416 | 15 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
16 | # include <openssl/buffer.h> |
17 | # include <openssl/crypto.h> | |
18 | # include <openssl/engine.h> | |
19 | # include <openssl/err.h> | |
b66411f6 | 20 | # include "testutil.h" |
5270e702 | 21 | |
41a15c4f | 22 | static void display_engine_list(void) |
0f113f3e MC |
23 | { |
24 | ENGINE *h; | |
25 | int loop; | |
5270e702 | 26 | |
0f113f3e | 27 | loop = 0; |
b66411f6 RS |
28 | for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) { |
29 | TEST_info("#%d: id = \"%s\", name = \"%s\"", | |
0f113f3e | 30 | loop++, ENGINE_get_id(h), ENGINE_get_name(h)); |
0f113f3e | 31 | } |
b66411f6 | 32 | |
0f113f3e MC |
33 | /* |
34 | * ENGINE_get_first() increases the struct_ref counter, so we must call | |
35 | * ENGINE_free() to decrease it again | |
36 | */ | |
37 | ENGINE_free(h); | |
38 | } | |
5270e702 | 39 | |
b66411f6 RS |
40 | #define NUMTOADD 512 |
41 | ||
42 | static int test_engines(void) | |
0f113f3e | 43 | { |
b66411f6 | 44 | ENGINE *block[NUMTOADD]; |
0f113f3e | 45 | char buf[256]; |
b66411f6 | 46 | const char *id, *name; |
0f113f3e MC |
47 | ENGINE *ptr; |
48 | int loop; | |
b66411f6 | 49 | int to_return = 0; |
0f113f3e MC |
50 | ENGINE *new_h1 = NULL; |
51 | ENGINE *new_h2 = NULL; | |
52 | ENGINE *new_h3 = NULL; | |
53 | ENGINE *new_h4 = NULL; | |
5270e702 | 54 | |
16f8d4eb | 55 | memset(block, 0, sizeof(block)); |
b66411f6 RS |
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"))) | |
0f113f3e | 68 | goto end; |
b66411f6 | 69 | TEST_info("Engines:"); |
0f113f3e | 70 | display_engine_list(); |
b66411f6 RS |
71 | |
72 | if (!TEST_true(ENGINE_add(new_h1))) | |
0f113f3e | 73 | goto end; |
b66411f6 | 74 | TEST_info("Engines:"); |
0f113f3e | 75 | display_engine_list(); |
b66411f6 | 76 | |
0f113f3e | 77 | ptr = ENGINE_get_first(); |
b66411f6 | 78 | if (!TEST_true(ENGINE_remove(ptr))) |
0f113f3e | 79 | goto end; |
efa7dd64 | 80 | ENGINE_free(ptr); |
b66411f6 | 81 | TEST_info("Engines:"); |
0f113f3e | 82 | display_engine_list(); |
b66411f6 RS |
83 | |
84 | if (!TEST_true(ENGINE_add(new_h3)) | |
85 | || !TEST_true(ENGINE_add(new_h2))) | |
0f113f3e | 86 | goto end; |
b66411f6 | 87 | TEST_info("Engines:"); |
0f113f3e | 88 | display_engine_list(); |
b66411f6 RS |
89 | |
90 | if (!TEST_true(ENGINE_remove(new_h2))) | |
0f113f3e | 91 | goto end; |
b66411f6 | 92 | TEST_info("Engines:"); |
0f113f3e | 93 | display_engine_list(); |
b66411f6 RS |
94 | |
95 | if (!TEST_true(ENGINE_add(new_h4))) | |
0f113f3e | 96 | goto end; |
b66411f6 | 97 | TEST_info("Engines:"); |
0f113f3e | 98 | display_engine_list(); |
b66411f6 RS |
99 | |
100 | /* Should fail. */ | |
101 | if (!TEST_false(ENGINE_add(new_h3))) | |
0f113f3e | 102 | goto end; |
0f113f3e | 103 | ERR_clear_error(); |
b66411f6 RS |
104 | |
105 | /* Should fail. */ | |
106 | if (!TEST_false(ENGINE_remove(new_h2))) | |
0f113f3e | 107 | goto end; |
0f113f3e | 108 | ERR_clear_error(); |
b66411f6 RS |
109 | |
110 | if (!TEST_true(ENGINE_remove(new_h3))) | |
0f113f3e | 111 | goto end; |
b66411f6 | 112 | TEST_info("Engines:"); |
0f113f3e | 113 | display_engine_list(); |
b66411f6 RS |
114 | |
115 | if (!TEST_true(ENGINE_remove(new_h4))) | |
0f113f3e | 116 | goto end; |
b66411f6 | 117 | TEST_info("Engines:"); |
0f113f3e | 118 | display_engine_list(); |
b66411f6 | 119 | |
0f113f3e MC |
120 | /* |
121 | * Depending on whether there's any hardware support compiled in, this | |
122 | * remove may be destined to fail. | |
123 | */ | |
b66411f6 | 124 | if ((ptr = ENGINE_get_first()) != NULL) { |
0f113f3e | 125 | if (!ENGINE_remove(ptr)) |
b66411f6 RS |
126 | TEST_info("Remove failed - probably no hardware support present"); |
127 | } | |
efa7dd64 | 128 | ENGINE_free(ptr); |
b66411f6 | 129 | TEST_info("Engines:"); |
0f113f3e | 130 | display_engine_list(); |
b66411f6 RS |
131 | |
132 | if (!TEST_true(ENGINE_add(new_h1)) | |
133 | || !TEST_true(ENGINE_remove(new_h1))) | |
0f113f3e | 134 | goto end; |
b66411f6 RS |
135 | |
136 | TEST_info("About to beef up the engine-type list"); | |
137 | for (loop = 0; loop < NUMTOADD; loop++) { | |
138 | sprintf(buf, "id%d", loop); | |
7644a9ae | 139 | id = OPENSSL_strdup(buf); |
b66411f6 | 140 | sprintf(buf, "Fake engine type %d", loop); |
7644a9ae | 141 | name = OPENSSL_strdup(buf); |
b66411f6 RS |
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))) | |
0f113f3e | 145 | goto end; |
0f113f3e | 146 | } |
b66411f6 RS |
147 | for (loop = 0; loop < NUMTOADD; loop++) { |
148 | if (!TEST_true(ENGINE_add(block[loop]))) { | |
8fe3127c P |
149 | test_note("Adding stopped at %d, (%s,%s)", |
150 | loop, ENGINE_get_id(block[loop]), | |
151 | ENGINE_get_name(block[loop])); | |
0f113f3e | 152 | goto cleanup_loop; |
b66411f6 | 153 | } |
0f113f3e MC |
154 | } |
155 | cleanup_loop: | |
b66411f6 | 156 | TEST_info("About to empty the engine-type list"); |
0f113f3e | 157 | while ((ptr = ENGINE_get_first()) != NULL) { |
b66411f6 | 158 | if (!TEST_true(ENGINE_remove(ptr))) |
0f113f3e | 159 | goto end; |
0f113f3e | 160 | ENGINE_free(ptr); |
0f113f3e | 161 | } |
b66411f6 | 162 | for (loop = 0; loop < NUMTOADD; loop++) { |
0f113f3e MC |
163 | OPENSSL_free((void *)ENGINE_get_id(block[loop])); |
164 | OPENSSL_free((void *)ENGINE_get_name(block[loop])); | |
165 | } | |
b66411f6 RS |
166 | to_return = 1; |
167 | ||
0f113f3e | 168 | end: |
efa7dd64 RS |
169 | ENGINE_free(new_h1); |
170 | ENGINE_free(new_h2); | |
171 | ENGINE_free(new_h3); | |
172 | ENGINE_free(new_h4); | |
b66411f6 | 173 | for (loop = 0; loop < NUMTOADD; loop++) |
efa7dd64 | 174 | ENGINE_free(block[loop]); |
0f113f3e MC |
175 | return to_return; |
176 | } | |
ad887416 | 177 | #endif |
b66411f6 | 178 | |
ad887416 | 179 | int setup_tests(void) |
b66411f6 | 180 | { |
ad887416 P |
181 | #ifdef OPENSSL_NO_ENGINE |
182 | TEST_note("No ENGINE support"); | |
183 | #else | |
b66411f6 | 184 | ADD_TEST(test_engines); |
0b13e9f0 | 185 | #endif |
ad887416 P |
186 | return 1; |
187 | } |