blob: 1e5c666ff82e47eddffeaae507de45ff50982e52 [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:
David Horstmann14bae832022-11-03 17:49:29 +00006 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +01007 *
David Horstmann14bae832022-11-03 17:49:29 +00008 * Test file : __MBEDTLS_TEST_TEMPLATE__TEST_FILE
Azim Khan191e9042017-06-09 12:39:00 +01009 *
10 * The following files were used to create this file.
11 *
David Horstmann14bae832022-11-03 17:49:29 +000012 * Main code file : __MBEDTLS_TEST_TEMPLATE__TEST_MAIN_FILE
13 * Platform code file : __MBEDTLS_TEST_TEMPLATE__TEST_PLATFORM_FILE
14 * Helper file : __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPER_FILE
15 * Test suite file : __MBEDTLS_TEST_TEMPLATE__TEST_CASE_FILE
16 * Test suite data : __MBEDTLS_TEST_TEMPLATE__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
David Horstmann14bae832022-11-03 17:49:29 +000052__MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS
Paul Bakkerde56ca12013-09-15 17:05:21 +020053
David Horstmann14bae832022-11-03 17:49:29 +000054#line __MBEDTLS_TEST_TEMPLATE__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
David Horstmann14bae832022-11-03 17:49:29 +000063__MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE
Azim Khan191e9042017-06-09 12:39:00 +010064
David Horstmann14bae832022-11-03 17:49:29 +000065#line __MBEDTLS_TEST_TEMPLATE__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:
David Horstmann14bae832022-11-03 17:49:29 +000077 * __MBEDTLS_TEST_TEMPLATE__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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084int 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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 switch (exp_id) {
92 __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE
David Horstmann14bae832022-11-03 17:49:29 +000093#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khanb1c2d0f2017-07-07 17:14:02 +010094 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 {
96 ret = KEY_VALUE_MAPPING_NOT_FOUND;
97 }
98 break;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010099 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100101}
Paul Bakker19343182013-08-16 13:31:10 +0200102
Paul Bakker19343182013-08-16 13:31:10 +0200103
Azim Khan191e9042017-06-09 12:39:00 +0100104/**
105 * \brief Checks if the dependency i.e. the compile flag is set.
106 * For optimizing space for embedded targets each dependency
107 * is identified by a unique identifier instead of string literals.
108 * Identifiers and check code is generated by script:
David Horstmann14bae832022-11-03 17:49:29 +0000109 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +0100110 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100111 * \param dep_id Dependency identifier.
Azim Khan191e9042017-06-09 12:39:00 +0100112 *
113 * \return DEPENDENCY_SUPPORTED if set else DEPENDENCY_NOT_SUPPORTED
114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115int dep_check(int dep_id)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100116{
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100117 int ret = DEPENDENCY_NOT_SUPPORTED;
118
119 (void) dep_id;
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 switch (dep_id) {
122 __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE
David Horstmann14bae832022-11-03 17:49:29 +0000123#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100124 default:
125 break;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100126 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100128}
Paul Bakker19343182013-08-16 13:31:10 +0200129
SimonB8ca7bc42016-04-17 23:24:50 +0100130
Azim Khan191e9042017-06-09 12:39:00 +0100131/**
132 * \brief Function pointer type for test function wrappers.
133 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100134 * A test function wrapper decodes the parameters and passes them to the
135 * underlying test function. Both the wrapper and the underlying function
136 * return void. Test wrappers assume that they are passed a suitable
137 * parameter array and do not perform any error detection.
Azim Khan191e9042017-06-09 12:39:00 +0100138 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100139 * \param param_array The array of parameters. Each element is a `void *`
140 * which the wrapper casts to the correct type and
141 * dereferences. Each wrapper function hard-codes the
142 * number and types of the parameters.
Azim Khan191e9042017-06-09 12:39:00 +0100143 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144typedef void (*TestWrapper_t)(void **param_array);
Simon Butcher65b1fa62016-05-23 23:18:26 +0100145
Azim Khan191e9042017-06-09 12:39:00 +0100146
147/**
148 * \brief Table of test function wrappers. Used by dispatch_test().
149 * This table is populated by script:
David Horstmann14bae832022-11-03 17:49:29 +0000150 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +0100151 *
152 */
153TestWrapper_t test_funcs[] =
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100154{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE
David Horstmann14bae832022-11-03 17:49:29 +0000156#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100157};
Azim Khan191e9042017-06-09 12:39:00 +0100158
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500159/**
160 * \brief Execute the test function.
161 *
162 * This is a wrapper function around the test function execution
163 * to allow the setjmp() call used to catch any calls to the
164 * parameter failure callback, to be used. Calls to setjmp()
165 * can invalidate the state of any local auto variables.
166 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100167 * \param fp Function pointer to the test function.
168 * \param params Parameters to pass to the #TestWrapper_t wrapper function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 *
170 */
171void execute_function_ptr(TestWrapper_t fp, void **params)
172{
Gilles Peskine76175ba2020-11-24 18:39:12 +0100173#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 mbedtls_test_enable_insecure_external_rng();
Gilles Peskine76175ba2020-11-24 18:39:12 +0100175#endif
176
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500177#if defined(MBEDTLS_CHECK_PARAMS)
Ronald Cron4e665872020-06-30 17:44:27 +0200178 mbedtls_test_param_failed_location_record_t location_record;
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if (setjmp(mbedtls_test_param_failed_get_state_buf()) == 0) {
181 fp(params);
182 } else {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 /* Unexpected parameter validation error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 mbedtls_test_param_failed_get_location_record(&location_record);
185 mbedtls_test_fail(location_record.failure_condition,
186 location_record.line,
187 location_record.file);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 }
189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_test_param_failed_reset_state();
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 fp(params);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193#endif
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100194
195#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 mbedtls_test_mutex_usage_check();
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100197#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198}
Azim Khan191e9042017-06-09 12:39:00 +0100199
200/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 * \brief Dispatches test functions based on function index.
Azim Khan191e9042017-06-09 12:39:00 +0100202 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100203 * \param func_idx Test function index.
204 * \param params The array of parameters to pass to the test function.
205 * It will be decoded by the #TestWrapper_t wrapper function.
Azim Khan191e9042017-06-09 12:39:00 +0100206 *
207 * \return DISPATCH_TEST_SUCCESS if found
208 * DISPATCH_TEST_FN_NOT_FOUND if not found
209 * DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
210 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211int dispatch_test(size_t func_idx, void **params)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100212{
Azim Khan191e9042017-06-09 12:39:00 +0100213 int ret = DISPATCH_TEST_SUCCESS;
214 TestWrapper_t fp = NULL;
215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (func_idx < (int) (sizeof(test_funcs) / sizeof(TestWrapper_t))) {
Azim Khan191e9042017-06-09 12:39:00 +0100217 fp = test_funcs[func_idx];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 if (fp) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 execute_function_ptr(fp, params);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 } else {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100221 ret = DISPATCH_UNSUPPORTED_SUITE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 }
223 } else {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100224 ret = DISPATCH_TEST_FN_NOT_FOUND;
225 }
Azim Khan191e9042017-06-09 12:39:00 +0100226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100228}
Paul Bakker19343182013-08-16 13:31:10 +0200229
SimonB152ea182016-02-15 23:27:28 +0000230
Azim Khan13c6bfb2017-06-15 14:45:56 +0100231/**
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100232 * \brief Checks if test function is supported in this build-time
233 * configuration.
Azim Khan13c6bfb2017-06-15 14:45:56 +0100234 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100235 * \param func_idx Test function index.
Azim Khan13c6bfb2017-06-15 14:45:56 +0100236 *
237 * \return DISPATCH_TEST_SUCCESS if found
238 * DISPATCH_TEST_FN_NOT_FOUND if not found
239 * DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
240 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241int check_test(size_t func_idx)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100242{
Azim Khan13c6bfb2017-06-15 14:45:56 +0100243 int ret = DISPATCH_TEST_SUCCESS;
244 TestWrapper_t fp = NULL;
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 if (func_idx < (int) (sizeof(test_funcs)/sizeof(TestWrapper_t))) {
Azim Khan13c6bfb2017-06-15 14:45:56 +0100247 fp = test_funcs[func_idx];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if (fp == NULL) {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100249 ret = DISPATCH_UNSUPPORTED_SUITE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 }
251 } else {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100252 ret = DISPATCH_TEST_FN_NOT_FOUND;
253 }
Azim Khan13c6bfb2017-06-15 14:45:56 +0100254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100256}
Azim Khan13c6bfb2017-06-15 14:45:56 +0100257
258
David Horstmann14bae832022-11-03 17:49:29 +0000259__MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE
Azim Khan191e9042017-06-09 12:39:00 +0100260
David Horstmann14bae832022-11-03 17:49:29 +0000261#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khan191e9042017-06-09 12:39:00 +0100262
SimonB152ea182016-02-15 23:27:28 +0000263/*----------------------------------------------------------------------------*/
264/* Main Test code */
265
SimonB15942102016-04-25 21:34:49 +0100266
Azim Khan191e9042017-06-09 12:39:00 +0100267/**
268 * \brief Program main. Invokes platform specific execute_tests().
Gilles Peskine964faeb2017-09-29 18:00:25 +0200269 *
Azim Khan191e9042017-06-09 12:39:00 +0100270 * \param argc Command line arguments count.
271 * \param argv Array of command line arguments.
272 *
273 * \return Exit code.
Gilles Peskine964faeb2017-09-29 18:00:25 +0200274 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275int main(int argc, const char *argv[])
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100276{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ERROR_C)
Chris Jones74392092021-04-01 16:00:01 +0100278 mbedtls_test_hook_error_add = &mbedtls_test_err_add_check;
Chris Jones96ae73b2021-01-08 17:04:59 +0000279#endif
280
Ronald Crone9c09f12020-06-08 16:44:58 +0200281 int ret = mbedtls_test_platform_setup();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if (ret != 0) {
283 mbedtls_fprintf(stderr,
284 "FATAL: Failed to initialize platform - error %d\n",
285 ret);
286 return -1;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100287 }
Hanno Becker1cfc5dd2018-11-12 13:18:45 +0000288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 ret = execute_tests(argc, argv);
Ronald Crone9c09f12020-06-08 16:44:58 +0200290 mbedtls_test_platform_teardown();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100292}