helpers.function 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #line 2 "suites/helpers.function"
  2. /*----------------------------------------------------------------------------*/
  3. /* Headers */
  4. #include <test/helpers.h>
  5. #include <test/macros.h>
  6. #include <test/random.h>
  7. #include <test/bignum_helpers.h>
  8. #include <test/psa_crypto_helpers.h>
  9. #include <stdlib.h>
  10. #if defined(MBEDTLS_ERROR_C)
  11. #include "mbedtls/error.h"
  12. #endif
  13. #include "mbedtls/platform.h"
  14. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  15. #include "mbedtls/memory_buffer_alloc.h"
  16. #endif
  17. #ifdef _MSC_VER
  18. #include <basetsd.h>
  19. typedef UINT8 uint8_t;
  20. typedef INT32 int32_t;
  21. typedef UINT32 uint32_t;
  22. #define strncasecmp _strnicmp
  23. #define strcasecmp _stricmp
  24. #else
  25. #include <stdint.h>
  26. #endif
  27. #include <string.h>
  28. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__MINGW32__)
  29. #include <strings.h>
  30. #endif
  31. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  32. #include <unistd.h>
  33. #endif
  34. /*----------------------------------------------------------------------------*/
  35. /* Status and error constants */
  36. #define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
  37. #define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
  38. #define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
  39. #define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
  40. #define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
  41. #define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
  42. #define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
  43. Only int, string, binary data
  44. and integer expressions are
  45. allowed */
  46. #define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
  47. build */
  48. /*----------------------------------------------------------------------------*/
  49. /* Global variables */
  50. /*----------------------------------------------------------------------------*/
  51. /* Helper flags for complex dependencies */
  52. /* Indicates whether we expect mbedtls_entropy_init
  53. * to initialize some strong entropy source. */
  54. #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
  55. (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
  56. defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
  57. defined(ENTROPY_NV_SEED))
  58. #define ENTROPY_HAVE_STRONG
  59. #endif
  60. /*----------------------------------------------------------------------------*/
  61. /* Helper Functions */
  62. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  63. static int redirect_output(FILE *out_stream, const char *path)
  64. {
  65. int out_fd, dup_fd;
  66. FILE *path_stream;
  67. out_fd = fileno(out_stream);
  68. dup_fd = dup(out_fd);
  69. if (dup_fd == -1) {
  70. return -1;
  71. }
  72. path_stream = fopen(path, "w");
  73. if (path_stream == NULL) {
  74. close(dup_fd);
  75. return -1;
  76. }
  77. fflush(out_stream);
  78. if (dup2(fileno(path_stream), out_fd) == -1) {
  79. close(dup_fd);
  80. fclose(path_stream);
  81. return -1;
  82. }
  83. fclose(path_stream);
  84. return dup_fd;
  85. }
  86. static int restore_output(FILE *out_stream, int dup_fd)
  87. {
  88. int out_fd = fileno(out_stream);
  89. fflush(out_stream);
  90. if (dup2(dup_fd, out_fd) == -1) {
  91. close(out_fd);
  92. close(dup_fd);
  93. return -1;
  94. }
  95. close(dup_fd);
  96. return 0;
  97. }
  98. #endif /* __unix__ || __APPLE__ __MACH__ */