blob: 60eae9a64f96db2bb3b47ba7246d92a09dbf6e1c [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 Cronb6d6d4c2020-06-03 10:11:18 +02005#include <test/helpers.h>
Mateusz Starzyk3911e702021-05-27 14:44:31 +02006#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02007#include <test/random.h>
Gilles Peskine881447d2022-12-08 15:24:52 +01008#include <test/bignum_helpers.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +01009#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +020010
Simon Butcheredb7fd92016-05-17 13:35:51 +010011#include <stdlib.h>
12
Gilles Peskine449bd832023-01-11 14:50:10 +010013#if defined(MBEDTLS_ERROR_C)
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020014#include "mbedtls/error.h"
15#endif
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
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000022#ifdef _MSC_VER
23#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010024typedef UINT8 uint8_t;
25typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000026typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000027#define strncasecmp _strnicmp
28#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000029#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020030#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#endif
32
Paul Bakker19343182013-08-16 13:31:10 +020033#include <string.h>
34
Cameron Cawleyea5496c2021-03-08 23:29:26 +000035#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__MINGW32__)
36#include <strings.h>
37#endif
38
Janos Follath8ca53b52016-10-05 10:57:49 +010039#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
40#include <unistd.h>
41#endif
SimonB0269dad2016-02-17 23:34:30 +000042
43/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010044/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000045
Azim Khan62a5d7d2018-06-29 10:02:54 +010046#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
47#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
48#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010049
Azim Khan62a5d7d2018-06-29 10:02:54 +010050#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
51#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
52#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
53#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
54 Only int, string, binary data
55 and integer expressions are
56 allowed */
57#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
58 build */
SimonB0269dad2016-02-17 23:34:30 +000059
SimonB0269dad2016-02-17 23:34:30 +000060/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010061/* Global variables */
62
SimonB8ca7bc42016-04-17 23:24:50 +010063/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010064/* Helper flags for complex dependencies */
65
66/* Indicates whether we expect mbedtls_entropy_init
67 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020068#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010069 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
70 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
71 defined(ENTROPY_NV_SEED))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010072#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010073#endif
74
75
76/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000077/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078
Janos Follath8ca53b52016-10-05 10:57:49 +010079#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +010080static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010081{
gufe44067f6e02020-07-30 09:02:27 +020082 int out_fd, dup_fd;
Gilles Peskine449bd832023-01-11 14:50:10 +010083 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 out_fd = fileno(out_stream);
86 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (dup_fd == -1) {
89 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010090 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 path_stream = fopen(path, "w");
93 if (path_stream == NULL) {
94 close(dup_fd);
95 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010096 }
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 fflush(out_stream);
99 if (dup2(fileno(path_stream), out_fd) == -1) {
100 close(dup_fd);
101 fclose(path_stream);
102 return -1;
gufe44067f6e02020-07-30 09:02:27 +0200103 }
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 fclose(path_stream);
106 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +0100107}
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +0100110{
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 fflush(out_stream);
114 if (dup2(dup_fd, out_fd) == -1) {
115 close(out_fd);
116 close(dup_fd);
117 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100118 }
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 close(dup_fd);
121 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100122}
Janos Follath8ca53b52016-10-05 10:57:49 +0100123#endif /* __unix__ || __APPLE__ __MACH__ */