blob: 6c8d98e8b90b8bb5e0b95a2121b43c4287d8b324 [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 Horstmannb85838f2022-11-03 17:49:29 +00006 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +01007 *
David Horstmannb85838f2022-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 Horstmannb85838f2022-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
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker19343182013-08-16 13:31:10 +020027
Gilles Peskine2ff02c32019-11-29 12:17:21 +010028/* Test code may use deprecated identifiers only if the preprocessor symbol
29 * MBEDTLS_TEST_DEPRECATED is defined. When building tests, set
30 * MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is
31 * enabled but the corresponding warnings are not treated as errors.
32 */
Gilles Peskine841b14b2019-11-26 17:37:37 +010033#if !defined(MBEDTLS_DEPRECATED_REMOVED) && !defined(MBEDTLS_DEPRECATED_WARNING)
34#define MBEDTLS_TEST_DEPRECATED
35#endif
36
SimonB152ea182016-02-15 23:27:28 +000037/*----------------------------------------------------------------------------*/
Azim Khan191e9042017-06-09 12:39:00 +010038/* Common helper code */
SimonB152ea182016-02-15 23:27:28 +000039
David Horstmannb85838f2022-11-03 17:49:29 +000040__MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS
Paul Bakkerde56ca12013-09-15 17:05:21 +020041
David Horstmannb85838f2022-11-03 17:49:29 +000042#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khan191e9042017-06-09 12:39:00 +010043
44
45/*----------------------------------------------------------------------------*/
46/* Test Suite Code */
47
48
49#define TEST_SUITE_ACTIVE
50
David Horstmannb85838f2022-11-03 17:49:29 +000051__MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE
Azim Khan191e9042017-06-09 12:39:00 +010052
David Horstmannb85838f2022-11-03 17:49:29 +000053#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
SimonB15942102016-04-25 21:34:49 +010054
SimonB152ea182016-02-15 23:27:28 +000055
56/*----------------------------------------------------------------------------*/
57/* Test dispatch code */
58
Paul Bakker19343182013-08-16 13:31:10 +020059
Azim Khan191e9042017-06-09 12:39:00 +010060/**
61 * \brief Evaluates an expression/macro into its literal integer value.
62 * For optimizing space for embedded targets each expression/macro
63 * is identified by a unique identifier instead of string literals.
64 * Identifiers and evaluation code is generated by script:
David Horstmannb85838f2022-11-03 17:49:29 +000065 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +010066 *
67 * \param exp_id Expression identifier.
68 * \param out_value Pointer to int to hold the integer.
69 *
70 * \return 0 if exp_id is found. 1 otherwise.
71 */
Gilles Peskine872948c2022-12-04 15:32:54 +010072int get_expression(int32_t exp_id, intmax_t *out_value)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010073{
Azim Khanb1c2d0f2017-07-07 17:14:02 +010074 int ret = KEY_VALUE_MAPPING_FOUND;
75
76 (void) exp_id;
77 (void) out_value;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 switch (exp_id) {
80 __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE
David Horstmannb85838f2022-11-03 17:49:29 +000081#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khanb1c2d0f2017-07-07 17:14:02 +010082 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010083 {
84 ret = KEY_VALUE_MAPPING_NOT_FOUND;
85 }
86 break;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010087 }
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +010089}
Paul Bakker19343182013-08-16 13:31:10 +020090
Paul Bakker19343182013-08-16 13:31:10 +020091
Azim Khan191e9042017-06-09 12:39:00 +010092/**
93 * \brief Checks if the dependency i.e. the compile flag is set.
94 * For optimizing space for embedded targets each dependency
95 * is identified by a unique identifier instead of string literals.
96 * Identifiers and check code is generated by script:
David Horstmannb85838f2022-11-03 17:49:29 +000097 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +010098 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +010099 * \param dep_id Dependency identifier.
Azim Khan191e9042017-06-09 12:39:00 +0100100 *
101 * \return DEPENDENCY_SUPPORTED if set else DEPENDENCY_NOT_SUPPORTED
102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int dep_check(int dep_id)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100104{
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100105 int ret = DEPENDENCY_NOT_SUPPORTED;
106
107 (void) dep_id;
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 switch (dep_id) {
110 __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE
David Horstmannb85838f2022-11-03 17:49:29 +0000111#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khanb1c2d0f2017-07-07 17:14:02 +0100112 default:
113 break;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100114 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100116}
Paul Bakker19343182013-08-16 13:31:10 +0200117
SimonB8ca7bc42016-04-17 23:24:50 +0100118
Azim Khan191e9042017-06-09 12:39:00 +0100119/**
120 * \brief Function pointer type for test function wrappers.
121 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100122 * A test function wrapper decodes the parameters and passes them to the
123 * underlying test function. Both the wrapper and the underlying function
124 * return void. Test wrappers assume that they are passed a suitable
125 * parameter array and do not perform any error detection.
Azim Khan191e9042017-06-09 12:39:00 +0100126 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100127 * \param param_array The array of parameters. Each element is a `void *`
128 * which the wrapper casts to the correct type and
129 * dereferences. Each wrapper function hard-codes the
130 * number and types of the parameters.
Azim Khan191e9042017-06-09 12:39:00 +0100131 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132typedef void (*TestWrapper_t)(void **param_array);
Simon Butcher65b1fa62016-05-23 23:18:26 +0100133
Azim Khan191e9042017-06-09 12:39:00 +0100134
135/**
136 * \brief Table of test function wrappers. Used by dispatch_test().
137 * This table is populated by script:
David Horstmannb85838f2022-11-03 17:49:29 +0000138 * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
Azim Khan191e9042017-06-09 12:39:00 +0100139 *
140 */
141TestWrapper_t test_funcs[] =
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100142{
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE
David Horstmannb85838f2022-11-03 17:49:29 +0000144#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100145};
Azim Khan191e9042017-06-09 12:39:00 +0100146
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * \brief Dispatches test functions based on function index.
Azim Khan191e9042017-06-09 12:39:00 +0100149 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100150 * \param func_idx Test function index.
151 * \param params The array of parameters to pass to the test function.
152 * It will be decoded by the #TestWrapper_t wrapper function.
Azim Khan191e9042017-06-09 12:39:00 +0100153 *
154 * \return DISPATCH_TEST_SUCCESS if found
155 * DISPATCH_TEST_FN_NOT_FOUND if not found
156 * DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int dispatch_test(size_t func_idx, void **params)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100159{
Azim Khan191e9042017-06-09 12:39:00 +0100160 int ret = DISPATCH_TEST_SUCCESS;
161 TestWrapper_t fp = NULL;
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (func_idx < (int) (sizeof(test_funcs) / sizeof(TestWrapper_t))) {
Azim Khan191e9042017-06-09 12:39:00 +0100164 fp = test_funcs[func_idx];
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (fp) {
TRodziewicz2d0769d2021-05-27 15:24:33 +0200166 #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_test_enable_insecure_external_rng();
TRodziewicz2d0769d2021-05-27 15:24:33 +0200168 #endif
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 fp(params);
TRodziewicz2d0769d2021-05-27 15:24:33 +0200171
172 #if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_test_mutex_usage_check();
TRodziewicz2d0769d2021-05-27 15:24:33 +0200174 #endif /* MBEDTLS_TEST_MUTEX_USAGE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 } else {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100176 ret = DISPATCH_UNSUPPORTED_SUITE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 }
178 } else {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100179 ret = DISPATCH_TEST_FN_NOT_FOUND;
180 }
Azim Khan191e9042017-06-09 12:39:00 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100183}
Paul Bakker19343182013-08-16 13:31:10 +0200184
SimonB152ea182016-02-15 23:27:28 +0000185
Azim Khan13c6bfb2017-06-15 14:45:56 +0100186/**
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100187 * \brief Checks if test function is supported in this build-time
188 * configuration.
Azim Khan13c6bfb2017-06-15 14:45:56 +0100189 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100190 * \param func_idx Test function index.
Azim Khan13c6bfb2017-06-15 14:45:56 +0100191 *
192 * \return DISPATCH_TEST_SUCCESS if found
193 * DISPATCH_TEST_FN_NOT_FOUND if not found
194 * DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196int check_test(size_t func_idx)
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100197{
Azim Khan13c6bfb2017-06-15 14:45:56 +0100198 int ret = DISPATCH_TEST_SUCCESS;
199 TestWrapper_t fp = NULL;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (func_idx < (int) (sizeof(test_funcs)/sizeof(TestWrapper_t))) {
Azim Khan13c6bfb2017-06-15 14:45:56 +0100202 fp = test_funcs[func_idx];
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (fp == NULL) {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100204 ret = DISPATCH_UNSUPPORTED_SUITE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
206 } else {
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100207 ret = DISPATCH_TEST_FN_NOT_FOUND;
208 }
Azim Khan13c6bfb2017-06-15 14:45:56 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100211}
Azim Khan13c6bfb2017-06-15 14:45:56 +0100212
213
David Horstmannb85838f2022-11-03 17:49:29 +0000214__MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE
Azim Khan191e9042017-06-09 12:39:00 +0100215
David Horstmannb85838f2022-11-03 17:49:29 +0000216#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
Azim Khan191e9042017-06-09 12:39:00 +0100217
SimonB152ea182016-02-15 23:27:28 +0000218/*----------------------------------------------------------------------------*/
219/* Main Test code */
220
SimonB15942102016-04-25 21:34:49 +0100221
Azim Khan191e9042017-06-09 12:39:00 +0100222/**
223 * \brief Program main. Invokes platform specific execute_tests().
Gilles Peskine964faeb2017-09-29 18:00:25 +0200224 *
Azim Khan191e9042017-06-09 12:39:00 +0100225 * \param argc Command line arguments count.
226 * \param argv Array of command line arguments.
227 *
228 * \return Exit code.
Gilles Peskine964faeb2017-09-29 18:00:25 +0200229 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230int main(int argc, const char *argv[])
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100231{
TRodziewiczd9be6522021-07-05 15:16:00 +0200232#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
TRodziewicz7871c2e2021-07-07 17:29:43 +0200234 mbedtls_test_hook_test_fail = &mbedtls_test_fail;
TRodziewicz829fe702021-07-13 12:23:12 +0200235#if defined(MBEDTLS_ERROR_C)
236 mbedtls_test_hook_error_add = &mbedtls_test_err_add_check;
237#endif
TRodziewiczd9be6522021-07-05 15:16:00 +0200238#endif
239
Ronald Crone9c09f12020-06-08 16:44:58 +0200240 int ret = mbedtls_test_platform_setup();
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (ret != 0) {
242 mbedtls_fprintf(stderr,
243 "FATAL: Failed to initialize platform - error %d\n",
244 ret);
245 return -1;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100246 }
Hanno Becker1cfc5dd2018-11-12 13:18:45 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 ret = execute_tests(argc, argv);
Ronald Crone9c09f12020-06-08 16:44:58 +0200249 mbedtls_test_platform_teardown();
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return ret;
Mohammad Azim Khan5cb70172018-07-19 11:32:30 +0100251}