blob: 7cae0da2b2883046aa56daba93b7405acbbe296b [file] [log] [blame]
Azim Khan1de892b2017-06-09 15:02:36 +01001#line 2 "suites/main_test.function"
Azim Khan191e9042017-06-09 12:39:00 +01002/*
3 * *** THIS FILE HAS BEEN MACHINE GENERATED ***
4 *
5 * This file has been machine generated using the script:
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +01006 * $generator_script
Azim Khan191e9042017-06-09 12:39:00 +01007 *
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +01008 * Test file : $test_file
Azim Khan191e9042017-06-09 12:39:00 +01009 *
10 * The following files were used to create this file.
11 *
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010012 * Main code file : $test_main_file
13 * Platform code file : $test_platform_file
14 * Helper file : $test_common_helper_file
15 * Test suite file : $test_case_file
16 * Test suite data : $test_case_data_file
Azim Khan191e9042017-06-09 12:39:00 +010017 *
Azim Khan191e9042017-06-09 12:39:00 +010018 */
Paul Bakkerde56ca12013-09-15 17:05:21 +020019
Gilles Peskine31f88a22020-04-14 19:39:56 +020020#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
21#if !defined(_POSIX_C_SOURCE)
nia1c0c8372020-06-11 12:03:45 +010022#define _POSIX_C_SOURCE 200112L // for fileno() from <stdio.h>
Gilles Peskine31f88a22020-04-14 19:39:56 +020023#endif
24#endif
25
Azim Khan191e9042017-06-09 12:39:00 +010026#if !defined(MBEDTLS_CONFIG_FILE)
27#include <mbedtls/config.h>
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
Paul Bakker19343182013-08-16 13:31:10 +020031
Hanno Becker1cfc5dd2018-11-12 13:18:45 +000032#if defined(MBEDTLS_USE_PSA_CRYPTO)
33#include "psa/crypto.h"
34#endif /* MBEDTLS_USE_PSA_CRYPTO */
SimonB152ea182016-02-15 23:27:28 +000035
Chris Jones808b7c82021-01-13 12:33:36 +000036#if defined(MBEDTLS_TEST_HOOKS)
Chris Jonesef180af2021-01-26 17:50:48 +000037#include "mbedtls/error.h"
Chris Jones808b7c82021-01-13 12:33:36 +000038#endif
39
Gilles Peskine2ff02c32019-11-29 12:17:21 +010040/* Test code may use deprecated identifiers only if the preprocessor symbol
41 * MBEDTLS_TEST_DEPRECATED is defined. When building tests, set
42 * MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is
43 * enabled but the corresponding warnings are not treated as errors.
44 */
Gilles Peskine841b14b2019-11-26 17:37:37 +010045#if !defined(MBEDTLS_DEPRECATED_REMOVED) && !defined(MBEDTLS_DEPRECATED_WARNING)
46#define MBEDTLS_TEST_DEPRECATED
47#endif
48
SimonB152ea182016-02-15 23:27:28 +000049/*----------------------------------------------------------------------------*/
Azim Khan191e9042017-06-09 12:39:00 +010050/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +000051
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010052$test_common_helpers
Paul Bakkerde56ca12013-09-15 17:05:21 +020053
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010054#line $line_no "suites/main_test.function"
Azim Khan191e9042017-06-09 12:39:00 +010055
56
57/*----------------------------------------------------------------------------*/
58/* Test Suite Code */
59
60
61#define TEST_SUITE_ACTIVE
62
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010063$functions_code
Azim Khan191e9042017-06-09 12:39:00 +010064
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010065#line $line_no "suites/main_test.function"
SimonB15942102016-04-25 21:34:49 +010066
SimonB152ea182016-02-15 23:27:28 +000067
68/*----------------------------------------------------------------------------*/
69/* Test dispatch code */
70
Paul Bakker19343182013-08-16 13:31:10 +020071
Azim Khan191e9042017-06-09 12:39:00 +010072/**
73 * \brief Evaluates an expression/macro into its literal integer value.
74 * For optimizing space for embedded targets each expression/macro
75 * is identified by a unique identifier instead of string literals.
76 * Identifiers and evaluation code is generated by script:
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010077 * $generator_script
Azim Khan191e9042017-06-09 12:39:00 +010078 *
79 * \param exp_id Expression identifier.
80 * \param out_value Pointer to int to hold the integer.
81 *
82 * \return 0 if exp_id is found. 1 otherwise.
83 */
84int get_expression( int32_t exp_id, int32_t * out_value )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010085{
Azim Khanb1c2d0f2017-07-07 17:14:02 +010086 int ret = KEY_VALUE_MAPPING_FOUND;
87
88 (void) exp_id;
89 (void) out_value;
90
91 switch( exp_id )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010092 {
93$expression_code
94#line $line_no "suites/main_test.function"
Azim Khanb1c2d0f2017-07-07 17:14:02 +010095 default:
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010096 {
Azim Khanb1c2d0f2017-07-07 17:14:02 +010097 ret = KEY_VALUE_MAPPING_NOT_FOUND;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010098 }
Azim Khanb1c2d0f2017-07-07 17:14:02 +010099 break;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100100 }
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100101 return( ret );
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100102}
Paul Bakker19343182013-08-16 13:31:10 +0200103
Paul Bakker19343182013-08-16 13:31:10 +0200104
Azim Khan191e9042017-06-09 12:39:00 +0100105/**
106 * \brief Checks if the dependency i.e. the compile flag is set.
107 * For optimizing space for embedded targets each dependency
108 * is identified by a unique identifier instead of string literals.
109 * Identifiers and check code is generated by script:
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100110 * $generator_script
Azim Khan191e9042017-06-09 12:39:00 +0100111 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100112 * \param dep_id Dependency identifier.
Azim Khan191e9042017-06-09 12:39:00 +0100113 *
114 * \return DEPENDENCY_SUPPORTED if set else DEPENDENCY_NOT_SUPPORTED
115 */
116int dep_check( int dep_id )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100117{
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100118 int ret = DEPENDENCY_NOT_SUPPORTED;
119
120 (void) dep_id;
121
122 switch( dep_id )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100123 {
124$dep_check_code
125#line $line_no "suites/main_test.function"
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100126 default:
127 break;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100128 }
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100129 return( ret );
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100130}
Paul Bakker19343182013-08-16 13:31:10 +0200131
SimonB8ca7bc42016-04-17 23:24:50 +0100132
Azim Khan191e9042017-06-09 12:39:00 +0100133/**
134 * \brief Function pointer type for test function wrappers.
135 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100136 * A test function wrapper decodes the parameters and passes them to the
137 * underlying test function. Both the wrapper and the underlying function
138 * return void. Test wrappers assume that they are passed a suitable
139 * parameter array and do not perform any error detection.
Azim Khan191e9042017-06-09 12:39:00 +0100140 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100141 * \param param_array The array of parameters. Each element is a `void *`
142 * which the wrapper casts to the correct type and
143 * dereferences. Each wrapper function hard-codes the
144 * number and types of the parameters.
Azim Khan191e9042017-06-09 12:39:00 +0100145 */
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100146typedef void (*TestWrapper_t)( void **param_array );
Simon Butcher65b1fa62016-05-23 23:18:26 +0100147
Azim Khan191e9042017-06-09 12:39:00 +0100148
149/**
150 * \brief Table of test function wrappers. Used by dispatch_test().
151 * This table is populated by script:
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100152 * $generator_script
Azim Khan191e9042017-06-09 12:39:00 +0100153 *
154 */
155TestWrapper_t test_funcs[] =
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100156{
157$dispatch_code
158#line $line_no "suites/main_test.function"
159};
Azim Khan191e9042017-06-09 12:39:00 +0100160
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161/**
162 * \brief Execute the test function.
163 *
164 * This is a wrapper function around the test function execution
165 * to allow the setjmp() call used to catch any calls to the
166 * parameter failure callback, to be used. Calls to setjmp()
167 * can invalidate the state of any local auto variables.
168 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100169 * \param fp Function pointer to the test function.
170 * \param params Parameters to pass to the #TestWrapper_t wrapper function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 *
172 */
173void execute_function_ptr(TestWrapper_t fp, void **params)
174{
Gilles Peskine76175ba2020-11-24 18:39:12 +0100175#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
176 mbedtls_test_enable_insecure_external_rng( );
177#endif
178
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179#if defined(MBEDTLS_CHECK_PARAMS)
Ronald Cron4e665872020-06-30 17:44:27 +0200180 mbedtls_test_param_failed_location_record_t location_record;
181
Ronald Cron579fd282020-07-01 15:17:05 +0200182 if ( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 {
184 fp( params );
185 }
186 else
187 {
188 /* Unexpected parameter validation error */
Ronald Cron4e665872020-06-30 17:44:27 +0200189 mbedtls_test_param_failed_get_location_record( &location_record );
Chris Jones9634bb12021-01-20 15:56:42 +0000190 mbedtls_test_fail( location_record.failure_condition,
191 location_record.line,
192 location_record.file );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193 }
194
Ronald Cron579fd282020-07-01 15:17:05 +0200195 mbedtls_test_param_failed_reset_state( );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196#else
197 fp( params );
198#endif
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100199
200#if defined(MBEDTLS_TEST_MUTEX_USAGE)
201 mbedtls_test_mutex_usage_check( );
202#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203}
Azim Khan191e9042017-06-09 12:39:00 +0100204
205/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 * \brief Dispatches test functions based on function index.
Azim Khan191e9042017-06-09 12:39:00 +0100207 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100208 * \param func_idx Test function index.
209 * \param params The array of parameters to pass to the test function.
210 * It will be decoded by the #TestWrapper_t wrapper function.
Azim Khan191e9042017-06-09 12:39:00 +0100211 *
212 * \return DISPATCH_TEST_SUCCESS if found
213 * DISPATCH_TEST_FN_NOT_FOUND if not found
214 * DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
215 */
k-stachowiak03954f22019-09-16 10:23:10 +0200216int dispatch_test( size_t func_idx, void ** params )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100217{
Azim Khan191e9042017-06-09 12:39:00 +0100218 int ret = DISPATCH_TEST_SUCCESS;
219 TestWrapper_t fp = NULL;
220
Mohammad Azim Khand2d01122018-07-18 17:48:37 +0100221 if ( func_idx < (int)( sizeof( test_funcs ) / sizeof( TestWrapper_t ) ) )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100222 {
Azim Khan191e9042017-06-09 12:39:00 +0100223 fp = test_funcs[func_idx];
224 if ( fp )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500225 execute_function_ptr(fp, params);
Azim Khan191e9042017-06-09 12:39:00 +0100226 else
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100227 ret = DISPATCH_UNSUPPORTED_SUITE;
228 }
Azim Khan191e9042017-06-09 12:39:00 +0100229 else
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100230 {
231 ret = DISPATCH_TEST_FN_NOT_FOUND;
232 }
Azim Khan191e9042017-06-09 12:39:00 +0100233
Paul Bakker19343182013-08-16 13:31:10 +0200234 return( ret );
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100235}
Paul Bakker19343182013-08-16 13:31:10 +0200236
SimonB152ea182016-02-15 23:27:28 +0000237
Azim Khan13c6bfb2017-06-15 14:45:56 +0100238/**
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100239 * \brief Checks if test function is supported in this build-time
240 * configuration.
Azim Khan13c6bfb2017-06-15 14:45:56 +0100241 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100242 * \param func_idx Test function index.
Azim Khan13c6bfb2017-06-15 14:45:56 +0100243 *
244 * \return DISPATCH_TEST_SUCCESS if found
245 * DISPATCH_TEST_FN_NOT_FOUND if not found
246 * DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
247 */
k-stachowiak03954f22019-09-16 10:23:10 +0200248int check_test( size_t func_idx )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100249{
Azim Khan13c6bfb2017-06-15 14:45:56 +0100250 int ret = DISPATCH_TEST_SUCCESS;
251 TestWrapper_t fp = NULL;
252
253 if ( func_idx < (int)( sizeof(test_funcs)/sizeof( TestWrapper_t ) ) )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100254 {
Azim Khan13c6bfb2017-06-15 14:45:56 +0100255 fp = test_funcs[func_idx];
256 if ( fp == NULL )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100257 ret = DISPATCH_UNSUPPORTED_SUITE;
258 }
Azim Khan13c6bfb2017-06-15 14:45:56 +0100259 else
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100260 {
261 ret = DISPATCH_TEST_FN_NOT_FOUND;
262 }
Azim Khan13c6bfb2017-06-15 14:45:56 +0100263
264 return( ret );
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100265}
Azim Khan13c6bfb2017-06-15 14:45:56 +0100266
267
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100268$platform_code
Azim Khan191e9042017-06-09 12:39:00 +0100269
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100270#line $line_no "suites/main_test.function"
Azim Khan191e9042017-06-09 12:39:00 +0100271
SimonB152ea182016-02-15 23:27:28 +0000272/*----------------------------------------------------------------------------*/
273/* Main Test code */
274
SimonB15942102016-04-25 21:34:49 +0100275
Azim Khan191e9042017-06-09 12:39:00 +0100276/**
277 * \brief Program main. Invokes platform specific execute_tests().
Gilles Peskine964faeb2017-09-29 18:00:25 +0200278 *
Azim Khan191e9042017-06-09 12:39:00 +0100279 * \param argc Command line arguments count.
280 * \param argv Array of command line arguments.
281 *
282 * \return Exit code.
Gilles Peskine964faeb2017-09-29 18:00:25 +0200283 */
Azim Khan191e9042017-06-09 12:39:00 +0100284int main( int argc, const char *argv[] )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100285{
Chris Jones759e30b2021-02-09 15:30:54 +0000286#if defined(MBEDTLS_TEST_HOOKS) && defined (MBEDTLS_ERROR_C)
Chris Jones808b7c82021-01-13 12:33:36 +0000287 mbedtls_set_err_add_hook( &mbedtls_test_err_add_check );
Chris Jones96ae73b2021-01-08 17:04:59 +0000288#endif
289
Ronald Crone9c09f12020-06-08 16:44:58 +0200290 int ret = mbedtls_test_platform_setup();
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400291 if( ret != 0 )
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100292 {
Andrzej Kurekf13ca952018-04-18 04:14:31 -0400293 mbedtls_fprintf( stderr,
294 "FATAL: Failed to initialize platform - error %d\n",
295 ret );
296 return( -1 );
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100297 }
Hanno Becker1cfc5dd2018-11-12 13:18:45 +0000298
Azim Khan191e9042017-06-09 12:39:00 +0100299 ret = execute_tests( argc, argv );
Ronald Crone9c09f12020-06-08 16:44:58 +0200300 mbedtls_test_platform_teardown();
Azim Khan191e9042017-06-09 12:39:00 +0100301 return( ret );
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100302}