blob: 2d68f7d86c435f875e5e496f4bd31347f1c513b6 [file] [log] [blame]
Ronald Cron4b8b1992020-06-09 13:52:23 +02001/**
2 * \file macros.h
3 *
4 * \brief This file contains generic macros for the purpose of testing.
5 */
6
Bence Szépkúti86974652020-06-15 11:59:37 +02007/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Ronald Cron4b8b1992020-06-09 13:52:23 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Ronald Cron4b8b1992020-06-09 13:52:23 +020022 */
23
24#ifndef TEST_MACROS_H
25#define TEST_MACROS_H
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
Ronald Cron849930a2020-06-03 08:06:47 +020033#include <stdlib.h>
34
35#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#else
38#include <stdio.h>
39#define mbedtls_fprintf fprintf
40#define mbedtls_snprintf snprintf
41#define mbedtls_calloc calloc
42#define mbedtls_free free
43#define mbedtls_exit exit
44#define mbedtls_time time
45#define mbedtls_time_t time_t
46#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
47#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
48#endif
49
50#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
51#include "mbedtls/memory_buffer_alloc.h"
52#endif
53
Chris Jonesa6d155f2021-02-09 12:09:33 +000054/**
55 * \brief This macro tests the expression passed to it as a test step or
56 * individual test in a test case.
57 *
58 * It allows a library function to return a value and return an error
59 * code that can be tested.
60 *
61 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
62 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
63 * failure.
64 *
65 * This macro is not suitable for negative parameter validation tests,
66 * as it assumes the test step will not create an error.
67 *
68 * Failing the test means:
69 * - Mark this test case as failed.
70 * - Print a message identifying the failure.
71 * - Jump to the \c exit label.
72 *
73 * This macro expands to an instruction, not an expression.
74 * It may jump to the \c exit label.
75 *
76 * \param TEST The test expression to be tested.
77 */
78#define TEST_ASSERT( TEST ) \
79 do { \
80 if( ! (TEST) ) \
81 { \
82 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
83 goto exit; \
84 } \
85 } while( 0 )
86
Gilles Peskineb4366492021-04-29 20:28:54 +020087/** Evaluate two integer expressions and fail the test case if they have
88 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000089 *
Gilles Peskineb4366492021-04-29 20:28:54 +020090 * The two expressions should have the same signedness, otherwise the
91 * comparison is not meaningful if the signed value is negative.
92 *
93 * \param expr1 An integral-typed expression to evaluate.
94 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000095 */
Gilles Peskineb4366492021-04-29 20:28:54 +020096#define TEST_EQUAL( expr1, expr2 ) \
97 do { \
98 if( ! mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
99 expr1, expr2 ) ) \
100 goto exit; \
101 } while( 0 )
Chris Jonesa6d155f2021-02-09 12:09:33 +0000102
Gilles Peskine063700d2022-04-13 23:59:52 +0200103/** Evaluate two unsigned integer expressions and fail the test case
104 * if they are not in increasing order (left <= right).
105 *
106 * \param expr1 An integral-typed expression to evaluate.
107 * \param expr2 Another integral-typed expression to evaluate.
108 */
109#define TEST_LE_U( expr1, expr2 ) \
110 do { \
111 if( ! mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
112 expr1, expr2 ) ) \
113 goto exit; \
114 } while( 0 )
115
116/** Evaluate two signed integer expressions and fail the test case
117 * if they are not in increasing order (left <= right).
118 *
119 * \param expr1 An integral-typed expression to evaluate.
120 * \param expr2 Another integral-typed expression to evaluate.
121 */
122#define TEST_LE_S( expr1, expr2 ) \
123 do { \
124 if( ! mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
125 expr1, expr2 ) ) \
126 goto exit; \
127 } while( 0 )
128
Chris Jonesa6d155f2021-02-09 12:09:33 +0000129/** Allocate memory dynamically and fail the test case if this fails.
130 * The allocated memory will be filled with zeros.
131 *
132 * You must set \p pointer to \c NULL before calling this macro and
133 * put `mbedtls_free( pointer )` in the test's cleanup code.
134 *
135 * If \p length is zero, the resulting \p pointer will be \c NULL.
136 * This is usually what we want in tests since API functions are
137 * supposed to accept null pointers when a buffer size is zero.
138 *
139 * This macro expands to an instruction, not an expression.
140 * It may jump to the \c exit label.
141 *
142 * \param pointer An lvalue where the address of the allocated buffer
143 * will be stored.
144 * This expression may be evaluated multiple times.
145 * \param length Number of elements to allocate.
146 * This expression may be evaluated multiple times.
147 *
148 */
149#define ASSERT_ALLOC( pointer, length ) \
150 do \
151 { \
152 TEST_ASSERT( ( pointer ) == NULL ); \
153 if( ( length ) != 0 ) \
154 { \
155 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
156 ( length ) ); \
157 TEST_ASSERT( ( pointer ) != NULL ); \
158 } \
159 } \
160 while( 0 )
161
162/** Allocate memory dynamically. If the allocation fails, skip the test case.
163 *
164 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
165 * fails, it marks the test as skipped rather than failed.
166 */
167#define ASSERT_ALLOC_WEAK( pointer, length ) \
168 do \
169 { \
170 TEST_ASSERT( ( pointer ) == NULL ); \
171 if( ( length ) != 0 ) \
172 { \
173 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
174 ( length ) ); \
175 TEST_ASSUME( ( pointer ) != NULL ); \
176 } \
177 } \
178 while( 0 )
179
180/** Compare two buffers and fail the test case if they differ.
181 *
182 * This macro expands to an instruction, not an expression.
183 * It may jump to the \c exit label.
184 *
185 * \param p1 Pointer to the start of the first buffer.
186 * \param size1 Size of the first buffer in bytes.
187 * This expression may be evaluated multiple times.
188 * \param p2 Pointer to the start of the second buffer.
189 * \param size2 Size of the second buffer in bytes.
190 * This expression may be evaluated multiple times.
191 */
192#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
193 do \
194 { \
195 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
196 if( ( size1 ) != 0 ) \
197 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
198 } \
199 while( 0 )
200
201/**
202 * \brief This macro tests the expression passed to it and skips the
203 * running test if it doesn't evaluate to 'true'.
204 *
205 * \param TEST The test expression to be tested.
206 */
207#define TEST_ASSUME( TEST ) \
208 do { \
209 if( ! (TEST) ) \
210 { \
211 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
212 goto exit; \
213 } \
214 } while( 0 )
215
216#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
217/**
218 * \brief This macro tests the statement passed to it as a test step or
219 * individual test in a test case. The macro assumes the test will fail
220 * and will generate an error.
221 *
222 * It allows a library function to return a value and tests the return
223 * code on return to confirm the given error code was returned.
224 *
225 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
226 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
227 * expected failure, and the test will pass.
228 *
229 * This macro is intended for negative parameter validation tests,
230 * where the failing function may return an error value or call
231 * MBEDTLS_PARAM_FAILED() to indicate the error.
232 *
233 * \param PARAM_ERROR_VALUE The expected error code.
234 *
235 * \param TEST The test expression to be tested.
236 */
237#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
238 do { \
239 mbedtls_test_param_failed_expect_call( ); \
240 if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
241 ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
242 { \
243 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
244 goto exit; \
245 } \
246 mbedtls_test_param_failed_check_expected_call( ); \
247 } while( 0 )
248
249/**
250 * \brief This macro tests the statement passed to it as a test step or
251 * individual test in a test case. The macro assumes the test will fail
252 * and will generate an error.
253 *
254 * It assumes the library function under test cannot return a value and
255 * assumes errors can only be indicated byt calls to
256 * MBEDTLS_PARAM_FAILED().
257 *
258 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
259 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
260 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
261 * can be made.
262 *
263 * This macro is intended for negative parameter validation tests,
264 * where the failing function can only return an error by calling
265 * MBEDTLS_PARAM_FAILED() to indicate the error.
266 *
267 * \param TEST The test expression to be tested.
268 */
269#define TEST_INVALID_PARAM( TEST ) \
270 do { \
271 memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
272 sizeof( jmp_tmp ) ); \
273 if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
274 { \
275 TEST; \
276 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
277 goto exit; \
278 } \
279 mbedtls_test_param_failed_reset_state( ); \
280 } while( 0 )
281#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
282
283/**
284 * \brief This macro tests the statement passed to it as a test step or
285 * individual test in a test case. The macro assumes the test will not fail.
286 *
287 * It assumes the library function under test cannot return a value and
288 * assumes errors can only be indicated by calls to
289 * MBEDTLS_PARAM_FAILED().
290 *
291 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
292 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
293 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
294 * can be made.
295 *
296 * This macro is intended to test that functions returning void
297 * accept all of the parameter values they're supposed to accept - eg
298 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
299 * that's allowed to be NULL happens to be NULL.
300 *
301 * Note: for functions that return something other that void,
302 * checking that they accept all the parameters they're supposed to
303 * accept is best done by using TEST_ASSERT() and checking the return
304 * value as well.
305 *
306 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
307 * disabled, as it makes sense to check that the functions accept all
308 * legal values even if this option is disabled - only in that case,
309 * the test is more about whether the function segfaults than about
310 * whether it invokes MBEDTLS_PARAM_FAILED().
311 *
312 * \param TEST The test expression to be tested.
313 */
314#define TEST_VALID_PARAM( TEST ) \
315 TEST_ASSERT( ( TEST, 1 ) );
316
Ronald Cron849930a2020-06-03 08:06:47 +0200317#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
318{ \
319 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
320 __FILE__, __LINE__, #a ); \
321 mbedtls_exit( 1 ); \
322}
323
Gilles Peskinec86a1652021-02-15 12:17:00 +0100324/** \def ARRAY_LENGTH
325 * Return the number of elements of a static or stack array.
326 *
327 * \param array A value of array (not pointer) type.
328 *
329 * \return The number of elements of the array.
330 */
331/* A correct implementation of ARRAY_LENGTH, but which silently gives
332 * a nonsensical result if called with a pointer rather than an array. */
333#define ARRAY_LENGTH_UNSAFE( array ) \
334 ( sizeof( array ) / sizeof( *( array ) ) )
335
Ronald Cron849930a2020-06-03 08:06:47 +0200336#if defined(__GNUC__)
337/* Test if arg and &(arg)[0] have the same type. This is true if arg is
338 * an array but not if it's a pointer. */
339#define IS_ARRAY_NOT_POINTER( arg ) \
340 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
341 __typeof__( &( arg )[0] ) ) )
Ronald Cron849930a2020-06-03 08:06:47 +0200342/* A compile-time constant with the value 0. If `const_expr` is not a
343 * compile-time constant with a nonzero value, cause a compile-time error. */
344#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +0300345 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Gilles Peskinec86a1652021-02-15 12:17:00 +0100346
Ronald Cron849930a2020-06-03 08:06:47 +0200347/* Return the scalar value `value` (possibly promoted). This is a compile-time
348 * constant if `value` is. `condition` must be a compile-time constant.
349 * If `condition` is false, arrange to cause a compile-time error. */
350#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
351 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
352
Ronald Cron849930a2020-06-03 08:06:47 +0200353#define ARRAY_LENGTH( array ) \
354 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
355 ARRAY_LENGTH_UNSAFE( array ) ) )
356
Gilles Peskinec86a1652021-02-15 12:17:00 +0100357#else
358/* If we aren't sure the compiler supports our non-standard tricks,
359 * fall back to the unsafe implementation. */
360#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
361#endif
362
Ronald Cron849930a2020-06-03 08:06:47 +0200363/** Return the smaller of two values.
364 *
365 * \param x An integer-valued expression without side effects.
366 * \param y An integer-valued expression without side effects.
367 *
368 * \return The smaller of \p x and \p y.
369 */
370#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
371
372/** Return the larger of two values.
373 *
374 * \param x An integer-valued expression without side effects.
375 * \param y An integer-valued expression without side effects.
376 *
377 * \return The larger of \p x and \p y.
378 */
379#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
380
381/*
382 * 32-bit integer manipulation macros (big endian)
383 */
384#ifndef GET_UINT32_BE
385#define GET_UINT32_BE(n,b,i) \
386{ \
387 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
388 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
389 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
390 | ( (uint32_t) (b)[(i) + 3] ); \
391}
392#endif
393
394#ifndef PUT_UINT32_BE
395#define PUT_UINT32_BE(n,b,i) \
396{ \
397 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
398 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
399 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
400 (b)[(i) + 3] = (unsigned char) ( (n) ); \
401}
402#endif
403
Ronald Cron4b8b1992020-06-09 13:52:23 +0200404#endif /* TEST_MACROS_H */