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