• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/psa_crypto_helpers.h>
10
11#include <errno.h>
12#include <limits.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "mbedtls/platform.h"
18
19#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
20#include "mbedtls/memory_buffer_alloc.h"
21#endif
22
23#if defined(MBEDTLS_CHECK_PARAMS)
24#include "mbedtls/platform_util.h"
25#include <setjmp.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#if defined(MBEDTLS_CHECK_PARAMS)
53jmp_buf jmp_tmp;
54#endif
55
56/*----------------------------------------------------------------------------*/
57/* Helper flags for complex dependencies */
58
59/* Indicates whether we expect mbedtls_entropy_init
60 * to initialize some strong entropy source. */
61#if defined(MBEDTLS_TEST_NULL_ENTROPY) ||             \
62    (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
63    (!defined(MBEDTLS_NO_PLATFORM_ENTROPY)  ||     \
64    defined(MBEDTLS_HAVEGE_C)             ||     \
65    defined(MBEDTLS_ENTROPY_HARDWARE_ALT) ||     \
66    defined(ENTROPY_NV_SEED)))
67#define ENTROPY_HAVE_STRONG
68#endif
69
70
71/*----------------------------------------------------------------------------*/
72/* Helper Functions */
73
74#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
75static int redirect_output(FILE *out_stream, const char *path)
76{
77    int out_fd, dup_fd;
78    FILE *path_stream;
79
80    out_fd = fileno(out_stream);
81    dup_fd = dup(out_fd);
82
83    if (dup_fd == -1) {
84        return -1;
85    }
86
87    path_stream = fopen(path, "w");
88    if (path_stream == NULL) {
89        close(dup_fd);
90        return -1;
91    }
92
93    fflush(out_stream);
94    if (dup2(fileno(path_stream), out_fd) == -1) {
95        close(dup_fd);
96        fclose(path_stream);
97        return -1;
98    }
99
100    fclose(path_stream);
101    return dup_fd;
102}
103
104static int restore_output(FILE *out_stream, int dup_fd)
105{
106    int out_fd = fileno(out_stream);
107
108    fflush(out_stream);
109    if (dup2(dup_fd, out_fd) == -1) {
110        close(out_fd);
111        close(dup_fd);
112        return -1;
113    }
114
115    close(dup_fd);
116    return 0;
117}
118#endif /* __unix__ || __APPLE__ __MACH__ */
119