blob: 12828f5ef26d766fa111beab208bd31b6060d93c [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
Gilles Peskine9ad7bd32022-12-04 13:10:55 +01005#include <test/arguments.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.h>
Gilles Peskine9ad7bd32022-12-04 13:10:55 +01007#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02008#include <test/random.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +01009#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +020010
Gilles Peskinea2990432022-12-04 00:28:56 +010011#include <errno.h>
12#include <limits.h>
Gilles Peskineaf2fc502022-12-04 13:18:58 +010013#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010014#include <stdlib.h>
Gilles Peskineaf2fc502022-12-04 13:18:58 +010015#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010016
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020018
SimonB0269dad2016-02-17 23:34:30 +000019#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
20#include "mbedtls/memory_buffer_alloc.h"
21#endif
22
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050023#if defined(MBEDTLS_CHECK_PARAMS)
24#include "mbedtls/platform_util.h"
25#include <setjmp.h>
26#endif
27
Janos Follath8ca53b52016-10-05 10:57:49 +010028#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
29#include <unistd.h>
30#endif
SimonB0269dad2016-02-17 23:34:30 +000031
32/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010033/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000034
Azim Khan62a5d7d2018-06-29 10:02:54 +010035#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 */
SimonB8ca7bc42016-04-17 23:24:50 +010038
Azim Khan62a5d7d2018-06-29 10:02:54 +010039#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 */
SimonB0269dad2016-02-17 23:34:30 +000048
SimonB0269dad2016-02-17 23:34:30 +000049/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010050/* Global variables */
51
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050053jmp_buf jmp_tmp;
54#endif
55
SimonB8ca7bc42016-04-17 23:24:50 +010056/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010057/* 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) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 (!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)))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010067#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010068#endif
69
70
71/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000072/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073
Janos Follath8ca53b52016-10-05 10:57:49 +010074#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010076{
gufe44067f6e02020-07-30 09:02:27 +020077 int out_fd, dup_fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 out_fd = fileno(out_stream);
81 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 if (dup_fd == -1) {
84 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010085 }
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 path_stream = fopen(path, "w");
88 if (path_stream == NULL) {
89 close(dup_fd);
90 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010091 }
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 fflush(out_stream);
94 if (dup2(fileno(path_stream), out_fd) == -1) {
95 close(dup_fd);
96 fclose(path_stream);
97 return -1;
gufe44067f6e02020-07-30 09:02:27 +020098 }
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 fclose(path_stream);
101 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100102}
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100105{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 fflush(out_stream);
109 if (dup2(dup_fd, out_fd) == -1) {
110 close(out_fd);
111 close(dup_fd);
112 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100113 }
114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 close(dup_fd);
116 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100117}
Janos Follath8ca53b52016-10-05 10:57:49 +0100118#endif /* __unix__ || __APPLE__ __MACH__ */