blob: 6af48c4aaef681fa5fc80d5720f454b5ebbe3118 [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 Peskinea2990432022-12-04 00:28:56 +010010#include <errno.h>
11#include <limits.h>
Gilles Peskineaf2fc502022-12-04 13:18:58 +010012#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010013#include <stdlib.h>
Gilles Peskineaf2fc502022-12-04 13:18:58 +010014#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020017
SimonB0269dad2016-02-17 23:34:30 +000018#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
19#include "mbedtls/memory_buffer_alloc.h"
20#endif
21
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050022#if defined(MBEDTLS_CHECK_PARAMS)
23#include "mbedtls/platform_util.h"
24#include <setjmp.h>
25#endif
26
Janos Follath8ca53b52016-10-05 10:57:49 +010027#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
28#include <unistd.h>
29#endif
SimonB0269dad2016-02-17 23:34:30 +000030
31/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010032/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000033
Azim Khan62a5d7d2018-06-29 10:02:54 +010034#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
35#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
36#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010037
Azim Khan62a5d7d2018-06-29 10:02:54 +010038#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
39#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
40#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
41#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
42 Only int, string, binary data
43 and integer expressions are
44 allowed */
45#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
46 build */
SimonB0269dad2016-02-17 23:34:30 +000047
SimonB0269dad2016-02-17 23:34:30 +000048/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010049/* Global variables */
50
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050051#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052jmp_buf jmp_tmp;
53#endif
54
SimonB8ca7bc42016-04-17 23:24:50 +010055/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010056/* Helper flags for complex dependencies */
57
58/* Indicates whether we expect mbedtls_entropy_init
59 * to initialize some strong entropy source. */
60#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
62 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
63 defined(MBEDTLS_HAVEGE_C) || \
64 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
65 defined(ENTROPY_NV_SEED)))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010066#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010067#endif
68
69
70/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000071/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072
Janos Follath8ca53b52016-10-05 10:57:49 +010073#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010075{
gufe44067f6e02020-07-30 09:02:27 +020076 int out_fd, dup_fd;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 out_fd = fileno(out_stream);
80 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 if (dup_fd == -1) {
83 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010084 }
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 path_stream = fopen(path, "w");
87 if (path_stream == NULL) {
88 close(dup_fd);
89 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010090 }
91
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 fflush(out_stream);
93 if (dup2(fileno(path_stream), out_fd) == -1) {
94 close(dup_fd);
95 fclose(path_stream);
96 return -1;
gufe44067f6e02020-07-30 09:02:27 +020097 }
98
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 fclose(path_stream);
100 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100101}
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100104{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 fflush(out_stream);
108 if (dup2(dup_fd, out_fd) == -1) {
109 close(out_fd);
110 close(dup_fd);
111 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100112 }
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 close(dup_fd);
115 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100116}
Janos Follath8ca53b52016-10-05 10:57:49 +0100117#endif /* __unix__ || __APPLE__ __MACH__ */