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 | |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 64 | /** Evaluate two integer expressions and fail the test case if they have |
| 65 | * different values. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 66 | * |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 67 | * The two expressions should have the same signedness, otherwise the |
| 68 | * comparison is not meaningful if the signed value is negative. |
| 69 | * |
| 70 | * \param expr1 An integral-typed expression to evaluate. |
| 71 | * \param expr2 Another integral-typed expression to evaluate. |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 72 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | #define TEST_EQUAL(expr1, expr2) \ |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 74 | do { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \ |
| 76 | expr1, expr2)) \ |
| 77 | goto exit; \ |
| 78 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 79 | |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 80 | /** Evaluate two unsigned integer expressions and fail the test case |
| 81 | * if they are not in increasing order (left <= right). |
| 82 | * |
| 83 | * \param expr1 An integral-typed expression to evaluate. |
| 84 | * \param expr2 Another integral-typed expression to evaluate. |
| 85 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | #define TEST_LE_U(expr1, expr2) \ |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 87 | do { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \ |
| 89 | expr1, expr2)) \ |
| 90 | goto exit; \ |
| 91 | } while (0) |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 92 | |
| 93 | /** Evaluate two signed integer expressions and fail the test case |
| 94 | * if they are not in increasing order (left <= right). |
| 95 | * |
| 96 | * \param expr1 An integral-typed expression to evaluate. |
| 97 | * \param expr2 Another integral-typed expression to evaluate. |
| 98 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | #define TEST_LE_S(expr1, expr2) \ |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 100 | do { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \ |
| 102 | expr1, expr2)) \ |
| 103 | goto exit; \ |
| 104 | } while (0) |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 105 | |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 106 | /** Allocate memory dynamically and fail the test case if this fails. |
| 107 | * The allocated memory will be filled with zeros. |
| 108 | * |
| 109 | * You must set \p pointer to \c NULL before calling this macro and |
| 110 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 111 | * |
| 112 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
| 113 | * This is usually what we want in tests since API functions are |
| 114 | * supposed to accept null pointers when a buffer size is zero. |
| 115 | * |
| 116 | * This macro expands to an instruction, not an expression. |
| 117 | * It may jump to the \c exit label. |
| 118 | * |
| 119 | * \param pointer An lvalue where the address of the allocated buffer |
| 120 | * will be stored. |
| 121 | * This expression may be evaluated multiple times. |
| 122 | * \param length Number of elements to allocate. |
| 123 | * This expression may be evaluated multiple times. |
| 124 | * |
| 125 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | #define ASSERT_ALLOC(pointer, length) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 127 | do \ |
| 128 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | TEST_ASSERT((pointer) == NULL); \ |
| 130 | if ((length) != 0) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 131 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), \ |
| 133 | (length)); \ |
| 134 | TEST_ASSERT((pointer) != NULL); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 135 | } \ |
| 136 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 138 | |
| 139 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
| 140 | * |
| 141 | * This macro behaves like #ASSERT_ALLOC, except that if the allocation |
| 142 | * fails, it marks the test as skipped rather than failed. |
| 143 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | #define ASSERT_ALLOC_WEAK(pointer, length) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 145 | do \ |
| 146 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | TEST_ASSERT((pointer) == NULL); \ |
| 148 | if ((length) != 0) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 149 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), \ |
| 151 | (length)); \ |
| 152 | TEST_ASSUME((pointer) != NULL); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 153 | } \ |
| 154 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 156 | |
| 157 | /** Compare two buffers and fail the test case if they differ. |
| 158 | * |
| 159 | * This macro expands to an instruction, not an expression. |
| 160 | * It may jump to the \c exit label. |
| 161 | * |
| 162 | * \param p1 Pointer to the start of the first buffer. |
| 163 | * \param size1 Size of the first buffer in bytes. |
| 164 | * This expression may be evaluated multiple times. |
| 165 | * \param p2 Pointer to the start of the second buffer. |
| 166 | * \param size2 Size of the second buffer in bytes. |
| 167 | * This expression may be evaluated multiple times. |
| 168 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | #define ASSERT_COMPARE(p1, size1, p2, size2) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 170 | do \ |
| 171 | { \ |
Manuel Pégourié-Gonnard | a9a1b21 | 2023-02-09 09:15:04 +0100 | [diff] [blame] | 172 | TEST_EQUAL((size1), (size2)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | if ((size1) != 0) \ |
| 174 | TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 175 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * \brief This macro tests the expression passed to it and skips the |
| 180 | * running test if it doesn't evaluate to 'true'. |
| 181 | * |
| 182 | * \param TEST The test expression to be tested. |
| 183 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | #define TEST_ASSUME(TEST) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 185 | do { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | if (!(TEST)) \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 187 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | mbedtls_test_skip( #TEST, __LINE__, __FILE__); \ |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 189 | goto exit; \ |
| 190 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | #define TEST_HELPER_ASSERT(a) if (!(a)) \ |
| 194 | { \ |
| 195 | mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \ |
| 196 | __FILE__, __LINE__, #a); \ |
| 197 | mbedtls_exit(1); \ |
| 198 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 199 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 200 | /** Return the smaller of two values. |
| 201 | * |
| 202 | * \param x An integer-valued expression without side effects. |
| 203 | * \param y An integer-valued expression without side effects. |
| 204 | * |
| 205 | * \return The smaller of \p x and \p y. |
| 206 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 208 | |
| 209 | /** Return the larger of two values. |
| 210 | * |
| 211 | * \param x An integer-valued expression without side effects. |
| 212 | * \param y An integer-valued expression without side effects. |
| 213 | * |
| 214 | * \return The larger of \p x and \p y. |
| 215 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 217 | |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 218 | #endif /* TEST_MACROS_H */ |