blob: aafcf5c18bf2cb111d2350d35aa6a163ef0de419 [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.
Dave Rodgman2e680342020-10-15 14:00:40 +0100124 * The allocated memory will be filled with zeros.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200125 *
126 * You must set \p pointer to \c NULL before calling this macro and
127 * put `mbedtls_free( pointer )` in the test's cleanup code.
128 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100129 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200130 * This is usually what we want in tests since API functions are
131 * supposed to accept null pointers when a buffer size is zero.
132 *
133 * This macro expands to an instruction, not an expression.
134 * It may jump to the \c exit label.
135 *
136 * \param pointer An lvalue where the address of the allocated buffer
137 * will be stored.
138 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100139 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200140 * This expression may be evaluated multiple times.
141 *
142 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100143#define ASSERT_ALLOC( pointer, length ) \
144 do \
145 { \
146 TEST_ASSERT( ( pointer ) == NULL ); \
147 if( ( length ) != 0 ) \
148 { \
149 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
150 ( length ) ); \
151 TEST_ASSERT( ( pointer ) != NULL ); \
152 } \
153 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200154 while( 0 )
155
Gilles Peskine292672e2020-01-21 16:20:04 +0100156/** Allocate memory dynamically. If the allocation fails, skip the test case.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100157 *
158 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
Gilles Peskine292672e2020-01-21 16:20:04 +0100159 * fails, it marks the test as skipped rather than failed.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100160 */
161#define ASSERT_ALLOC_WEAK( pointer, length ) \
162 do \
163 { \
164 TEST_ASSERT( ( pointer ) == NULL ); \
165 if( ( length ) != 0 ) \
166 { \
167 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
168 ( length ) ); \
Gilles Peskine292672e2020-01-21 16:20:04 +0100169 TEST_ASSUME( ( pointer ) != NULL ); \
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100170 } \
171 } \
172 while( 0 )
173
Gilles Peskine3c225962018-09-27 13:56:31 +0200174/** Compare two buffers and fail the test case if they differ.
175 *
176 * This macro expands to an instruction, not an expression.
177 * It may jump to the \c exit label.
178 *
179 * \param p1 Pointer to the start of the first buffer.
180 * \param size1 Size of the first buffer in bytes.
181 * This expression may be evaluated multiple times.
182 * \param p2 Pointer to the start of the second buffer.
183 * \param size2 Size of the second buffer in bytes.
184 * This expression may be evaluated multiple times.
185 */
186#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
187 do \
188 { \
189 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
190 if( ( size1 ) != 0 ) \
191 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
192 } \
193 while( 0 )
194
Hanno Beckere69d0152019-07-05 13:31:30 +0100195/**
196 * \brief This macro tests the expression passed to it and skips the
197 * running test if it doesn't evaluate to 'true'.
198 *
199 * \param TEST The test expression to be tested.
200 */
201#define TEST_ASSUME( TEST ) \
202 do { \
203 if( ! (TEST) ) \
204 { \
205 test_skip( #TEST, __LINE__, __FILE__ ); \
206 goto exit; \
207 } \
208 } while( 0 )
209
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
211/**
212 * \brief This macro tests the statement passed to it as a test step or
213 * individual test in a test case. The macro assumes the test will fail
214 * and will generate an error.
215 *
216 * It allows a library function to return a value and tests the return
217 * code on return to confirm the given error code was returned.
218 *
219 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
220 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
221 * expected failure, and the test will pass.
222 *
223 * This macro is intended for negative parameter validation tests,
224 * where the failing function may return an error value or call
225 * MBEDTLS_PARAM_FAILED() to indicate the error.
226 *
227 * \param PARAM_ERROR_VALUE The expected error code.
228 *
229 * \param TEST The test expression to be tested.
230 */
Ronald Cron76883ec2020-07-01 15:05:21 +0200231#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
232 do { \
233 mbedtls_test_param_failed_expect_call( ); \
234 if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
235 ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
236 { \
237 test_fail( #TEST, __LINE__, __FILE__ ); \
238 goto exit; \
239 } \
240 mbedtls_test_param_failed_check_expected_call( ); \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500241 } while( 0 )
242
243/**
244 * \brief This macro tests the statement passed to it as a test step or
245 * individual test in a test case. The macro assumes the test will fail
246 * and will generate an error.
247 *
248 * It assumes the library function under test cannot return a value and
249 * assumes errors can only be indicated byt calls to
250 * MBEDTLS_PARAM_FAILED().
251 *
252 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
253 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
254 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
255 * can be made.
256 *
257 * This macro is intended for negative parameter validation tests,
258 * where the failing function can only return an error by calling
259 * MBEDTLS_PARAM_FAILED() to indicate the error.
260 *
261 * \param TEST The test expression to be tested.
262 */
Ronald Cron579fd282020-07-01 15:17:05 +0200263#define TEST_INVALID_PARAM( TEST ) \
264 do { \
265 memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
266 sizeof( jmp_tmp ) ); \
267 if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
268 { \
269 TEST; \
270 test_fail( #TEST, __LINE__, __FILE__ ); \
271 goto exit; \
272 } \
273 mbedtls_test_param_failed_reset_state( ); \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 } while( 0 )
275#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
276
277/**
278 * \brief This macro tests the statement passed to it as a test step or
279 * individual test in a test case. The macro assumes the test will not fail.
280 *
281 * It assumes the library function under test cannot return a value and
282 * assumes errors can only be indicated by calls to
283 * MBEDTLS_PARAM_FAILED().
284 *
285 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
286 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
287 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
288 * can be made.
289 *
290 * This macro is intended to test that functions returning void
291 * accept all of the parameter values they're supposed to accept - eg
292 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
293 * that's allowed to be NULL happens to be NULL.
294 *
295 * Note: for functions that return something other that void,
296 * checking that they accept all the parameters they're supposed to
297 * accept is best done by using TEST_ASSERT() and checking the return
298 * value as well.
299 *
300 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
301 * disabled, as it makes sense to check that the functions accept all
302 * legal values even if this option is disabled - only in that case,
303 * the test is more about whether the function segfaults than about
304 * whether it invokes MBEDTLS_PARAM_FAILED().
305 *
306 * \param TEST The test expression to be tested.
307 */
308#define TEST_VALID_PARAM( TEST ) \
309 TEST_ASSERT( ( TEST, 1 ) );
310
Gilles Peskine28405302018-09-27 13:52:16 +0200311/** Allocate memory dynamically and fail the test case if this fails.
312 *
313 * You must set \p pointer to \c NULL before calling this macro and
314 * put `mbedtls_free( pointer )` in the test's cleanup code.
315 *
Gilles Peskine6608e712018-11-30 18:51:45 +0100316 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200317 * This is usually what we want in tests since API functions are
318 * supposed to accept null pointers when a buffer size is zero.
319 *
320 * This macro expands to an instruction, not an expression.
321 * It may jump to the \c exit label.
322 *
323 * \param pointer An lvalue where the address of the allocated buffer
324 * will be stored.
325 * This expression may be evaluated multiple times.
Gilles Peskine6608e712018-11-30 18:51:45 +0100326 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200327 * This expression may be evaluated multiple times.
328 *
329 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100330#define ASSERT_ALLOC( pointer, length ) \
331 do \
332 { \
333 TEST_ASSERT( ( pointer ) == NULL ); \
334 if( ( length ) != 0 ) \
335 { \
336 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
337 ( length ) ); \
338 TEST_ASSERT( ( pointer ) != NULL ); \
339 } \
340 } \
Gilles Peskine28405302018-09-27 13:52:16 +0200341 while( 0 )
342
SimonB0269dad2016-02-17 23:34:30 +0000343/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100344/* Global variables */
345
Hanno Beckere69d0152019-07-05 13:31:30 +0100346typedef enum
347{
348 TEST_RESULT_SUCCESS = 0,
349 TEST_RESULT_FAILED,
350 TEST_RESULT_SKIPPED
351} test_result_t;
352
Gilles Peskine47b75402019-09-16 15:12:51 +0200353typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100354{
Hanno Beckere69d0152019-07-05 13:31:30 +0100355 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100356 const char *test;
357 const char *filename;
358 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100359 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100360}
Gilles Peskine47b75402019-09-16 15:12:51 +0200361test_info_t;
362static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100363
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364#if defined(MBEDTLS_CHECK_PARAMS)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500365jmp_buf jmp_tmp;
366#endif
367
SimonB8ca7bc42016-04-17 23:24:50 +0100368/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100369/* Helper flags for complex dependencies */
370
371/* Indicates whether we expect mbedtls_entropy_init
372 * to initialize some strong entropy source. */
373#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
374 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
375 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
376 defined(MBEDTLS_HAVEGE_C) || \
377 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
378 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100379#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100380#endif
381
382
383/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000384/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385
Gilles Peskine56055912019-03-01 14:26:30 +0100386/** Set the test step number for failure reports.
387 *
388 * Call this function to display "step NNN" in addition to the line number
389 * and file name if a test fails. Typically the "step number" is the index
390 * of a for loop but it can be whatever you want.
391 *
392 * \param step The step number to report.
393 */
394void test_set_step( unsigned long step )
395{
396 test_info.step = step;
397}
398
Simon Butcherecff2192018-10-03 16:17:41 +0100399void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500400{
Gilles Peskine349eadc2020-08-29 15:18:23 +0200401 if( test_info.result == TEST_RESULT_FAILED )
402 {
403 /* We've already recorded the test as having failed. Don't
404 * overwrite any previous information about the failure. */
405 return;
406 }
Hanno Beckere69d0152019-07-05 13:31:30 +0100407 test_info.result = TEST_RESULT_FAILED;
408 test_info.test = test;
409 test_info.line_no = line_no;
410 test_info.filename = filename;
411}
412
413void test_skip( const char *test, int line_no, const char* filename )
414{
415 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500416 test_info.test = test;
417 test_info.line_no = line_no;
418 test_info.filename = filename;
419}
420
Janos Follath8ca53b52016-10-05 10:57:49 +0100421#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44067f6e02020-07-30 09:02:27 +0200422static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100423{
gufe44067f6e02020-07-30 09:02:27 +0200424 int out_fd, dup_fd;
425 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100426
gufe44067f6e02020-07-30 09:02:27 +0200427 out_fd = fileno( out_stream );
428 dup_fd = dup( out_fd );
429
430 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100431 {
gufe44067f6e02020-07-30 09:02:27 +0200432 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100433 }
434
gufe44067f6e02020-07-30 09:02:27 +0200435 path_stream = fopen( path, "w" );
436 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100437 {
gufe44067f6e02020-07-30 09:02:27 +0200438 close( dup_fd );
439 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100440 }
441
gufe44067f6e02020-07-30 09:02:27 +0200442 fflush( out_stream );
443 if( dup2( fileno( path_stream ), out_fd ) == -1 )
444 {
445 close( dup_fd );
446 fclose( path_stream );
447 return( -1 );
448 }
449
450 fclose( path_stream );
451 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100452}
453
gufe44067f6e02020-07-30 09:02:27 +0200454static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100455{
gufe44067f6e02020-07-30 09:02:27 +0200456 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100457
gufe44067f6e02020-07-30 09:02:27 +0200458 fflush( out_stream );
459 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100460 {
gufe44067f6e02020-07-30 09:02:27 +0200461 close( out_fd );
462 close( dup_fd );
463 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100464 }
465
gufe44067f6e02020-07-30 09:02:27 +0200466 close( dup_fd );
467 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100468}
Janos Follath8ca53b52016-10-05 10:57:49 +0100469#endif /* __unix__ || __APPLE__ __MACH__ */