blob: 8535b9307706f2e1bd9070697caba9cc7c5c4d87 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020027#include "mbedtls/build_info.h"
Ronald Cron4b8b1992020-06-09 13:52:23 +020028
Ronald Cron849930a2020-06-03 08:06:47 +020029#include <stdlib.h>
30
31#if defined(MBEDTLS_PLATFORM_C)
32#include "mbedtls/platform.h"
33#else
34#include <stdio.h>
35#define mbedtls_fprintf fprintf
36#define mbedtls_snprintf snprintf
37#define mbedtls_calloc calloc
38#define mbedtls_free free
39#define mbedtls_exit exit
40#define mbedtls_time time
41#define mbedtls_time_t time_t
42#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
43#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
44#endif
45
46#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
47#include "mbedtls/memory_buffer_alloc.h"
48#endif
49
Chris Jonesa6d155f2021-02-09 12:09:33 +000050/**
51 * \brief This macro tests the expression passed to it as a test step or
52 * individual test in a test case.
53 *
54 * It allows a library function to return a value and return an error
55 * code that can be tested.
56 *
Chris Jonesa6d155f2021-02-09 12:09:33 +000057 * Failing the test means:
58 * - Mark this test case as failed.
59 * - Print a message identifying the failure.
60 * - Jump to the \c exit label.
61 *
62 * This macro expands to an instruction, not an expression.
63 * It may jump to the \c exit label.
64 *
65 * \param TEST The test expression to be tested.
66 */
67#define TEST_ASSERT( TEST ) \
68 do { \
69 if( ! (TEST) ) \
70 { \
71 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
72 goto exit; \
73 } \
74 } while( 0 )
75
Gilles Peskine89615ee2021-04-29 20:28:54 +020076/** Evaluate two integer expressions and fail the test case if they have
77 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000078 *
Gilles Peskine89615ee2021-04-29 20:28:54 +020079 * The two expressions should have the same signedness, otherwise the
80 * comparison is not meaningful if the signed value is negative.
81 *
82 * \param expr1 An integral-typed expression to evaluate.
83 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000084 */
Gilles Peskine89615ee2021-04-29 20:28:54 +020085#define TEST_EQUAL( expr1, expr2 ) \
86 do { \
87 if( ! mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
88 expr1, expr2 ) ) \
89 goto exit; \
90 } while( 0 )
Chris Jonesa6d155f2021-02-09 12:09:33 +000091
Gilles Peskined1465422022-04-13 23:59:52 +020092/** Evaluate two unsigned integer expressions and fail the test case
93 * if they are not in increasing order (left <= right).
94 *
95 * \param expr1 An integral-typed expression to evaluate.
96 * \param expr2 Another integral-typed expression to evaluate.
97 */
98#define TEST_LE_U( expr1, expr2 ) \
99 do { \
100 if( ! mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
101 expr1, expr2 ) ) \
102 goto exit; \
103 } while( 0 )
104
105/** Evaluate two signed integer expressions and fail the test case
106 * if they are not in increasing order (left <= right).
107 *
108 * \param expr1 An integral-typed expression to evaluate.
109 * \param expr2 Another integral-typed expression to evaluate.
110 */
111#define TEST_LE_S( expr1, expr2 ) \
112 do { \
113 if( ! mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
114 expr1, expr2 ) ) \
115 goto exit; \
116 } while( 0 )
117
Chris Jonesa6d155f2021-02-09 12:09:33 +0000118/** Allocate memory dynamically and fail the test case if this fails.
119 * The allocated memory will be filled with zeros.
120 *
121 * You must set \p pointer to \c NULL before calling this macro and
122 * put `mbedtls_free( pointer )` in the test's cleanup code.
123 *
124 * If \p length is zero, the resulting \p pointer will be \c NULL.
125 * This is usually what we want in tests since API functions are
126 * supposed to accept null pointers when a buffer size is zero.
127 *
128 * This macro expands to an instruction, not an expression.
129 * It may jump to the \c exit label.
130 *
131 * \param pointer An lvalue where the address of the allocated buffer
132 * will be stored.
133 * This expression may be evaluated multiple times.
134 * \param length Number of elements to allocate.
135 * This expression may be evaluated multiple times.
136 *
137 */
138#define ASSERT_ALLOC( pointer, length ) \
139 do \
140 { \
141 TEST_ASSERT( ( pointer ) == NULL ); \
142 if( ( length ) != 0 ) \
143 { \
144 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
145 ( length ) ); \
146 TEST_ASSERT( ( pointer ) != NULL ); \
147 } \
148 } \
149 while( 0 )
150
151/** Allocate memory dynamically. If the allocation fails, skip the test case.
152 *
153 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
154 * fails, it marks the test as skipped rather than failed.
155 */
156#define ASSERT_ALLOC_WEAK( pointer, length ) \
157 do \
158 { \
159 TEST_ASSERT( ( pointer ) == NULL ); \
160 if( ( length ) != 0 ) \
161 { \
162 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
163 ( length ) ); \
164 TEST_ASSUME( ( pointer ) != NULL ); \
165 } \
166 } \
167 while( 0 )
168
169/** Compare two buffers and fail the test case if they differ.
170 *
171 * This macro expands to an instruction, not an expression.
172 * It may jump to the \c exit label.
173 *
174 * \param p1 Pointer to the start of the first buffer.
175 * \param size1 Size of the first buffer in bytes.
176 * This expression may be evaluated multiple times.
177 * \param p2 Pointer to the start of the second buffer.
178 * \param size2 Size of the second buffer in bytes.
179 * This expression may be evaluated multiple times.
180 */
181#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
182 do \
183 { \
184 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
185 if( ( size1 ) != 0 ) \
186 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
187 } \
188 while( 0 )
189
190/**
191 * \brief This macro tests the expression passed to it and skips the
192 * running test if it doesn't evaluate to 'true'.
193 *
194 * \param TEST The test expression to be tested.
195 */
196#define TEST_ASSUME( TEST ) \
197 do { \
198 if( ! (TEST) ) \
199 { \
200 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
201 goto exit; \
202 } \
203 } while( 0 )
204
Ronald Cron849930a2020-06-03 08:06:47 +0200205#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
206{ \
207 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
208 __FILE__, __LINE__, #a ); \
209 mbedtls_exit( 1 ); \
210}
211
Gilles Peskinec86a1652021-02-15 12:17:00 +0100212/** \def ARRAY_LENGTH
213 * Return the number of elements of a static or stack array.
214 *
215 * \param array A value of array (not pointer) type.
216 *
217 * \return The number of elements of the array.
218 */
219/* A correct implementation of ARRAY_LENGTH, but which silently gives
220 * a nonsensical result if called with a pointer rather than an array. */
221#define ARRAY_LENGTH_UNSAFE( array ) \
222 ( sizeof( array ) / sizeof( *( array ) ) )
223
Ronald Cron849930a2020-06-03 08:06:47 +0200224#if defined(__GNUC__)
225/* Test if arg and &(arg)[0] have the same type. This is true if arg is
226 * an array but not if it's a pointer. */
227#define IS_ARRAY_NOT_POINTER( arg ) \
228 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
229 __typeof__( &( arg )[0] ) ) )
Ronald Cron849930a2020-06-03 08:06:47 +0200230/* A compile-time constant with the value 0. If `const_expr` is not a
231 * compile-time constant with a nonzero value, cause a compile-time error. */
232#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +0300233 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Gilles Peskinec86a1652021-02-15 12:17:00 +0100234
Ronald Cron849930a2020-06-03 08:06:47 +0200235/* Return the scalar value `value` (possibly promoted). This is a compile-time
236 * constant if `value` is. `condition` must be a compile-time constant.
237 * If `condition` is false, arrange to cause a compile-time error. */
238#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
239 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
240
Ronald Cron849930a2020-06-03 08:06:47 +0200241#define ARRAY_LENGTH( array ) \
242 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
243 ARRAY_LENGTH_UNSAFE( array ) ) )
244
Gilles Peskinec86a1652021-02-15 12:17:00 +0100245#else
246/* If we aren't sure the compiler supports our non-standard tricks,
247 * fall back to the unsafe implementation. */
248#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
249#endif
250
Ronald Cron849930a2020-06-03 08:06:47 +0200251/** Return the smaller of two values.
252 *
253 * \param x An integer-valued expression without side effects.
254 * \param y An integer-valued expression without side effects.
255 *
256 * \return The smaller of \p x and \p y.
257 */
258#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
259
260/** Return the larger of two values.
261 *
262 * \param x An integer-valued expression without side effects.
263 * \param y An integer-valued expression without side effects.
264 *
265 * \return The larger of \p x and \p y.
266 */
267#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
268
269/*
270 * 32-bit integer manipulation macros (big endian)
271 */
272#ifndef GET_UINT32_BE
273#define GET_UINT32_BE(n,b,i) \
274{ \
275 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
276 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
277 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
278 | ( (uint32_t) (b)[(i) + 3] ); \
279}
280#endif
281
282#ifndef PUT_UINT32_BE
283#define PUT_UINT32_BE(n,b,i) \
284{ \
285 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
286 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
287 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
288 (b)[(i) + 3] = (unsigned char) ( (n) ); \
289}
290#endif
291
Ronald Cron4b8b1992020-06-09 13:52:23 +0200292#endif /* TEST_MACROS_H */