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#include <test/threading_helpers.h>
12
13#include <errno.h>
14#include <limits.h>
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18
19#if defined(MBEDTLS_ERROR_C)
20#include "mbedtls/error.h"
21#endif
22#include "mbedtls/platform.h"
23
24#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
25#include "mbedtls/memory_buffer_alloc.h"
26#endif
27
28#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
29#include <unistd.h>
30#endif
31
32/*----------------------------------------------------------------------------*/
33/* Status and error constants */
34
35#define DEPENDENCY_SUPPORTED            0   /* Dependency supported by build */
36#define KEY_VALUE_MAPPING_FOUND         0   /* Integer expression found */
37#define DISPATCH_TEST_SUCCESS           0   /* Test dispatch successful */
38
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/*----------------------------------------------------------------------------*/
50/* Global variables */
51
52/*----------------------------------------------------------------------------*/
53/* Helper flags for complex dependencies */
54
55/* Indicates whether we expect mbedtls_entropy_init
56 * to initialize some strong entropy source. */
57#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
58    (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) ||      \
59    defined(MBEDTLS_ENTROPY_HARDWARE_ALT) ||    \
60    defined(ENTROPY_NV_SEED))
61#define ENTROPY_HAVE_STRONG
62#endif
63
64
65/*----------------------------------------------------------------------------*/
66/* Helper Functions */
67
68#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
69static int redirect_output(FILE *out_stream, const char *path)
70{
71    int out_fd, dup_fd;
72    FILE *path_stream;
73
74    out_fd = fileno(out_stream);
75    dup_fd = dup(out_fd);
76
77    if (dup_fd == -1) {
78        return -1;
79    }
80
81    path_stream = fopen(path, "w");
82    if (path_stream == NULL) {
83        close(dup_fd);
84        return -1;
85    }
86
87    fflush(out_stream);
88    if (dup2(fileno(path_stream), out_fd) == -1) {
89        close(dup_fd);
90        fclose(path_stream);
91        return -1;
92    }
93
94    fclose(path_stream);
95    return dup_fd;
96}
97
98static int restore_output(FILE *out_stream, int dup_fd)
99{
100    int out_fd = fileno(out_stream);
101
102    fflush(out_stream);
103    if (dup2(dup_fd, out_fd) == -1) {
104        close(out_fd);
105        close(dup_fd);
106        return -1;
107    }
108
109    close(dup_fd);
110    return 0;
111}
112#endif /* __unix__ || __APPLE__ __MACH__ */
113