blob: 3180a27e6a450c242d3806b8b64f2869c4fb4138 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Ronald Cron4b8b1992020-06-09 13:52:23 +02005#include <test/macros.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +02007#include <test/random.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +02008
Simon Butcheredb7fd92016-05-17 13:35:51 +01009#include <stdlib.h>
10
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020013#else
Rich Evans00ab4702015-02-06 13:43:58 +000014#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010016#define mbedtls_snprintf snprintf
17#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#define mbedtls_free free
19#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010020#define mbedtls_time time
21#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010022#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
23#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020024#endif
25
SimonB0269dad2016-02-17 23:34:30 +000026#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
27#include "mbedtls/memory_buffer_alloc.h"
28#endif
29
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#if defined(MBEDTLS_CHECK_PARAMS)
31#include "mbedtls/platform_util.h"
32#include <setjmp.h>
33#endif
34
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035#ifdef _MSC_VER
36#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010037typedef UINT8 uint8_t;
38typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000039typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000040#define strncasecmp _strnicmp
41#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000042#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020043#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000044#endif
45
Paul Bakker19343182013-08-16 13:31:10 +020046#include <string.h>
47
Janos Follath8ca53b52016-10-05 10:57:49 +010048#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
49#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000050#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010051#endif
SimonB0269dad2016-02-17 23:34:30 +000052
Azim Khand30ca132017-06-09 04:32:58 +010053/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010054typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010055{
56 uint8_t * x;
57 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010058} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010059
SimonB0269dad2016-02-17 23:34:30 +000060/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010061/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000062
Azim Khan62a5d7d2018-06-29 10:02:54 +010063#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
64#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
65#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010066
Azim Khan62a5d7d2018-06-29 10:02:54 +010067#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
68#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
69#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
70#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
71 Only int, string, binary data
72 and integer expressions are
73 allowed */
74#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
75 build */
SimonB0269dad2016-02-17 23:34:30 +000076
SimonB0269dad2016-02-17 23:34:30 +000077/*----------------------------------------------------------------------------*/
78/* Macros */
79
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080/**
81 * \brief This macro tests the expression passed to it as a test step or
82 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020083 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 * It allows a library function to return a value and return an error
85 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020086 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050087 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
88 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
89 * failure.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020090 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050091 * This macro is not suitable for negative parameter validation tests,
92 * as it assumes the test step will not create an error.
93 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +000094 * Failing the test means:
95 * - Mark this test case as failed.
96 * - Print a message identifying the failure.
97 * - Jump to the \c exit label.
98 *
99 * This macro expands to an instruction, not an expression.
100 * It may jump to the \c exit label.
101 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200103 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104#define TEST_ASSERT( TEST ) \
105 do { \
106 if( ! (TEST) ) \
107 { \
108 test_fail( #TEST, __LINE__, __FILE__ ); \
109 goto exit; \
110 } \
SimonB0269dad2016-02-17 23:34:30 +0000111 } while( 0 )
112
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100113/** Evaluate two expressions and fail the test case if they have different
114 * values.
115 *
116 * \param expr1 An expression to evaluate.
117 * \param expr2 The expected value of \p expr1. This can be any
118 * expression, but it is typically a constant.
119 */
120#define TEST_EQUAL( expr1, expr2 ) \
121 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
122
Gilles Peskineb75125c2018-09-27 13:52:16 +0200123/** Allocate memory dynamically and fail the test case if this fails.
124 *
125 * You must set \p pointer to \c NULL before calling this macro and
126 * put `mbedtls_free( pointer )` in the test's cleanup code.
127 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100128 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200129 * This is usually what we want in tests since API functions are
130 * supposed to accept null pointers when a buffer size is zero.
131 *
132 * This macro expands to an instruction, not an expression.
133 * It may jump to the \c exit label.
134 *
135 * \param pointer An lvalue where the address of the allocated buffer
136 * will be stored.
137 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100138 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200139 * This expression may be evaluated multiple times.
140 *
141 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100142#define ASSERT_ALLOC( pointer, length ) \
143 do \
144 { \
145 TEST_ASSERT( ( pointer ) == NULL ); \
146 if( ( length ) != 0 ) \
147 { \
148 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
149 ( length ) ); \
150 TEST_ASSERT( ( pointer ) != NULL ); \
151 } \
152 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200153 while( 0 )
154
Gilles Peskine292672e2020-01-21 16:20:04 +0100155/** Allocate memory dynamically. If the allocation fails, skip the test case.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100156 *
157 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
Gilles Peskine292672e2020-01-21 16:20:04 +0100158 * fails, it marks the test as skipped rather than failed.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100159 */
160#define ASSERT_ALLOC_WEAK( pointer, length ) \
161 do \
162 { \
163 TEST_ASSERT( ( pointer ) == NULL ); \
164 if( ( length ) != 0 ) \
165 { \
166 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
167 ( length ) ); \
Gilles Peskine292672e2020-01-21 16:20:04 +0100168 TEST_ASSUME( ( pointer ) != NULL ); \
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100169 } \
170 } \
171 while( 0 )
172
Gilles Peskine3c225962018-09-27 13:56:31 +0200173/** Compare two buffers and fail the test case if they differ.
174 *
175 * This macro expands to an instruction, not an expression.
176 * It may jump to the \c exit label.
177 *
178 * \param p1 Pointer to the start of the first buffer.
179 * \param size1 Size of the first buffer in bytes.
180 * This expression may be evaluated multiple times.
181 * \param p2 Pointer to the start of the second buffer.
182 * \param size2 Size of the second buffer in bytes.
183 * This expression may be evaluated multiple times.
184 */
185#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
186 do \
187 { \
188 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
189 if( ( size1 ) != 0 ) \
190 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
191 } \
192 while( 0 )
193
Hanno Beckere69d0152019-07-05 13:31:30 +0100194/**
195 * \brief This macro tests the expression passed to it and skips the
196 * running test if it doesn't evaluate to 'true'.
197 *
198 * \param TEST The test expression to be tested.
199 */
200#define TEST_ASSUME( TEST ) \
201 do { \
202 if( ! (TEST) ) \
203 { \
204 test_skip( #TEST, __LINE__, __FILE__ ); \
205 goto exit; \
206 } \
207 } while( 0 )
208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
210/**
211 * \brief This macro tests the statement passed to it as a test step or
212 * individual test in a test case. The macro assumes the test will fail
213 * and will generate an error.
214 *
215 * It allows a library function to return a value and tests the return
216 * code on return to confirm the given error code was returned.
217 *
218 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
219 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
220 * expected failure, and the test will pass.
221 *
222 * This macro is intended for negative parameter validation tests,
223 * where the failing function may return an error value or call
224 * MBEDTLS_PARAM_FAILED() to indicate the error.
225 *
226 * \param PARAM_ERROR_VALUE The expected error code.
227 *
228 * \param TEST The test expression to be tested.
229 */
Ronald Cron76883ec2020-07-01 15:05:21 +0200230#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
231 do { \
232 mbedtls_test_param_failed_expect_call( ); \
233 if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
234 ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
235 { \
236 test_fail( #TEST, __LINE__, __FILE__ ); \
237 goto exit; \
238 } \
239 mbedtls_test_param_failed_check_expected_call( ); \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500240 } while( 0 )
241
242/**
243 * \brief This macro tests the statement passed to it as a test step or
244 * individual test in a test case. The macro assumes the test will fail
245 * and will generate an error.
246 *
247 * It assumes the library function under test cannot return a value and
248 * assumes errors can only be indicated byt calls to
249 * MBEDTLS_PARAM_FAILED().
250 *
251 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
252 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
253 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
254 * can be made.
255 *
256 * This macro is intended for negative parameter validation tests,
257 * where the failing function can only return an error by calling
258 * MBEDTLS_PARAM_FAILED() to indicate the error.
259 *
260 * \param TEST The test expression to be tested.
261 */
Ronald Cron579fd282020-07-01 15:17:05 +0200262#define TEST_INVALID_PARAM( TEST ) \
263 do { \
264 memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
265 sizeof( jmp_tmp ) ); \
266 if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
267 { \
268 TEST; \
269 test_fail( #TEST, __LINE__, __FILE__ ); \
270 goto exit; \
271 } \
272 mbedtls_test_param_failed_reset_state( ); \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273 } while( 0 )
274#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
275
276/**
277 * \brief This macro tests the statement passed to it as a test step or
278 * individual test in a test case. The macro assumes the test will not fail.
279 *
280 * It assumes the library function under test cannot return a value and
281 * assumes errors can only be indicated by calls to
282 * MBEDTLS_PARAM_FAILED().
283 *
284 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
285 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
286 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
287 * can be made.
288 *
289 * This macro is intended to test that functions returning void
290 * accept all of the parameter values they're supposed to accept - eg
291 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
292 * that's allowed to be NULL happens to be NULL.
293 *
294 * Note: for functions that return something other that void,
295 * checking that they accept all the parameters they're supposed to
296 * accept is best done by using TEST_ASSERT() and checking the return
297 * value as well.
298 *
299 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
300 * disabled, as it makes sense to check that the functions accept all
301 * legal values even if this option is disabled - only in that case,
302 * the test is more about whether the function segfaults than about
303 * whether it invokes MBEDTLS_PARAM_FAILED().
304 *
305 * \param TEST The test expression to be tested.
306 */
307#define TEST_VALID_PARAM( TEST ) \
308 TEST_ASSERT( ( TEST, 1 ) );
309
Gilles Peskine28405302018-09-27 13:52:16 +0200310/** Allocate memory dynamically and fail the test case if this fails.
311 *
312 * You must set \p pointer to \c NULL before calling this macro and
313 * put `mbedtls_free( pointer )` in the test's cleanup code.
314 *
Gilles Peskine6608e712018-11-30 18:51:45 +0100315 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200316 * This is usually what we want in tests since API functions are
317 * supposed to accept null pointers when a buffer size is zero.
318 *
319 * This macro expands to an instruction, not an expression.
320 * It may jump to the \c exit label.
321 *
322 * \param pointer An lvalue where the address of the allocated buffer
323 * will be stored.
324 * This expression may be evaluated multiple times.
Gilles Peskine6608e712018-11-30 18:51:45 +0100325 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200326 * This expression may be evaluated multiple times.
327 *
328 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100329#define ASSERT_ALLOC( pointer, length ) \
330 do \
331 { \
332 TEST_ASSERT( ( pointer ) == NULL ); \
333 if( ( length ) != 0 ) \
334 { \
335 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
336 ( length ) ); \
337 TEST_ASSERT( ( pointer ) != NULL ); \
338 } \
339 } \
Gilles Peskine28405302018-09-27 13:52:16 +0200340 while( 0 )
341
SimonB0269dad2016-02-17 23:34:30 +0000342/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100343/* Global variables */
344
Hanno Beckere69d0152019-07-05 13:31:30 +0100345typedef enum
346{
347 TEST_RESULT_SUCCESS = 0,
348 TEST_RESULT_FAILED,
349 TEST_RESULT_SKIPPED
350} test_result_t;
351
Gilles Peskine47b75402019-09-16 15:12:51 +0200352typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100353{
Hanno Beckere69d0152019-07-05 13:31:30 +0100354 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100355 const char *test;
356 const char *filename;
357 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100358 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100359}
Gilles Peskine47b75402019-09-16 15:12:51 +0200360test_info_t;
361static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100362
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363#if defined(MBEDTLS_CHECK_PARAMS)
Ronald Cron4e665872020-06-30 17:44:27 +0200364typedef struct
365{
366 const char *failure_condition;
367 const char *file;
368 int line;
369}
370mbedtls_test_param_failed_location_record_t;
371
372typedef struct
373{
Ronald Cron76883ec2020-07-01 15:05:21 +0200374 uint8_t expected_call;
375 uint8_t expected_call_happened;
376
Ronald Cron579fd282020-07-01 15:17:05 +0200377 jmp_buf state;
378
Ronald Cron4e665872020-06-30 17:44:27 +0200379 mbedtls_test_param_failed_location_record_t location_record;
380}
381param_failed_ctx_t;
382static param_failed_ctx_t param_failed_ctx;
383
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384jmp_buf jmp_tmp;
385#endif
386
SimonB8ca7bc42016-04-17 23:24:50 +0100387/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100388/* Helper flags for complex dependencies */
389
390/* Indicates whether we expect mbedtls_entropy_init
391 * to initialize some strong entropy source. */
392#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
393 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
394 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
395 defined(MBEDTLS_HAVEGE_C) || \
396 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
397 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100398#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100399#endif
400
401
402/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000403/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500404
Gilles Peskine56055912019-03-01 14:26:30 +0100405/** Set the test step number for failure reports.
406 *
407 * Call this function to display "step NNN" in addition to the line number
408 * and file name if a test fails. Typically the "step number" is the index
409 * of a for loop but it can be whatever you want.
410 *
411 * \param step The step number to report.
412 */
413void test_set_step( unsigned long step )
414{
415 test_info.step = step;
416}
417
Simon Butcherecff2192018-10-03 16:17:41 +0100418void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500419{
Hanno Beckere69d0152019-07-05 13:31:30 +0100420 test_info.result = TEST_RESULT_FAILED;
421 test_info.test = test;
422 test_info.line_no = line_no;
423 test_info.filename = filename;
424}
425
426void test_skip( const char *test, int line_no, const char* filename )
427{
428 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 test_info.test = test;
430 test_info.line_no = line_no;
431 test_info.filename = filename;
432}
433
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500434#if defined(MBEDTLS_CHECK_PARAMS)
Ronald Cron4e665872020-06-30 17:44:27 +0200435/**
436 * \brief Get the location record of the last call to
437 * mbedtls_test_param_failed().
438 *
439 * \note The call expectation is set up and active until the next call to
440 * mbedtls_test_param_failed_check_expected_call() or
441 * mbedtls_param_failed() that cancels it.
442 */
443void mbedtls_test_param_failed_get_location_record(
444 mbedtls_test_param_failed_location_record_t *location_record )
445{
446 *location_record = param_failed_ctx.location_record;
447}
448
Ronald Cron76883ec2020-07-01 15:05:21 +0200449/**
450 * \brief State that a call to mbedtls_param_failed() is expected.
451 *
452 * \note The call expectation is set up and active until the next call to
453 * mbedtls_test_param_failed_check_expected_call() or
454 * mbedtls_param_failed that cancel it.
455 */
456void mbedtls_test_param_failed_expect_call( void )
457{
458 param_failed_ctx.expected_call_happened = 0;
459 param_failed_ctx.expected_call = 1;
460}
461
462/**
463 * \brief Check whether mbedtls_param_failed() has been called as expected.
464 *
465 * \note Check whether mbedtls_param_failed() has been called between the
466 * last call to mbedtls_test_param_failed_expect_call() and the call
467 * to this function.
468 *
469 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
470 * mbedtls_param_failed() has been called.
471 * \c -1 Otherwise.
472 */
473int mbedtls_test_param_failed_check_expected_call( void )
474{
475 param_failed_ctx.expected_call = 0;
476
477 if( param_failed_ctx.expected_call_happened != 0 )
478 return( 0 );
479
480 return( -1 );
481}
482
Ronald Cron579fd282020-07-01 15:17:05 +0200483/**
484 * \brief Get a pointer to the object of type jmp_buf holding the execution
485 * state information used by mbedtls_param_failed() to do a long jump.
486 *
487 * \note If a call to mbedtls_param_failed() is not expected in the sense
488 * that there is no call to mbedtls_test_param_failed_expect_call()
489 * preceding it, then mbedtls_param_failed() will try to restore the
490 * execution to the state stored in the jmp_buf object whose address
491 * is returned by the present function.
492 *
493 * \note The returned pointer is of type void* as its type is opaque,
494 * implementation dependent (jmp_buf is an array type not the type of
495 * one element of an array).
496 *
497 * \return Address of the object of type jmp_buf holding the execution state
498 * information used by mbedtls_param_failed() to do a long jump.
499 */
500void* mbedtls_test_param_failed_get_state_buf( void )
501{
502 return &param_failed_ctx.state[0];
503}
504
505/**
506 * \brief Reset the execution state used by mbedtls_param_failed() to do a
507 * long jump.
508 *
509 * \note If a call to mbedtls_param_failed() is not expected in the sense
510 * that there is no call to mbedtls_test_param_failed_expect_call()
511 * preceding it, then mbedtls_param_failed() will try to restore the
512 * execution state that this function reset.
513 *
514 * \note It is recommended to reset the execution state when the state
515 * is not relevant anymore. That way an unexpected call to
516 * mbedtls_param_failed() will not trigger a long jump with
517 * undefined behavior but rather a long jump that will rather fault.
518 */
519void mbedtls_test_param_failed_reset_state( void )
520{
521 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
522}
523
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500524void mbedtls_param_failed( const char *failure_condition,
525 const char *file,
526 int line )
527{
Ronald Cron4e665872020-06-30 17:44:27 +0200528 /* Record the location of the failure */
529 param_failed_ctx.location_record.failure_condition = failure_condition;
530 param_failed_ctx.location_record.file = file;
531 param_failed_ctx.location_record.line = line;
532
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500533 /* If we are testing the callback function... */
Ronald Cron76883ec2020-07-01 15:05:21 +0200534 if( param_failed_ctx.expected_call != 0 )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500535 {
Ronald Cron76883ec2020-07-01 15:05:21 +0200536 param_failed_ctx.expected_call = 0;
537 param_failed_ctx.expected_call_happened = 1;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500538 }
539 else
540 {
Ronald Cron579fd282020-07-01 15:17:05 +0200541 /* ...else try a long jump. If the execution state has not been set-up
542 * or reset then the long jump buffer is all zero's and the call will
543 * with high probability fault, emphasizing there is something to look
544 * at.
545 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500546
Ronald Cron579fd282020-07-01 15:17:05 +0200547 longjmp( param_failed_ctx.state, 1 );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500548 }
549}
550#endif
551
Janos Follath8ca53b52016-10-05 10:57:49 +0100552#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
553static int redirect_output( FILE** out_stream, const char* path )
554{
555 int stdout_fd = dup( fileno( *out_stream ) );
556
557 if( stdout_fd == -1 )
558 {
559 return -1;
560 }
561
562 fflush( *out_stream );
563 fclose( *out_stream );
564 *out_stream = fopen( path, "w" );
565
566 if( *out_stream == NULL )
567 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500568 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100569 return -1;
570 }
571
572 return stdout_fd;
573}
574
575static int restore_output( FILE** out_stream, int old_fd )
576{
577 fflush( *out_stream );
578 fclose( *out_stream );
579
580 *out_stream = fdopen( old_fd, "w" );
581 if( *out_stream == NULL )
582 {
583 return -1;
584 }
585
586 return 0;
587}
Simon Butchere0192962016-10-12 23:07:30 +0100588
Janos Follathe709f7c2016-10-13 11:26:29 +0100589static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100590{
Janos Follathe709f7c2016-10-13 11:26:29 +0100591 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100592}
Janos Follath8ca53b52016-10-05 10:57:49 +0100593#endif /* __unix__ || __APPLE__ __MACH__ */