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