blob: cad39aacaa28875214f2454fca3beac3e464c553 [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 *
Chris Jonesa6d155f2021-02-09 12:09:33 +000061 * This macro is not suitable for negative parameter validation tests,
62 * as it assumes the test step will not create an error.
63 *
64 * Failing the test means:
65 * - Mark this test case as failed.
66 * - Print a message identifying the failure.
67 * - Jump to the \c exit label.
68 *
69 * This macro expands to an instruction, not an expression.
70 * It may jump to the \c exit label.
71 *
72 * \param TEST The test expression to be tested.
73 */
74#define TEST_ASSERT( TEST ) \
75 do { \
76 if( ! (TEST) ) \
77 { \
78 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
79 goto exit; \
80 } \
81 } while( 0 )
82
83/** Evaluate two expressions and fail the test case if they have different
84 * values.
85 *
86 * \param expr1 An expression to evaluate.
87 * \param expr2 The expected value of \p expr1. This can be any
88 * expression, but it is typically a constant.
89 */
90#define TEST_EQUAL( expr1, expr2 ) \
91 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
92
93/** Allocate memory dynamically and fail the test case if this fails.
94 * The allocated memory will be filled with zeros.
95 *
96 * You must set \p pointer to \c NULL before calling this macro and
97 * put `mbedtls_free( pointer )` in the test's cleanup code.
98 *
99 * If \p length is zero, the resulting \p pointer will be \c NULL.
100 * This is usually what we want in tests since API functions are
101 * supposed to accept null pointers when a buffer size is zero.
102 *
103 * This macro expands to an instruction, not an expression.
104 * It may jump to the \c exit label.
105 *
106 * \param pointer An lvalue where the address of the allocated buffer
107 * will be stored.
108 * This expression may be evaluated multiple times.
109 * \param length Number of elements to allocate.
110 * This expression may be evaluated multiple times.
111 *
112 */
113#define ASSERT_ALLOC( pointer, length ) \
114 do \
115 { \
116 TEST_ASSERT( ( pointer ) == NULL ); \
117 if( ( length ) != 0 ) \
118 { \
119 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
120 ( length ) ); \
121 TEST_ASSERT( ( pointer ) != NULL ); \
122 } \
123 } \
124 while( 0 )
125
126/** Allocate memory dynamically. If the allocation fails, skip the test case.
127 *
128 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
129 * fails, it marks the test as skipped rather than failed.
130 */
131#define ASSERT_ALLOC_WEAK( pointer, length ) \
132 do \
133 { \
134 TEST_ASSERT( ( pointer ) == NULL ); \
135 if( ( length ) != 0 ) \
136 { \
137 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
138 ( length ) ); \
139 TEST_ASSUME( ( pointer ) != NULL ); \
140 } \
141 } \
142 while( 0 )
143
144/** Compare two buffers and fail the test case if they differ.
145 *
146 * This macro expands to an instruction, not an expression.
147 * It may jump to the \c exit label.
148 *
149 * \param p1 Pointer to the start of the first buffer.
150 * \param size1 Size of the first buffer in bytes.
151 * This expression may be evaluated multiple times.
152 * \param p2 Pointer to the start of the second buffer.
153 * \param size2 Size of the second buffer in bytes.
154 * This expression may be evaluated multiple times.
155 */
156#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
157 do \
158 { \
159 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
160 if( ( size1 ) != 0 ) \
161 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
162 } \
163 while( 0 )
164
165/**
166 * \brief This macro tests the expression passed to it and skips the
167 * running test if it doesn't evaluate to 'true'.
168 *
169 * \param TEST The test expression to be tested.
170 */
171#define TEST_ASSUME( TEST ) \
172 do { \
173 if( ! (TEST) ) \
174 { \
175 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
176 goto exit; \
177 } \
178 } while( 0 )
179
180#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
Ronald Cron875b5fb2021-05-21 08:50:00 +0200181#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
182 do { if( ( TEST ) != ( PARAM_ERR_VALUE ) ) goto exit; } while( 0 )
Chris Jonesa6d155f2021-02-09 12:09:33 +0000183
Ronald Cron875b5fb2021-05-21 08:50:00 +0200184#define TEST_INVALID_PARAM( TEST ) \
185 do { TEST; } while( 0 )
Chris Jonesa6d155f2021-02-09 12:09:33 +0000186#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
187
188/**
189 * \brief This macro tests the statement passed to it as a test step or
190 * individual test in a test case. The macro assumes the test will not fail.
191 *
192 * It assumes the library function under test cannot return a value and
193 * assumes errors can only be indicated by calls to
194 * MBEDTLS_PARAM_FAILED().
195 *
Chris Jonesa6d155f2021-02-09 12:09:33 +0000196 * This macro is intended to test that functions returning void
197 * accept all of the parameter values they're supposed to accept - eg
198 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
199 * that's allowed to be NULL happens to be NULL.
200 *
201 * Note: for functions that return something other that void,
202 * checking that they accept all the parameters they're supposed to
203 * accept is best done by using TEST_ASSERT() and checking the return
204 * value as well.
205 *
Chris Jonesa6d155f2021-02-09 12:09:33 +0000206 * \param TEST The test expression to be tested.
207 */
208#define TEST_VALID_PARAM( TEST ) \
209 TEST_ASSERT( ( TEST, 1 ) );
210
211/** Allocate memory dynamically and fail the test case if this fails.
212 *
213 * You must set \p pointer to \c NULL before calling this macro and
214 * put `mbedtls_free( pointer )` in the test's cleanup code.
215 *
216 * If \p length is zero, the resulting \p pointer will be \c NULL.
217 * This is usually what we want in tests since API functions are
218 * supposed to accept null pointers when a buffer size is zero.
219 *
220 * This macro expands to an instruction, not an expression.
221 * It may jump to the \c exit label.
222 *
223 * \param pointer An lvalue where the address of the allocated buffer
224 * will be stored.
225 * This expression may be evaluated multiple times.
226 * \param length Number of elements to allocate.
227 * This expression may be evaluated multiple times.
228 *
229 */
230#define ASSERT_ALLOC( pointer, length ) \
231 do \
232 { \
233 TEST_ASSERT( ( pointer ) == NULL ); \
234 if( ( length ) != 0 ) \
235 { \
236 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
237 ( length ) ); \
238 TEST_ASSERT( ( pointer ) != NULL ); \
239 } \
240 } \
241 while( 0 )
242
Ronald Cron849930a2020-06-03 08:06:47 +0200243#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
244{ \
245 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
246 __FILE__, __LINE__, #a ); \
247 mbedtls_exit( 1 ); \
248}
249
Gilles Peskinec86a1652021-02-15 12:17:00 +0100250/** \def ARRAY_LENGTH
251 * Return the number of elements of a static or stack array.
252 *
253 * \param array A value of array (not pointer) type.
254 *
255 * \return The number of elements of the array.
256 */
257/* A correct implementation of ARRAY_LENGTH, but which silently gives
258 * a nonsensical result if called with a pointer rather than an array. */
259#define ARRAY_LENGTH_UNSAFE( array ) \
260 ( sizeof( array ) / sizeof( *( array ) ) )
261
Ronald Cron849930a2020-06-03 08:06:47 +0200262#if defined(__GNUC__)
263/* Test if arg and &(arg)[0] have the same type. This is true if arg is
264 * an array but not if it's a pointer. */
265#define IS_ARRAY_NOT_POINTER( arg ) \
266 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
267 __typeof__( &( arg )[0] ) ) )
Ronald Cron849930a2020-06-03 08:06:47 +0200268/* A compile-time constant with the value 0. If `const_expr` is not a
269 * compile-time constant with a nonzero value, cause a compile-time error. */
270#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +0300271 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Gilles Peskinec86a1652021-02-15 12:17:00 +0100272
Ronald Cron849930a2020-06-03 08:06:47 +0200273/* Return the scalar value `value` (possibly promoted). This is a compile-time
274 * constant if `value` is. `condition` must be a compile-time constant.
275 * If `condition` is false, arrange to cause a compile-time error. */
276#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
277 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
278
Ronald Cron849930a2020-06-03 08:06:47 +0200279#define ARRAY_LENGTH( array ) \
280 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
281 ARRAY_LENGTH_UNSAFE( array ) ) )
282
Gilles Peskinec86a1652021-02-15 12:17:00 +0100283#else
284/* If we aren't sure the compiler supports our non-standard tricks,
285 * fall back to the unsafe implementation. */
286#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
287#endif
288
Ronald Cron849930a2020-06-03 08:06:47 +0200289/** Return the smaller of two values.
290 *
291 * \param x An integer-valued expression without side effects.
292 * \param y An integer-valued expression without side effects.
293 *
294 * \return The smaller of \p x and \p y.
295 */
296#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
297
298/** Return the larger of two values.
299 *
300 * \param x An integer-valued expression without side effects.
301 * \param y An integer-valued expression without side effects.
302 *
303 * \return The larger of \p x and \p y.
304 */
305#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
306
307/*
308 * 32-bit integer manipulation macros (big endian)
309 */
310#ifndef GET_UINT32_BE
311#define GET_UINT32_BE(n,b,i) \
312{ \
313 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
314 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
315 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
316 | ( (uint32_t) (b)[(i) + 3] ); \
317}
318#endif
319
320#ifndef PUT_UINT32_BE
321#define PUT_UINT32_BE(n,b,i) \
322{ \
323 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
324 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
325 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
326 (b)[(i) + 3] = (unsigned char) ( (n) ); \
327}
328#endif
329
Ronald Cron4b8b1992020-06-09 13:52:23 +0200330#endif /* TEST_MACROS_H */