helpers.function 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #line 2 "suites/helpers.function"
  2. /*----------------------------------------------------------------------------*/
  3. /* Headers */
  4. #include <test/macros.h>
  5. #include <test/helpers.h>
  6. #include <test/random.h>
  7. #include <test/psa_crypto_helpers.h>
  8. #include <stdlib.h>
  9. #if defined(MBEDTLS_PLATFORM_C)
  10. #include "mbedtls/platform.h"
  11. #else
  12. #include <stdio.h>
  13. #define mbedtls_fprintf fprintf
  14. #define mbedtls_snprintf snprintf
  15. #define mbedtls_calloc calloc
  16. #define mbedtls_free free
  17. #define mbedtls_exit exit
  18. #define mbedtls_time time
  19. #define mbedtls_time_t time_t
  20. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  21. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  22. #endif
  23. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  24. #include "mbedtls/memory_buffer_alloc.h"
  25. #endif
  26. #if defined(MBEDTLS_CHECK_PARAMS)
  27. #include "mbedtls/platform_util.h"
  28. #include <setjmp.h>
  29. #endif
  30. #ifdef _MSC_VER
  31. #include <basetsd.h>
  32. typedef UINT8 uint8_t;
  33. typedef INT32 int32_t;
  34. typedef UINT32 uint32_t;
  35. #define strncasecmp _strnicmp
  36. #define strcasecmp _stricmp
  37. #else
  38. #include <stdint.h>
  39. #endif
  40. #include <string.h>
  41. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  42. #include <unistd.h>
  43. #include <strings.h>
  44. #endif
  45. /* Type for Hex parameters */
  46. typedef struct data_tag
  47. {
  48. uint8_t * x;
  49. uint32_t len;
  50. } data_t;
  51. /*----------------------------------------------------------------------------*/
  52. /* Status and error constants */
  53. #define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
  54. #define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
  55. #define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
  56. #define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
  57. #define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
  58. #define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
  59. #define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
  60. Only int, string, binary data
  61. and integer expressions are
  62. allowed */
  63. #define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
  64. build */
  65. /*----------------------------------------------------------------------------*/
  66. /* Global variables */
  67. #if defined(MBEDTLS_CHECK_PARAMS)
  68. jmp_buf jmp_tmp;
  69. #endif
  70. /*----------------------------------------------------------------------------*/
  71. /* Helper flags for complex dependencies */
  72. /* Indicates whether we expect mbedtls_entropy_init
  73. * to initialize some strong entropy source. */
  74. #if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
  75. ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
  76. ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
  77. defined(MBEDTLS_HAVEGE_C) || \
  78. defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
  79. defined(ENTROPY_NV_SEED) ) )
  80. #define ENTROPY_HAVE_STRONG
  81. #endif
  82. /*----------------------------------------------------------------------------*/
  83. /* Helper Functions */
  84. #if defined(MBEDTLS_PSA_CRYPTO_C)
  85. /** Check that no PSA Crypto key slots are in use.
  86. *
  87. * If any slots are in use, mark the current test as failed.
  88. *
  89. * \return 0 if the key store is empty, 1 otherwise.
  90. */
  91. int test_fail_if_psa_leaking( int line_no, const char *filename )
  92. {
  93. const char *msg = mbedtls_test_helper_is_psa_leaking( );
  94. if( msg == NULL )
  95. return 0;
  96. else
  97. {
  98. mbedtls_test_fail( msg, line_no, filename );
  99. return 1;
  100. }
  101. }
  102. #endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
  103. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  104. static int redirect_output( FILE* out_stream, const char* path )
  105. {
  106. int out_fd, dup_fd;
  107. FILE* path_stream;
  108. out_fd = fileno( out_stream );
  109. dup_fd = dup( out_fd );
  110. if( dup_fd == -1 )
  111. {
  112. return( -1 );
  113. }
  114. path_stream = fopen( path, "w" );
  115. if( path_stream == NULL )
  116. {
  117. close( dup_fd );
  118. return( -1 );
  119. }
  120. fflush( out_stream );
  121. if( dup2( fileno( path_stream ), out_fd ) == -1 )
  122. {
  123. close( dup_fd );
  124. fclose( path_stream );
  125. return( -1 );
  126. }
  127. fclose( path_stream );
  128. return( dup_fd );
  129. }
  130. static int restore_output( FILE* out_stream, int dup_fd )
  131. {
  132. int out_fd = fileno( out_stream );
  133. fflush( out_stream );
  134. if( dup2( dup_fd, out_fd ) == -1 )
  135. {
  136. close( out_fd );
  137. close( dup_fd );
  138. return( -1 );
  139. }
  140. close( dup_fd );
  141. return( 0 );
  142. }
  143. #endif /* __unix__ || __APPLE__ __MACH__ */