Convert key exchange to one shot call
[openssl.git] / test / enginetest.c
1 /*
2  * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <openssl/e_os2.h>
13
14 #ifdef OPENSSL_NO_ENGINE
15 int main(int argc, char *argv[])
16 {
17     printf("No ENGINE support\n");
18     return (0);
19 }
20 #else
21 # include <openssl/buffer.h>
22 # include <openssl/crypto.h>
23 # include <openssl/engine.h>
24 # include <openssl/err.h>
25 # include "testutil.h"
26
27 static void display_engine_list(void)
28 {
29     ENGINE *h;
30     int loop;
31
32     loop = 0;
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));
36     }
37
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 }
44
45 #define NUMTOADD 512
46
47 static int test_engines(void)
48 {
49     ENGINE *block[NUMTOADD];
50     char buf[256];
51     const char *id, *name;
52     ENGINE *ptr;
53     int loop;
54     int to_return = 0;
55     ENGINE *new_h1 = NULL;
56     ENGINE *new_h2 = NULL;
57     ENGINE *new_h3 = NULL;
58     ENGINE *new_h4 = NULL;
59
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")))
73         goto end;
74     TEST_info("Engines:");
75     display_engine_list();
76
77     if (!TEST_true(ENGINE_add(new_h1)))
78         goto end;
79     TEST_info("Engines:");
80     display_engine_list();
81
82     ptr = ENGINE_get_first();
83     if (!TEST_true(ENGINE_remove(ptr)))
84         goto end;
85     ENGINE_free(ptr);
86     TEST_info("Engines:");
87     display_engine_list();
88
89     if (!TEST_true(ENGINE_add(new_h3))
90             || !TEST_true(ENGINE_add(new_h2)))
91         goto end;
92     TEST_info("Engines:");
93     display_engine_list();
94
95     if (!TEST_true(ENGINE_remove(new_h2)))
96         goto end;
97     TEST_info("Engines:");
98     display_engine_list();
99
100     if (!TEST_true(ENGINE_add(new_h4)))
101         goto end;
102     TEST_info("Engines:");
103     display_engine_list();
104
105     /* Should fail. */
106     if (!TEST_false(ENGINE_add(new_h3)))
107         goto end;
108     ERR_clear_error();
109
110     /* Should fail. */
111     if (!TEST_false(ENGINE_remove(new_h2)))
112         goto end;
113     ERR_clear_error();
114
115     if (!TEST_true(ENGINE_remove(new_h3)))
116         goto end;
117     TEST_info("Engines:");
118     display_engine_list();
119
120     if (!TEST_true(ENGINE_remove(new_h4)))
121         goto end;
122     TEST_info("Engines:");
123     display_engine_list();
124
125     /*
126      * Depending on whether there's any hardware support compiled in, this
127      * remove may be destined to fail.
128      */
129     if ((ptr = ENGINE_get_first()) != NULL) {
130         if (!ENGINE_remove(ptr))
131             TEST_info("Remove failed - probably no hardware support present");
132     }
133     ENGINE_free(ptr);
134     TEST_info("Engines:");
135     display_engine_list();
136
137     if (!TEST_true(ENGINE_add(new_h1))
138             || !TEST_true(ENGINE_remove(new_h1)))
139         goto end;
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);
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)))
150             goto end;
151     }
152     for (loop = 0; loop < NUMTOADD; loop++) {
153         if (!TEST_true(ENGINE_add(block[loop]))) {
154             test_note("Adding stopped at %d, (%s,%s)",
155                       loop, ENGINE_get_id(block[loop]),
156                       ENGINE_get_name(block[loop]));
157             goto cleanup_loop;
158         }
159     }
160  cleanup_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)))
164             goto end;
165         ENGINE_free(ptr);
166     }
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]));
170     }
171     to_return = 1;
172
173  end:
174     ENGINE_free(new_h1);
175     ENGINE_free(new_h2);
176     ENGINE_free(new_h3);
177     ENGINE_free(new_h4);
178     for (loop = 0; loop < NUMTOADD; loop++)
179         ENGINE_free(block[loop]);
180     return to_return;
181 }
182
183 void register_tests(void)
184 {
185     ADD_TEST(test_engines);
186 }
187 #endif