1a8e1175bSopenharmony_ci#line 2 "suites/main_test.function"
2a8e1175bSopenharmony_ci/*
3a8e1175bSopenharmony_ci * *** THIS FILE HAS BEEN MACHINE GENERATED ***
4a8e1175bSopenharmony_ci *
5a8e1175bSopenharmony_ci * This file has been machine generated using the script:
6a8e1175bSopenharmony_ci * __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
7a8e1175bSopenharmony_ci *
8a8e1175bSopenharmony_ci * Test file      : __MBEDTLS_TEST_TEMPLATE__TEST_FILE
9a8e1175bSopenharmony_ci *
10a8e1175bSopenharmony_ci * The following files were used to create this file.
11a8e1175bSopenharmony_ci *
12a8e1175bSopenharmony_ci *      Main code file      : __MBEDTLS_TEST_TEMPLATE__TEST_MAIN_FILE
13a8e1175bSopenharmony_ci *      Platform code file  : __MBEDTLS_TEST_TEMPLATE__TEST_PLATFORM_FILE
14a8e1175bSopenharmony_ci *      Helper file         : __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPER_FILE
15a8e1175bSopenharmony_ci *      Test suite file     : __MBEDTLS_TEST_TEMPLATE__TEST_CASE_FILE
16a8e1175bSopenharmony_ci *      Test suite data     : __MBEDTLS_TEST_TEMPLATE__TEST_CASE_DATA_FILE
17a8e1175bSopenharmony_ci *
18a8e1175bSopenharmony_ci */
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
21a8e1175bSopenharmony_ci#if !defined(_POSIX_C_SOURCE)
22a8e1175bSopenharmony_ci#define _POSIX_C_SOURCE 200112L // for fileno() from <stdio.h>
23a8e1175bSopenharmony_ci#endif
24a8e1175bSopenharmony_ci#endif
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_ci#include "mbedtls/build_info.h"
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_ci/* Test code may use deprecated identifiers only if the preprocessor symbol
29a8e1175bSopenharmony_ci * MBEDTLS_TEST_DEPRECATED is defined. When building tests, set
30a8e1175bSopenharmony_ci * MBEDTLS_TEST_DEPRECATED explicitly if MBEDTLS_DEPRECATED_WARNING is
31a8e1175bSopenharmony_ci * enabled but the corresponding warnings are not treated as errors.
32a8e1175bSopenharmony_ci */
33a8e1175bSopenharmony_ci#if !defined(MBEDTLS_DEPRECATED_REMOVED) && !defined(MBEDTLS_DEPRECATED_WARNING)
34a8e1175bSopenharmony_ci#define MBEDTLS_TEST_DEPRECATED
35a8e1175bSopenharmony_ci#endif
36a8e1175bSopenharmony_ci
37a8e1175bSopenharmony_ci/*----------------------------------------------------------------------------*/
38a8e1175bSopenharmony_ci/* Common helper code */
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_ci__MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS
41a8e1175bSopenharmony_ci
42a8e1175bSopenharmony_ci#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci
45a8e1175bSopenharmony_ci/*----------------------------------------------------------------------------*/
46a8e1175bSopenharmony_ci/* Test Suite Code */
47a8e1175bSopenharmony_ci
48a8e1175bSopenharmony_ci
49a8e1175bSopenharmony_ci#define TEST_SUITE_ACTIVE
50a8e1175bSopenharmony_ci
51a8e1175bSopenharmony_ci__MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
54a8e1175bSopenharmony_ci
55a8e1175bSopenharmony_ci
56a8e1175bSopenharmony_ci/*----------------------------------------------------------------------------*/
57a8e1175bSopenharmony_ci/* Test dispatch code */
58a8e1175bSopenharmony_ci
59a8e1175bSopenharmony_ci
60a8e1175bSopenharmony_ci/**
61a8e1175bSopenharmony_ci * \brief       Evaluates an expression/macro into its literal integer value.
62a8e1175bSopenharmony_ci *              For optimizing space for embedded targets each expression/macro
63a8e1175bSopenharmony_ci *              is identified by a unique identifier instead of string literals.
64a8e1175bSopenharmony_ci *              Identifiers and evaluation code is generated by script:
65a8e1175bSopenharmony_ci *              __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
66a8e1175bSopenharmony_ci *
67a8e1175bSopenharmony_ci * \param exp_id    Expression identifier.
68a8e1175bSopenharmony_ci * \param out_value Pointer to int to hold the integer.
69a8e1175bSopenharmony_ci *
70a8e1175bSopenharmony_ci * \return       0 if exp_id is found. 1 otherwise.
71a8e1175bSopenharmony_ci */
72a8e1175bSopenharmony_ciint get_expression(int32_t exp_id, intmax_t *out_value)
73a8e1175bSopenharmony_ci{
74a8e1175bSopenharmony_ci    int ret = KEY_VALUE_MAPPING_FOUND;
75a8e1175bSopenharmony_ci
76a8e1175bSopenharmony_ci    (void) exp_id;
77a8e1175bSopenharmony_ci    (void) out_value;
78a8e1175bSopenharmony_ci
79a8e1175bSopenharmony_ci    switch (exp_id) {
80a8e1175bSopenharmony_ci    __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE
81a8e1175bSopenharmony_ci#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
82a8e1175bSopenharmony_ci        default:
83a8e1175bSopenharmony_ci        {
84a8e1175bSopenharmony_ci            ret = KEY_VALUE_MAPPING_NOT_FOUND;
85a8e1175bSopenharmony_ci        }
86a8e1175bSopenharmony_ci        break;
87a8e1175bSopenharmony_ci    }
88a8e1175bSopenharmony_ci    return ret;
89a8e1175bSopenharmony_ci}
90a8e1175bSopenharmony_ci
91a8e1175bSopenharmony_ci
92a8e1175bSopenharmony_ci/**
93a8e1175bSopenharmony_ci * \brief       Checks if the dependency i.e. the compile flag is set.
94a8e1175bSopenharmony_ci *              For optimizing space for embedded targets each dependency
95a8e1175bSopenharmony_ci *              is identified by a unique identifier instead of string literals.
96a8e1175bSopenharmony_ci *              Identifiers and check code is generated by script:
97a8e1175bSopenharmony_ci *              __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
98a8e1175bSopenharmony_ci *
99a8e1175bSopenharmony_ci * \param dep_id    Dependency identifier.
100a8e1175bSopenharmony_ci *
101a8e1175bSopenharmony_ci * \return       DEPENDENCY_SUPPORTED if set else DEPENDENCY_NOT_SUPPORTED
102a8e1175bSopenharmony_ci */
103a8e1175bSopenharmony_ciint dep_check(int dep_id)
104a8e1175bSopenharmony_ci{
105a8e1175bSopenharmony_ci    int ret = DEPENDENCY_NOT_SUPPORTED;
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci    (void) dep_id;
108a8e1175bSopenharmony_ci
109a8e1175bSopenharmony_ci    switch (dep_id) {
110a8e1175bSopenharmony_ci    __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE
111a8e1175bSopenharmony_ci#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
112a8e1175bSopenharmony_ci        default:
113a8e1175bSopenharmony_ci            break;
114a8e1175bSopenharmony_ci    }
115a8e1175bSopenharmony_ci    return ret;
116a8e1175bSopenharmony_ci}
117a8e1175bSopenharmony_ci
118a8e1175bSopenharmony_ci
119a8e1175bSopenharmony_ci/**
120a8e1175bSopenharmony_ci * \brief       Function pointer type for test function wrappers.
121a8e1175bSopenharmony_ci *
122a8e1175bSopenharmony_ci * A test function wrapper decodes the parameters and passes them to the
123a8e1175bSopenharmony_ci * underlying test function. Both the wrapper and the underlying function
124a8e1175bSopenharmony_ci * return void. Test wrappers assume that they are passed a suitable
125a8e1175bSopenharmony_ci * parameter array and do not perform any error detection.
126a8e1175bSopenharmony_ci *
127a8e1175bSopenharmony_ci * \param param_array   The array of parameters. Each element is a `void *`
128a8e1175bSopenharmony_ci *                      which the wrapper casts to the correct type and
129a8e1175bSopenharmony_ci *                      dereferences. Each wrapper function hard-codes the
130a8e1175bSopenharmony_ci *                      number and types of the parameters.
131a8e1175bSopenharmony_ci */
132a8e1175bSopenharmony_citypedef void (*TestWrapper_t)(void **param_array);
133a8e1175bSopenharmony_ci
134a8e1175bSopenharmony_ci
135a8e1175bSopenharmony_ci/**
136a8e1175bSopenharmony_ci * \brief       Table of test function wrappers. Used by dispatch_test().
137a8e1175bSopenharmony_ci *              This table is populated by script:
138a8e1175bSopenharmony_ci *              __MBEDTLS_TEST_TEMPLATE__GENERATOR_SCRIPT
139a8e1175bSopenharmony_ci *
140a8e1175bSopenharmony_ci */
141a8e1175bSopenharmony_ciTestWrapper_t test_funcs[] =
142a8e1175bSopenharmony_ci{
143a8e1175bSopenharmony_ci    __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE
144a8e1175bSopenharmony_ci#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
145a8e1175bSopenharmony_ci};
146a8e1175bSopenharmony_ci
147a8e1175bSopenharmony_ci/**
148a8e1175bSopenharmony_ci * \brief        Dispatches test functions based on function index.
149a8e1175bSopenharmony_ci *
150a8e1175bSopenharmony_ci * \param func_idx    Test function index.
151a8e1175bSopenharmony_ci * \param params      The array of parameters to pass to the test function.
152a8e1175bSopenharmony_ci *                    It will be decoded by the #TestWrapper_t wrapper function.
153a8e1175bSopenharmony_ci *
154a8e1175bSopenharmony_ci * \return       DISPATCH_TEST_SUCCESS if found
155a8e1175bSopenharmony_ci *               DISPATCH_TEST_FN_NOT_FOUND if not found
156a8e1175bSopenharmony_ci *               DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
157a8e1175bSopenharmony_ci */
158a8e1175bSopenharmony_ciint dispatch_test(size_t func_idx, void **params)
159a8e1175bSopenharmony_ci{
160a8e1175bSopenharmony_ci    int ret = DISPATCH_TEST_SUCCESS;
161a8e1175bSopenharmony_ci    TestWrapper_t fp = NULL;
162a8e1175bSopenharmony_ci
163a8e1175bSopenharmony_ci    if (func_idx < (int) (sizeof(test_funcs) / sizeof(TestWrapper_t))) {
164a8e1175bSopenharmony_ci        fp = test_funcs[func_idx];
165a8e1175bSopenharmony_ci        if (fp) {
166a8e1175bSopenharmony_ci            #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
167a8e1175bSopenharmony_ci            mbedtls_test_enable_insecure_external_rng();
168a8e1175bSopenharmony_ci            #endif
169a8e1175bSopenharmony_ci
170a8e1175bSopenharmony_ci            fp(params);
171a8e1175bSopenharmony_ci
172a8e1175bSopenharmony_ci            #if defined(MBEDTLS_TEST_MUTEX_USAGE)
173a8e1175bSopenharmony_ci            mbedtls_test_mutex_usage_check();
174a8e1175bSopenharmony_ci            #endif /* MBEDTLS_TEST_MUTEX_USAGE */
175a8e1175bSopenharmony_ci        } else {
176a8e1175bSopenharmony_ci            ret = DISPATCH_UNSUPPORTED_SUITE;
177a8e1175bSopenharmony_ci        }
178a8e1175bSopenharmony_ci    } else {
179a8e1175bSopenharmony_ci        ret = DISPATCH_TEST_FN_NOT_FOUND;
180a8e1175bSopenharmony_ci    }
181a8e1175bSopenharmony_ci
182a8e1175bSopenharmony_ci    return ret;
183a8e1175bSopenharmony_ci}
184a8e1175bSopenharmony_ci
185a8e1175bSopenharmony_ci
186a8e1175bSopenharmony_ci/**
187a8e1175bSopenharmony_ci * \brief       Checks if test function is supported in this build-time
188a8e1175bSopenharmony_ci *              configuration.
189a8e1175bSopenharmony_ci *
190a8e1175bSopenharmony_ci * \param func_idx    Test function index.
191a8e1175bSopenharmony_ci *
192a8e1175bSopenharmony_ci * \return       DISPATCH_TEST_SUCCESS if found
193a8e1175bSopenharmony_ci *               DISPATCH_TEST_FN_NOT_FOUND if not found
194a8e1175bSopenharmony_ci *               DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
195a8e1175bSopenharmony_ci */
196a8e1175bSopenharmony_ciint check_test(size_t func_idx)
197a8e1175bSopenharmony_ci{
198a8e1175bSopenharmony_ci    int ret = DISPATCH_TEST_SUCCESS;
199a8e1175bSopenharmony_ci    TestWrapper_t fp = NULL;
200a8e1175bSopenharmony_ci
201a8e1175bSopenharmony_ci    if (func_idx < (int) (sizeof(test_funcs)/sizeof(TestWrapper_t))) {
202a8e1175bSopenharmony_ci        fp = test_funcs[func_idx];
203a8e1175bSopenharmony_ci        if (fp == NULL) {
204a8e1175bSopenharmony_ci            ret = DISPATCH_UNSUPPORTED_SUITE;
205a8e1175bSopenharmony_ci        }
206a8e1175bSopenharmony_ci    } else {
207a8e1175bSopenharmony_ci        ret = DISPATCH_TEST_FN_NOT_FOUND;
208a8e1175bSopenharmony_ci    }
209a8e1175bSopenharmony_ci
210a8e1175bSopenharmony_ci    return ret;
211a8e1175bSopenharmony_ci}
212a8e1175bSopenharmony_ci
213a8e1175bSopenharmony_ci
214a8e1175bSopenharmony_ci__MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE
215a8e1175bSopenharmony_ci
216a8e1175bSopenharmony_ci#line __MBEDTLS_TEST_TEMPLATE__LINE_NO "suites/main_test.function"
217a8e1175bSopenharmony_ci
218a8e1175bSopenharmony_ci/*----------------------------------------------------------------------------*/
219a8e1175bSopenharmony_ci/* Main Test code */
220a8e1175bSopenharmony_ci
221a8e1175bSopenharmony_ci
222a8e1175bSopenharmony_ci/**
223a8e1175bSopenharmony_ci * \brief       Program main. Invokes platform specific execute_tests().
224a8e1175bSopenharmony_ci *
225a8e1175bSopenharmony_ci * \param argc      Command line arguments count.
226a8e1175bSopenharmony_ci * \param argv      Array of command line arguments.
227a8e1175bSopenharmony_ci *
228a8e1175bSopenharmony_ci * \return       Exit code.
229a8e1175bSopenharmony_ci */
230a8e1175bSopenharmony_ciint main(int argc, const char *argv[])
231a8e1175bSopenharmony_ci{
232a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS)
233a8e1175bSopenharmony_ci    extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
234a8e1175bSopenharmony_ci    mbedtls_test_hook_test_fail = &mbedtls_test_fail;
235a8e1175bSopenharmony_ci#if defined(MBEDTLS_ERROR_C)
236a8e1175bSopenharmony_ci    mbedtls_test_hook_error_add = &mbedtls_test_err_add_check;
237a8e1175bSopenharmony_ci#endif
238a8e1175bSopenharmony_ci#endif
239a8e1175bSopenharmony_ci
240a8e1175bSopenharmony_ci    /* Try changing to the directory containing the executable, if
241a8e1175bSopenharmony_ci     * using the default data file. This allows running the executable
242a8e1175bSopenharmony_ci     * from another directory (e.g. the project root) and still access
243a8e1175bSopenharmony_ci     * the .datax file as well as data files used by test cases
244a8e1175bSopenharmony_ci     * (typically from tests/data_files).
245a8e1175bSopenharmony_ci     *
246a8e1175bSopenharmony_ci     * Note that we do this before the platform setup (which may access
247a8e1175bSopenharmony_ci     * files such as a random seed). We also do this before accessing
248a8e1175bSopenharmony_ci     * test-specific files such as the outcome file, which is arguably
249a8e1175bSopenharmony_ci     * not desirable and should be fixed later.
250a8e1175bSopenharmony_ci     */
251a8e1175bSopenharmony_ci    if (argc == 1) {
252a8e1175bSopenharmony_ci        try_chdir_if_supported(argv[0]);
253a8e1175bSopenharmony_ci    }
254a8e1175bSopenharmony_ci
255a8e1175bSopenharmony_ci    int ret = mbedtls_test_platform_setup();
256a8e1175bSopenharmony_ci    if (ret != 0) {
257a8e1175bSopenharmony_ci        mbedtls_fprintf(stderr,
258a8e1175bSopenharmony_ci                        "FATAL: Failed to initialize platform - error %d\n",
259a8e1175bSopenharmony_ci                        ret);
260a8e1175bSopenharmony_ci        return -1;
261a8e1175bSopenharmony_ci    }
262a8e1175bSopenharmony_ci
263a8e1175bSopenharmony_ci    ret = execute_tests(argc, argv);
264a8e1175bSopenharmony_ci    mbedtls_test_platform_teardown();
265a8e1175bSopenharmony_ci    return ret;
266a8e1175bSopenharmony_ci}
267