main_test.function 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #line 1 "main_test.function"
  2. SUITE_PRE_DEP
  3. #define TEST_SUITE_ACTIVE
  4. int verify_string( char **str )
  5. {
  6. if( (*str)[0] != '"' ||
  7. (*str)[strlen( *str ) - 1] != '"' )
  8. {
  9. mbedtls_fprintf( stderr,
  10. "Expected string (with \"\") for parameter and got: %s\n", *str );
  11. return( -1 );
  12. }
  13. (*str)++;
  14. (*str)[strlen( *str ) - 1] = '\0';
  15. return( 0 );
  16. }
  17. int verify_int( char *str, int *value )
  18. {
  19. size_t i;
  20. int minus = 0;
  21. int digits = 1;
  22. int hex = 0;
  23. for( i = 0; i < strlen( str ); i++ )
  24. {
  25. if( i == 0 && str[i] == '-' )
  26. {
  27. minus = 1;
  28. continue;
  29. }
  30. if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
  31. str[i - 1] == '0' && str[i] == 'x' )
  32. {
  33. hex = 1;
  34. continue;
  35. }
  36. if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
  37. ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
  38. ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
  39. {
  40. digits = 0;
  41. break;
  42. }
  43. }
  44. if( digits )
  45. {
  46. if( hex )
  47. *value = strtol( str, NULL, 16 );
  48. else
  49. *value = strtol( str, NULL, 10 );
  50. return( 0 );
  51. }
  52. MAPPING_CODE
  53. mbedtls_fprintf( stderr,
  54. "Expected integer for parameter and got: %s\n", str );
  55. return( KEY_VALUE_MAPPING_NOT_FOUND );
  56. }
  57. /*----------------------------------------------------------------------------*/
  58. /* Test Case code */
  59. FUNCTION_CODE
  60. SUITE_POST_DEP
  61. #line !LINE_NO! "main_test.function"
  62. /*----------------------------------------------------------------------------*/
  63. /* Test dispatch code */
  64. int dep_check( char *str )
  65. {
  66. if( str == NULL )
  67. return( 1 );
  68. DEP_CHECK_CODE
  69. #line !LINE_NO! "main_test.function"
  70. return( DEPENDENCY_NOT_SUPPORTED );
  71. }
  72. int dispatch_test(int cnt, char *params[50])
  73. {
  74. int ret;
  75. ((void) cnt);
  76. ((void) params);
  77. #if defined(TEST_SUITE_ACTIVE)
  78. ret = DISPATCH_TEST_SUCCESS;
  79. // Cast to void to avoid compiler warnings
  80. (void)ret;
  81. DISPATCH_FUNCTION
  82. {
  83. #line !LINE_NO! "main_test.function"
  84. mbedtls_fprintf( stdout,
  85. "FAILED\nSkipping unknown test function '%s'\n",
  86. params[0] );
  87. fflush( stdout );
  88. ret = DISPATCH_TEST_FN_NOT_FOUND;
  89. }
  90. #else
  91. ret = DISPATCH_UNSUPPORTED_SUITE;
  92. #endif
  93. return( ret );
  94. }
  95. /*----------------------------------------------------------------------------*/
  96. /* Main Test code */
  97. #line !LINE_NO! "main_test.function"
  98. #define USAGE \
  99. "Usage: %s [OPTIONS] files...\n\n" \
  100. " Command line arguments:\n" \
  101. " files... One or more test data file. If no file is specified\n" \
  102. " the followimg default test case is used:\n" \
  103. " %s\n\n" \
  104. " Options:\n" \
  105. " -v | --verbose Display full information about each test\n" \
  106. " -h | --help Display this information\n\n", \
  107. argv[0], \
  108. "TESTCASE_FILENAME"
  109. int get_line( FILE *f, char *buf, size_t len )
  110. {
  111. char *ret;
  112. ret = fgets( buf, len, f );
  113. if( ret == NULL )
  114. return( -1 );
  115. if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
  116. buf[strlen(buf) - 1] = '\0';
  117. if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
  118. buf[strlen(buf) - 1] = '\0';
  119. return( 0 );
  120. }
  121. int parse_arguments( char *buf, size_t len, char *params[50] )
  122. {
  123. int cnt = 0, i;
  124. char *cur = buf;
  125. char *p = buf, *q;
  126. params[cnt++] = cur;
  127. while( *p != '\0' && p < buf + len )
  128. {
  129. if( *p == '\\' )
  130. {
  131. p++;
  132. p++;
  133. continue;
  134. }
  135. if( *p == ':' )
  136. {
  137. if( p + 1 < buf + len )
  138. {
  139. cur = p + 1;
  140. params[cnt++] = cur;
  141. }
  142. *p = '\0';
  143. }
  144. p++;
  145. }
  146. /* Replace newlines, question marks and colons in strings */
  147. for( i = 0; i < cnt; i++ )
  148. {
  149. p = params[i];
  150. q = params[i];
  151. while( *p != '\0' )
  152. {
  153. if( *p == '\\' && *(p + 1) == 'n' )
  154. {
  155. p += 2;
  156. *(q++) = '\n';
  157. }
  158. else if( *p == '\\' && *(p + 1) == ':' )
  159. {
  160. p += 2;
  161. *(q++) = ':';
  162. }
  163. else if( *p == '\\' && *(p + 1) == '?' )
  164. {
  165. p += 2;
  166. *(q++) = '?';
  167. }
  168. else
  169. *(q++) = *(p++);
  170. }
  171. *q = '\0';
  172. }
  173. return( cnt );
  174. }
  175. static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
  176. {
  177. int ret;
  178. char buf[10] = "xxxxxxxxx";
  179. const char ref[10] = "xxxxxxxxx";
  180. ret = mbedtls_snprintf( buf, n, "%s", "123" );
  181. if( ret < 0 || (size_t) ret >= n )
  182. ret = -1;
  183. if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
  184. ref_ret != ret ||
  185. memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
  186. {
  187. return( 1 );
  188. }
  189. return( 0 );
  190. }
  191. static int run_test_snprintf( void )
  192. {
  193. return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
  194. test_snprintf( 1, "", -1 ) != 0 ||
  195. test_snprintf( 2, "1", -1 ) != 0 ||
  196. test_snprintf( 3, "12", -1 ) != 0 ||
  197. test_snprintf( 4, "123", 3 ) != 0 ||
  198. test_snprintf( 5, "123", 3 ) != 0 );
  199. }
  200. int main(int argc, const char *argv[])
  201. {
  202. /* Local Configurations and options */
  203. const char *default_filename = "TESTCASE_FILENAME";
  204. const char *test_filename = NULL;
  205. const char **test_files = NULL;
  206. int testfile_count = 0;
  207. int option_verbose = 0;
  208. /* Other Local variables */
  209. int arg_index = 1;
  210. const char *next_arg;
  211. int testfile_index, ret, i, cnt;
  212. int total_errors = 0, total_tests = 0, total_skipped = 0;
  213. FILE *file;
  214. char buf[5000];
  215. char *params[50];
  216. void *pointer;
  217. int stdout_fd = -1;
  218. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
  219. !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
  220. unsigned char alloc_buf[1000000];
  221. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  222. #endif
  223. /*
  224. * The C standard doesn't guarantee that all-bits-0 is the representation
  225. * of a NULL pointer. We do however use that in our code for initializing
  226. * structures, which should work on every modern platform. Let's be sure.
  227. */
  228. memset( &pointer, 0, sizeof( void * ) );
  229. if( pointer != NULL )
  230. {
  231. mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
  232. return( 1 );
  233. }
  234. /*
  235. * Make sure we have a snprintf that correctly zero-terminates
  236. */
  237. if( run_test_snprintf() != 0 )
  238. {
  239. mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
  240. return( 0 );
  241. }
  242. while( arg_index < argc)
  243. {
  244. next_arg = argv[ arg_index ];
  245. if( strcmp(next_arg, "--verbose" ) == 0 ||
  246. strcmp(next_arg, "-v" ) == 0 )
  247. {
  248. option_verbose = 1;
  249. }
  250. else if( strcmp(next_arg, "--help" ) == 0 ||
  251. strcmp(next_arg, "-h" ) == 0 )
  252. {
  253. mbedtls_fprintf( stdout, USAGE );
  254. mbedtls_exit( EXIT_SUCCESS );
  255. }
  256. else
  257. {
  258. /* Not an option, therefore treat all further arguments as the file
  259. * list.
  260. */
  261. test_files = &argv[ arg_index ];
  262. testfile_count = argc - arg_index;
  263. }
  264. arg_index++;
  265. }
  266. /* If no files were specified, assume a default */
  267. if ( test_files == NULL || testfile_count == 0 )
  268. {
  269. test_files = &default_filename;
  270. testfile_count = 1;
  271. }
  272. /* Now begin to execute the tests in the testfiles */
  273. for ( testfile_index = 0;
  274. testfile_index < testfile_count;
  275. testfile_index++ )
  276. {
  277. int unmet_dep_count = 0;
  278. char *unmet_dependencies[20];
  279. test_filename = test_files[ testfile_index ];
  280. file = fopen( test_filename, "r" );
  281. if( file == NULL )
  282. {
  283. mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
  284. test_filename );
  285. return( 1 );
  286. }
  287. while( !feof( file ) )
  288. {
  289. if( unmet_dep_count > 0 )
  290. {
  291. mbedtls_fprintf( stderr,
  292. "FATAL: Dep count larger than zero at start of loop\n" );
  293. mbedtls_exit( MBEDTLS_EXIT_FAILURE );
  294. }
  295. unmet_dep_count = 0;
  296. if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
  297. break;
  298. mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
  299. mbedtls_fprintf( stdout, " " );
  300. for( i = strlen( buf ) + 1; i < 67; i++ )
  301. mbedtls_fprintf( stdout, "." );
  302. mbedtls_fprintf( stdout, " " );
  303. fflush( stdout );
  304. total_tests++;
  305. if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
  306. break;
  307. cnt = parse_arguments( buf, strlen(buf), params );
  308. if( strcmp( params[0], "depends_on" ) == 0 )
  309. {
  310. for( i = 1; i < cnt; i++ )
  311. {
  312. if( dep_check( params[i] ) != DEPENDENCY_SUPPORTED )
  313. {
  314. if( 0 == option_verbose )
  315. {
  316. /* Only one count is needed if not verbose */
  317. unmet_dep_count++;
  318. break;
  319. }
  320. unmet_dependencies[ unmet_dep_count ] = strdup(params[i]);
  321. if( unmet_dependencies[ unmet_dep_count ] == NULL )
  322. {
  323. mbedtls_fprintf( stderr, "FATAL: Out of memory\n" );
  324. mbedtls_exit( MBEDTLS_EXIT_FAILURE );
  325. }
  326. unmet_dep_count++;
  327. }
  328. }
  329. if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
  330. break;
  331. cnt = parse_arguments( buf, strlen(buf), params );
  332. }
  333. // If there are no unmet dependencies execute the test
  334. if( unmet_dep_count == 0 )
  335. {
  336. test_errors = 0;
  337. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  338. /* Suppress all output from the library unless we're verbose
  339. * mode
  340. */
  341. if( !option_verbose )
  342. {
  343. stdout_fd = redirect_output( &stdout, "/dev/null" );
  344. if( stdout_fd == -1 )
  345. {
  346. /* Redirection has failed with no stdout so exit */
  347. exit( 1 );
  348. }
  349. }
  350. #endif /* __unix__ || __APPLE__ __MACH__ */
  351. ret = dispatch_test( cnt, params );
  352. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  353. if( !option_verbose && restore_output( &stdout, stdout_fd ) )
  354. {
  355. /* Redirection has failed with no stdout so exit */
  356. exit( 1 );
  357. }
  358. #endif /* __unix__ || __APPLE__ __MACH__ */
  359. }
  360. if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
  361. {
  362. total_skipped++;
  363. mbedtls_fprintf( stdout, "----\n" );
  364. if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
  365. {
  366. mbedtls_fprintf( stdout, " Test Suite not enabled" );
  367. }
  368. if( 1 == option_verbose && unmet_dep_count > 0 )
  369. {
  370. mbedtls_fprintf( stdout, " Unmet dependencies: " );
  371. for( i = 0; i < unmet_dep_count; i++ )
  372. {
  373. mbedtls_fprintf(stdout, "%s ",
  374. unmet_dependencies[i]);
  375. free(unmet_dependencies[i]);
  376. }
  377. mbedtls_fprintf( stdout, "\n" );
  378. }
  379. fflush( stdout );
  380. unmet_dep_count = 0;
  381. }
  382. else if( ret == DISPATCH_TEST_SUCCESS && test_errors == 0 )
  383. {
  384. mbedtls_fprintf( stdout, "PASS\n" );
  385. fflush( stdout );
  386. }
  387. else if( ret == DISPATCH_INVALID_TEST_DATA )
  388. {
  389. mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
  390. fclose(file);
  391. mbedtls_exit( 2 );
  392. }
  393. else
  394. total_errors++;
  395. if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
  396. break;
  397. if( strlen(buf) != 0 )
  398. {
  399. mbedtls_fprintf( stderr, "Should be empty %d\n",
  400. (int) strlen(buf) );
  401. return( 1 );
  402. }
  403. }
  404. fclose(file);
  405. /* In case we encounter early end of file */
  406. for( i = 0; i < unmet_dep_count; i++ )
  407. free( unmet_dependencies[i] );
  408. }
  409. mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
  410. if( total_errors == 0 )
  411. mbedtls_fprintf( stdout, "PASSED" );
  412. else
  413. mbedtls_fprintf( stdout, "FAILED" );
  414. mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
  415. total_tests - total_errors, total_tests, total_skipped );
  416. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
  417. !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
  418. #if defined(MBEDTLS_MEMORY_DEBUG)
  419. mbedtls_memory_buffer_alloc_status();
  420. #endif
  421. mbedtls_memory_buffer_alloc_free();
  422. #endif
  423. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  424. if( stdout_fd != -1 )
  425. close_output( stdout );
  426. #endif /* __unix__ || __APPLE__ __MACH__ */
  427. return( total_errors != 0 );
  428. }