blob: 4465a60a8cdf67d2b7b17ae89c7e2aa9d8d109b0 [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
Simon Butcheredb7fd92016-05-17 13:35:51 +010010#include <stdlib.h>
11
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020013
SimonB0269dad2016-02-17 23:34:30 +000014#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
15#include "mbedtls/memory_buffer_alloc.h"
16#endif
17
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050018#if defined(MBEDTLS_CHECK_PARAMS)
19#include "mbedtls/platform_util.h"
20#include <setjmp.h>
21#endif
22
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000023#ifdef _MSC_VER
24#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010025typedef UINT8 uint8_t;
26typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000027typedef UINT32 uint32_t;
28#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020029#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000030#endif
31
Paul Bakker19343182013-08-16 13:31:10 +020032#include <string.h>
33
Janos Follath8ca53b52016-10-05 10:57:49 +010034#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
35#include <unistd.h>
36#endif
SimonB0269dad2016-02-17 23:34:30 +000037
38/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010039/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000040
Azim Khan62a5d7d2018-06-29 10:02:54 +010041#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
42#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
43#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010044
Azim Khan62a5d7d2018-06-29 10:02:54 +010045#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
46#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
47#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
48#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
49 Only int, string, binary data
50 and integer expressions are
51 allowed */
52#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
53 build */
SimonB0269dad2016-02-17 23:34:30 +000054
SimonB0269dad2016-02-17 23:34:30 +000055/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010056/* Global variables */
57
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050058#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050059jmp_buf jmp_tmp;
60#endif
61
SimonB8ca7bc42016-04-17 23:24:50 +010062/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010063/* Helper flags for complex dependencies */
64
65/* Indicates whether we expect mbedtls_entropy_init
66 * to initialize some strong entropy source. */
67#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
69 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
70 defined(MBEDTLS_HAVEGE_C) || \
71 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
72 defined(ENTROPY_NV_SEED)))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010073#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010074#endif
75
76
77/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000078/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079
Janos Follath8ca53b52016-10-05 10:57:49 +010080#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010082{
gufe44067f6e02020-07-30 09:02:27 +020083 int out_fd, dup_fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 out_fd = fileno(out_stream);
87 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 if (dup_fd == -1) {
90 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010091 }
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 path_stream = fopen(path, "w");
94 if (path_stream == NULL) {
95 close(dup_fd);
96 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010097 }
98
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 fflush(out_stream);
100 if (dup2(fileno(path_stream), out_fd) == -1) {
101 close(dup_fd);
102 fclose(path_stream);
103 return -1;
gufe44067f6e02020-07-30 09:02:27 +0200104 }
105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 fclose(path_stream);
107 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100108}
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100111{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 fflush(out_stream);
115 if (dup2(dup_fd, out_fd) == -1) {
116 close(out_fd);
117 close(dup_fd);
118 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100119 }
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 close(dup_fd);
122 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100123}
Janos Follath8ca53b52016-10-05 10:57:49 +0100124#endif /* __unix__ || __APPLE__ __MACH__ */