os/ossrv/ssl/tsrc/crypto_test/src/enginetest.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/enginetest.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,543 @@
     1.4 +/* crypto/engine/enginetest.c */
     1.5 +/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
     1.6 + * project 2000.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + *
    1.15 + * 1. Redistributions of source code must retain the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer. 
    1.17 + *
    1.18 + * 2. Redistributions in binary form must reproduce the above copyright
    1.19 + *    notice, this list of conditions and the following disclaimer in
    1.20 + *    the documentation and/or other materials provided with the
    1.21 + *    distribution.
    1.22 + *
    1.23 + * 3. All advertising materials mentioning features or use of this
    1.24 + *    software must display the following acknowledgment:
    1.25 + *    "This product includes software developed by the OpenSSL Project
    1.26 + *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
    1.27 + *
    1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.29 + *    endorse or promote products derived from this software without
    1.30 + *    prior written permission. For written permission, please contact
    1.31 + *    licensing@OpenSSL.org.
    1.32 + *
    1.33 + * 5. Products derived from this software may not be called "OpenSSL"
    1.34 + *    nor may "OpenSSL" appear in their names without prior written
    1.35 + *    permission of the OpenSSL Project.
    1.36 + *
    1.37 + * 6. Redistributions of any form whatsoever must retain the following
    1.38 + *    acknowledgment:
    1.39 + *    "This product includes software developed by the OpenSSL Project
    1.40 + *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
    1.41 + *
    1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.45 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.54 + * ====================================================================
    1.55 + *	
    1.56 + * This product includes cryptographic software written by Eric Young
    1.57 + * (eay@cryptsoft.com).  This product includes software written by Tim
    1.58 + * Hudson (tjh@cryptsoft.com).
    1.59 + *
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 +#include <stdio.h>
    1.65 +#include <string.h>
    1.66 +
    1.67 +#ifdef OPENSSL_NO_ENGINE
    1.68 +int main(int argc, char *argv[])
    1.69 +{
    1.70 +    printf("No ENGINE support\n");
    1.71 +    return(0);
    1.72 +}
    1.73 +#else
    1.74 +#include <openssl/e_os2.h>
    1.75 +#include <openssl/buffer.h>
    1.76 +#include <openssl/crypto.h>
    1.77 +#include <openssl/engine.h>
    1.78 +#include <openssl/err.h>
    1.79 +
    1.80 +#ifdef SYMBIAN
    1.81 +#ifdef stdin
    1.82 +#undef stdin
    1.83 +#endif
    1.84 +#ifdef stdout
    1.85 +#undef stdout
    1.86 +#endif
    1.87 +#ifdef stderr
    1.88 +#undef stderr
    1.89 +#endif
    1.90 +
    1.91 +#define stdin fp_stdin
    1.92 +#define stdout fp_stdout
    1.93 +#define stderr fp_stderr
    1.94 +
    1.95 +extern FILE *fp_stdout;
    1.96 +extern FILE *fp_stderr;
    1.97 +#endif
    1.98 +
    1.99 +static void display_engine_list(void)
   1.100 +	{
   1.101 +	ENGINE *h;
   1.102 +	int loop;
   1.103 +
   1.104 +	h = ENGINE_get_first();
   1.105 +	if(errno==ENOMEM)
   1.106 +  {
   1.107 +	  return ;
   1.108 +	}
   1.109 +	loop = 0;
   1.110 +	fprintf(stdout,"listing available engine types\n");
   1.111 +	if(errno==ENOMEM)
   1.112 +  {
   1.113 +	  return ;
   1.114 +	}
   1.115 +	while(h)
   1.116 +		{
   1.117 +		fprintf(stdout,"engine %i, id = \"%s\", name = \"%s\"\n",
   1.118 +			loop++, ENGINE_get_id(h), ENGINE_get_name(h));
   1.119 +		if(errno==ENOMEM)
   1.120 +   	{
   1.121 +	  return ;
   1.122 +	  }	
   1.123 +		h = ENGINE_get_next(h);
   1.124 +		if(errno==ENOMEM)
   1.125 +   	{
   1.126 +	  return ;
   1.127 +	  }
   1.128 +		}
   1.129 +	fprintf(stdout,"end of list\n");
   1.130 +	if(errno==ENOMEM)
   1.131 +  {
   1.132 +	  return ;
   1.133 +	}
   1.134 +	/* ENGINE_get_first() increases the struct_ref counter, so we 
   1.135 +           must call ENGINE_free() to decrease it again */
   1.136 +	ENGINE_free(h);
   1.137 +	if(errno==ENOMEM)
   1.138 +  {
   1.139 +	  return ;
   1.140 +	}
   1.141 +	}
   1.142 +
   1.143 +#ifndef SYMBIAN
   1.144 +int main(int argc, char *argv[])
   1.145 +#else
   1.146 +int engine_main(int argc, char *argv[])
   1.147 +#endif
   1.148 +
   1.149 +	{
   1.150 +	ENGINE *block[512];
   1.151 +	char buf[256];
   1.152 +	const char *id, *name;
   1.153 +	ENGINE *ptr;
   1.154 +	int loop;
   1.155 +	int to_return = 1;
   1.156 +	ENGINE *new_h1 = NULL;
   1.157 +	ENGINE *new_h2 = NULL;
   1.158 +	ENGINE *new_h3 = NULL;
   1.159 +	ENGINE *new_h4 = NULL;
   1.160 +    
   1.161 +	/* enable memory leak checking unless explicitly disabled */
   1.162 +
   1.163 +	ERR_load_crypto_strings();
   1.164 +	if(errno==ENOMEM)
   1.165 +  {
   1.166 +	  return 1;
   1.167 +	}
   1.168 +
   1.169 +	memset(block, 0, 512 * sizeof(ENGINE *));
   1.170 +	if(errno==ENOMEM)
   1.171 +  {
   1.172 +	  return 1;
   1.173 +	}
   1.174 +	if(((new_h1 = ENGINE_new()) == NULL) ||
   1.175 +			!ENGINE_set_id(new_h1, "test_id0") ||
   1.176 +			!ENGINE_set_name(new_h1, "First test item") ||
   1.177 +			((new_h2 = ENGINE_new()) == NULL) ||
   1.178 +			!ENGINE_set_id(new_h2, "test_id1") ||
   1.179 +			!ENGINE_set_name(new_h2, "Second test item") ||
   1.180 +			((new_h3 = ENGINE_new()) == NULL) ||
   1.181 +			!ENGINE_set_id(new_h3, "test_id2") ||
   1.182 +			!ENGINE_set_name(new_h3, "Third test item") ||
   1.183 +			((new_h4 = ENGINE_new()) == NULL) ||
   1.184 +			!ENGINE_set_id(new_h4, "test_id3") ||
   1.185 +			!ENGINE_set_name(new_h4, "Fourth test item"))
   1.186 +		{
   1.187 +		if(errno==ENOMEM)
   1.188 +   	{
   1.189 +	  return 1;
   1.190 +	  }	
   1.191 +		fprintf(stdout,"Couldn't set up test ENGINE structures\n");
   1.192 +		goto end;
   1.193 +		}
   1.194 +	fprintf(stdout,"\nenginetest beginning\n\n");
   1.195 +	if(errno==ENOMEM)
   1.196 +  {
   1.197 +	  return 1;
   1.198 +	}
   1.199 +	display_engine_list();
   1.200 +	if(!ENGINE_add(new_h1))
   1.201 +		{
   1.202 +		fprintf(stdout,"Add failed!\n");
   1.203 +		goto end;
   1.204 +		}
   1.205 +	display_engine_list();
   1.206 +	if(errno==ENOMEM)
   1.207 +  {
   1.208 +	  return 1;
   1.209 +	}
   1.210 +	ptr = ENGINE_get_first();
   1.211 +	if(errno==ENOMEM)
   1.212 +  {
   1.213 +	  return 1;
   1.214 +	}
   1.215 +	if(!ENGINE_remove(ptr))
   1.216 +		{
   1.217 +		if(errno==ENOMEM)
   1.218 +   	{
   1.219 +	  return 1;
   1.220 +	  }	
   1.221 +		fprintf(stdout,"Remove failed!\n");
   1.222 +		goto end;
   1.223 +		}
   1.224 +	if (ptr)
   1.225 +	{
   1.226 +		ENGINE_free(ptr);
   1.227 +		if(errno==ENOMEM)
   1.228 +   	{
   1.229 +	  return 1;
   1.230 +	  }
   1.231 +	}	
   1.232 +	display_engine_list();
   1.233 +	if(errno==ENOMEM)
   1.234 +  {
   1.235 +	  return 1;
   1.236 +	}
   1.237 +	if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
   1.238 +		{
   1.239 +		if(errno==ENOMEM)
   1.240 +   	{
   1.241 +	  return 1;
   1.242 +	  }	
   1.243 +		fprintf(stdout,"Add failed!\n");
   1.244 +		goto end;
   1.245 +		}
   1.246 +	display_engine_list();
   1.247 +	if(errno==ENOMEM)
   1.248 +  {
   1.249 +	  return 1;
   1.250 +	}
   1.251 +	if(!ENGINE_remove(new_h2))
   1.252 +		{
   1.253 +		if(errno==ENOMEM)
   1.254 +   	{
   1.255 +	  return 1;
   1.256 +	  }	
   1.257 +		fprintf(stdout,"Remove failed!\n");
   1.258 +		goto end;
   1.259 +		}
   1.260 +	display_engine_list();
   1.261 +	if(errno==ENOMEM)
   1.262 +  {
   1.263 +	  return 1;
   1.264 +	}
   1.265 +	if(!ENGINE_add(new_h4))
   1.266 +		{
   1.267 +		if(errno==ENOMEM)
   1.268 +   	{
   1.269 +	  return 1;
   1.270 +	  }	
   1.271 +		fprintf(stdout,"Add failed!\n");
   1.272 +		goto end;
   1.273 +		}
   1.274 +	display_engine_list();
   1.275 +	if(errno==ENOMEM)
   1.276 +  {
   1.277 +	  return 1;
   1.278 +	}
   1.279 +	if(ENGINE_add(new_h3))
   1.280 +		{
   1.281 +		if(errno==ENOMEM)
   1.282 +   	{
   1.283 +	  return 1;
   1.284 +	  }	
   1.285 +		fprintf(stdout,"Add *should* have failed but didn't!\n");
   1.286 +		goto end;
   1.287 +		}
   1.288 +	else
   1.289 +		fprintf(stdout,"Add that should fail did.\n");
   1.290 +	ERR_clear_error();
   1.291 +	if(errno==ENOMEM)
   1.292 +  {
   1.293 +	  return 1;
   1.294 +	}
   1.295 +	if(ENGINE_remove(new_h2))
   1.296 +		{
   1.297 +		if(errno==ENOMEM)
   1.298 +   	{
   1.299 +	  return 1;
   1.300 +	  }	
   1.301 +		fprintf(stdout,"Remove *should* have failed but didn't!\n");
   1.302 +		goto end;
   1.303 +		}
   1.304 +	else
   1.305 +		fprintf(stdout,"Remove that should fail did.\n");
   1.306 +	ERR_clear_error();
   1.307 +	if(errno==ENOMEM)
   1.308 +  {
   1.309 +	  return 1;
   1.310 +	}
   1.311 +	if(!ENGINE_remove(new_h3))
   1.312 +		{
   1.313 +		if(errno==ENOMEM)
   1.314 +   	{
   1.315 +	  return 1;
   1.316 +	  }	
   1.317 +		fprintf(stdout,"Remove failed!\n");
   1.318 +		goto end;
   1.319 +		}
   1.320 +	display_engine_list();
   1.321 +	if(errno==ENOMEM)
   1.322 +  {
   1.323 +	  return 1;
   1.324 +	}
   1.325 +	if(!ENGINE_remove(new_h4))
   1.326 +		{
   1.327 +		if(errno==ENOMEM)
   1.328 +   	{
   1.329 +	  return 1;
   1.330 +	  }	
   1.331 +		fprintf(stdout,"Remove failed!\n");
   1.332 +		goto end;
   1.333 +		}
   1.334 +	display_engine_list();
   1.335 +	if(errno==ENOMEM)
   1.336 +  {
   1.337 +	  return 1;
   1.338 +	}
   1.339 +	/* Depending on whether there's any hardware support compiled
   1.340 +	 * in, this remove may be destined to fail. */
   1.341 +	ptr = ENGINE_get_first();
   1.342 +	if(errno==ENOMEM)
   1.343 +  {
   1.344 +	  return 1;
   1.345 +	}
   1.346 +	if(ptr)
   1.347 +		if(!ENGINE_remove(ptr))
   1.348 +		{
   1.349 +			if(errno==ENOMEM)
   1.350 +    	{
   1.351 +	      return 1;
   1.352 +	    }
   1.353 +			fprintf(stdout,"Remove failed!i - probably no hardware "
   1.354 +				"support present.\n");
   1.355 +		}		
   1.356 +	if (ptr)
   1.357 +	{
   1.358 +		ENGINE_free(ptr);
   1.359 +		if(errno==ENOMEM)
   1.360 +   	{
   1.361 +	  return 1;
   1.362 +	  }
   1.363 +	}	
   1.364 +	display_engine_list();
   1.365 +	if(errno==ENOMEM)
   1.366 +  {
   1.367 +	  return 1;
   1.368 +	}
   1.369 +	if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
   1.370 +		{
   1.371 +		if(errno==ENOMEM)
   1.372 +   	{
   1.373 +	  return 1;
   1.374 +	  }	
   1.375 +		fprintf(stdout,"Couldn't add and remove to an empty list!\n");
   1.376 +		goto end;
   1.377 +		}
   1.378 +	else
   1.379 +		fprintf(stdout,"Successfully added and removed to an empty list!\n");
   1.380 +	fprintf(stdout,"About to beef up the engine-type list\n");
   1.381 +	for(loop = 0; loop < 512; loop++)
   1.382 +		{
   1.383 +		sprintf(buf, "id%i", loop);
   1.384 +		id = BUF_strdup(buf);
   1.385 +		if(errno==ENOMEM)
   1.386 +   	{
   1.387 +	  return 1;
   1.388 +	  }
   1.389 +		sprintf(buf, "Fake engine type %i", loop);
   1.390 +		name = BUF_strdup(buf);
   1.391 +		if(errno==ENOMEM)
   1.392 +   	{
   1.393 +	  return 1;
   1.394 +	  }
   1.395 +		if(((block[loop] = ENGINE_new()) == NULL) ||
   1.396 +				!ENGINE_set_id(block[loop], id) ||
   1.397 +				!ENGINE_set_name(block[loop], name))
   1.398 +			{
   1.399 +			if(errno==ENOMEM)
   1.400 +     	{
   1.401 +	      return 1;
   1.402 +	    }	
   1.403 +			fprintf(stdout,"Couldn't create block of ENGINE structures.\n"
   1.404 +				"I'll probably also core-dump now, damn.\n");
   1.405 +			goto end;
   1.406 +			}
   1.407 +		}
   1.408 +	for(loop = 0; loop < 512; loop++)
   1.409 +		{
   1.410 +		if(!ENGINE_add(block[loop]))
   1.411 +			{
   1.412 +			if(errno==ENOMEM)
   1.413 +     	{
   1.414 +	     return 1;
   1.415 +	    } 	
   1.416 +			fprintf(stdout,"\nAdding stopped at %i, (%s,%s)\n",
   1.417 +				loop, ENGINE_get_id(block[loop]),
   1.418 +				ENGINE_get_name(block[loop]));
   1.419 +			if(errno==ENOMEM)
   1.420 +     	{
   1.421 +	     return 1;
   1.422 +	    } 	
   1.423 +						
   1.424 +			goto cleanup_loop;
   1.425 +			}
   1.426 +		else
   1.427 +			fprintf(stdout,"."); fflush(stdout);
   1.428 +		}
   1.429 +cleanup_loop:
   1.430 +	fprintf(stdout,"\nAbout to empty the engine-type list\n");
   1.431 +	while((ptr = ENGINE_get_first()) != NULL)
   1.432 +		{
   1.433 +			if(errno==ENOMEM)
   1.434 +   	 {
   1.435 +	    return 1;
   1.436 +	   }
   1.437 +		if(!ENGINE_remove(ptr))
   1.438 +			{
   1.439 +			if(errno==ENOMEM)
   1.440 +     	{
   1.441 +	      return 1;
   1.442 +	    }
   1.443 +			fprintf(stdout,"\nRemove failed!\n");
   1.444 +			goto end;
   1.445 +			}
   1.446 +		ENGINE_free(ptr);
   1.447 +		if(errno==ENOMEM)
   1.448 +   	{
   1.449 +	  return 1;
   1.450 +	  }
   1.451 +		fprintf(stdout,"."); fflush(stdout);
   1.452 +		}
   1.453 +	for(loop = 0; loop < 512; loop++)
   1.454 +		{
   1.455 +		OPENSSL_free((void *)ENGINE_get_id(block[loop]));
   1.456 +		if(errno==ENOMEM)
   1.457 +     	{
   1.458 +	     return 1;
   1.459 +	    } 	
   1.460 +		
   1.461 +		OPENSSL_free((void *)ENGINE_get_name(block[loop]));
   1.462 +			if(errno==ENOMEM)
   1.463 +     	{
   1.464 +	     return 1;
   1.465 +	    } 	
   1.466 +		
   1.467 +		}
   1.468 +	fprintf(stdout,"\nTests completed happily\n");
   1.469 +	to_return = 0;
   1.470 +end:
   1.471 +	if(to_return)
   1.472 +	{
   1.473 +		ERR_print_errors_fp(stderr);
   1.474 +		if(errno==ENOMEM)
   1.475 +   	{
   1.476 +	  return 1;
   1.477 +	  }
   1.478 +	}	
   1.479 +	if(new_h1)
   1.480 +	{ ENGINE_free(new_h1);
   1.481 +		if(errno==ENOMEM)
   1.482 +   	{
   1.483 +	  return 1;
   1.484 +	  }
   1.485 +	}  
   1.486 +	if(new_h2) 
   1.487 +	{
   1.488 +		ENGINE_free(new_h2);
   1.489 +		if(errno==ENOMEM)
   1.490 +   	{
   1.491 +	  return 1;
   1.492 +	  }
   1.493 +	}  
   1.494 +	if(new_h3)
   1.495 +	{ 
   1.496 +		ENGINE_free(new_h3);
   1.497 +		if(errno==ENOMEM)
   1.498 +   	{
   1.499 +	  return 1;
   1.500 +	  }
   1.501 +	}  
   1.502 +	if(new_h4)
   1.503 +	{
   1.504 +		 ENGINE_free(new_h4);
   1.505 +		 if(errno==ENOMEM)
   1.506 +   	{
   1.507 +	  return 1;
   1.508 +	  }
   1.509 +	}  
   1.510 +	for(loop = 0; loop < 512; loop++)
   1.511 +		if(block[loop])
   1.512 +		{
   1.513 +			ENGINE_free(block[loop]);
   1.514 +			if(errno==ENOMEM)
   1.515 +   	 {
   1.516 +	    return 1;
   1.517 +	   }
   1.518 +		}	
   1.519 +	ENGINE_cleanup();
   1.520 +	if(errno==ENOMEM)
   1.521 +  {
   1.522 +	  return 1;
   1.523 +	}
   1.524 +	CRYPTO_cleanup_all_ex_data();
   1.525 +	if(errno==ENOMEM)
   1.526 +  {
   1.527 +	  return 1;
   1.528 +	}
   1.529 +	ERR_free_strings();
   1.530 +	if(errno==ENOMEM)
   1.531 + 	{
   1.532 +	  return 1;
   1.533 +	}
   1.534 +	ERR_remove_state(0);
   1.535 +	if(errno==ENOMEM)
   1.536 +  {
   1.537 +  		  return 1;
   1.538 +	}
   1.539 +	CRYPTO_mem_leaks_fp(stderr);
   1.540 +	if(errno==ENOMEM)
   1.541 +  {
   1.542 +	  return 1;
   1.543 +	}
   1.544 +	return to_return;
   1.545 +	}
   1.546 +#endif