blob: 7615d27153df03ce87fe3f35c86989b87d3e1137 [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 Peskine5226eb52022-12-04 00:28:56 +010011#include <errno.h>
12#include <limits.h>
Gilles Peskine18793262022-12-04 13:18:58 +010013#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010014#include <stdlib.h>
Gilles Peskine18793262022-12-04 13:18:58 +010015#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010016
Gilles Peskine449bd832023-01-11 14:50:10 +010017#if defined(MBEDTLS_ERROR_C)
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020018#include "mbedtls/error.h"
19#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020021
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Janos Follath8ca53b52016-10-05 10:57:49 +010026#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
27#include <unistd.h>
28#endif
SimonB0269dad2016-02-17 23:34:30 +000029
30/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010031/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000032
Azim Khan62a5d7d2018-06-29 10:02:54 +010033#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
34#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
35#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010036
Azim Khan62a5d7d2018-06-29 10:02:54 +010037#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
38#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
39#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
40#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
41 Only int, string, binary data
42 and integer expressions are
43 allowed */
44#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
45 build */
SimonB0269dad2016-02-17 23:34:30 +000046
SimonB0269dad2016-02-17 23:34:30 +000047/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010048/* Global variables */
49
SimonB8ca7bc42016-04-17 23:24:50 +010050/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010051/* Helper flags for complex dependencies */
52
53/* Indicates whether we expect mbedtls_entropy_init
54 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020055#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010056 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
57 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
58 defined(ENTROPY_NV_SEED))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010059#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010060#endif
61
62
63/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000064/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050065
Janos Follath8ca53b52016-10-05 10:57:49 +010066#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +010067static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010068{
gufe44067f6e02020-07-30 09:02:27 +020069 int out_fd, dup_fd;
Gilles Peskine449bd832023-01-11 14:50:10 +010070 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 out_fd = fileno(out_stream);
73 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (dup_fd == -1) {
76 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010077 }
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 path_stream = fopen(path, "w");
80 if (path_stream == NULL) {
81 close(dup_fd);
82 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010083 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 fflush(out_stream);
86 if (dup2(fileno(path_stream), out_fd) == -1) {
87 close(dup_fd);
88 fclose(path_stream);
89 return -1;
gufe44067f6e02020-07-30 09:02:27 +020090 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fclose(path_stream);
93 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +010094}
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +010097{
Gilles Peskine449bd832023-01-11 14:50:10 +010098 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +010099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 fflush(out_stream);
101 if (dup2(dup_fd, out_fd) == -1) {
102 close(out_fd);
103 close(dup_fd);
104 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 close(dup_fd);
108 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100109}
Janos Follath8ca53b52016-10-05 10:57:49 +0100110#endif /* __unix__ || __APPLE__ __MACH__ */