blob: b45702d834935481562a2d2227fefceb112e0f0e [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Ronald Cron4b8b1992020-06-09 13:52:23 +02005#include <test/macros.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02007#include <test/random.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +01008#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +02009
Gilles Peskineaf2fc502022-12-04 13:18:58 +010010#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010011#include <stdlib.h>
Gilles Peskineaf2fc502022-12-04 13:18:58 +010012#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020015
SimonB0269dad2016-02-17 23:34:30 +000016#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
17#include "mbedtls/memory_buffer_alloc.h"
18#endif
19
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020#if defined(MBEDTLS_CHECK_PARAMS)
21#include "mbedtls/platform_util.h"
22#include <setjmp.h>
23#endif
24
Janos Follath8ca53b52016-10-05 10:57:49 +010025#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
26#include <unistd.h>
27#endif
SimonB0269dad2016-02-17 23:34:30 +000028
29/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010030/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000031
Azim Khan62a5d7d2018-06-29 10:02:54 +010032#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
33#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
34#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010035
Azim Khan62a5d7d2018-06-29 10:02:54 +010036#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
37#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
38#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
39#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
40 Only int, string, binary data
41 and integer expressions are
42 allowed */
43#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
44 build */
SimonB0269dad2016-02-17 23:34:30 +000045
SimonB0269dad2016-02-17 23:34:30 +000046/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010047/* Global variables */
48
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050049#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050050jmp_buf jmp_tmp;
51#endif
52
SimonB8ca7bc42016-04-17 23:24:50 +010053/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010054/* Helper flags for complex dependencies */
55
56/* Indicates whether we expect mbedtls_entropy_init
57 * to initialize some strong entropy source. */
58#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
60 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
61 defined(MBEDTLS_HAVEGE_C) || \
62 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
63 defined(ENTROPY_NV_SEED)))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010064#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010065#endif
66
67
68/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000069/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070
Janos Follath8ca53b52016-10-05 10:57:49 +010071#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010073{
gufe44067f6e02020-07-30 09:02:27 +020074 int out_fd, dup_fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 out_fd = fileno(out_stream);
78 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (dup_fd == -1) {
81 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010082 }
83
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 path_stream = fopen(path, "w");
85 if (path_stream == NULL) {
86 close(dup_fd);
87 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010088 }
89
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 fflush(out_stream);
91 if (dup2(fileno(path_stream), out_fd) == -1) {
92 close(dup_fd);
93 fclose(path_stream);
94 return -1;
gufe44067f6e02020-07-30 09:02:27 +020095 }
96
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 fclose(path_stream);
98 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +010099}
100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100102{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 fflush(out_stream);
106 if (dup2(dup_fd, out_fd) == -1) {
107 close(out_fd);
108 close(dup_fd);
109 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100110 }
111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 close(dup_fd);
113 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100114}
Janos Follath8ca53b52016-10-05 10:57:49 +0100115#endif /* __unix__ || __APPLE__ __MACH__ */