blob: 2414057f7b328ea69878cde470957f25c6cc6959 [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 */
262#define TEST_INVALID_PARAM( TEST ) \
263 do { \
264 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
265 if( setjmp( param_fail_jmp ) == 0 ) \
266 { \
267 TEST; \
268 test_fail( #TEST, __LINE__, __FILE__ ); \
269 goto exit; \
270 } \
271 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
272 } while( 0 )
273#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
274
275/**
276 * \brief This macro tests the statement passed to it as a test step or
277 * individual test in a test case. The macro assumes the test will not fail.
278 *
279 * It assumes the library function under test cannot return a value and
280 * assumes errors can only be indicated by calls to
281 * MBEDTLS_PARAM_FAILED().
282 *
283 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
284 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
285 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
286 * can be made.
287 *
288 * This macro is intended to test that functions returning void
289 * accept all of the parameter values they're supposed to accept - eg
290 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
291 * that's allowed to be NULL happens to be NULL.
292 *
293 * Note: for functions that return something other that void,
294 * checking that they accept all the parameters they're supposed to
295 * accept is best done by using TEST_ASSERT() and checking the return
296 * value as well.
297 *
298 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
299 * disabled, as it makes sense to check that the functions accept all
300 * legal values even if this option is disabled - only in that case,
301 * the test is more about whether the function segfaults than about
302 * whether it invokes MBEDTLS_PARAM_FAILED().
303 *
304 * \param TEST The test expression to be tested.
305 */
306#define TEST_VALID_PARAM( TEST ) \
307 TEST_ASSERT( ( TEST, 1 ) );
308
Gilles Peskine28405302018-09-27 13:52:16 +0200309/** Allocate memory dynamically and fail the test case if this fails.
310 *
311 * You must set \p pointer to \c NULL before calling this macro and
312 * put `mbedtls_free( pointer )` in the test's cleanup code.
313 *
Gilles Peskine6608e712018-11-30 18:51:45 +0100314 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200315 * This is usually what we want in tests since API functions are
316 * supposed to accept null pointers when a buffer size is zero.
317 *
318 * This macro expands to an instruction, not an expression.
319 * It may jump to the \c exit label.
320 *
321 * \param pointer An lvalue where the address of the allocated buffer
322 * will be stored.
323 * This expression may be evaluated multiple times.
Gilles Peskine6608e712018-11-30 18:51:45 +0100324 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200325 * This expression may be evaluated multiple times.
326 *
327 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100328#define ASSERT_ALLOC( pointer, length ) \
329 do \
330 { \
331 TEST_ASSERT( ( pointer ) == NULL ); \
332 if( ( length ) != 0 ) \
333 { \
334 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
335 ( length ) ); \
336 TEST_ASSERT( ( pointer ) != NULL ); \
337 } \
338 } \
Gilles Peskine28405302018-09-27 13:52:16 +0200339 while( 0 )
340
SimonB0269dad2016-02-17 23:34:30 +0000341/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100342/* Global variables */
343
Hanno Beckere69d0152019-07-05 13:31:30 +0100344typedef enum
345{
346 TEST_RESULT_SUCCESS = 0,
347 TEST_RESULT_FAILED,
348 TEST_RESULT_SKIPPED
349} test_result_t;
350
Gilles Peskine47b75402019-09-16 15:12:51 +0200351typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100352{
Hanno Beckere69d0152019-07-05 13:31:30 +0100353 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100354 const char *test;
355 const char *filename;
356 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100357 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100358}
Gilles Peskine47b75402019-09-16 15:12:51 +0200359test_info_t;
360static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100361
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500362#if defined(MBEDTLS_CHECK_PARAMS)
Ronald Cron4e665872020-06-30 17:44:27 +0200363typedef struct
364{
365 const char *failure_condition;
366 const char *file;
367 int line;
368}
369mbedtls_test_param_failed_location_record_t;
370
371typedef struct
372{
Ronald Cron76883ec2020-07-01 15:05:21 +0200373 uint8_t expected_call;
374 uint8_t expected_call_happened;
375
Ronald Cron4e665872020-06-30 17:44:27 +0200376 mbedtls_test_param_failed_location_record_t location_record;
377}
378param_failed_ctx_t;
379static param_failed_ctx_t param_failed_ctx;
380
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500381jmp_buf param_fail_jmp;
382jmp_buf jmp_tmp;
383#endif
384
SimonB8ca7bc42016-04-17 23:24:50 +0100385/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100386/* Helper flags for complex dependencies */
387
388/* Indicates whether we expect mbedtls_entropy_init
389 * to initialize some strong entropy source. */
390#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
391 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
392 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
393 defined(MBEDTLS_HAVEGE_C) || \
394 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
395 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100396#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100397#endif
398
399
400/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000401/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500402
Gilles Peskine56055912019-03-01 14:26:30 +0100403/** Set the test step number for failure reports.
404 *
405 * Call this function to display "step NNN" in addition to the line number
406 * and file name if a test fails. Typically the "step number" is the index
407 * of a for loop but it can be whatever you want.
408 *
409 * \param step The step number to report.
410 */
411void test_set_step( unsigned long step )
412{
413 test_info.step = step;
414}
415
Simon Butcherecff2192018-10-03 16:17:41 +0100416void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417{
Hanno Beckere69d0152019-07-05 13:31:30 +0100418 test_info.result = TEST_RESULT_FAILED;
419 test_info.test = test;
420 test_info.line_no = line_no;
421 test_info.filename = filename;
422}
423
424void test_skip( const char *test, int line_no, const char* filename )
425{
426 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 test_info.test = test;
428 test_info.line_no = line_no;
429 test_info.filename = filename;
430}
431
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500432#if defined(MBEDTLS_CHECK_PARAMS)
Ronald Cron4e665872020-06-30 17:44:27 +0200433/**
434 * \brief Get the location record of the last call to
435 * mbedtls_test_param_failed().
436 *
437 * \note The call expectation is set up and active until the next call to
438 * mbedtls_test_param_failed_check_expected_call() or
439 * mbedtls_param_failed() that cancels it.
440 */
441void mbedtls_test_param_failed_get_location_record(
442 mbedtls_test_param_failed_location_record_t *location_record )
443{
444 *location_record = param_failed_ctx.location_record;
445}
446
Ronald Cron76883ec2020-07-01 15:05:21 +0200447/**
448 * \brief State that a call to mbedtls_param_failed() is expected.
449 *
450 * \note The call expectation is set up and active until the next call to
451 * mbedtls_test_param_failed_check_expected_call() or
452 * mbedtls_param_failed that cancel it.
453 */
454void mbedtls_test_param_failed_expect_call( void )
455{
456 param_failed_ctx.expected_call_happened = 0;
457 param_failed_ctx.expected_call = 1;
458}
459
460/**
461 * \brief Check whether mbedtls_param_failed() has been called as expected.
462 *
463 * \note Check whether mbedtls_param_failed() has been called between the
464 * last call to mbedtls_test_param_failed_expect_call() and the call
465 * to this function.
466 *
467 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
468 * mbedtls_param_failed() has been called.
469 * \c -1 Otherwise.
470 */
471int mbedtls_test_param_failed_check_expected_call( void )
472{
473 param_failed_ctx.expected_call = 0;
474
475 if( param_failed_ctx.expected_call_happened != 0 )
476 return( 0 );
477
478 return( -1 );
479}
480
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500481void mbedtls_param_failed( const char *failure_condition,
482 const char *file,
483 int line )
484{
Ronald Cron4e665872020-06-30 17:44:27 +0200485 /* Record the location of the failure */
486 param_failed_ctx.location_record.failure_condition = failure_condition;
487 param_failed_ctx.location_record.file = file;
488 param_failed_ctx.location_record.line = line;
489
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500490 /* If we are testing the callback function... */
Ronald Cron76883ec2020-07-01 15:05:21 +0200491 if( param_failed_ctx.expected_call != 0 )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500492 {
Ronald Cron76883ec2020-07-01 15:05:21 +0200493 param_failed_ctx.expected_call = 0;
494 param_failed_ctx.expected_call_happened = 1;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495 }
496 else
497 {
498 /* ...else we treat this as an error */
499
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500500 longjmp( param_fail_jmp, 1 );
501 }
502}
503#endif
504
Janos Follath8ca53b52016-10-05 10:57:49 +0100505#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
506static int redirect_output( FILE** out_stream, const char* path )
507{
508 int stdout_fd = dup( fileno( *out_stream ) );
509
510 if( stdout_fd == -1 )
511 {
512 return -1;
513 }
514
515 fflush( *out_stream );
516 fclose( *out_stream );
517 *out_stream = fopen( path, "w" );
518
519 if( *out_stream == NULL )
520 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500521 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100522 return -1;
523 }
524
525 return stdout_fd;
526}
527
528static int restore_output( FILE** out_stream, int old_fd )
529{
530 fflush( *out_stream );
531 fclose( *out_stream );
532
533 *out_stream = fdopen( old_fd, "w" );
534 if( *out_stream == NULL )
535 {
536 return -1;
537 }
538
539 return 0;
540}
Simon Butchere0192962016-10-12 23:07:30 +0100541
Janos Follathe709f7c2016-10-13 11:26:29 +0100542static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100543{
Janos Follathe709f7c2016-10-13 11:26:29 +0100544 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100545}
Janos Follath8ca53b52016-10-05 10:57:49 +0100546#endif /* __unix__ || __APPLE__ __MACH__ */