00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_BIGNUM_C
00004 #ifdef POLARSSL_FS_IO
00005 #ifdef POLARSSL_PK_PARSE_C
00006
00007 #include <polarssl/x509_crt.h>
00008 #include <polarssl/x509_csr.h>
00009 #include <polarssl/pem.h>
00010 #include <polarssl/oid.h>
00011 #endif
00012 #endif
00013 #endif
00014
00015
00016 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00017 #include "polarssl/memory.h"
00018 #endif
00019
00020 #if defined(WANT_NOT_RND_MPI)
00021 #if defined(POLARSSL_BIGNUM_C)
00022 #include "polarssl/bignum.h"
00023 #else
00024 #error "not_rnd_mpi() need bignum.c"
00025 #endif
00026 #endif
00027
00028 #ifdef _MSC_VER
00029 #include <basetsd.h>
00030 typedef UINT32 uint32_t;
00031 #else
00032 #include <inttypes.h>
00033 #endif
00034
00035 #include <assert.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038
00039
00040
00041
00042 #ifndef GET_UINT32_BE
00043 #define GET_UINT32_BE(n,b,i) \
00044 { \
00045 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00046 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00047 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00048 | ( (uint32_t) (b)[(i) + 3] ); \
00049 }
00050 #endif
00051
00052 #ifndef PUT_UINT32_BE
00053 #define PUT_UINT32_BE(n,b,i) \
00054 { \
00055 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00056 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00057 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00058 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00059 }
00060 #endif
00061
00062 static int unhexify(unsigned char *obuf, const char *ibuf)
00063 {
00064 unsigned char c, c2;
00065 int len = strlen(ibuf) / 2;
00066 assert(!(strlen(ibuf) %1));
00067
00068 while (*ibuf != 0)
00069 {
00070 c = *ibuf++;
00071 if( c >= '0' && c <= '9' )
00072 c -= '0';
00073 else if( c >= 'a' && c <= 'f' )
00074 c -= 'a' - 10;
00075 else if( c >= 'A' && c <= 'F' )
00076 c -= 'A' - 10;
00077 else
00078 assert( 0 );
00079
00080 c2 = *ibuf++;
00081 if( c2 >= '0' && c2 <= '9' )
00082 c2 -= '0';
00083 else if( c2 >= 'a' && c2 <= 'f' )
00084 c2 -= 'a' - 10;
00085 else if( c2 >= 'A' && c2 <= 'F' )
00086 c2 -= 'A' - 10;
00087 else
00088 assert( 0 );
00089
00090 *obuf++ = ( c << 4 ) | c2;
00091 }
00092
00093 return len;
00094 }
00095
00096 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00097 {
00098 unsigned char l, h;
00099
00100 while (len != 0)
00101 {
00102 h = (*ibuf) / 16;
00103 l = (*ibuf) % 16;
00104
00105 if( h < 10 )
00106 *obuf++ = '0' + h;
00107 else
00108 *obuf++ = 'a' + h - 10;
00109
00110 if( l < 10 )
00111 *obuf++ = '0' + l;
00112 else
00113 *obuf++ = 'a' + l - 10;
00114
00115 ++ibuf;
00116 len--;
00117 }
00118 }
00119
00129 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00130 {
00131 size_t i;
00132
00133 if( rng_state != NULL )
00134 rng_state = NULL;
00135
00136 for( i = 0; i < len; ++i )
00137 output[i] = rand();
00138
00139 return( 0 );
00140 }
00141
00147 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00148 {
00149 if( rng_state != NULL )
00150 rng_state = NULL;
00151
00152 memset( output, 0, len );
00153
00154 return( 0 );
00155 }
00156
00157 typedef struct
00158 {
00159 unsigned char *buf;
00160 size_t length;
00161 } rnd_buf_info;
00162
00174 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00175 {
00176 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00177 size_t use_len;
00178
00179 if( rng_state == NULL )
00180 return( rnd_std_rand( NULL, output, len ) );
00181
00182 use_len = len;
00183 if( len > info->length )
00184 use_len = info->length;
00185
00186 if( use_len )
00187 {
00188 memcpy( output, info->buf, use_len );
00189 info->buf += use_len;
00190 info->length -= use_len;
00191 }
00192
00193 if( len - use_len > 0 )
00194 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00195
00196 return( 0 );
00197 }
00198
00206 typedef struct
00207 {
00208 uint32_t key[16];
00209 uint32_t v0, v1;
00210 } rnd_pseudo_info;
00211
00220 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00221 {
00222 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00223 uint32_t i, *k, sum, delta=0x9E3779B9;
00224 unsigned char result[4];
00225
00226 if( rng_state == NULL )
00227 return( rnd_std_rand( NULL, output, len ) );
00228
00229 k = info->key;
00230
00231 while( len > 0 )
00232 {
00233 size_t use_len = ( len > 4 ) ? 4 : len;
00234 sum = 0;
00235
00236 for( i = 0; i < 32; i++ )
00237 {
00238 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00239 sum += delta;
00240 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00241 }
00242
00243 PUT_UINT32_BE( info->v0, result, 0 );
00244 memcpy( output, result, use_len );
00245 len -= use_len;
00246 }
00247
00248 return( 0 );
00249 }
00250
00251 #if defined(WANT_NOT_RND_MPI)
00252
00260 #define ciL (sizeof(t_uint))
00261 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00262 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00263 {
00264 char *str = (char *) in;
00265 mpi X;
00266
00267
00268
00269
00270
00271 X.s = 1;
00272 X.p = (t_uint *) out;
00273 X.n = CHARS_TO_LIMBS( len );
00274
00275
00276
00277
00278
00279 assert( strlen( str ) / 2 == len );
00280
00281 return( mpi_read_string( &X, 16, str ) );
00282 }
00283 #endif
00284
00285
00286 #include <stdio.h>
00287 #include <string.h>
00288
00289 static int test_errors = 0;
00290
00291 #ifdef POLARSSL_BIGNUM_C
00292 #ifdef POLARSSL_FS_IO
00293 #ifdef POLARSSL_PK_PARSE_C
00294
00295 #define TEST_SUITE_ACTIVE
00296
00297 static int test_assert( int correct, char *test )
00298 {
00299 if( correct )
00300 return( 0 );
00301
00302 test_errors++;
00303 if( test_errors == 1 )
00304 printf( "FAILED\n" );
00305 printf( " %s\n", test );
00306
00307 return( 1 );
00308 }
00309
00310 #define TEST_ASSERT( TEST ) \
00311 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00312 if( test_errors) return; \
00313 } while (0)
00314
00315 int verify_string( char **str )
00316 {
00317 if( (*str)[0] != '"' ||
00318 (*str)[strlen( *str ) - 1] != '"' )
00319 {
00320 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00321 return( -1 );
00322 }
00323
00324 (*str)++;
00325 (*str)[strlen( *str ) - 1] = '\0';
00326
00327 return( 0 );
00328 }
00329
00330 int verify_int( char *str, int *value )
00331 {
00332 size_t i;
00333 int minus = 0;
00334 int digits = 1;
00335 int hex = 0;
00336
00337 for( i = 0; i < strlen( str ); i++ )
00338 {
00339 if( i == 0 && str[i] == '-' )
00340 {
00341 minus = 1;
00342 continue;
00343 }
00344
00345 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00346 str[i - 1] == '0' && str[i] == 'x' )
00347 {
00348 hex = 1;
00349 continue;
00350 }
00351
00352 if( str[i] < '0' || str[i] > '9' )
00353 {
00354 digits = 0;
00355 break;
00356 }
00357 }
00358
00359 if( digits )
00360 {
00361 if( hex )
00362 *value = strtol( str, NULL, 16 );
00363 else
00364 *value = strtol( str, NULL, 10 );
00365
00366 return( 0 );
00367 }
00368
00369 #ifdef POLARSSL_PEM_WRITE_C
00370 #ifdef POLARSSL_X509_CSR_WRITE_C
00371 if( strcmp( str, "POLARSSL_MD_MD4" ) == 0 )
00372 {
00373 *value = ( POLARSSL_MD_MD4 );
00374 return( 0 );
00375 }
00376 #endif // POLARSSL_PEM_WRITE_C
00377 #endif // POLARSSL_X509_CSR_WRITE_C
00378 #ifdef POLARSSL_PEM_WRITE_C
00379 #ifdef POLARSSL_X509_CSR_WRITE_C
00380 if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
00381 {
00382 *value = ( POLARSSL_MD_SHA512 );
00383 return( 0 );
00384 }
00385 #endif // POLARSSL_PEM_WRITE_C
00386 #endif // POLARSSL_X509_CSR_WRITE_C
00387 #ifdef POLARSSL_PEM_WRITE_C
00388 #ifdef POLARSSL_X509_CSR_WRITE_C
00389 if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
00390 {
00391 *value = ( POLARSSL_MD_SHA256 );
00392 return( 0 );
00393 }
00394 #endif // POLARSSL_PEM_WRITE_C
00395 #endif // POLARSSL_X509_CSR_WRITE_C
00396 #ifdef POLARSSL_PEM_WRITE_C
00397 #ifdef POLARSSL_X509_CRT_WRITE_C
00398 #ifdef POLARSSL_SHA1_C
00399 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
00400 {
00401 *value = ( POLARSSL_MD_SHA1 );
00402 return( 0 );
00403 }
00404 #endif // POLARSSL_PEM_WRITE_C
00405 #endif // POLARSSL_X509_CRT_WRITE_C
00406 #endif // POLARSSL_SHA1_C
00407 #ifdef POLARSSL_PEM_WRITE_C
00408 #ifdef POLARSSL_X509_CSR_WRITE_C
00409 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
00410 {
00411 *value = ( POLARSSL_MD_SHA1 );
00412 return( 0 );
00413 }
00414 #endif // POLARSSL_PEM_WRITE_C
00415 #endif // POLARSSL_X509_CSR_WRITE_C
00416 #ifdef POLARSSL_PEM_WRITE_C
00417 #ifdef POLARSSL_X509_CSR_WRITE_C
00418 if( strcmp( str, "POLARSSL_MD_SHA224" ) == 0 )
00419 {
00420 *value = ( POLARSSL_MD_SHA224 );
00421 return( 0 );
00422 }
00423 #endif // POLARSSL_PEM_WRITE_C
00424 #endif // POLARSSL_X509_CSR_WRITE_C
00425 #ifdef POLARSSL_PEM_WRITE_C
00426 #ifdef POLARSSL_X509_CSR_WRITE_C
00427 if( strcmp( str, "POLARSSL_MD_SHA384" ) == 0 )
00428 {
00429 *value = ( POLARSSL_MD_SHA384 );
00430 return( 0 );
00431 }
00432 #endif // POLARSSL_PEM_WRITE_C
00433 #endif // POLARSSL_X509_CSR_WRITE_C
00434 #ifdef POLARSSL_PEM_WRITE_C
00435 #ifdef POLARSSL_X509_CSR_WRITE_C
00436 if( strcmp( str, "POLARSSL_MD_MD5" ) == 0 )
00437 {
00438 *value = ( POLARSSL_MD_MD5 );
00439 return( 0 );
00440 }
00441 #endif // POLARSSL_PEM_WRITE_C
00442 #endif // POLARSSL_X509_CSR_WRITE_C
00443
00444
00445 printf( "Expected integer for parameter and got: %s\n", str );
00446 return( -1 );
00447 }
00448
00449 #ifdef POLARSSL_PEM_WRITE_C
00450 #ifdef POLARSSL_X509_CSR_WRITE_C
00451 void test_suite_x509_csr_check( char *key_file, int md_type,
00452 char *cert_req_check_file )
00453 {
00454 pk_context key;
00455 x509write_csr req;
00456 unsigned char buf[4000];
00457 unsigned char check_buf[4000];
00458 int ret;
00459 size_t olen = 0, pem_len = 0;
00460 FILE *f;
00461 char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
00462 rnd_pseudo_info rnd_info;
00463
00464 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
00465
00466 pk_init( &key );
00467 TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
00468
00469 x509write_csr_init( &req );
00470 x509write_csr_set_md_alg( &req, md_type );
00471 x509write_csr_set_key( &req, &key );
00472 TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
00473
00474 ret = x509write_csr_pem( &req, buf, sizeof(buf),
00475 rnd_pseudo_rand, &rnd_info );
00476 TEST_ASSERT( ret == 0 );
00477
00478 pem_len = strlen( (char *) buf );
00479
00480 f = fopen( cert_req_check_file, "r" );
00481 TEST_ASSERT( f != NULL );
00482 olen = fread( check_buf, 1, sizeof( check_buf ), f );
00483 fclose( f );
00484
00485 TEST_ASSERT( olen >= pem_len - 1 );
00486 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
00487
00488 x509write_csr_free( &req );
00489 pk_free( &key );
00490 }
00491 #endif
00492 #endif
00493
00494 #ifdef POLARSSL_PEM_WRITE_C
00495 #ifdef POLARSSL_X509_CRT_WRITE_C
00496 #ifdef POLARSSL_SHA1_C
00497 void test_suite_x509_crt_check( char *subject_key_file, char *subject_pwd,
00498 char *subject_name, char *issuer_key_file,
00499 char *issuer_pwd, char *issuer_name,
00500 char *serial_str, char *not_before, char *not_after,
00501 int md_type, char *cert_check_file )
00502 {
00503 pk_context subject_key, issuer_key;
00504 x509write_cert crt;
00505 unsigned char buf[4000];
00506 unsigned char check_buf[5000];
00507 mpi serial;
00508 int ret;
00509 size_t olen = 0, pem_len = 0;
00510 FILE *f;
00511 rnd_pseudo_info rnd_info;
00512
00513 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
00514 mpi_init( &serial );
00515 pk_init( &subject_key );
00516 pk_init( &issuer_key );
00517
00518 TEST_ASSERT( pk_parse_keyfile( &subject_key, subject_key_file,
00519 subject_pwd ) == 0 );
00520 TEST_ASSERT( pk_parse_keyfile( &issuer_key, issuer_key_file,
00521 issuer_pwd ) == 0 );
00522 TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
00523
00524 x509write_crt_init( &crt );
00525 x509write_crt_set_serial( &crt, &serial );
00526 TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
00527 not_after ) == 0 );
00528 x509write_crt_set_md_alg( &crt, md_type );
00529 TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
00530 TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
00531 x509write_crt_set_subject_key( &crt, &subject_key );
00532 x509write_crt_set_issuer_key( &crt, &issuer_key );
00533
00534 TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
00535 TEST_ASSERT( x509write_crt_set_subject_key_identifier( &crt ) == 0 );
00536 TEST_ASSERT( x509write_crt_set_authority_key_identifier( &crt ) == 0 );
00537
00538 ret = x509write_crt_pem( &crt, buf, sizeof(buf),
00539 rnd_pseudo_rand, &rnd_info );
00540 TEST_ASSERT( ret == 0 );
00541
00542 pem_len = strlen( (char *) buf );
00543
00544 f = fopen( cert_check_file, "r" );
00545 TEST_ASSERT( f != NULL );
00546 TEST_ASSERT( ( olen = fread( check_buf, 1, sizeof(check_buf), f ) ) <
00547 sizeof(check_buf) );
00548 fclose( f );
00549
00550 TEST_ASSERT( olen >= pem_len - 1 );
00551 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
00552
00553 x509write_crt_free( &crt );
00554 pk_free( &issuer_key );
00555 pk_free( &subject_key );
00556 mpi_free( &serial );
00557 }
00558 #endif
00559 #endif
00560 #endif
00561
00562
00563 #endif
00564 #endif
00565 #endif
00566
00567
00568 int dep_check( char *str )
00569 {
00570 if( str == NULL )
00571 return( 1 );
00572
00573 if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
00574 {
00575 #if defined(POLARSSL_MD4_C)
00576 return( 0 );
00577 #else
00578 return( 1 );
00579 #endif
00580 }
00581 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
00582 {
00583 #if defined(POLARSSL_SHA1_C)
00584 return( 0 );
00585 #else
00586 return( 1 );
00587 #endif
00588 }
00589 if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
00590 {
00591 #if defined(POLARSSL_PKCS1_V15)
00592 return( 0 );
00593 #else
00594 return( 1 );
00595 #endif
00596 }
00597 if( strcmp( str, "POLARSSL_DES_C" ) == 0 )
00598 {
00599 #if defined(POLARSSL_DES_C)
00600 return( 0 );
00601 #else
00602 return( 1 );
00603 #endif
00604 }
00605 if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
00606 {
00607 #if defined(POLARSSL_CIPHER_MODE_CBC)
00608 return( 0 );
00609 #else
00610 return( 1 );
00611 #endif
00612 }
00613 if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
00614 {
00615 #if defined(POLARSSL_MD5_C)
00616 return( 0 );
00617 #else
00618 return( 1 );
00619 #endif
00620 }
00621 if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
00622 {
00623 #if defined(POLARSSL_SHA256_C)
00624 return( 0 );
00625 #else
00626 return( 1 );
00627 #endif
00628 }
00629 if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
00630 {
00631 #if defined(POLARSSL_RSA_C)
00632 return( 0 );
00633 #else
00634 return( 1 );
00635 #endif
00636 }
00637 if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
00638 {
00639 #if defined(POLARSSL_SHA512_C)
00640 return( 0 );
00641 #else
00642 return( 1 );
00643 #endif
00644 }
00645
00646
00647 return( 1 );
00648 }
00649
00650 int dispatch_test(int cnt, char *params[50])
00651 {
00652 int ret;
00653 ((void) cnt);
00654 ((void) params);
00655
00656 #if defined(TEST_SUITE_ACTIVE)
00657 if( strcmp( params[0], "x509_csr_check" ) == 0 )
00658 {
00659 #ifdef POLARSSL_PEM_WRITE_C
00660 #ifdef POLARSSL_X509_CSR_WRITE_C
00661
00662 char *param1 = params[1];
00663 int param2;
00664 char *param3 = params[3];
00665
00666 if( cnt != 4 )
00667 {
00668 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
00669 return( 2 );
00670 }
00671
00672 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00673 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00674 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00675
00676 test_suite_x509_csr_check( param1, param2, param3 );
00677 return ( 0 );
00678 #endif
00679 #endif
00680
00681 return ( 3 );
00682 }
00683 else
00684 if( strcmp( params[0], "x509_crt_check" ) == 0 )
00685 {
00686 #ifdef POLARSSL_PEM_WRITE_C
00687 #ifdef POLARSSL_X509_CRT_WRITE_C
00688 #ifdef POLARSSL_SHA1_C
00689
00690 char *param1 = params[1];
00691 char *param2 = params[2];
00692 char *param3 = params[3];
00693 char *param4 = params[4];
00694 char *param5 = params[5];
00695 char *param6 = params[6];
00696 char *param7 = params[7];
00697 char *param8 = params[8];
00698 char *param9 = params[9];
00699 int param10;
00700 char *param11 = params[11];
00701
00702 if( cnt != 12 )
00703 {
00704 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 12 );
00705 return( 2 );
00706 }
00707
00708 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00709 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00710 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00711 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00712 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00713 if( verify_string( ¶m6 ) != 0 ) return( 2 );
00714 if( verify_string( ¶m7 ) != 0 ) return( 2 );
00715 if( verify_string( ¶m8 ) != 0 ) return( 2 );
00716 if( verify_string( ¶m9 ) != 0 ) return( 2 );
00717 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
00718 if( verify_string( ¶m11 ) != 0 ) return( 2 );
00719
00720 test_suite_x509_crt_check( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
00721 return ( 0 );
00722 #endif
00723 #endif
00724 #endif
00725
00726 return ( 3 );
00727 }
00728 else
00729
00730 {
00731 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00732 fflush( stdout );
00733 return( 1 );
00734 }
00735 #else
00736 return( 3 );
00737 #endif
00738 return( ret );
00739 }
00740
00741 int get_line( FILE *f, char *buf, size_t len )
00742 {
00743 char *ret;
00744
00745 ret = fgets( buf, len, f );
00746 if( ret == NULL )
00747 return( -1 );
00748
00749 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00750 buf[strlen(buf) - 1] = '\0';
00751 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00752 buf[strlen(buf) - 1] = '\0';
00753
00754 return( 0 );
00755 }
00756
00757 int parse_arguments( char *buf, size_t len, char *params[50] )
00758 {
00759 int cnt = 0, i;
00760 char *cur = buf;
00761 char *p = buf, *q;
00762
00763 params[cnt++] = cur;
00764
00765 while( *p != '\0' && p < buf + len )
00766 {
00767 if( *p == '\\' )
00768 {
00769 *p++;
00770 *p++;
00771 continue;
00772 }
00773 if( *p == ':' )
00774 {
00775 if( p + 1 < buf + len )
00776 {
00777 cur = p + 1;
00778 params[cnt++] = cur;
00779 }
00780 *p = '\0';
00781 }
00782
00783 *p++;
00784 }
00785
00786
00787 for( i = 0; i < cnt; i++ )
00788 {
00789 p = params[i];
00790 q = params[i];
00791
00792 while( *p != '\0' )
00793 {
00794 if( *p == '\\' && *(p + 1) == 'n' )
00795 {
00796 p += 2;
00797 *(q++) = '\n';
00798 }
00799 else if( *p == '\\' && *(p + 1) == ':' )
00800 {
00801 p += 2;
00802 *(q++) = ':';
00803 }
00804 else if( *p == '\\' && *(p + 1) == '?' )
00805 {
00806 p += 2;
00807 *(q++) = '?';
00808 }
00809 else
00810 *(q++) = *(p++);
00811 }
00812 *q = '\0';
00813 }
00814
00815 return( cnt );
00816 }
00817
00818 int main()
00819 {
00820 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
00821 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_x509write.data";
00822 FILE *file;
00823 char buf[5000];
00824 char *params[50];
00825
00826 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00827 unsigned char alloc_buf[1000000];
00828 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
00829 #endif
00830
00831 file = fopen( filename, "r" );
00832 if( file == NULL )
00833 {
00834 fprintf( stderr, "Failed to open\n" );
00835 return( 1 );
00836 }
00837
00838 while( !feof( file ) )
00839 {
00840 int skip = 0;
00841
00842 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00843 break;
00844 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
00845 fprintf( stdout, " " );
00846 for( i = strlen( buf ) + 1; i < 67; i++ )
00847 fprintf( stdout, "." );
00848 fprintf( stdout, " " );
00849 fflush( stdout );
00850
00851 total_tests++;
00852
00853 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00854 break;
00855 cnt = parse_arguments( buf, strlen(buf), params );
00856
00857 if( strcmp( params[0], "depends_on" ) == 0 )
00858 {
00859 for( i = 1; i < cnt; i++ )
00860 if( dep_check( params[i] ) != 0 )
00861 skip = 1;
00862
00863 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00864 break;
00865 cnt = parse_arguments( buf, strlen(buf), params );
00866 }
00867
00868 if( skip == 0 )
00869 {
00870 test_errors = 0;
00871 ret = dispatch_test( cnt, params );
00872 }
00873
00874 if( skip == 1 || ret == 3 )
00875 {
00876 total_skipped++;
00877 fprintf( stdout, "----\n" );
00878 fflush( stdout );
00879 }
00880 else if( ret == 0 && test_errors == 0 )
00881 {
00882 fprintf( stdout, "PASS\n" );
00883 fflush( stdout );
00884 }
00885 else if( ret == 2 )
00886 {
00887 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
00888 fclose(file);
00889 exit( 2 );
00890 }
00891 else
00892 total_errors++;
00893
00894 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00895 break;
00896 if( strlen(buf) != 0 )
00897 {
00898 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
00899 return( 1 );
00900 }
00901 }
00902 fclose(file);
00903
00904 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
00905 if( total_errors == 0 )
00906 fprintf( stdout, "PASSED" );
00907 else
00908 fprintf( stdout, "FAILED" );
00909
00910 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
00911 total_tests - total_errors, total_tests, total_skipped );
00912
00913 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00914 #if defined(POLARSSL_MEMORY_DEBUG)
00915 memory_buffer_alloc_status();
00916 #endif
00917 memory_buffer_alloc_free();
00918 #endif
00919
00920 return( total_errors != 0 );
00921 }
00922
00923