Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 1 | /* crypto/engine/enginetest.c */ |
| 2 | /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL |
| 3 | * project 2000. |
| 4 | */ |
| 5 | /* ==================================================================== |
Geoff Thorpe | 2b67158 | 2001-09-14 18:31:57 +0000 | [diff] [blame] | 6 | * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * licensing@OpenSSL.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). |
| 56 | * |
| 57 | */ |
| 58 | |
Richard Levitte | 1ae6dda | 2001-06-23 16:44:15 +0000 | [diff] [blame] | 59 | #include <stdio.h> |
Geoff Thorpe | 1023cfe | 2001-09-10 21:02:06 +0000 | [diff] [blame] | 60 | #include <string.h> |
Richard Levitte | 0b13e9f | 2003-01-30 17:39:26 +0000 | [diff] [blame^] | 61 | |
| 62 | #ifdef OPENSSL_NO_ENGINE |
| 63 | int main(int argc, char *argv[]) |
| 64 | { |
| 65 | printf("No ENGINE support\n"); |
| 66 | return(0); |
| 67 | } |
| 68 | #else |
| 69 | #include <openssl/e_os2.h> |
Bodo Möller | 619b2c0 | 2001-09-06 13:09:00 +0000 | [diff] [blame] | 70 | #include <openssl/buffer.h> |
| 71 | #include <openssl/crypto.h> |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 72 | #include <openssl/engine.h> |
| 73 | #include <openssl/err.h> |
| 74 | |
| 75 | static void display_engine_list() |
| 76 | { |
| 77 | ENGINE *h; |
| 78 | int loop; |
| 79 | |
| 80 | h = ENGINE_get_first(); |
| 81 | loop = 0; |
| 82 | printf("listing available engine types\n"); |
| 83 | while(h) |
| 84 | { |
| 85 | printf("engine %i, id = \"%s\", name = \"%s\"\n", |
| 86 | loop++, ENGINE_get_id(h), ENGINE_get_name(h)); |
| 87 | h = ENGINE_get_next(h); |
| 88 | } |
| 89 | printf("end of list\n"); |
Bodo Möller | e8e7fbd | 2002-03-25 23:18:11 +0000 | [diff] [blame] | 90 | /* ENGINE_get_first() increases the struct_ref counter, so we |
| 91 | must call ENGINE_free() to decrease it again */ |
| 92 | ENGINE_free(h); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int main(int argc, char *argv[]) |
| 96 | { |
| 97 | ENGINE *block[512]; |
| 98 | char buf[256]; |
| 99 | const char *id, *name; |
| 100 | ENGINE *ptr; |
| 101 | int loop; |
| 102 | int to_return = 1; |
| 103 | ENGINE *new_h1 = NULL; |
| 104 | ENGINE *new_h2 = NULL; |
| 105 | ENGINE *new_h3 = NULL; |
| 106 | ENGINE *new_h4 = NULL; |
| 107 | |
Bodo Möller | e8e7fbd | 2002-03-25 23:18:11 +0000 | [diff] [blame] | 108 | /* enable memory leak checking unless explicitly disabled */ |
| 109 | if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) |
| 110 | { |
| 111 | CRYPTO_malloc_debug_init(); |
| 112 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | /* OPENSSL_DEBUG_MEMORY=off */ |
| 117 | CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); |
| 118 | } |
| 119 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 120 | ERR_load_crypto_strings(); |
| 121 | |
| 122 | memset(block, 0, 512 * sizeof(ENGINE *)); |
| 123 | if(((new_h1 = ENGINE_new()) == NULL) || |
| 124 | !ENGINE_set_id(new_h1, "test_id0") || |
| 125 | !ENGINE_set_name(new_h1, "First test item") || |
| 126 | ((new_h2 = ENGINE_new()) == NULL) || |
| 127 | !ENGINE_set_id(new_h2, "test_id1") || |
| 128 | !ENGINE_set_name(new_h2, "Second test item") || |
| 129 | ((new_h3 = ENGINE_new()) == NULL) || |
| 130 | !ENGINE_set_id(new_h3, "test_id2") || |
| 131 | !ENGINE_set_name(new_h3, "Third test item") || |
| 132 | ((new_h4 = ENGINE_new()) == NULL) || |
| 133 | !ENGINE_set_id(new_h4, "test_id3") || |
| 134 | !ENGINE_set_name(new_h4, "Fourth test item")) |
| 135 | { |
| 136 | printf("Couldn't set up test ENGINE structures\n"); |
| 137 | goto end; |
| 138 | } |
| 139 | printf("\nenginetest beginning\n\n"); |
| 140 | display_engine_list(); |
| 141 | if(!ENGINE_add(new_h1)) |
| 142 | { |
| 143 | printf("Add failed!\n"); |
| 144 | goto end; |
| 145 | } |
| 146 | display_engine_list(); |
| 147 | ptr = ENGINE_get_first(); |
| 148 | if(!ENGINE_remove(ptr)) |
| 149 | { |
| 150 | printf("Remove failed!\n"); |
| 151 | goto end; |
| 152 | } |
Bodo Möller | e8e7fbd | 2002-03-25 23:18:11 +0000 | [diff] [blame] | 153 | if (ptr) |
| 154 | ENGINE_free(ptr); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 155 | display_engine_list(); |
| 156 | if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) |
| 157 | { |
| 158 | printf("Add failed!\n"); |
| 159 | goto end; |
| 160 | } |
| 161 | display_engine_list(); |
| 162 | if(!ENGINE_remove(new_h2)) |
| 163 | { |
| 164 | printf("Remove failed!\n"); |
| 165 | goto end; |
| 166 | } |
| 167 | display_engine_list(); |
| 168 | if(!ENGINE_add(new_h4)) |
| 169 | { |
| 170 | printf("Add failed!\n"); |
| 171 | goto end; |
| 172 | } |
| 173 | display_engine_list(); |
| 174 | if(ENGINE_add(new_h3)) |
| 175 | { |
| 176 | printf("Add *should* have failed but didn't!\n"); |
| 177 | goto end; |
| 178 | } |
| 179 | else |
| 180 | printf("Add that should fail did.\n"); |
| 181 | ERR_clear_error(); |
| 182 | if(ENGINE_remove(new_h2)) |
| 183 | { |
| 184 | printf("Remove *should* have failed but didn't!\n"); |
| 185 | goto end; |
| 186 | } |
| 187 | else |
| 188 | printf("Remove that should fail did.\n"); |
Geoff Thorpe | b6d1e52 | 2001-09-25 20:00:51 +0000 | [diff] [blame] | 189 | ERR_clear_error(); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 190 | if(!ENGINE_remove(new_h3)) |
| 191 | { |
| 192 | printf("Remove failed!\n"); |
| 193 | goto end; |
| 194 | } |
| 195 | display_engine_list(); |
| 196 | if(!ENGINE_remove(new_h4)) |
| 197 | { |
| 198 | printf("Remove failed!\n"); |
| 199 | goto end; |
| 200 | } |
| 201 | display_engine_list(); |
| 202 | /* Depending on whether there's any hardware support compiled |
| 203 | * in, this remove may be destined to fail. */ |
| 204 | ptr = ENGINE_get_first(); |
| 205 | if(ptr) |
| 206 | if(!ENGINE_remove(ptr)) |
| 207 | printf("Remove failed!i - probably no hardware " |
| 208 | "support present.\n"); |
Bodo Möller | e8e7fbd | 2002-03-25 23:18:11 +0000 | [diff] [blame] | 209 | if (ptr) |
| 210 | ENGINE_free(ptr); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 211 | display_engine_list(); |
| 212 | if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) |
| 213 | { |
| 214 | printf("Couldn't add and remove to an empty list!\n"); |
| 215 | goto end; |
| 216 | } |
| 217 | else |
| 218 | printf("Successfully added and removed to an empty list!\n"); |
| 219 | printf("About to beef up the engine-type list\n"); |
| 220 | for(loop = 0; loop < 512; loop++) |
| 221 | { |
| 222 | sprintf(buf, "id%i", loop); |
Bodo Möller | 619b2c0 | 2001-09-06 13:09:00 +0000 | [diff] [blame] | 223 | id = BUF_strdup(buf); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 224 | sprintf(buf, "Fake engine type %i", loop); |
Bodo Möller | 619b2c0 | 2001-09-06 13:09:00 +0000 | [diff] [blame] | 225 | name = BUF_strdup(buf); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 226 | if(((block[loop] = ENGINE_new()) == NULL) || |
| 227 | !ENGINE_set_id(block[loop], id) || |
| 228 | !ENGINE_set_name(block[loop], name)) |
| 229 | { |
| 230 | printf("Couldn't create block of ENGINE structures.\n" |
| 231 | "I'll probably also core-dump now, damn.\n"); |
| 232 | goto end; |
| 233 | } |
| 234 | } |
| 235 | for(loop = 0; loop < 512; loop++) |
| 236 | { |
| 237 | if(!ENGINE_add(block[loop])) |
| 238 | { |
| 239 | printf("\nAdding stopped at %i, (%s,%s)\n", |
| 240 | loop, ENGINE_get_id(block[loop]), |
| 241 | ENGINE_get_name(block[loop])); |
| 242 | goto cleanup_loop; |
| 243 | } |
| 244 | else |
| 245 | printf("."); fflush(stdout); |
| 246 | } |
| 247 | cleanup_loop: |
| 248 | printf("\nAbout to empty the engine-type list\n"); |
| 249 | while((ptr = ENGINE_get_first()) != NULL) |
| 250 | { |
| 251 | if(!ENGINE_remove(ptr)) |
| 252 | { |
| 253 | printf("\nRemove failed!\n"); |
| 254 | goto end; |
| 255 | } |
Bodo Möller | e8e7fbd | 2002-03-25 23:18:11 +0000 | [diff] [blame] | 256 | ENGINE_free(ptr); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 257 | printf("."); fflush(stdout); |
| 258 | } |
| 259 | for(loop = 0; loop < 512; loop++) |
| 260 | { |
Bodo Möller | 8573fa1 | 2001-09-10 17:46:54 +0000 | [diff] [blame] | 261 | OPENSSL_free((void *)ENGINE_get_id(block[loop])); |
| 262 | OPENSSL_free((void *)ENGINE_get_name(block[loop])); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 263 | } |
| 264 | printf("\nTests completed happily\n"); |
| 265 | to_return = 0; |
| 266 | end: |
| 267 | if(to_return) |
| 268 | ERR_print_errors_fp(stderr); |
| 269 | if(new_h1) ENGINE_free(new_h1); |
| 270 | if(new_h2) ENGINE_free(new_h2); |
| 271 | if(new_h3) ENGINE_free(new_h3); |
| 272 | if(new_h4) ENGINE_free(new_h4); |
| 273 | for(loop = 0; loop < 512; loop++) |
| 274 | if(block[loop]) |
| 275 | ENGINE_free(block[loop]); |
Bodo Möller | e8e7fbd | 2002-03-25 23:18:11 +0000 | [diff] [blame] | 276 | ENGINE_cleanup(); |
| 277 | CRYPTO_cleanup_all_ex_data(); |
| 278 | ERR_free_strings(); |
| 279 | ERR_remove_state(0); |
| 280 | CRYPTO_mem_leaks_fp(stderr); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 281 | return to_return; |
| 282 | } |
Richard Levitte | 0b13e9f | 2003-01-30 17:39:26 +0000 | [diff] [blame^] | 283 | #endif |