blob: f9de64643aaa92bc43314649abf911693151aaa3 [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
Ronald Cron849930a2020-06-03 08:06:47 +020035#include "mbedtls/platform.h"
Ronald Cron849930a2020-06-03 08:06:47 +020036
37#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
38#include "mbedtls/memory_buffer_alloc.h"
39#endif
40
Chris Jonesa6d155f2021-02-09 12:09:33 +000041/**
42 * \brief This macro tests the expression passed to it as a test step or
43 * individual test in a test case.
44 *
45 * It allows a library function to return a value and return an error
46 * code that can be tested.
47 *
48 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
49 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
50 * failure.
51 *
52 * This macro is not suitable for negative parameter validation tests,
53 * as it assumes the test step will not create an error.
54 *
55 * Failing the test means:
56 * - Mark this test case as failed.
57 * - Print a message identifying the failure.
58 * - Jump to the \c exit label.
59 *
60 * This macro expands to an instruction, not an expression.
61 * It may jump to the \c exit label.
62 *
63 * \param TEST The test expression to be tested.
64 */
65#define TEST_ASSERT( TEST ) \
66 do { \
67 if( ! (TEST) ) \
68 { \
69 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
70 goto exit; \
71 } \
72 } while( 0 )
73
Gilles Peskineb4366492021-04-29 20:28:54 +020074/** Evaluate two integer expressions and fail the test case if they have
75 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000076 *
Gilles Peskineb4366492021-04-29 20:28:54 +020077 * The two expressions should have the same signedness, otherwise the
78 * comparison is not meaningful if the signed value is negative.
79 *
80 * \param expr1 An integral-typed expression to evaluate.
81 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000082 */
Gilles Peskineb4366492021-04-29 20:28:54 +020083#define TEST_EQUAL( expr1, expr2 ) \
84 do { \
85 if( ! mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
86 expr1, expr2 ) ) \
87 goto exit; \
88 } while( 0 )
Chris Jonesa6d155f2021-02-09 12:09:33 +000089
Gilles Peskine063700d2022-04-13 23:59:52 +020090/** Evaluate two unsigned integer expressions and fail the test case
91 * if they are not in increasing order (left <= right).
92 *
93 * \param expr1 An integral-typed expression to evaluate.
94 * \param expr2 Another integral-typed expression to evaluate.
95 */
96#define TEST_LE_U( expr1, expr2 ) \
97 do { \
98 if( ! mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
99 expr1, expr2 ) ) \
100 goto exit; \
101 } while( 0 )
102
103/** Evaluate two signed 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_S( expr1, expr2 ) \
110 do { \
111 if( ! mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
112 expr1, expr2 ) ) \
113 goto exit; \
114 } while( 0 )
115
Chris Jonesa6d155f2021-02-09 12:09:33 +0000116/** Allocate memory dynamically and fail the test case if this fails.
117 * The allocated memory will be filled with zeros.
118 *
119 * You must set \p pointer to \c NULL before calling this macro and
120 * put `mbedtls_free( pointer )` in the test's cleanup code.
121 *
122 * If \p length is zero, the resulting \p pointer will be \c NULL.
123 * This is usually what we want in tests since API functions are
124 * supposed to accept null pointers when a buffer size is zero.
125 *
126 * This macro expands to an instruction, not an expression.
127 * It may jump to the \c exit label.
128 *
129 * \param pointer An lvalue where the address of the allocated buffer
130 * will be stored.
131 * This expression may be evaluated multiple times.
132 * \param length Number of elements to allocate.
133 * This expression may be evaluated multiple times.
134 *
135 */
136#define ASSERT_ALLOC( pointer, length ) \
137 do \
138 { \
139 TEST_ASSERT( ( pointer ) == NULL ); \
140 if( ( length ) != 0 ) \
141 { \
142 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
143 ( length ) ); \
144 TEST_ASSERT( ( pointer ) != NULL ); \
145 } \
146 } \
147 while( 0 )
148
149/** Allocate memory dynamically. If the allocation fails, skip the test case.
150 *
151 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
152 * fails, it marks the test as skipped rather than failed.
153 */
154#define ASSERT_ALLOC_WEAK( pointer, length ) \
155 do \
156 { \
157 TEST_ASSERT( ( pointer ) == NULL ); \
158 if( ( length ) != 0 ) \
159 { \
160 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
161 ( length ) ); \
162 TEST_ASSUME( ( pointer ) != NULL ); \
163 } \
164 } \
165 while( 0 )
166
167/** Compare two buffers and fail the test case if they differ.
168 *
169 * This macro expands to an instruction, not an expression.
170 * It may jump to the \c exit label.
171 *
172 * \param p1 Pointer to the start of the first buffer.
173 * \param size1 Size of the first buffer in bytes.
174 * This expression may be evaluated multiple times.
175 * \param p2 Pointer to the start of the second buffer.
176 * \param size2 Size of the second buffer in bytes.
177 * This expression may be evaluated multiple times.
178 */
179#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
180 do \
181 { \
182 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
183 if( ( size1 ) != 0 ) \
184 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
185 } \
186 while( 0 )
187
188/**
189 * \brief This macro tests the expression passed to it and skips the
190 * running test if it doesn't evaluate to 'true'.
191 *
192 * \param TEST The test expression to be tested.
193 */
194#define TEST_ASSUME( TEST ) \
195 do { \
196 if( ! (TEST) ) \
197 { \
198 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
199 goto exit; \
200 } \
201 } while( 0 )
202
203#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
204/**
205 * \brief This macro tests the statement passed to it as a test step or
206 * individual test in a test case. The macro assumes the test will fail
207 * and will generate an error.
208 *
209 * It allows a library function to return a value and tests the return
210 * code on return to confirm the given error code was returned.
211 *
212 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
213 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
214 * expected failure, and the test will pass.
215 *
216 * This macro is intended for negative parameter validation tests,
217 * where the failing function may return an error value or call
218 * MBEDTLS_PARAM_FAILED() to indicate the error.
219 *
220 * \param PARAM_ERROR_VALUE The expected error code.
221 *
222 * \param TEST The test expression to be tested.
223 */
224#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
225 do { \
226 mbedtls_test_param_failed_expect_call( ); \
227 if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
228 ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
229 { \
230 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
231 goto exit; \
232 } \
233 mbedtls_test_param_failed_check_expected_call( ); \
234 } while( 0 )
235
236/**
237 * \brief This macro tests the statement passed to it as a test step or
238 * individual test in a test case. The macro assumes the test will fail
239 * and will generate an error.
240 *
241 * It assumes the library function under test cannot return a value and
242 * assumes errors can only be indicated byt calls to
243 * MBEDTLS_PARAM_FAILED().
244 *
245 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
246 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
247 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
248 * can be made.
249 *
250 * This macro is intended for negative parameter validation tests,
251 * where the failing function can only return an error by calling
252 * MBEDTLS_PARAM_FAILED() to indicate the error.
253 *
254 * \param TEST The test expression to be tested.
255 */
256#define TEST_INVALID_PARAM( TEST ) \
257 do { \
258 memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
259 sizeof( jmp_tmp ) ); \
260 if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
261 { \
262 TEST; \
263 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
264 goto exit; \
265 } \
266 mbedtls_test_param_failed_reset_state( ); \
267 } while( 0 )
268#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
269
270/**
271 * \brief This macro tests the statement passed to it as a test step or
272 * individual test in a test case. The macro assumes the test will not fail.
273 *
274 * It assumes the library function under test cannot return a value and
275 * assumes errors can only be indicated by calls to
276 * MBEDTLS_PARAM_FAILED().
277 *
278 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
279 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
280 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
281 * can be made.
282 *
283 * This macro is intended to test that functions returning void
284 * accept all of the parameter values they're supposed to accept - eg
285 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
286 * that's allowed to be NULL happens to be NULL.
287 *
288 * Note: for functions that return something other that void,
289 * checking that they accept all the parameters they're supposed to
290 * accept is best done by using TEST_ASSERT() and checking the return
291 * value as well.
292 *
293 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
294 * disabled, as it makes sense to check that the functions accept all
295 * legal values even if this option is disabled - only in that case,
296 * the test is more about whether the function segfaults than about
297 * whether it invokes MBEDTLS_PARAM_FAILED().
298 *
299 * \param TEST The test expression to be tested.
300 */
301#define TEST_VALID_PARAM( TEST ) \
302 TEST_ASSERT( ( TEST, 1 ) );
303
Ronald Cron849930a2020-06-03 08:06:47 +0200304#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
305{ \
306 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
307 __FILE__, __LINE__, #a ); \
308 mbedtls_exit( 1 ); \
309}
310
Gilles Peskinec86a1652021-02-15 12:17:00 +0100311/** \def ARRAY_LENGTH
312 * Return the number of elements of a static or stack array.
313 *
314 * \param array A value of array (not pointer) type.
315 *
316 * \return The number of elements of the array.
317 */
318/* A correct implementation of ARRAY_LENGTH, but which silently gives
319 * a nonsensical result if called with a pointer rather than an array. */
320#define ARRAY_LENGTH_UNSAFE( array ) \
321 ( sizeof( array ) / sizeof( *( array ) ) )
322
Ronald Cron849930a2020-06-03 08:06:47 +0200323#if defined(__GNUC__)
324/* Test if arg and &(arg)[0] have the same type. This is true if arg is
325 * an array but not if it's a pointer. */
326#define IS_ARRAY_NOT_POINTER( arg ) \
327 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
328 __typeof__( &( arg )[0] ) ) )
Ronald Cron849930a2020-06-03 08:06:47 +0200329/* A compile-time constant with the value 0. If `const_expr` is not a
330 * compile-time constant with a nonzero value, cause a compile-time error. */
331#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +0300332 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Gilles Peskinec86a1652021-02-15 12:17:00 +0100333
Ronald Cron849930a2020-06-03 08:06:47 +0200334/* Return the scalar value `value` (possibly promoted). This is a compile-time
335 * constant if `value` is. `condition` must be a compile-time constant.
336 * If `condition` is false, arrange to cause a compile-time error. */
337#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
338 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
339
Ronald Cron849930a2020-06-03 08:06:47 +0200340#define ARRAY_LENGTH( array ) \
341 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
342 ARRAY_LENGTH_UNSAFE( array ) ) )
343
Gilles Peskinec86a1652021-02-15 12:17:00 +0100344#else
345/* If we aren't sure the compiler supports our non-standard tricks,
346 * fall back to the unsafe implementation. */
347#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
348#endif
349
Ronald Cron849930a2020-06-03 08:06:47 +0200350/** Return the smaller of two values.
351 *
352 * \param x An integer-valued expression without side effects.
353 * \param y An integer-valued expression without side effects.
354 *
355 * \return The smaller of \p x and \p y.
356 */
357#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
358
359/** Return the larger of two values.
360 *
361 * \param x An integer-valued expression without side effects.
362 * \param y An integer-valued expression without side effects.
363 *
364 * \return The larger of \p x and \p y.
365 */
366#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
367
368/*
369 * 32-bit integer manipulation macros (big endian)
370 */
371#ifndef GET_UINT32_BE
372#define GET_UINT32_BE(n,b,i) \
373{ \
374 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
375 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
376 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
377 | ( (uint32_t) (b)[(i) + 3] ); \
378}
379#endif
380
381#ifndef PUT_UINT32_BE
382#define PUT_UINT32_BE(n,b,i) \
383{ \
384 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
385 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
386 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
387 (b)[(i) + 3] = (unsigned char) ( (n) ); \
388}
389#endif
390
Ronald Cron4b8b1992020-06-09 13:52:23 +0200391#endif /* TEST_MACROS_H */