blob: 86ff5b489350ab054f7a329eac73b3d5f86e7890 [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>
Ronald Cron4b8b1992020-06-09 13:52:23 +020011
Gilles Peskine5226eb52022-12-04 00:28:56 +010012#include <errno.h>
13#include <limits.h>
Gilles Peskine18793262022-12-04 13:18:58 +010014#include <stdint.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010015#include <stdlib.h>
Gilles Peskine18793262022-12-04 13:18:58 +010016#include <string.h>
Simon Butcheredb7fd92016-05-17 13:35:51 +010017
Gilles Peskine449bd832023-01-11 14:50:10 +010018#if defined(MBEDTLS_ERROR_C)
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020019#include "mbedtls/error.h"
20#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020022
SimonB0269dad2016-02-17 23:34:30 +000023#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
24#include "mbedtls/memory_buffer_alloc.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
SimonB8ca7bc42016-04-17 23:24:50 +010051/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010052/* Helper flags for complex dependencies */
53
54/* Indicates whether we expect mbedtls_entropy_init
55 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020056#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010057 (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
58 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
59 defined(ENTROPY_NV_SEED))
Hanno Beckerd4a872e2017-09-07 08:09:33 +010060#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010061#endif
62
63
64/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000065/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050066
Janos Follath8ca53b52016-10-05 10:57:49 +010067#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
Gilles Peskine449bd832023-01-11 14:50:10 +010068static int redirect_output(FILE *out_stream, const char *path)
Janos Follath8ca53b52016-10-05 10:57:49 +010069{
gufe44067f6e02020-07-30 09:02:27 +020070 int out_fd, dup_fd;
Gilles Peskine449bd832023-01-11 14:50:10 +010071 FILE *path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 out_fd = fileno(out_stream);
74 dup_fd = dup(out_fd);
gufe44067f6e02020-07-30 09:02:27 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (dup_fd == -1) {
77 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010078 }
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 path_stream = fopen(path, "w");
81 if (path_stream == NULL) {
82 close(dup_fd);
83 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +010084 }
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 fflush(out_stream);
87 if (dup2(fileno(path_stream), out_fd) == -1) {
88 close(dup_fd);
89 fclose(path_stream);
90 return -1;
gufe44067f6e02020-07-30 09:02:27 +020091 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 fclose(path_stream);
94 return dup_fd;
Janos Follath8ca53b52016-10-05 10:57:49 +010095}
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int restore_output(FILE *out_stream, int dup_fd)
Janos Follath8ca53b52016-10-05 10:57:49 +010098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 int out_fd = fileno(out_stream);
Janos Follath8ca53b52016-10-05 10:57:49 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 fflush(out_stream);
102 if (dup2(dup_fd, out_fd) == -1) {
103 close(out_fd);
104 close(dup_fd);
105 return -1;
Janos Follath8ca53b52016-10-05 10:57:49 +0100106 }
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 close(dup_fd);
109 return 0;
Simon Butchere0192962016-10-12 23:07:30 +0100110}
Janos Follath8ca53b52016-10-05 10:57:49 +0100111#endif /* __unix__ || __APPLE__ __MACH__ */