Minos Galanakis | 2c824b4 | 2025-03-20 09:28:45 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * \file macros.h |
| 3 | * |
| 4 | * \brief This file contains generic macros for the purpose of testing. |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Copyright The Mbed TLS Contributors |
| 9 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 10 | */ |
| 11 | |
| 12 | #ifndef TEST_MACROS_H |
| 13 | #define TEST_MACROS_H |
| 14 | |
| 15 | #include "mbedtls/build_info.h" |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include "mbedtls/platform.h" |
| 20 | |
| 21 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 22 | #include "mbedtls/memory_buffer_alloc.h" |
| 23 | #endif |
| 24 | #include "common.h" |
| 25 | |
| 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 | * |
| 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 | */ |
| 43 | #define TEST_ASSERT(TEST) \ |
| 44 | do { \ |
| 45 | if (!(TEST)) \ |
| 46 | { \ |
| 47 | mbedtls_test_fail( #TEST, __LINE__, __FILE__); \ |
| 48 | goto exit; \ |
| 49 | } \ |
| 50 | } while (0) |
| 51 | |
| 52 | /** This macro asserts fails the test with given output message. |
| 53 | * |
| 54 | * \param MESSAGE The message to be outputed on assertion |
| 55 | */ |
| 56 | #define TEST_FAIL(MESSAGE) \ |
| 57 | do { \ |
| 58 | mbedtls_test_fail(MESSAGE, __LINE__, __FILE__); \ |
| 59 | goto exit; \ |
| 60 | } while (0) |
| 61 | |
| 62 | /** Evaluate two integer expressions and fail the test case if they have |
| 63 | * different values. |
| 64 | * |
| 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. |
| 70 | */ |
| 71 | #define TEST_EQUAL(expr1, expr2) \ |
| 72 | do { \ |
| 73 | if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \ |
| 74 | (unsigned long long) (expr1), (unsigned long long) (expr2))) \ |
| 75 | goto exit; \ |
| 76 | } while (0) |
| 77 | |
| 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 | */ |
| 84 | #define TEST_LE_U(expr1, expr2) \ |
| 85 | do { \ |
| 86 | if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \ |
| 87 | expr1, expr2)) \ |
| 88 | goto exit; \ |
| 89 | } while (0) |
| 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 | */ |
| 97 | #define TEST_LE_S(expr1, expr2) \ |
| 98 | do { \ |
| 99 | if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \ |
| 100 | expr1, expr2)) \ |
| 101 | goto exit; \ |
| 102 | } while (0) |
| 103 | |
| 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 |
| 108 | * put `mbedtls_free(pointer)` in the test's cleanup code. |
| 109 | * |
| 110 | * If \p item_count is zero, the resulting \p pointer will be \c NULL. |
| 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 | * |
| 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. |
| 122 | * |
| 123 | */ |
| 124 | #define TEST_CALLOC(pointer, item_count) \ |
| 125 | do { \ |
| 126 | TEST_ASSERT((pointer) == NULL); \ |
| 127 | if ((item_count) != 0) { \ |
| 128 | (pointer) = mbedtls_calloc((item_count), \ |
| 129 | sizeof(*(pointer))); \ |
| 130 | TEST_ASSERT((pointer) != NULL); \ |
| 131 | } \ |
| 132 | } while (0) |
| 133 | |
| 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 | * |
| 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. |
| 154 | */ |
| 155 | #define TEST_CALLOC_NONNULL(pointer, item_count) \ |
| 156 | do { \ |
| 157 | TEST_ASSERT((pointer) == NULL); \ |
| 158 | (pointer) = mbedtls_calloc((item_count), \ |
| 159 | sizeof(*(pointer))); \ |
| 160 | if (((pointer) == NULL) && ((item_count) == 0)) { \ |
| 161 | (pointer) = mbedtls_calloc(1, 1); \ |
| 162 | } \ |
| 163 | TEST_ASSERT((pointer) != NULL); \ |
| 164 | } while (0) |
| 165 | |
| 166 | /* For backwards compatibility */ |
| 167 | #define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count) |
| 168 | |
| 169 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
| 170 | * |
| 171 | * This macro behaves like #TEST_CALLOC, except that if the allocation |
| 172 | * fails, it marks the test as skipped rather than failed. |
| 173 | */ |
| 174 | #define TEST_CALLOC_OR_SKIP(pointer, item_count) \ |
| 175 | do { \ |
| 176 | TEST_ASSERT((pointer) == NULL); \ |
| 177 | if ((item_count) != 0) { \ |
| 178 | (pointer) = mbedtls_calloc((item_count), \ |
| 179 | sizeof(*(pointer))); \ |
| 180 | TEST_ASSUME((pointer) != NULL); \ |
| 181 | } \ |
| 182 | } while (0) |
| 183 | |
| 184 | /* For backwards compatibility */ |
| 185 | #define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count) |
| 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 | */ |
| 199 | #define TEST_MEMORY_COMPARE(p1, size1, p2, size2) \ |
| 200 | do { \ |
| 201 | TEST_EQUAL((size1), (size2)); \ |
| 202 | if ((size1) != 0) { \ |
| 203 | TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \ |
| 204 | } \ |
| 205 | } while (0) |
| 206 | |
| 207 | /* For backwards compatibility */ |
| 208 | #define ASSERT_COMPARE(p1, size1, p2, size2) TEST_MEMORY_COMPARE(p1, size1, p2, size2) |
| 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 | */ |
| 216 | #define TEST_ASSUME(TEST) \ |
| 217 | do { \ |
| 218 | if (!(TEST)) \ |
| 219 | { \ |
| 220 | mbedtls_test_skip( #TEST, __LINE__, __FILE__); \ |
| 221 | goto exit; \ |
| 222 | } \ |
| 223 | } while (0) |
| 224 | |
| 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 | } |
| 231 | |
| 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 | */ |
| 239 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 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 | */ |
| 248 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
| 249 | |
| 250 | #endif /* TEST_MACROS_H */ |