Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file macros.h |
| 3 | * |
| 4 | * \brief This file contains generic macros for the purpose of testing. |
| 5 | */ |
| 6 | |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 7 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef TEST_MACROS_H |
| 13 | #define TEST_MACROS_H |
| 14 | |
| 15 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 16 | #include "mbedtls/config.h" |
| 17 | #else |
| 18 | #include MBEDTLS_CONFIG_FILE |
| 19 | #endif |
| 20 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 23 | #include "mbedtls/platform.h" |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 24 | |
| 25 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 26 | #include "mbedtls/memory_buffer_alloc.h" |
| 27 | #endif |
| 28 | |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 29 | /** |
| 30 | * \brief This macro tests the expression passed to it as a test step or |
| 31 | * individual test in a test case. |
| 32 | * |
| 33 | * It allows a library function to return a value and return an error |
| 34 | * code that can be tested. |
| 35 | * |
| 36 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 37 | * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test |
| 38 | * failure. |
| 39 | * |
| 40 | * This macro is not suitable for negative parameter validation tests, |
| 41 | * as it assumes the test step will not create an error. |
| 42 | * |
| 43 | * Failing the test means: |
| 44 | * - Mark this test case as failed. |
| 45 | * - Print a message identifying the failure. |
| 46 | * - Jump to the \c exit label. |
| 47 | * |
| 48 | * This macro expands to an instruction, not an expression. |
| 49 | * It may jump to the \c exit label. |
| 50 | * |
| 51 | * \param TEST The test expression to be tested. |
| 52 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | #define TEST_ASSERT(TEST) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 54 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 55 | if (!(TEST)) \ |
| 56 | { \ |
| 57 | mbedtls_test_fail( #TEST, __LINE__, __FILE__); \ |
| 58 | goto exit; \ |
| 59 | } \ |
| 60 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 61 | |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 62 | /** This macro asserts fails the test with given output message. |
| 63 | * |
| 64 | * \param MESSAGE The message to be outputed on assertion |
| 65 | */ |
| 66 | #define TEST_FAIL(MESSAGE) \ |
| 67 | do { \ |
| 68 | mbedtls_test_fail(MESSAGE, __LINE__, __FILE__); \ |
Agathiyan Bragadeesh | 1dd20a3 | 2023-07-21 17:07:00 +0100 | [diff] [blame] | 69 | goto exit; \ |
| 70 | } while (0) |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 71 | |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 72 | /** Evaluate two integer expressions and fail the test case if they have |
| 73 | * different values. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 74 | * |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 75 | * The two expressions should have the same signedness, otherwise the |
| 76 | * comparison is not meaningful if the signed value is negative. |
| 77 | * |
| 78 | * \param expr1 An integral-typed expression to evaluate. |
| 79 | * \param expr2 Another integral-typed expression to evaluate. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 80 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 81 | #define TEST_EQUAL(expr1, expr2) \ |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 82 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \ |
| 84 | expr1, expr2)) \ |
| 85 | goto exit; \ |
| 86 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 87 | |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 88 | /** Evaluate two unsigned integer expressions and fail the test case |
| 89 | * if they are not in increasing order (left <= right). |
| 90 | * |
| 91 | * \param expr1 An integral-typed expression to evaluate. |
| 92 | * \param expr2 Another integral-typed expression to evaluate. |
| 93 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | #define TEST_LE_U(expr1, expr2) \ |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 95 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \ |
| 97 | expr1, expr2)) \ |
| 98 | goto exit; \ |
| 99 | } while (0) |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 100 | |
| 101 | /** Evaluate two signed integer expressions and fail the test case |
| 102 | * if they are not in increasing order (left <= right). |
| 103 | * |
| 104 | * \param expr1 An integral-typed expression to evaluate. |
| 105 | * \param expr2 Another integral-typed expression to evaluate. |
| 106 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | #define TEST_LE_S(expr1, expr2) \ |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 108 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \ |
| 110 | expr1, expr2)) \ |
| 111 | goto exit; \ |
| 112 | } while (0) |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 113 | |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 114 | /** Allocate memory dynamically and fail the test case if this fails. |
| 115 | * The allocated memory will be filled with zeros. |
| 116 | * |
| 117 | * You must set \p pointer to \c NULL before calling this macro and |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 118 | * put `mbedtls_free(pointer)` in the test's cleanup code. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 119 | * |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 120 | * If \p item_count is zero, the resulting \p pointer will be \c NULL. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 121 | * This is usually what we want in tests since API functions are |
| 122 | * supposed to accept null pointers when a buffer size is zero. |
| 123 | * |
| 124 | * This macro expands to an instruction, not an expression. |
| 125 | * It may jump to the \c exit label. |
| 126 | * |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 127 | * \param pointer An lvalue where the address of the allocated buffer |
| 128 | * will be stored. |
| 129 | * This expression may be evaluated multiple times. |
| 130 | * \param item_count Number of elements to allocate. |
| 131 | * This expression may be evaluated multiple times. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 132 | * |
| 133 | */ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 134 | #define TEST_CALLOC(pointer, item_count) \ |
Tom Cosgrove | 1357502 | 2023-09-04 11:05:59 +0100 | [diff] [blame] | 135 | do { \ |
| 136 | TEST_ASSERT((pointer) == NULL); \ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 137 | if ((item_count) != 0) { \ |
Tom Cosgrove | 1357502 | 2023-09-04 11:05:59 +0100 | [diff] [blame] | 138 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), \ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 139 | (item_count)); \ |
Tom Cosgrove | 1357502 | 2023-09-04 11:05:59 +0100 | [diff] [blame] | 140 | TEST_ASSERT((pointer) != NULL); \ |
| 141 | } \ |
| 142 | } while (0) |
| 143 | |
Dave Rodgman | 72aa683 | 2023-09-19 17:34:39 +0100 | [diff] [blame] | 144 | /** Allocate memory dynamically and fail the test case if this fails. |
| 145 | * The allocated memory will be filled with zeros. |
| 146 | * |
| 147 | * You must set \p pointer to \c NULL before calling this macro and |
| 148 | * put `mbedtls_free(pointer)` in the test's cleanup code. |
| 149 | * |
| 150 | * If \p item_count is zero, the resulting \p pointer will not be \c NULL. |
| 151 | * |
| 152 | * This macro expands to an instruction, not an expression. |
| 153 | * It may jump to the \c exit label. |
| 154 | * |
| 155 | * \param pointer An lvalue where the address of the allocated buffer |
| 156 | * will be stored. |
| 157 | * This expression may be evaluated multiple times. |
| 158 | * \param item_count Number of elements to allocate. |
| 159 | * This expression may be evaluated multiple times. |
| 160 | * |
Dave Rodgman | 3ca2f5c | 2023-09-19 18:30:25 +0100 | [diff] [blame] | 161 | * Note: if passing size 0, mbedtls_calloc may return NULL. In this case, |
| 162 | * we reattempt to allocate with the smallest possible buffer to assure a |
| 163 | * non-NULL pointer. |
Dave Rodgman | 72aa683 | 2023-09-19 17:34:39 +0100 | [diff] [blame] | 164 | */ |
| 165 | #define TEST_CALLOC_NONNULL(pointer, item_count) \ |
| 166 | do { \ |
| 167 | TEST_ASSERT((pointer) == NULL); \ |
| 168 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), \ |
| 169 | (item_count)); \ |
Dave Rodgman | 3ca2f5c | 2023-09-19 18:30:25 +0100 | [diff] [blame] | 170 | if (((pointer) == NULL) && ((item_count) == 0)) { \ |
| 171 | (pointer) = mbedtls_calloc(1, 1); \ |
| 172 | } \ |
Dave Rodgman | 72aa683 | 2023-09-19 17:34:39 +0100 | [diff] [blame] | 173 | TEST_ASSERT((pointer) != NULL); \ |
| 174 | } while (0) |
| 175 | |
Tom Cosgrove | 1357502 | 2023-09-04 11:05:59 +0100 | [diff] [blame] | 176 | /* For backwards compatibility */ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 177 | #define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 178 | |
| 179 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
| 180 | * |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 181 | * This macro behaves like #TEST_CALLOC, except that if the allocation |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 182 | * fails, it marks the test as skipped rather than failed. |
| 183 | */ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 184 | #define TEST_CALLOC_OR_SKIP(pointer, item_count) \ |
Tom Cosgrove | 20e27de | 2023-09-04 11:09:08 +0100 | [diff] [blame] | 185 | do { \ |
| 186 | TEST_ASSERT((pointer) == NULL); \ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 187 | if ((item_count) != 0) { \ |
Tom Cosgrove | 20e27de | 2023-09-04 11:09:08 +0100 | [diff] [blame] | 188 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), \ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 189 | (item_count)); \ |
Tom Cosgrove | 20e27de | 2023-09-04 11:09:08 +0100 | [diff] [blame] | 190 | TEST_ASSUME((pointer) != NULL); \ |
| 191 | } \ |
| 192 | } while (0) |
| 193 | |
| 194 | /* For backwards compatibility */ |
Tom Cosgrove | cd5a7c7 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 195 | #define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 196 | |
| 197 | /** Compare two buffers and fail the test case if they differ. |
| 198 | * |
| 199 | * This macro expands to an instruction, not an expression. |
| 200 | * It may jump to the \c exit label. |
| 201 | * |
| 202 | * \param p1 Pointer to the start of the first buffer. |
| 203 | * \param size1 Size of the first buffer in bytes. |
| 204 | * This expression may be evaluated multiple times. |
| 205 | * \param p2 Pointer to the start of the second buffer. |
| 206 | * \param size2 Size of the second buffer in bytes. |
| 207 | * This expression may be evaluated multiple times. |
| 208 | */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 209 | #define TEST_MEMORY_COMPARE(p1, size1, p2, size2) \ |
Tom Cosgrove | f88ee8b | 2023-09-04 11:04:40 +0100 | [diff] [blame] | 210 | do { \ |
Manuel Pégourié-Gonnard | 3c30191 | 2023-02-09 09:15:04 +0100 | [diff] [blame] | 211 | TEST_EQUAL((size1), (size2)); \ |
Tom Cosgrove | f88ee8b | 2023-09-04 11:04:40 +0100 | [diff] [blame] | 212 | if ((size1) != 0) { \ |
| 213 | TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \ |
| 214 | } \ |
| 215 | } while (0) |
| 216 | |
| 217 | /* For backwards compatibility */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 218 | #define ASSERT_COMPARE(p1, size1, p2, size2) TEST_MEMORY_COMPARE(p1, size1, p2, size2) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * \brief This macro tests the expression passed to it and skips the |
| 222 | * running test if it doesn't evaluate to 'true'. |
| 223 | * |
| 224 | * \param TEST The test expression to be tested. |
| 225 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | #define TEST_ASSUME(TEST) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 227 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | if (!(TEST)) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 229 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | mbedtls_test_skip( #TEST, __LINE__, __FILE__); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 231 | goto exit; \ |
| 232 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 234 | |
| 235 | #if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT) |
| 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 allows a library function to return a value and tests the return |
| 242 | * code on return to confirm the given error code was returned. |
| 243 | * |
| 244 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 245 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 246 | * expected failure, and the test will pass. |
| 247 | * |
| 248 | * This macro is intended for negative parameter validation tests, |
| 249 | * where the failing function may return an error value or call |
| 250 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
| 251 | * |
| 252 | * \param PARAM_ERROR_VALUE The expected error code. |
| 253 | * |
| 254 | * \param TEST The test expression to be tested. |
| 255 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 256 | #define TEST_INVALID_PARAM_RET(PARAM_ERR_VALUE, TEST) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 257 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | mbedtls_test_param_failed_expect_call(); \ |
| 259 | if (((TEST) != (PARAM_ERR_VALUE)) || \ |
| 260 | (mbedtls_test_param_failed_check_expected_call() != 0)) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 261 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | mbedtls_test_fail( #TEST, __LINE__, __FILE__); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 263 | goto exit; \ |
| 264 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | mbedtls_test_param_failed_check_expected_call(); \ |
| 266 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 267 | |
| 268 | /** |
| 269 | * \brief This macro tests the statement passed to it as a test step or |
| 270 | * individual test in a test case. The macro assumes the test will fail |
| 271 | * and will generate an error. |
| 272 | * |
| 273 | * It assumes the library function under test cannot return a value and |
| 274 | * assumes errors can only be indicated byt calls to |
| 275 | * MBEDTLS_PARAM_FAILED(). |
| 276 | * |
| 277 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 278 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 279 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 280 | * can be made. |
| 281 | * |
| 282 | * This macro is intended for negative parameter validation tests, |
| 283 | * where the failing function can only return an error by calling |
| 284 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
| 285 | * |
| 286 | * \param TEST The test expression to be tested. |
| 287 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 288 | #define TEST_INVALID_PARAM(TEST) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 289 | do { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | memcpy(jmp_tmp, mbedtls_test_param_failed_get_state_buf(), \ |
| 291 | sizeof(jmp_tmp)); \ |
| 292 | if (setjmp(mbedtls_test_param_failed_get_state_buf()) == 0) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 293 | { \ |
| 294 | TEST; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 295 | mbedtls_test_fail( #TEST, __LINE__, __FILE__); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 296 | goto exit; \ |
| 297 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | mbedtls_test_param_failed_reset_state(); \ |
| 299 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 300 | #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */ |
| 301 | |
| 302 | /** |
| 303 | * \brief This macro tests the statement passed to it as a test step or |
| 304 | * individual test in a test case. The macro assumes the test will not fail. |
| 305 | * |
| 306 | * It assumes the library function under test cannot return a value and |
| 307 | * assumes errors can only be indicated by calls to |
| 308 | * MBEDTLS_PARAM_FAILED(). |
| 309 | * |
| 310 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 311 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 312 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 313 | * can be made. |
| 314 | * |
| 315 | * This macro is intended to test that functions returning void |
| 316 | * accept all of the parameter values they're supposed to accept - eg |
| 317 | * that they don't call MBEDTLS_PARAM_FAILED() when a parameter |
| 318 | * that's allowed to be NULL happens to be NULL. |
| 319 | * |
| 320 | * Note: for functions that return something other that void, |
| 321 | * checking that they accept all the parameters they're supposed to |
| 322 | * accept is best done by using TEST_ASSERT() and checking the return |
| 323 | * value as well. |
| 324 | * |
| 325 | * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is |
| 326 | * disabled, as it makes sense to check that the functions accept all |
| 327 | * legal values even if this option is disabled - only in that case, |
| 328 | * the test is more about whether the function segfaults than about |
| 329 | * whether it invokes MBEDTLS_PARAM_FAILED(). |
| 330 | * |
| 331 | * \param TEST The test expression to be tested. |
| 332 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | #define TEST_VALID_PARAM(TEST) \ |
| 334 | TEST_ASSERT((TEST, 1)); |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 335 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 336 | #define TEST_HELPER_ASSERT(a) if (!(a)) \ |
| 337 | { \ |
| 338 | mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \ |
| 339 | __FILE__, __LINE__, #a); \ |
| 340 | mbedtls_exit(1); \ |
| 341 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 342 | |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 343 | /** \def ARRAY_LENGTH |
| 344 | * Return the number of elements of a static or stack array. |
| 345 | * |
| 346 | * \param array A value of array (not pointer) type. |
| 347 | * |
| 348 | * \return The number of elements of the array. |
| 349 | */ |
| 350 | /* A correct implementation of ARRAY_LENGTH, but which silently gives |
| 351 | * a nonsensical result if called with a pointer rather than an array. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 352 | #define ARRAY_LENGTH_UNSAFE(array) \ |
| 353 | (sizeof(array) / sizeof(*(array))) |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 354 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 355 | #if defined(__GNUC__) |
| 356 | /* Test if arg and &(arg)[0] have the same type. This is true if arg is |
| 357 | * an array but not if it's a pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 358 | #define IS_ARRAY_NOT_POINTER(arg) \ |
| 359 | (!__builtin_types_compatible_p(__typeof__(arg), \ |
| 360 | __typeof__(&(arg)[0]))) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 361 | /* A compile-time constant with the value 0. If `const_expr` is not a |
| 362 | * compile-time constant with a nonzero value, cause a compile-time error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | #define STATIC_ASSERT_EXPR(const_expr) \ |
| 364 | (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); })) |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 365 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 366 | /* Return the scalar value `value` (possibly promoted). This is a compile-time |
| 367 | * constant if `value` is. `condition` must be a compile-time constant. |
| 368 | * If `condition` is false, arrange to cause a compile-time error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 369 | #define STATIC_ASSERT_THEN_RETURN(condition, value) \ |
| 370 | (STATIC_ASSERT_EXPR(condition) ? 0 : (value)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 371 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | #define ARRAY_LENGTH(array) \ |
| 373 | (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \ |
| 374 | ARRAY_LENGTH_UNSAFE(array))) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 375 | |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 376 | #else |
| 377 | /* If we aren't sure the compiler supports our non-standard tricks, |
| 378 | * fall back to the unsafe implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | #define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array) |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 380 | #endif |
| 381 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 382 | /** Return the smaller of two values. |
| 383 | * |
| 384 | * \param x An integer-valued expression without side effects. |
| 385 | * \param y An integer-valued expression without side effects. |
| 386 | * |
| 387 | * \return The smaller of \p x and \p y. |
| 388 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 390 | |
| 391 | /** Return the larger of two values. |
| 392 | * |
| 393 | * \param x An integer-valued expression without side effects. |
| 394 | * \param y An integer-valued expression without side effects. |
| 395 | * |
| 396 | * \return The larger of \p x and \p y. |
| 397 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * 32-bit integer manipulation macros (big endian) |
| 402 | */ |
| 403 | #ifndef GET_UINT32_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | #define GET_UINT32_BE(n, b, i) \ |
| 405 | { \ |
| 406 | (n) = ((uint32_t) (b)[(i)] << 24) \ |
| 407 | | ((uint32_t) (b)[(i) + 1] << 16) \ |
| 408 | | ((uint32_t) (b)[(i) + 2] << 8) \ |
| 409 | | ((uint32_t) (b)[(i) + 3]); \ |
| 410 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 411 | #endif |
| 412 | |
| 413 | #ifndef PUT_UINT32_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | #define PUT_UINT32_BE(n, b, i) \ |
| 415 | { \ |
| 416 | (b)[(i)] = (unsigned char) ((n) >> 24); \ |
| 417 | (b)[(i) + 1] = (unsigned char) ((n) >> 16); \ |
| 418 | (b)[(i) + 2] = (unsigned char) ((n) >> 8); \ |
| 419 | (b)[(i) + 3] = (unsigned char) ((n)); \ |
| 420 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 421 | #endif |
| 422 | |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 423 | #endif /* TEST_MACROS_H */ |