]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/enginetest.c
Simplify the handling of shared library version numbers
[thirdparty/openssl.git] / test / enginetest.c
CommitLineData
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
RL
14
15#ifdef OPENSSL_NO_ENGINE
16int main(int argc, char *argv[])
17{
18 printf("No ENGINE support\n");
c2500f65 19 return EXIT_SUCCESS;
0b13e9f0
RL
20}
21#else
0f113f3e
MC
22# include <openssl/buffer.h>
23# include <openssl/crypto.h>
24# include <openssl/engine.h>
25# include <openssl/err.h>
b66411f6 26# include "testutil.h"
5270e702 27
41a15c4f 28static void display_engine_list(void)
0f113f3e
MC
29{
30 ENGINE *h;
31 int loop;
5270e702 32
0f113f3e 33 loop = 0;
b66411f6
RS
34 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
35 TEST_info("#%d: id = \"%s\", name = \"%s\"",
0f113f3e 36 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
0f113f3e 37 }
b66411f6 38
0f113f3e
MC
39 /*
40 * ENGINE_get_first() increases the struct_ref counter, so we must call
41 * ENGINE_free() to decrease it again
42 */
43 ENGINE_free(h);
44}
5270e702 45
b66411f6
RS
46#define NUMTOADD 512
47
48static int test_engines(void)
0f113f3e 49{
b66411f6 50 ENGINE *block[NUMTOADD];
0f113f3e 51 char buf[256];
b66411f6 52 const char *id, *name;
0f113f3e
MC
53 ENGINE *ptr;
54 int loop;
b66411f6 55 int to_return = 0;
0f113f3e
MC
56 ENGINE *new_h1 = NULL;
57 ENGINE *new_h2 = NULL;
58 ENGINE *new_h3 = NULL;
59 ENGINE *new_h4 = NULL;
5270e702 60
16f8d4eb 61 memset(block, 0, sizeof(block));
b66411f6
RS
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")))
0f113f3e 74 goto end;
b66411f6 75 TEST_info("Engines:");
0f113f3e 76 display_engine_list();
b66411f6
RS
77
78 if (!TEST_true(ENGINE_add(new_h1)))
0f113f3e 79 goto end;
b66411f6 80 TEST_info("Engines:");
0f113f3e 81 display_engine_list();
b66411f6 82
0f113f3e 83 ptr = ENGINE_get_first();
b66411f6 84 if (!TEST_true(ENGINE_remove(ptr)))
0f113f3e 85 goto end;
efa7dd64 86 ENGINE_free(ptr);
b66411f6 87 TEST_info("Engines:");
0f113f3e 88 display_engine_list();
b66411f6
RS
89
90 if (!TEST_true(ENGINE_add(new_h3))
91 || !TEST_true(ENGINE_add(new_h2)))
0f113f3e 92 goto end;
b66411f6 93 TEST_info("Engines:");
0f113f3e 94 display_engine_list();
b66411f6
RS
95
96 if (!TEST_true(ENGINE_remove(new_h2)))
0f113f3e 97 goto end;
b66411f6 98 TEST_info("Engines:");
0f113f3e 99 display_engine_list();
b66411f6
RS
100
101 if (!TEST_true(ENGINE_add(new_h4)))
0f113f3e 102 goto end;
b66411f6 103 TEST_info("Engines:");
0f113f3e 104 display_engine_list();
b66411f6
RS
105
106 /* Should fail. */
107 if (!TEST_false(ENGINE_add(new_h3)))
0f113f3e 108 goto end;
0f113f3e 109 ERR_clear_error();
b66411f6
RS
110
111 /* Should fail. */
112 if (!TEST_false(ENGINE_remove(new_h2)))
0f113f3e 113 goto end;
0f113f3e 114 ERR_clear_error();
b66411f6
RS
115
116 if (!TEST_true(ENGINE_remove(new_h3)))
0f113f3e 117 goto end;
b66411f6 118 TEST_info("Engines:");
0f113f3e 119 display_engine_list();
b66411f6
RS
120
121 if (!TEST_true(ENGINE_remove(new_h4)))
0f113f3e 122 goto end;
b66411f6 123 TEST_info("Engines:");
0f113f3e 124 display_engine_list();
b66411f6 125
0f113f3e
MC
126 /*
127 * Depending on whether there's any hardware support compiled in, this
128 * remove may be destined to fail.
129 */
b66411f6 130 if ((ptr = ENGINE_get_first()) != NULL) {
0f113f3e 131 if (!ENGINE_remove(ptr))
b66411f6
RS
132 TEST_info("Remove failed - probably no hardware support present");
133 }
efa7dd64 134 ENGINE_free(ptr);
b66411f6 135 TEST_info("Engines:");
0f113f3e 136 display_engine_list();
b66411f6
RS
137
138 if (!TEST_true(ENGINE_add(new_h1))
139 || !TEST_true(ENGINE_remove(new_h1)))
0f113f3e 140 goto end;
b66411f6
RS
141
142 TEST_info("About to beef up the engine-type list");
143 for (loop = 0; loop < NUMTOADD; loop++) {
144 sprintf(buf, "id%d", loop);
7644a9ae 145 id = OPENSSL_strdup(buf);
b66411f6 146 sprintf(buf, "Fake engine type %d", loop);
7644a9ae 147 name = OPENSSL_strdup(buf);
b66411f6
RS
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)))
0f113f3e 151 goto end;
0f113f3e 152 }
b66411f6
RS
153 for (loop = 0; loop < NUMTOADD; loop++) {
154 if (!TEST_true(ENGINE_add(block[loop]))) {
8fe3127c
P
155 test_note("Adding stopped at %d, (%s,%s)",
156 loop, ENGINE_get_id(block[loop]),
157 ENGINE_get_name(block[loop]));
0f113f3e 158 goto cleanup_loop;
b66411f6 159 }
0f113f3e
MC
160 }
161 cleanup_loop:
b66411f6 162 TEST_info("About to empty the engine-type list");
0f113f3e 163 while ((ptr = ENGINE_get_first()) != NULL) {
b66411f6 164 if (!TEST_true(ENGINE_remove(ptr)))
0f113f3e 165 goto end;
0f113f3e 166 ENGINE_free(ptr);
0f113f3e 167 }
b66411f6 168 for (loop = 0; loop < NUMTOADD; loop++) {
0f113f3e
MC
169 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
170 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
171 }
b66411f6
RS
172 to_return = 1;
173
0f113f3e 174 end:
efa7dd64
RS
175 ENGINE_free(new_h1);
176 ENGINE_free(new_h2);
177 ENGINE_free(new_h3);
178 ENGINE_free(new_h4);
b66411f6 179 for (loop = 0; loop < NUMTOADD; loop++)
efa7dd64 180 ENGINE_free(block[loop]);
0f113f3e
MC
181 return to_return;
182}
b66411f6
RS
183
184void register_tests(void)
185{
186 ADD_TEST(test_engines);
187}
0b13e9f0 188#endif