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