blob: b561f4766ae601bb36c5012b932e4837b3378626 [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 Peskineb3c2eaf2022-12-04 13:10:55 +01005#include <test/arguments.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.h>
Mateusz Starzyk3911e702021-05-27 14:44:31 +02007#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02008#include <test/random.h>
Gilles Peskine881447d2022-12-08 15:24:52 +01009#include <test/bignum_helpers.h>
Gilles Peskinef6be5902020-11-24 18:33:13 +010010#include <test/psa_crypto_helpers.h>
Paul Elliott17c119a2023-12-08 16:55:03 +000011#include <test/threading_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +020012
Gilles Peskine5226eb52022-12-04 00:28:56 +010013#include <errno.h>
14#include <limits.h>
Gilles Peskine18793262022-12-04 13:18:58 +010015#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010016#include <stdlib.h>
Gilles Peskine18793262022-12-04 13:18:58 +010017#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010018
Gilles Peskine449bd832023-01-11 14:50:10 +010019#if defined(MBEDTLS_ERROR_C)
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020020#include "mbedtls/error.h"
21#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020023
SimonB0269dad2016-02-17 23:34:30 +000024#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
25#include "mbedtls/memory_buffer_alloc.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
SimonB8ca7bc42016-04-17 23:24:50 +010052/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010053/* Helper flags for complex dependencies */
54
55/* Indicates whether we expect mbedtls_entropy_init
56 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020057#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
Michael Schuster31b1cb82024-06-04 02:41:10 +020058 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
59 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +010060 defined(ENTROPY_NV_SEED))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010061#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010062#endif
63
64
65/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000066/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050067
Janos Follath8ca53b52016-10-05 10:57:49 +010068#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +010069static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010070{
gufe44067f6e02020-07-30 09:02:27 +020071 int out_fd, dup_fd;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 out_fd = fileno(out_stream);
75 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (dup_fd == -1) {
78 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010079 }
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 path_stream = fopen(path, "w");
82 if (path_stream == NULL) {
83 close(dup_fd);
84 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010085 }
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 fflush(out_stream);
88 if (dup2(fileno(path_stream), out_fd) == -1) {
89 close(dup_fd);
90 fclose(path_stream);
91 return -1;
gufe44067f6e02020-07-30 09:02:27 +020092 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 fclose(path_stream);
95 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +010096}
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +010099{
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 fflush(out_stream);
103 if (dup2(dup_fd, out_fd) == -1) {
104 close(out_fd);
105 close(dup_fd);
106 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100107 }
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 close(dup_fd);
110 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100111}
Janos Follath8ca53b52016-10-05 10:57:49 +0100112#endif /* __unix__ || __APPLE__ __MACH__ */