blob: 95bca239c67aa12823e97c536089cc0e2f9fd8fb [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
Ronald Cron849930a2020-06-03 08:06:47 +020035#include "mbedtls/platform.h"
Ronald Cron849930a2020-06-03 08:06:47 +020036
37#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
38#include "mbedtls/memory_buffer_alloc.h"
39#endif
40
Chris Jonesa6d155f2021-02-09 12:09:33 +000041/**
42 * \brief This macro tests the expression passed to it as a test step or
43 * individual test in a test case.
44 *
45 * It allows a library function to return a value and return an error
46 * code that can be tested.
47 *
48 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
49 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
50 * failure.
51 *
52 * This macro is not suitable for negative parameter validation tests,
53 * as it assumes the test step will not create an error.
54 *
55 * Failing the test means:
56 * - Mark this test case as failed.
57 * - Print a message identifying the failure.
58 * - Jump to the \c exit label.
59 *
60 * This macro expands to an instruction, not an expression.
61 * It may jump to the \c exit label.
62 *
63 * \param TEST The test expression to be tested.
64 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065#define TEST_ASSERT(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +000066 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if (!(TEST)) \
68 { \
69 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
70 goto exit; \
71 } \
72 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000073
Gilles Peskineb4366492021-04-29 20:28:54 +020074/** Evaluate two integer expressions and fail the test case if they have
75 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000076 *
Gilles Peskineb4366492021-04-29 20:28:54 +020077 * The two expressions should have the same signedness, otherwise the
78 * comparison is not meaningful if the signed value is negative.
79 *
80 * \param expr1 An integral-typed expression to evaluate.
81 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000082 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083#define TEST_EQUAL(expr1, expr2) \
Gilles Peskineb4366492021-04-29 20:28:54 +020084 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
86 expr1, expr2)) \
87 goto exit; \
88 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000089
Gilles Peskine063700d2022-04-13 23:59:52 +020090/** Evaluate two unsigned integer expressions and fail the test case
91 * if they are not in increasing order (left <= right).
92 *
93 * \param expr1 An integral-typed expression to evaluate.
94 * \param expr2 Another integral-typed expression to evaluate.
95 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096#define TEST_LE_U(expr1, expr2) \
Gilles Peskine063700d2022-04-13 23:59:52 +020097 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
99 expr1, expr2)) \
100 goto exit; \
101 } while (0)
Gilles Peskine063700d2022-04-13 23:59:52 +0200102
103/** Evaluate two signed integer expressions and fail the test case
104 * if they are not in increasing order (left <= right).
105 *
106 * \param expr1 An integral-typed expression to evaluate.
107 * \param expr2 Another integral-typed expression to evaluate.
108 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109#define TEST_LE_S(expr1, expr2) \
Gilles Peskine063700d2022-04-13 23:59:52 +0200110 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
112 expr1, expr2)) \
113 goto exit; \
114 } while (0)
Gilles Peskine063700d2022-04-13 23:59:52 +0200115
Chris Jonesa6d155f2021-02-09 12:09:33 +0000116/** Allocate memory dynamically and fail the test case if this fails.
117 * The allocated memory will be filled with zeros.
118 *
119 * You must set \p pointer to \c NULL before calling this macro and
120 * put `mbedtls_free( pointer )` in the test's cleanup code.
121 *
122 * If \p length is zero, the resulting \p pointer will be \c NULL.
123 * This is usually what we want in tests since API functions are
124 * supposed to accept null pointers when a buffer size is zero.
125 *
126 * This macro expands to an instruction, not an expression.
127 * It may jump to the \c exit label.
128 *
129 * \param pointer An lvalue where the address of the allocated buffer
130 * will be stored.
131 * This expression may be evaluated multiple times.
132 * \param length Number of elements to allocate.
133 * This expression may be evaluated multiple times.
134 *
135 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136#define ASSERT_ALLOC(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000137 do \
138 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 TEST_ASSERT((pointer) == NULL); \
140 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000141 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
143 (length)); \
144 TEST_ASSERT((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000145 } \
146 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000148
149/** Allocate memory dynamically. If the allocation fails, skip the test case.
150 *
151 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
152 * fails, it marks the test as skipped rather than failed.
153 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154#define ASSERT_ALLOC_WEAK(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000155 do \
156 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 TEST_ASSERT((pointer) == NULL); \
158 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000159 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
161 (length)); \
162 TEST_ASSUME((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000163 } \
164 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000166
167/** Compare two buffers and fail the test case if they differ.
168 *
169 * This macro expands to an instruction, not an expression.
170 * It may jump to the \c exit label.
171 *
172 * \param p1 Pointer to the start of the first buffer.
173 * \param size1 Size of the first buffer in bytes.
174 * This expression may be evaluated multiple times.
175 * \param p2 Pointer to the start of the second buffer.
176 * \param size2 Size of the second buffer in bytes.
177 * This expression may be evaluated multiple times.
178 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179#define ASSERT_COMPARE(p1, size1, p2, size2) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000180 do \
181 { \
Manuel Pégourié-Gonnard3c301912023-02-09 09:15:04 +0100182 TEST_EQUAL((size1), (size2)); \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 if ((size1) != 0) \
184 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000185 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000187
188/**
189 * \brief This macro tests the expression passed to it and skips the
190 * running test if it doesn't evaluate to 'true'.
191 *
192 * \param TEST The test expression to be tested.
193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000195 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000197 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000199 goto exit; \
200 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000202
203#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
204/**
205 * \brief This macro tests the statement passed to it as a test step or
206 * individual test in a test case. The macro assumes the test will fail
207 * and will generate an error.
208 *
209 * It allows a library function to return a value and tests the return
210 * code on return to confirm the given error code was returned.
211 *
212 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
213 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
214 * expected failure, and the test will pass.
215 *
216 * This macro is intended for negative parameter validation tests,
217 * where the failing function may return an error value or call
218 * MBEDTLS_PARAM_FAILED() to indicate the error.
219 *
220 * \param PARAM_ERROR_VALUE The expected error code.
221 *
222 * \param TEST The test expression to be tested.
223 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224#define TEST_INVALID_PARAM_RET(PARAM_ERR_VALUE, TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000225 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 mbedtls_test_param_failed_expect_call(); \
227 if (((TEST) != (PARAM_ERR_VALUE)) || \
228 (mbedtls_test_param_failed_check_expected_call() != 0)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000229 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000231 goto exit; \
232 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 mbedtls_test_param_failed_check_expected_call(); \
234 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000235
236/**
237 * \brief This macro tests the statement passed to it as a test step or
238 * individual test in a test case. The macro assumes the test will fail
239 * and will generate an error.
240 *
241 * It assumes the library function under test cannot return a value and
242 * assumes errors can only be indicated byt calls to
243 * MBEDTLS_PARAM_FAILED().
244 *
245 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
246 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
247 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
248 * can be made.
249 *
250 * This macro is intended for negative parameter validation tests,
251 * where the failing function can only return an error by calling
252 * MBEDTLS_PARAM_FAILED() to indicate the error.
253 *
254 * \param TEST The test expression to be tested.
255 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256#define TEST_INVALID_PARAM(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000257 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 memcpy(jmp_tmp, mbedtls_test_param_failed_get_state_buf(), \
259 sizeof(jmp_tmp)); \
260 if (setjmp(mbedtls_test_param_failed_get_state_buf()) == 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000261 { \
262 TEST; \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000264 goto exit; \
265 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 mbedtls_test_param_failed_reset_state(); \
267 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000268#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
269
270/**
271 * \brief This macro tests the statement passed to it as a test step or
272 * individual test in a test case. The macro assumes the test will not fail.
273 *
274 * It assumes the library function under test cannot return a value and
275 * assumes errors can only be indicated by calls to
276 * MBEDTLS_PARAM_FAILED().
277 *
278 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
279 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
280 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
281 * can be made.
282 *
283 * This macro is intended to test that functions returning void
284 * accept all of the parameter values they're supposed to accept - eg
285 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
286 * that's allowed to be NULL happens to be NULL.
287 *
288 * Note: for functions that return something other that void,
289 * checking that they accept all the parameters they're supposed to
290 * accept is best done by using TEST_ASSERT() and checking the return
291 * value as well.
292 *
293 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
294 * disabled, as it makes sense to check that the functions accept all
295 * legal values even if this option is disabled - only in that case,
296 * the test is more about whether the function segfaults than about
297 * whether it invokes MBEDTLS_PARAM_FAILED().
298 *
299 * \param TEST The test expression to be tested.
300 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301#define TEST_VALID_PARAM(TEST) \
302 TEST_ASSERT((TEST, 1));
Chris Jonesa6d155f2021-02-09 12:09:33 +0000303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304#define TEST_HELPER_ASSERT(a) if (!(a)) \
305 { \
306 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
307 __FILE__, __LINE__, #a); \
308 mbedtls_exit(1); \
309 }
Ronald Cron849930a2020-06-03 08:06:47 +0200310
Gilles Peskinec86a1652021-02-15 12:17:00 +0100311/** \def ARRAY_LENGTH
312 * Return the number of elements of a static or stack array.
313 *
314 * \param array A value of array (not pointer) type.
315 *
316 * \return The number of elements of the array.
317 */
318/* A correct implementation of ARRAY_LENGTH, but which silently gives
319 * a nonsensical result if called with a pointer rather than an array. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320#define ARRAY_LENGTH_UNSAFE(array) \
321 (sizeof(array) / sizeof(*(array)))
Gilles Peskinec86a1652021-02-15 12:17:00 +0100322
Ronald Cron849930a2020-06-03 08:06:47 +0200323#if defined(__GNUC__)
324/* Test if arg and &(arg)[0] have the same type. This is true if arg is
325 * an array but not if it's a pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326#define IS_ARRAY_NOT_POINTER(arg) \
327 (!__builtin_types_compatible_p(__typeof__(arg), \
328 __typeof__(&(arg)[0])))
Ronald Cron849930a2020-06-03 08:06:47 +0200329/* A compile-time constant with the value 0. If `const_expr` is not a
330 * compile-time constant with a nonzero value, cause a compile-time error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331#define STATIC_ASSERT_EXPR(const_expr) \
332 (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
Gilles Peskinec86a1652021-02-15 12:17:00 +0100333
Ronald Cron849930a2020-06-03 08:06:47 +0200334/* Return the scalar value `value` (possibly promoted). This is a compile-time
335 * constant if `value` is. `condition` must be a compile-time constant.
336 * If `condition` is false, arrange to cause a compile-time error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337#define STATIC_ASSERT_THEN_RETURN(condition, value) \
338 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
Ronald Cron849930a2020-06-03 08:06:47 +0200339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340#define ARRAY_LENGTH(array) \
341 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
342 ARRAY_LENGTH_UNSAFE(array)))
Ronald Cron849930a2020-06-03 08:06:47 +0200343
Gilles Peskinec86a1652021-02-15 12:17:00 +0100344#else
345/* If we aren't sure the compiler supports our non-standard tricks,
346 * fall back to the unsafe implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
Gilles Peskinec86a1652021-02-15 12:17:00 +0100348#endif
349
Ronald Cron849930a2020-06-03 08:06:47 +0200350/** Return the smaller of two values.
351 *
352 * \param x An integer-valued expression without side effects.
353 * \param y An integer-valued expression without side effects.
354 *
355 * \return The smaller of \p x and \p y.
356 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200358
359/** Return the larger of two values.
360 *
361 * \param x An integer-valued expression without side effects.
362 * \param y An integer-valued expression without side effects.
363 *
364 * \return The larger of \p x and \p y.
365 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200367
368/*
369 * 32-bit integer manipulation macros (big endian)
370 */
371#ifndef GET_UINT32_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372#define GET_UINT32_BE(n, b, i) \
373 { \
374 (n) = ((uint32_t) (b)[(i)] << 24) \
375 | ((uint32_t) (b)[(i) + 1] << 16) \
376 | ((uint32_t) (b)[(i) + 2] << 8) \
377 | ((uint32_t) (b)[(i) + 3]); \
378 }
Ronald Cron849930a2020-06-03 08:06:47 +0200379#endif
380
381#ifndef PUT_UINT32_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382#define PUT_UINT32_BE(n, b, i) \
383 { \
384 (b)[(i)] = (unsigned char) ((n) >> 24); \
385 (b)[(i) + 1] = (unsigned char) ((n) >> 16); \
386 (b)[(i) + 2] = (unsigned char) ((n) >> 8); \
387 (b)[(i) + 3] = (unsigned char) ((n)); \
388 }
Ronald Cron849930a2020-06-03 08:06:47 +0200389#endif
390
Ronald Cron4b8b1992020-06-09 13:52:23 +0200391#endif /* TEST_MACROS_H */