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