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