blob: 450bc2cc3bb2ccfa9a4fc7e23d2b51a61211d8f3 [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
87/** Evaluate two expressions and fail the test case if they have different
88 * values.
89 *
90 * \param expr1 An expression to evaluate.
91 * \param expr2 The expected value of \p expr1. This can be any
92 * expression, but it is typically a constant.
93 */
94#define TEST_EQUAL( expr1, expr2 ) \
95 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
96
97/** Allocate memory dynamically and fail the test case if this fails.
98 * The allocated memory will be filled with zeros.
99 *
100 * You must set \p pointer to \c NULL before calling this macro and
101 * put `mbedtls_free( pointer )` in the test's cleanup code.
102 *
103 * If \p length is zero, the resulting \p pointer will be \c NULL.
104 * This is usually what we want in tests since API functions are
105 * supposed to accept null pointers when a buffer size is zero.
106 *
107 * This macro expands to an instruction, not an expression.
108 * It may jump to the \c exit label.
109 *
110 * \param pointer An lvalue where the address of the allocated buffer
111 * will be stored.
112 * This expression may be evaluated multiple times.
113 * \param length Number of elements to allocate.
114 * This expression may be evaluated multiple times.
115 *
116 */
117#define ASSERT_ALLOC( pointer, length ) \
118 do \
119 { \
120 TEST_ASSERT( ( pointer ) == NULL ); \
121 if( ( length ) != 0 ) \
122 { \
123 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
124 ( length ) ); \
125 TEST_ASSERT( ( pointer ) != NULL ); \
126 } \
127 } \
128 while( 0 )
129
130/** Allocate memory dynamically. If the allocation fails, skip the test case.
131 *
132 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
133 * fails, it marks the test as skipped rather than failed.
134 */
135#define ASSERT_ALLOC_WEAK( pointer, length ) \
136 do \
137 { \
138 TEST_ASSERT( ( pointer ) == NULL ); \
139 if( ( length ) != 0 ) \
140 { \
141 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
142 ( length ) ); \
143 TEST_ASSUME( ( pointer ) != NULL ); \
144 } \
145 } \
146 while( 0 )
147
148/** Compare two buffers and fail the test case if they differ.
149 *
150 * This macro expands to an instruction, not an expression.
151 * It may jump to the \c exit label.
152 *
153 * \param p1 Pointer to the start of the first buffer.
154 * \param size1 Size of the first buffer in bytes.
155 * This expression may be evaluated multiple times.
156 * \param p2 Pointer to the start of the second buffer.
157 * \param size2 Size of the second buffer in bytes.
158 * This expression may be evaluated multiple times.
159 */
160#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
161 do \
162 { \
163 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
164 if( ( size1 ) != 0 ) \
165 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
166 } \
167 while( 0 )
168
169/**
170 * \brief This macro tests the expression passed to it and skips the
171 * running test if it doesn't evaluate to 'true'.
172 *
173 * \param TEST The test expression to be tested.
174 */
175#define TEST_ASSUME( TEST ) \
176 do { \
177 if( ! (TEST) ) \
178 { \
179 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
180 goto exit; \
181 } \
182 } while( 0 )
183
184#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
185/**
186 * \brief This macro tests the statement passed to it as a test step or
187 * individual test in a test case. The macro assumes the test will fail
188 * and will generate an error.
189 *
190 * It allows a library function to return a value and tests the return
191 * code on return to confirm the given error code was returned.
192 *
193 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
194 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
195 * expected failure, and the test will pass.
196 *
197 * This macro is intended for negative parameter validation tests,
198 * where the failing function may return an error value or call
199 * MBEDTLS_PARAM_FAILED() to indicate the error.
200 *
201 * \param PARAM_ERROR_VALUE The expected error code.
202 *
203 * \param TEST The test expression to be tested.
204 */
205#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
206 do { \
207 mbedtls_test_param_failed_expect_call( ); \
208 if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
209 ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
210 { \
211 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
212 goto exit; \
213 } \
214 mbedtls_test_param_failed_check_expected_call( ); \
215 } while( 0 )
216
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 assumes the library function under test cannot return a value and
223 * assumes errors can only be indicated byt calls to
224 * MBEDTLS_PARAM_FAILED().
225 *
226 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
227 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
228 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
229 * can be made.
230 *
231 * This macro is intended for negative parameter validation tests,
232 * where the failing function can only return an error by calling
233 * MBEDTLS_PARAM_FAILED() to indicate the error.
234 *
235 * \param TEST The test expression to be tested.
236 */
237#define TEST_INVALID_PARAM( TEST ) \
238 do { \
239 memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
240 sizeof( jmp_tmp ) ); \
241 if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
242 { \
243 TEST; \
244 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
245 goto exit; \
246 } \
247 mbedtls_test_param_failed_reset_state( ); \
248 } while( 0 )
249#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
250
251/**
252 * \brief This macro tests the statement passed to it as a test step or
253 * individual test in a test case. The macro assumes the test will not fail.
254 *
255 * It assumes the library function under test cannot return a value and
256 * assumes errors can only be indicated by calls to
257 * MBEDTLS_PARAM_FAILED().
258 *
259 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
260 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
261 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
262 * can be made.
263 *
264 * This macro is intended to test that functions returning void
265 * accept all of the parameter values they're supposed to accept - eg
266 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
267 * that's allowed to be NULL happens to be NULL.
268 *
269 * Note: for functions that return something other that void,
270 * checking that they accept all the parameters they're supposed to
271 * accept is best done by using TEST_ASSERT() and checking the return
272 * value as well.
273 *
274 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
275 * disabled, as it makes sense to check that the functions accept all
276 * legal values even if this option is disabled - only in that case,
277 * the test is more about whether the function segfaults than about
278 * whether it invokes MBEDTLS_PARAM_FAILED().
279 *
280 * \param TEST The test expression to be tested.
281 */
282#define TEST_VALID_PARAM( TEST ) \
283 TEST_ASSERT( ( TEST, 1 ) );
284
285/** Allocate memory dynamically and fail the test case if this fails.
286 *
287 * You must set \p pointer to \c NULL before calling this macro and
288 * put `mbedtls_free( pointer )` in the test's cleanup code.
289 *
290 * If \p length is zero, the resulting \p pointer will be \c NULL.
291 * This is usually what we want in tests since API functions are
292 * supposed to accept null pointers when a buffer size is zero.
293 *
294 * This macro expands to an instruction, not an expression.
295 * It may jump to the \c exit label.
296 *
297 * \param pointer An lvalue where the address of the allocated buffer
298 * will be stored.
299 * This expression may be evaluated multiple times.
300 * \param length Number of elements to allocate.
301 * This expression may be evaluated multiple times.
302 *
303 */
304#define ASSERT_ALLOC( pointer, length ) \
305 do \
306 { \
307 TEST_ASSERT( ( pointer ) == NULL ); \
308 if( ( length ) != 0 ) \
309 { \
310 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
311 ( length ) ); \
312 TEST_ASSERT( ( pointer ) != NULL ); \
313 } \
314 } \
315 while( 0 )
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 */