blob: fe33f9bf9bf83e020b294ce9b347acb8d3e7eb92 [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 Peskinef6be5902020-11-24 18:33:13 +01008#include <test/psa_crypto_helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +02009
Simon Butcheredb7fd92016-05-17 13:35:51 +010010#include <stdlib.h>
11
Mateusz Starzyk28c8cce2021-05-21 09:48:03 +020012#if defined (MBEDTLS_ERROR_C)
13#include "mbedtls/error.h"
14#endif
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020016
SimonB0269dad2016-02-17 23:34:30 +000017#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
18#include "mbedtls/memory_buffer_alloc.h"
19#endif
20
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000021#ifdef _MSC_VER
22#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010023typedef UINT8 uint8_t;
24typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000025typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000026#define strncasecmp _strnicmp
27#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000028#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020029#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000030#endif
31
Paul Bakker19343182013-08-16 13:31:10 +020032#include <string.h>
33
Cameron Cawleyea5496c2021-03-08 23:29:26 +000034#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__MINGW32__)
35#include <strings.h>
36#endif
37
Janos Follath8ca53b52016-10-05 10:57:49 +010038#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
39#include <unistd.h>
40#endif
SimonB0269dad2016-02-17 23:34:30 +000041
42/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010043/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000044
Azim Khan62a5d7d2018-06-29 10:02:54 +010045#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
46#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
47#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010048
Azim Khan62a5d7d2018-06-29 10:02:54 +010049#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
50#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
51#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
52#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
53 Only int, string, binary data
54 and integer expressions are
55 allowed */
56#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
57 build */
SimonB0269dad2016-02-17 23:34:30 +000058
SimonB0269dad2016-02-17 23:34:30 +000059/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010060/* Global variables */
61
SimonB8ca7bc42016-04-17 23:24:50 +010062/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +010063/* Helper flags for complex dependencies */
64
65/* Indicates whether we expect mbedtls_entropy_init
66 * to initialize some strong entropy source. */
Mateusz Starzyk72f60df2021-04-30 13:28:22 +020067#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
68 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
69 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
70 defined(ENTROPY_NV_SEED) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +010071#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +010072#endif
73
74
75/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +000076/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077
Gilles Peskinec85c2012021-01-06 20:47:16 +010078#if defined(MBEDTLS_PSA_CRYPTO_C)
79/** Check that no PSA Crypto key slots are in use.
80 *
81 * If any slots are in use, mark the current test as failed.
82 *
83 * \return 0 if the key store is empty, 1 otherwise.
84 */
85int test_fail_if_psa_leaking( int line_no, const char *filename )
86{
87 const char *msg = mbedtls_test_helper_is_psa_leaking( );
88 if( msg == NULL )
89 return 0;
90 else
91 {
Chris Jones9634bb12021-01-20 15:56:42 +000092 mbedtls_test_fail( msg, line_no, filename );
Gilles Peskinec85c2012021-01-06 20:47:16 +010093 return 1;
94 }
95}
96#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
97
Janos Follath8ca53b52016-10-05 10:57:49 +010098#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +020099static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100100{
gufe44067f6e02020-07-30 09:02:27 +0200101 int out_fd, dup_fd;
102 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100103
gufe44067f6e02020-07-30 09:02:27 +0200104 out_fd = fileno( out_stream );
105 dup_fd = dup( out_fd );
106
107 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100108 {
gufe44067f6e02020-07-30 09:02:27 +0200109 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100110 }
111
gufe44067f6e02020-07-30 09:02:27 +0200112 path_stream = fopen( path, "w" );
113 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100114 {
gufe44067f6e02020-07-30 09:02:27 +0200115 close( dup_fd );
116 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100117 }
118
gufe44067f6e02020-07-30 09:02:27 +0200119 fflush( out_stream );
120 if( dup2( fileno( path_stream ), out_fd ) == -1 )
121 {
122 close( dup_fd );
123 fclose( path_stream );
124 return( -1 );
125 }
126
127 fclose( path_stream );
128 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100129}
130
gufe44067f6e02020-07-30 09:02:27 +0200131static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100132{
gufe44067f6e02020-07-30 09:02:27 +0200133 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100134
gufe44067f6e02020-07-30 09:02:27 +0200135 fflush( out_stream );
136 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100137 {
gufe44067f6e02020-07-30 09:02:27 +0200138 close( out_fd );
139 close( dup_fd );
140 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100141 }
142
gufe44067f6e02020-07-30 09:02:27 +0200143 close( dup_fd );
144 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100145}
Janos Follath8ca53b52016-10-05 10:57:49 +0100146#endif /* __unix__ || __APPLE__ __MACH__ */