blob: 750fe33e69af7776df14f1e3d16acca6c5ccce23 [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
Agathiyan Bragadeesh27e29892023-07-14 17:28:27 +010074/** This macro asserts fails the test with given output message.
75 *
76 * \param MESSAGE The message to be outputed on assertion
77 */
78#define TEST_FAIL(MESSAGE) \
79 do { \
80 mbedtls_test_fail(MESSAGE, __LINE__, __FILE__); \
Agathiyan Bragadeesh1dd20a32023-07-21 17:07:00 +010081 goto exit; \
82 } while (0)
Agathiyan Bragadeesh27e29892023-07-14 17:28:27 +010083
Gilles Peskineb4366492021-04-29 20:28:54 +020084/** Evaluate two integer expressions and fail the test case if they have
85 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000086 *
Gilles Peskineb4366492021-04-29 20:28:54 +020087 * The two expressions should have the same signedness, otherwise the
88 * comparison is not meaningful if the signed value is negative.
89 *
90 * \param expr1 An integral-typed expression to evaluate.
91 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000092 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093#define TEST_EQUAL(expr1, expr2) \
Gilles Peskineb4366492021-04-29 20:28:54 +020094 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
96 expr1, expr2)) \
97 goto exit; \
98 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000099
Gilles Peskine063700d2022-04-13 23:59:52 +0200100/** Evaluate two unsigned integer expressions and fail the test case
101 * if they are not in increasing order (left <= right).
102 *
103 * \param expr1 An integral-typed expression to evaluate.
104 * \param expr2 Another integral-typed expression to evaluate.
105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106#define TEST_LE_U(expr1, expr2) \
Gilles Peskine063700d2022-04-13 23:59:52 +0200107 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
109 expr1, expr2)) \
110 goto exit; \
111 } while (0)
Gilles Peskine063700d2022-04-13 23:59:52 +0200112
113/** Evaluate two signed integer expressions and fail the test case
114 * if they are not in increasing order (left <= right).
115 *
116 * \param expr1 An integral-typed expression to evaluate.
117 * \param expr2 Another integral-typed expression to evaluate.
118 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119#define TEST_LE_S(expr1, expr2) \
Gilles Peskine063700d2022-04-13 23:59:52 +0200120 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
122 expr1, expr2)) \
123 goto exit; \
124 } while (0)
Gilles Peskine063700d2022-04-13 23:59:52 +0200125
Chris Jonesa6d155f2021-02-09 12:09:33 +0000126/** Allocate memory dynamically and fail the test case if this fails.
127 * The allocated memory will be filled with zeros.
128 *
129 * You must set \p pointer to \c NULL before calling this macro and
130 * put `mbedtls_free( pointer )` in the test's cleanup code.
131 *
132 * If \p length is zero, the resulting \p pointer will be \c NULL.
133 * This is usually what we want in tests since API functions are
134 * supposed to accept null pointers when a buffer size is zero.
135 *
136 * This macro expands to an instruction, not an expression.
137 * It may jump to the \c exit label.
138 *
139 * \param pointer An lvalue where the address of the allocated buffer
140 * will be stored.
141 * This expression may be evaluated multiple times.
142 * \param length Number of elements to allocate.
143 * This expression may be evaluated multiple times.
144 *
145 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146#define ASSERT_ALLOC(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000147 do \
148 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 TEST_ASSERT((pointer) == NULL); \
150 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000151 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
153 (length)); \
154 TEST_ASSERT((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000155 } \
156 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000158
159/** Allocate memory dynamically. If the allocation fails, skip the test case.
160 *
161 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
162 * fails, it marks the test as skipped rather than failed.
163 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164#define ASSERT_ALLOC_WEAK(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000165 do \
166 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 TEST_ASSERT((pointer) == NULL); \
168 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000169 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
171 (length)); \
172 TEST_ASSUME((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000173 } \
174 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000176
177/** Compare two buffers and fail the test case if they differ.
178 *
179 * This macro expands to an instruction, not an expression.
180 * It may jump to the \c exit label.
181 *
182 * \param p1 Pointer to the start of the first buffer.
183 * \param size1 Size of the first buffer in bytes.
184 * This expression may be evaluated multiple times.
185 * \param p2 Pointer to the start of the second buffer.
186 * \param size2 Size of the second buffer in bytes.
187 * This expression may be evaluated multiple times.
188 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189#define ASSERT_COMPARE(p1, size1, p2, size2) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000190 do \
191 { \
Manuel Pégourié-Gonnard3c301912023-02-09 09:15:04 +0100192 TEST_EQUAL((size1), (size2)); \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if ((size1) != 0) \
194 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000195 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000197
198/**
199 * \brief This macro tests the expression passed to it and skips the
200 * running test if it doesn't evaluate to 'true'.
201 *
202 * \param TEST The test expression to be tested.
203 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000205 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000207 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000209 goto exit; \
210 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000212
213#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
214/**
215 * \brief This macro tests the statement passed to it as a test step or
216 * individual test in a test case. The macro assumes the test will fail
217 * and will generate an error.
218 *
219 * It allows a library function to return a value and tests the return
220 * code on return to confirm the given error code was returned.
221 *
222 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
223 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
224 * expected failure, and the test will pass.
225 *
226 * This macro is intended for negative parameter validation tests,
227 * where the failing function may return an error value or call
228 * MBEDTLS_PARAM_FAILED() to indicate the error.
229 *
230 * \param PARAM_ERROR_VALUE The expected error code.
231 *
232 * \param TEST The test expression to be tested.
233 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234#define TEST_INVALID_PARAM_RET(PARAM_ERR_VALUE, TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000235 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 mbedtls_test_param_failed_expect_call(); \
237 if (((TEST) != (PARAM_ERR_VALUE)) || \
238 (mbedtls_test_param_failed_check_expected_call() != 0)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000239 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000241 goto exit; \
242 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 mbedtls_test_param_failed_check_expected_call(); \
244 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000245
246/**
247 * \brief This macro tests the statement passed to it as a test step or
248 * individual test in a test case. The macro assumes the test will fail
249 * and will generate an error.
250 *
251 * It assumes the library function under test cannot return a value and
252 * assumes errors can only be indicated byt calls to
253 * MBEDTLS_PARAM_FAILED().
254 *
255 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
256 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
257 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
258 * can be made.
259 *
260 * This macro is intended for negative parameter validation tests,
261 * where the failing function can only return an error by calling
262 * MBEDTLS_PARAM_FAILED() to indicate the error.
263 *
264 * \param TEST The test expression to be tested.
265 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266#define TEST_INVALID_PARAM(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000267 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 memcpy(jmp_tmp, mbedtls_test_param_failed_get_state_buf(), \
269 sizeof(jmp_tmp)); \
270 if (setjmp(mbedtls_test_param_failed_get_state_buf()) == 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000271 { \
272 TEST; \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000274 goto exit; \
275 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 mbedtls_test_param_failed_reset_state(); \
277 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000278#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
279
280/**
281 * \brief This macro tests the statement passed to it as a test step or
282 * individual test in a test case. The macro assumes the test will not fail.
283 *
284 * It assumes the library function under test cannot return a value and
285 * assumes errors can only be indicated by calls to
286 * MBEDTLS_PARAM_FAILED().
287 *
288 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
289 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
290 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
291 * can be made.
292 *
293 * This macro is intended to test that functions returning void
294 * accept all of the parameter values they're supposed to accept - eg
295 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
296 * that's allowed to be NULL happens to be NULL.
297 *
298 * Note: for functions that return something other that void,
299 * checking that they accept all the parameters they're supposed to
300 * accept is best done by using TEST_ASSERT() and checking the return
301 * value as well.
302 *
303 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
304 * disabled, as it makes sense to check that the functions accept all
305 * legal values even if this option is disabled - only in that case,
306 * the test is more about whether the function segfaults than about
307 * whether it invokes MBEDTLS_PARAM_FAILED().
308 *
309 * \param TEST The test expression to be tested.
310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311#define TEST_VALID_PARAM(TEST) \
312 TEST_ASSERT((TEST, 1));
Chris Jonesa6d155f2021-02-09 12:09:33 +0000313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314#define TEST_HELPER_ASSERT(a) if (!(a)) \
315 { \
316 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
317 __FILE__, __LINE__, #a); \
318 mbedtls_exit(1); \
319 }
Ronald Cron849930a2020-06-03 08:06:47 +0200320
Gilles Peskinec86a1652021-02-15 12:17:00 +0100321/** \def ARRAY_LENGTH
322 * Return the number of elements of a static or stack array.
323 *
324 * \param array A value of array (not pointer) type.
325 *
326 * \return The number of elements of the array.
327 */
328/* A correct implementation of ARRAY_LENGTH, but which silently gives
329 * a nonsensical result if called with a pointer rather than an array. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330#define ARRAY_LENGTH_UNSAFE(array) \
331 (sizeof(array) / sizeof(*(array)))
Gilles Peskinec86a1652021-02-15 12:17:00 +0100332
Ronald Cron849930a2020-06-03 08:06:47 +0200333#if defined(__GNUC__)
334/* Test if arg and &(arg)[0] have the same type. This is true if arg is
335 * an array but not if it's a pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336#define IS_ARRAY_NOT_POINTER(arg) \
337 (!__builtin_types_compatible_p(__typeof__(arg), \
338 __typeof__(&(arg)[0])))
Ronald Cron849930a2020-06-03 08:06:47 +0200339/* A compile-time constant with the value 0. If `const_expr` is not a
340 * compile-time constant with a nonzero value, cause a compile-time error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341#define STATIC_ASSERT_EXPR(const_expr) \
342 (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
Gilles Peskinec86a1652021-02-15 12:17:00 +0100343
Ronald Cron849930a2020-06-03 08:06:47 +0200344/* Return the scalar value `value` (possibly promoted). This is a compile-time
345 * constant if `value` is. `condition` must be a compile-time constant.
346 * If `condition` is false, arrange to cause a compile-time error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347#define STATIC_ASSERT_THEN_RETURN(condition, value) \
348 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
Ronald Cron849930a2020-06-03 08:06:47 +0200349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350#define ARRAY_LENGTH(array) \
351 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
352 ARRAY_LENGTH_UNSAFE(array)))
Ronald Cron849930a2020-06-03 08:06:47 +0200353
Gilles Peskinec86a1652021-02-15 12:17:00 +0100354#else
355/* If we aren't sure the compiler supports our non-standard tricks,
356 * fall back to the unsafe implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
Gilles Peskinec86a1652021-02-15 12:17:00 +0100358#endif
359
Ronald Cron849930a2020-06-03 08:06:47 +0200360/** Return the smaller of two values.
361 *
362 * \param x An integer-valued expression without side effects.
363 * \param y An integer-valued expression without side effects.
364 *
365 * \return The smaller of \p x and \p y.
366 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200368
369/** Return the larger of two values.
370 *
371 * \param x An integer-valued expression without side effects.
372 * \param y An integer-valued expression without side effects.
373 *
374 * \return The larger of \p x and \p y.
375 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200377
378/*
379 * 32-bit integer manipulation macros (big endian)
380 */
381#ifndef GET_UINT32_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382#define GET_UINT32_BE(n, b, i) \
383 { \
384 (n) = ((uint32_t) (b)[(i)] << 24) \
385 | ((uint32_t) (b)[(i) + 1] << 16) \
386 | ((uint32_t) (b)[(i) + 2] << 8) \
387 | ((uint32_t) (b)[(i) + 3]); \
388 }
Ronald Cron849930a2020-06-03 08:06:47 +0200389#endif
390
391#ifndef PUT_UINT32_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392#define PUT_UINT32_BE(n, b, i) \
393 { \
394 (b)[(i)] = (unsigned char) ((n) >> 24); \
395 (b)[(i) + 1] = (unsigned char) ((n) >> 16); \
396 (b)[(i) + 2] = (unsigned char) ((n) >> 8); \
397 (b)[(i) + 3] = (unsigned char) ((n)); \
398 }
Ronald Cron849930a2020-06-03 08:06:47 +0200399#endif
400
Ronald Cron4b8b1992020-06-09 13:52:23 +0200401#endif /* TEST_MACROS_H */