blob: 19647315c93896464d2cf0d81eeb2cc27c5087db [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
Gilles Peskine18793262022-12-04 13:18:58 +010011#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010012#include <stdlib.h>
Gilles Peskine18793262022-12-04 13:18:58 +010013#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010014
Gilles Peskine449bd832023-01-11 14:50:10 +010015#if defined(MBEDTLS_ERROR_C)
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020016#include "mbedtls/error.h"
17#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020019
SimonB0269dad2016-02-17 23:34:30 +000020#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
21#include "mbedtls/memory_buffer_alloc.h"
22#endif
23
Janos Follath8ca53b52016-10-05 10:57:49 +010024#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
25#include <unistd.h>
26#endif
SimonB0269dad2016-02-17 23:34:30 +000027
28/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010029/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000030
Azim Khan62a5d7d2018-06-29 10:02:54 +010031#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
32#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
33#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010034
Azim Khan62a5d7d2018-06-29 10:02:54 +010035#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
36#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
37#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
38#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
39 Only int, string, binary data
40 and integer expressions are
41 allowed */
42#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
43 build */
SimonB0269dad2016-02-17 23:34:30 +000044
SimonB0269dad2016-02-17 23:34:30 +000045/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010046/* Global variables */
47
SimonB8ca7bc42016-04-17 23:24:50 +010048/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010049/* Helper flags for complex dependencies */
50
51/* Indicates whether we expect mbedtls_entropy_init
52 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020053#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010054 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
55 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
56 defined(ENTROPY_NV_SEED))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010057#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010058#endif
59
60
61/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000062/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063
Janos Follath8ca53b52016-10-05 10:57:49 +010064#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +010065static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010066{
gufe44067f6e02020-07-30 09:02:27 +020067 int out_fd, dup_fd;
Gilles Peskine449bd832023-01-11 14:50:10 +010068 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 out_fd = fileno(out_stream);
71 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (dup_fd == -1) {
74 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010075 }
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 path_stream = fopen(path, "w");
78 if (path_stream == NULL) {
79 close(dup_fd);
80 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010081 }
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 fflush(out_stream);
84 if (dup2(fileno(path_stream), out_fd) == -1) {
85 close(dup_fd);
86 fclose(path_stream);
87 return -1;
gufe44067f6e02020-07-30 09:02:27 +020088 }
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 fclose(path_stream);
91 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +010092}
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +010095{
Gilles Peskine449bd832023-01-11 14:50:10 +010096 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +010097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 fflush(out_stream);
99 if (dup2(dup_fd, out_fd) == -1) {
100 close(out_fd);
101 close(dup_fd);
102 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100103 }
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 close(dup_fd);
106 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100107}
Janos Follath8ca53b52016-10-05 10:57:49 +0100108#endif /* __unix__ || __APPLE__ __MACH__ */