blob: 87e86d38e569c24f3827169849c1ffc68137788f [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 * Failing the test means:
62 * - Mark this test case as failed.
63 * - Print a message identifying the failure.
64 * - Jump to the \c exit label.
65 *
66 * This macro expands to an instruction, not an expression.
67 * It may jump to the \c exit label.
68 *
69 * \param TEST The test expression to be tested.
70 */
71#define TEST_ASSERT( TEST ) \
72 do { \
73 if( ! (TEST) ) \
74 { \
75 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
76 goto exit; \
77 } \
78 } while( 0 )
79
80/** Evaluate two expressions and fail the test case if they have different
81 * values.
82 *
83 * \param expr1 An expression to evaluate.
84 * \param expr2 The expected value of \p expr1. This can be any
85 * expression, but it is typically a constant.
86 */
87#define TEST_EQUAL( expr1, expr2 ) \
88 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
89
90/** Allocate memory dynamically and fail the test case if this fails.
91 * The allocated memory will be filled with zeros.
92 *
93 * You must set \p pointer to \c NULL before calling this macro and
94 * put `mbedtls_free( pointer )` in the test's cleanup code.
95 *
96 * If \p length is zero, the resulting \p pointer will be \c NULL.
97 * This is usually what we want in tests since API functions are
98 * supposed to accept null pointers when a buffer size is zero.
99 *
100 * This macro expands to an instruction, not an expression.
101 * It may jump to the \c exit label.
102 *
103 * \param pointer An lvalue where the address of the allocated buffer
104 * will be stored.
105 * This expression may be evaluated multiple times.
106 * \param length Number of elements to allocate.
107 * This expression may be evaluated multiple times.
108 *
109 */
110#define ASSERT_ALLOC( pointer, length ) \
111 do \
112 { \
113 TEST_ASSERT( ( pointer ) == NULL ); \
114 if( ( length ) != 0 ) \
115 { \
116 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
117 ( length ) ); \
118 TEST_ASSERT( ( pointer ) != NULL ); \
119 } \
120 } \
121 while( 0 )
122
123/** Allocate memory dynamically. If the allocation fails, skip the test case.
124 *
125 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
126 * fails, it marks the test as skipped rather than failed.
127 */
128#define ASSERT_ALLOC_WEAK( pointer, length ) \
129 do \
130 { \
131 TEST_ASSERT( ( pointer ) == NULL ); \
132 if( ( length ) != 0 ) \
133 { \
134 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
135 ( length ) ); \
136 TEST_ASSUME( ( pointer ) != NULL ); \
137 } \
138 } \
139 while( 0 )
140
141/** Compare two buffers and fail the test case if they differ.
142 *
143 * This macro expands to an instruction, not an expression.
144 * It may jump to the \c exit label.
145 *
146 * \param p1 Pointer to the start of the first buffer.
147 * \param size1 Size of the first buffer in bytes.
148 * This expression may be evaluated multiple times.
149 * \param p2 Pointer to the start of the second buffer.
150 * \param size2 Size of the second buffer in bytes.
151 * This expression may be evaluated multiple times.
152 */
153#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
154 do \
155 { \
156 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
157 if( ( size1 ) != 0 ) \
158 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
159 } \
160 while( 0 )
161
162/**
163 * \brief This macro tests the expression passed to it and skips the
164 * running test if it doesn't evaluate to 'true'.
165 *
166 * \param TEST The test expression to be tested.
167 */
168#define TEST_ASSUME( TEST ) \
169 do { \
170 if( ! (TEST) ) \
171 { \
172 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
173 goto exit; \
174 } \
175 } while( 0 )
176
Ronald Cron849930a2020-06-03 08:06:47 +0200177#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
178{ \
179 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
180 __FILE__, __LINE__, #a ); \
181 mbedtls_exit( 1 ); \
182}
183
Gilles Peskinec86a1652021-02-15 12:17:00 +0100184/** \def ARRAY_LENGTH
185 * Return the number of elements of a static or stack array.
186 *
187 * \param array A value of array (not pointer) type.
188 *
189 * \return The number of elements of the array.
190 */
191/* A correct implementation of ARRAY_LENGTH, but which silently gives
192 * a nonsensical result if called with a pointer rather than an array. */
193#define ARRAY_LENGTH_UNSAFE( array ) \
194 ( sizeof( array ) / sizeof( *( array ) ) )
195
Ronald Cron849930a2020-06-03 08:06:47 +0200196#if defined(__GNUC__)
197/* Test if arg and &(arg)[0] have the same type. This is true if arg is
198 * an array but not if it's a pointer. */
199#define IS_ARRAY_NOT_POINTER( arg ) \
200 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
201 __typeof__( &( arg )[0] ) ) )
Ronald Cron849930a2020-06-03 08:06:47 +0200202/* A compile-time constant with the value 0. If `const_expr` is not a
203 * compile-time constant with a nonzero value, cause a compile-time error. */
204#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +0300205 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Gilles Peskinec86a1652021-02-15 12:17:00 +0100206
Ronald Cron849930a2020-06-03 08:06:47 +0200207/* Return the scalar value `value` (possibly promoted). This is a compile-time
208 * constant if `value` is. `condition` must be a compile-time constant.
209 * If `condition` is false, arrange to cause a compile-time error. */
210#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
211 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
212
Ronald Cron849930a2020-06-03 08:06:47 +0200213#define ARRAY_LENGTH( array ) \
214 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
215 ARRAY_LENGTH_UNSAFE( array ) ) )
216
Gilles Peskinec86a1652021-02-15 12:17:00 +0100217#else
218/* If we aren't sure the compiler supports our non-standard tricks,
219 * fall back to the unsafe implementation. */
220#define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
221#endif
222
Ronald Cron849930a2020-06-03 08:06:47 +0200223/** Return the smaller of two values.
224 *
225 * \param x An integer-valued expression without side effects.
226 * \param y An integer-valued expression without side effects.
227 *
228 * \return The smaller of \p x and \p y.
229 */
230#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
231
232/** Return the larger of two values.
233 *
234 * \param x An integer-valued expression without side effects.
235 * \param y An integer-valued expression without side effects.
236 *
237 * \return The larger of \p x and \p y.
238 */
239#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
240
241/*
242 * 32-bit integer manipulation macros (big endian)
243 */
244#ifndef GET_UINT32_BE
245#define GET_UINT32_BE(n,b,i) \
246{ \
247 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
248 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
249 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
250 | ( (uint32_t) (b)[(i) + 3] ); \
251}
252#endif
253
254#ifndef PUT_UINT32_BE
255#define PUT_UINT32_BE(n,b,i) \
256{ \
257 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
258 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
259 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
260 (b)[(i) + 3] = (unsigned char) ( (n) ); \
261}
262#endif
263
Ronald Cron4b8b1992020-06-09 13:52:23 +0200264#endif /* TEST_MACROS_H */