• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#line 2 "suites/helpers.function"
2/*----------------------------------------------------------------------------*/
3/* Headers */
4
5#include <test/helpers.h>
6#include <test/macros.h>
7#include <test/random.h>
8#include <test/psa_crypto_helpers.h>
9
10#include <stdlib.h>
11
12#if defined (MBEDTLS_ERROR_C)
13#include "mbedtls/error.h"
14#endif
15#include "mbedtls/platform.h"
16
17#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
18#include "mbedtls/memory_buffer_alloc.h"
19#endif
20
21#ifdef _MSC_VER
22#include <basetsd.h>
23typedef UINT8 uint8_t;
24typedef INT32 int32_t;
25typedef UINT32 uint32_t;
26#define strncasecmp _strnicmp
27#define strcasecmp _stricmp
28#else
29#include <stdint.h>
30#endif
31
32#include <string.h>
33
34#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__MINGW32__)
35#include <strings.h>
36#endif
37
38#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
39#include <unistd.h>
40#endif
41
42/*----------------------------------------------------------------------------*/
43/* Status and error constants */
44
45#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 */
48
49#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 */
58
59/*----------------------------------------------------------------------------*/
60/* Global variables */
61
62/*----------------------------------------------------------------------------*/
63/* Helper flags for complex dependencies */
64
65/* Indicates whether we expect mbedtls_entropy_init
66 * to initialize some strong entropy source. */
67#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
68    ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) ||      \
69        defined(MBEDTLS_ENTROPY_HARDWARE_ALT) ||    \
70        defined(ENTROPY_NV_SEED) )
71#define ENTROPY_HAVE_STRONG
72#endif
73
74
75/*----------------------------------------------------------------------------*/
76/* Helper Functions */
77
78#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    {
92        mbedtls_test_fail( msg, line_no, filename );
93        return 1;
94    }
95}
96#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
97
98#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
99static int redirect_output( FILE* out_stream, const char* path )
100{
101    int out_fd, dup_fd;
102    FILE* path_stream;
103
104    out_fd = fileno( out_stream );
105    dup_fd = dup( out_fd );
106
107    if( dup_fd == -1 )
108    {
109        return( -1 );
110    }
111
112    path_stream = fopen( path, "w" );
113    if( path_stream == NULL )
114    {
115        close( dup_fd );
116        return( -1 );
117    }
118
119    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 );
129}
130
131static int restore_output( FILE* out_stream, int dup_fd )
132{
133    int out_fd = fileno( out_stream );
134
135    fflush( out_stream );
136    if( dup2( dup_fd, out_fd ) == -1 )
137    {
138        close( out_fd );
139        close( dup_fd );
140        return( -1 );
141    }
142
143    close( dup_fd );
144    return( 0 );
145}
146#endif /* __unix__ || __APPLE__ __MACH__ */
147