| 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 | 16799db | 2023-11-02 19:47:20 +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 |  | 
| Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 15 | #include "mbedtls/build_info.h" | 
| Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 16 |  | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 17 | #include <stdlib.h> | 
|  | 18 |  | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 19 | #include "mbedtls/platform.h" | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 20 |  | 
|  | 21 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) | 
|  | 22 | #include "mbedtls/memory_buffer_alloc.h" | 
|  | 23 | #endif | 
| Andrzej Kurek | f1b659e | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 24 | #include "common.h" | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 25 |  | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 26 | /** | 
|  | 27 | * \brief   This macro tests the expression passed to it as a test step or | 
|  | 28 | *          individual test in a test case. | 
|  | 29 | * | 
|  | 30 | *          It allows a library function to return a value and return an error | 
|  | 31 | *          code that can be tested. | 
|  | 32 | * | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 33 | *          Failing the test means: | 
|  | 34 | *          - Mark this test case as failed. | 
|  | 35 | *          - Print a message identifying the failure. | 
|  | 36 | *          - Jump to the \c exit label. | 
|  | 37 | * | 
|  | 38 | *          This macro expands to an instruction, not an expression. | 
|  | 39 | *          It may jump to the \c exit label. | 
|  | 40 | * | 
|  | 41 | * \param   TEST    The test expression to be tested. | 
|  | 42 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | #define TEST_ASSERT(TEST)                                 \ | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 44 | do {                                                    \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | if (!(TEST))                                       \ | 
|  | 46 | {                                                    \ | 
|  | 47 | mbedtls_test_fail( #TEST, __LINE__, __FILE__);   \ | 
|  | 48 | goto exit;                                        \ | 
|  | 49 | }                                                    \ | 
|  | 50 | } while (0) | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 51 |  | 
| Agathiyan Bragadeesh | ebb40bc | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 52 | /** This macro asserts fails the test with given output message. | 
|  | 53 | * | 
|  | 54 | * \param   MESSAGE The message to be outputed on assertion | 
|  | 55 | */ | 
| Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 56 | #define TEST_FAIL(MESSAGE)                           \ | 
| Agathiyan Bragadeesh | ebb40bc | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 57 | do {                                                  \ | 
|  | 58 | mbedtls_test_fail(MESSAGE, __LINE__, __FILE__);   \ | 
| Agathiyan Bragadeesh | 3dd3ae2 | 2023-07-21 17:07:00 +0100 | [diff] [blame] | 59 | goto exit;                                        \ | 
|  | 60 | } while (0) | 
| Agathiyan Bragadeesh | ebb40bc | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 61 |  | 
| Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 62 | /** Evaluate two integer expressions and fail the test case if they have | 
|  | 63 | * different values. | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 64 | * | 
| Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 65 | * The two expressions should have the same signedness, otherwise the | 
|  | 66 | * comparison is not meaningful if the signed value is negative. | 
|  | 67 | * | 
|  | 68 | * \param expr1     An integral-typed expression to evaluate. | 
|  | 69 | * \param expr2     Another integral-typed expression to evaluate. | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 70 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | #define TEST_EQUAL(expr1, expr2)                                      \ | 
| Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 72 | do {                                                                \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \ | 
| Agathiyan Bragadeesh | 2d310de | 2023-07-17 18:27:03 +0100 | [diff] [blame] | 74 | (unsigned long long) (expr1), (unsigned long long) (expr2)))                      \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | goto exit;                                                  \ | 
|  | 76 | } while (0) | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 77 |  | 
| Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 78 | /** Evaluate two unsigned integer expressions and fail the test case | 
|  | 79 | * if they are not in increasing order (left <= right). | 
|  | 80 | * | 
|  | 81 | * \param expr1     An integral-typed expression to evaluate. | 
|  | 82 | * \param expr2     Another integral-typed expression to evaluate. | 
|  | 83 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | #define TEST_LE_U(expr1, expr2)                                       \ | 
| Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 85 | do {                                                                \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \ | 
|  | 87 | expr1, expr2))                      \ | 
|  | 88 | goto exit;                                                  \ | 
|  | 89 | } while (0) | 
| Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 90 |  | 
|  | 91 | /** Evaluate two signed integer expressions and fail the test case | 
|  | 92 | * if they are not in increasing order (left <= right). | 
|  | 93 | * | 
|  | 94 | * \param expr1     An integral-typed expression to evaluate. | 
|  | 95 | * \param expr2     Another integral-typed expression to evaluate. | 
|  | 96 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | #define TEST_LE_S(expr1, expr2)                                       \ | 
| Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 98 | do {                                                                \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \ | 
|  | 100 | expr1, expr2))                      \ | 
|  | 101 | goto exit;                                                  \ | 
|  | 102 | } while (0) | 
| Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 103 |  | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 104 | /** Allocate memory dynamically and fail the test case if this fails. | 
|  | 105 | * The allocated memory will be filled with zeros. | 
|  | 106 | * | 
|  | 107 | * You must set \p pointer to \c NULL before calling this macro and | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 108 | * put `mbedtls_free(pointer)` in the test's cleanup code. | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 109 | * | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 110 | * 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] | 111 | * This is usually what we want in tests since API functions are | 
|  | 112 | * supposed to accept null pointers when a buffer size is zero. | 
|  | 113 | * | 
|  | 114 | * This macro expands to an instruction, not an expression. | 
|  | 115 | * It may jump to the \c exit label. | 
|  | 116 | * | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 117 | * \param pointer    An lvalue where the address of the allocated buffer | 
|  | 118 | *                   will be stored. | 
|  | 119 | *                   This expression may be evaluated multiple times. | 
|  | 120 | * \param item_count Number of elements to allocate. | 
|  | 121 | *                   This expression may be evaluated multiple times. | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 122 | * | 
|  | 123 | */ | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 124 | #define TEST_CALLOC(pointer, item_count)                    \ | 
| Tom Cosgrove | f9ffd11 | 2023-07-20 16:48:18 +0100 | [diff] [blame] | 125 | do {                                                    \ | 
|  | 126 | TEST_ASSERT((pointer) == NULL);                     \ | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 127 | if ((item_count) != 0) {                            \ | 
| Sergei Trofimovich | da2a33d | 2024-01-25 20:48:56 +0000 | [diff] [blame] | 128 | (pointer) = mbedtls_calloc((item_count),        \ | 
|  | 129 | sizeof(*(pointer))); \ | 
| Tom Cosgrove | f9ffd11 | 2023-07-20 16:48:18 +0100 | [diff] [blame] | 130 | TEST_ASSERT((pointer) != NULL);                 \ | 
|  | 131 | }                                                   \ | 
|  | 132 | } while (0) | 
|  | 133 |  | 
| Dave Rodgman | a328635 | 2023-09-19 17:34:39 +0100 | [diff] [blame] | 134 | /** Allocate memory dynamically and fail the test case if this fails. | 
|  | 135 | * The allocated memory will be filled with zeros. | 
|  | 136 | * | 
|  | 137 | * You must set \p pointer to \c NULL before calling this macro and | 
|  | 138 | * put `mbedtls_free(pointer)` in the test's cleanup code. | 
|  | 139 | * | 
|  | 140 | * If \p item_count is zero, the resulting \p pointer will not be \c NULL. | 
|  | 141 | * | 
|  | 142 | * This macro expands to an instruction, not an expression. | 
|  | 143 | * It may jump to the \c exit label. | 
|  | 144 | * | 
|  | 145 | * \param pointer    An lvalue where the address of the allocated buffer | 
|  | 146 | *                   will be stored. | 
|  | 147 | *                   This expression may be evaluated multiple times. | 
|  | 148 | * \param item_count Number of elements to allocate. | 
|  | 149 | *                   This expression may be evaluated multiple times. | 
|  | 150 | * | 
| Dave Rodgman | 986006e | 2023-09-19 18:30:25 +0100 | [diff] [blame] | 151 | * Note: if passing size 0, mbedtls_calloc may return NULL. In this case, | 
|  | 152 | * we reattempt to allocate with the smallest possible buffer to assure a | 
|  | 153 | * non-NULL pointer. | 
| Dave Rodgman | a328635 | 2023-09-19 17:34:39 +0100 | [diff] [blame] | 154 | */ | 
|  | 155 | #define TEST_CALLOC_NONNULL(pointer, item_count)            \ | 
|  | 156 | do {                                                    \ | 
|  | 157 | TEST_ASSERT((pointer) == NULL);                     \ | 
| Sergei Trofimovich | da2a33d | 2024-01-25 20:48:56 +0000 | [diff] [blame] | 158 | (pointer) = mbedtls_calloc((item_count),            \ | 
|  | 159 | sizeof(*(pointer)));     \ | 
| Dave Rodgman | 986006e | 2023-09-19 18:30:25 +0100 | [diff] [blame] | 160 | if (((pointer) == NULL) && ((item_count) == 0)) {   \ | 
|  | 161 | (pointer) = mbedtls_calloc(1, 1);               \ | 
|  | 162 | }                                                   \ | 
| Dave Rodgman | a328635 | 2023-09-19 17:34:39 +0100 | [diff] [blame] | 163 | TEST_ASSERT((pointer) != NULL);                     \ | 
|  | 164 | } while (0) | 
|  | 165 |  | 
| Tom Cosgrove | f9ffd11 | 2023-07-20 16:48:18 +0100 | [diff] [blame] | 166 | /* For backwards compatibility */ | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 167 | #define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count) | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 168 |  | 
|  | 169 | /** Allocate memory dynamically. If the allocation fails, skip the test case. | 
|  | 170 | * | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 171 | * This macro behaves like #TEST_CALLOC, except that if the allocation | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 172 | * fails, it marks the test as skipped rather than failed. | 
|  | 173 | */ | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 174 | #define TEST_CALLOC_OR_SKIP(pointer, item_count)            \ | 
| Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 175 | do {                                                    \ | 
|  | 176 | TEST_ASSERT((pointer) == NULL);                     \ | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 177 | if ((item_count) != 0) {                            \ | 
| Sergei Trofimovich | da2a33d | 2024-01-25 20:48:56 +0000 | [diff] [blame] | 178 | (pointer) = mbedtls_calloc((item_count),        \ | 
|  | 179 | sizeof(*(pointer))); \ | 
| Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 180 | TEST_ASSUME((pointer) != NULL);                 \ | 
|  | 181 | }                                                   \ | 
|  | 182 | } while (0) | 
|  | 183 |  | 
|  | 184 | /* For backwards compatibility */ | 
| Tom Cosgrove | a45d902 | 2023-07-21 11:34:44 +0100 | [diff] [blame] | 185 | #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] | 186 |  | 
|  | 187 | /** Compare two buffers and fail the test case if they differ. | 
|  | 188 | * | 
|  | 189 | * This macro expands to an instruction, not an expression. | 
|  | 190 | * It may jump to the \c exit label. | 
|  | 191 | * | 
|  | 192 | * \param p1        Pointer to the start of the first buffer. | 
|  | 193 | * \param size1     Size of the first buffer in bytes. | 
|  | 194 | *                  This expression may be evaluated multiple times. | 
|  | 195 | * \param p2        Pointer to the start of the second buffer. | 
|  | 196 | * \param size2     Size of the second buffer in bytes. | 
|  | 197 | *                  This expression may be evaluated multiple times. | 
|  | 198 | */ | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 199 | #define TEST_MEMORY_COMPARE(p1, size1, p2, size2)              \ | 
| Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 200 | do {                                                       \ | 
| Manuel Pégourié-Gonnard | a9a1b21 | 2023-02-09 09:15:04 +0100 | [diff] [blame] | 201 | TEST_EQUAL((size1), (size2));                          \ | 
| Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 202 | if ((size1) != 0) {                                    \ | 
|  | 203 | TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0);     \ | 
|  | 204 | }                                                      \ | 
|  | 205 | } while (0) | 
|  | 206 |  | 
|  | 207 | /* For backwards compatibility */ | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 208 | #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] | 209 |  | 
|  | 210 | /** | 
|  | 211 | * \brief   This macro tests the expression passed to it and skips the | 
|  | 212 | *          running test if it doesn't evaluate to 'true'. | 
|  | 213 | * | 
|  | 214 | * \param   TEST    The test expression to be tested. | 
|  | 215 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | #define TEST_ASSUME(TEST)                                 \ | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 217 | do {                                                    \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | if (!(TEST))                                      \ | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 219 | {                                                   \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | mbedtls_test_skip( #TEST, __LINE__, __FILE__); \ | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 221 | goto exit;                                      \ | 
|  | 222 | }                                                   \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | } while (0) | 
| Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 224 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | #define TEST_HELPER_ASSERT(a) if (!(a))                          \ | 
|  | 226 | {                                                                   \ | 
|  | 227 | mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n",    \ | 
|  | 228 | __FILE__, __LINE__, #a);              \ | 
|  | 229 | mbedtls_exit(1);                                              \ | 
|  | 230 | } | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 231 |  | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 232 | /** Return the smaller 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 smaller of \p x and \p y. | 
|  | 238 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 240 |  | 
|  | 241 | /** Return the larger of two values. | 
|  | 242 | * | 
|  | 243 | * \param x         An integer-valued expression without side effects. | 
|  | 244 | * \param y         An integer-valued expression without side effects. | 
|  | 245 | * | 
|  | 246 | * \return The larger of \p x and \p y. | 
|  | 247 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) | 
| Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 249 |  | 
| Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 250 | #endif /* TEST_MACROS_H */ |