blob: 443b58a292f2d6e8b4e29e3f84ab902b5649ed1a [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
Chris Jonesa6d155f2021-02-09 12:09:33 +0000180/**
181 * \brief This macro tests the statement passed to it as a test step or
182 * individual test in a test case. The macro assumes the test will not fail.
183 *
TRodziewiczb1c23252021-05-17 18:22:06 +0200184 * It assumes the library function under test cannot return a value.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000185 *
Chris Jonesa6d155f2021-02-09 12:09:33 +0000186 * This macro is intended to test that functions returning void
TRodziewiczb1c23252021-05-17 18:22:06 +0200187 * accept all of the parameter values they're supposed to accept.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000188 *
189 * Note: for functions that return something other that void,
190 * checking that they accept all the parameters they're supposed to
191 * accept is best done by using TEST_ASSERT() and checking the return
192 * value as well.
193 *
Chris Jonesa6d155f2021-02-09 12:09:33 +0000194 * \param TEST The test expression to be tested.
195 */
196#define TEST_VALID_PARAM( TEST ) \
197 TEST_ASSERT( ( TEST, 1 ) );
198
199/** Allocate memory dynamically and fail the test case if this fails.
200 *
201 * You must set \p pointer to \c NULL before calling this macro and
202 * put `mbedtls_free( pointer )` in the test's cleanup code.
203 *
204 * If \p length is zero, the resulting \p pointer will be \c NULL.
205 * This is usually what we want in tests since API functions are
206 * supposed to accept null pointers when a buffer size is zero.
207 *
208 * This macro expands to an instruction, not an expression.
209 * It may jump to the \c exit label.
210 *
211 * \param pointer An lvalue where the address of the allocated buffer
212 * will be stored.
213 * This expression may be evaluated multiple times.
214 * \param length Number of elements to allocate.
215 * This expression may be evaluated multiple times.
216 *
217 */
218#define ASSERT_ALLOC( pointer, length ) \
219 do \
220 { \
221 TEST_ASSERT( ( pointer ) == NULL ); \
222 if( ( length ) != 0 ) \
223 { \
224 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
225 ( length ) ); \
226 TEST_ASSERT( ( pointer ) != NULL ); \
227 } \
228 } \
229 while( 0 )
230
Ronald Cron849930a2020-06-03 08:06:47 +0200231#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
232{ \
233 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
234 __FILE__, __LINE__, #a ); \
235 mbedtls_exit( 1 ); \
236}
237
Gilles Peskinec86a1652021-02-15 12:17:00 +0100238/** \def ARRAY_LENGTH
239 * Return the number of elements of a static or stack array.
240 *
241 * \param array A value of array (not pointer) type.
242 *
243 * \return The number of elements of the array.
244 */
245/* A correct implementation of ARRAY_LENGTH, but which silently gives
246 * a nonsensical result if called with a pointer rather than an array. */
247#define ARRAY_LENGTH_UNSAFE( array ) \
248 ( sizeof( array ) / sizeof( *( array ) ) )
249
Ronald Cron849930a2020-06-03 08:06:47 +0200250#if defined(__GNUC__)
251/* Test if arg and &(arg)[0] have the same type. This is true if arg is
252 * an array but not if it's a pointer. */
253#define IS_ARRAY_NOT_POINTER( arg ) \
254 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
255 __typeof__( &( arg )[0] ) ) )
Ronald Cron849930a2020-06-03 08:06:47 +0200256/* A compile-time constant with the value 0. If `const_expr` is not a
257 * compile-time constant with a nonzero value, cause a compile-time error. */
258#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +0300259 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Gilles Peskinec86a1652021-02-15 12:17:00 +0100260
Ronald Cron849930a2020-06-03 08:06:47 +0200261/* Return the scalar value `value` (possibly promoted). This is a compile-time
262 * constant if `value` is. `condition` must be a compile-time constant.
263 * If `condition` is false, arrange to cause a compile-time error. */
264#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
265 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
266
Ronald Cron849930a2020-06-03 08:06:47 +0200267#define ARRAY_LENGTH( array ) \
268 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
269 ARRAY_LENGTH_UNSAFE( array ) ) )
270
Gilles Peskinec86a1652021-02-15 12:17:00 +0100271#else
272/* If we aren't sure the compiler supports our non-standard tricks,
273 * fall back to the unsafe implementation. */
274#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
275#endif
276
Ronald Cron849930a2020-06-03 08:06:47 +0200277/** Return the smaller of two values.
278 *
279 * \param x An integer-valued expression without side effects.
280 * \param y An integer-valued expression without side effects.
281 *
282 * \return The smaller of \p x and \p y.
283 */
284#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
285
286/** Return the larger of two values.
287 *
288 * \param x An integer-valued expression without side effects.
289 * \param y An integer-valued expression without side effects.
290 *
291 * \return The larger of \p x and \p y.
292 */
293#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
294
295/*
296 * 32-bit integer manipulation macros (big endian)
297 */
298#ifndef GET_UINT32_BE
299#define GET_UINT32_BE(n,b,i) \
300{ \
301 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
302 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
303 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
304 | ( (uint32_t) (b)[(i) + 3] ); \
305}
306#endif
307
308#ifndef PUT_UINT32_BE
309#define PUT_UINT32_BE(n,b,i) \
310{ \
311 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
312 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
313 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
314 (b)[(i) + 3] = (unsigned char) ( (n) ); \
315}
316#endif
317
Ronald Cron4b8b1992020-06-09 13:52:23 +0200318#endif /* TEST_MACROS_H */