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