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 | |
| 31 | #if defined(MBEDTLS_PLATFORM_C) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 32 | # include "mbedtls/platform.h" |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 33 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 34 | # include <stdio.h> |
| 35 | # define mbedtls_fprintf fprintf |
| 36 | # define mbedtls_snprintf snprintf |
| 37 | # define mbedtls_calloc calloc |
| 38 | # define mbedtls_free free |
| 39 | # define mbedtls_exit exit |
| 40 | # define mbedtls_time time |
| 41 | # define mbedtls_time_t time_t |
| 42 | # define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 43 | # define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 47 | # include "mbedtls/memory_buffer_alloc.h" |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 50 | /** |
| 51 | * \brief This macro tests the expression passed to it as a test step or |
| 52 | * individual test in a test case. |
| 53 | * |
| 54 | * It allows a library function to return a value and return an error |
| 55 | * code that can be tested. |
| 56 | * |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 57 | * Failing the test means: |
| 58 | * - Mark this test case as failed. |
| 59 | * - Print a message identifying the failure. |
| 60 | * - Jump to the \c exit label. |
| 61 | * |
| 62 | * This macro expands to an instruction, not an expression. |
| 63 | * It may jump to the \c exit label. |
| 64 | * |
| 65 | * \param TEST The test expression to be tested. |
| 66 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 67 | #define TEST_ASSERT(TEST) \ |
| 68 | do { \ |
| 69 | if (!(TEST)) { \ |
| 70 | mbedtls_test_fail(#TEST, __LINE__, __FILE__); \ |
| 71 | goto exit; \ |
| 72 | } \ |
| 73 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 74 | |
| 75 | /** Evaluate two expressions and fail the test case if they have different |
| 76 | * values. |
| 77 | * |
| 78 | * \param expr1 An expression to evaluate. |
| 79 | * \param expr2 The expected value of \p expr1. This can be any |
| 80 | * expression, but it is typically a constant. |
| 81 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 82 | #define TEST_EQUAL(expr1, expr2) TEST_ASSERT((expr1) == (expr2)) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 83 | |
| 84 | /** Allocate memory dynamically and fail the test case if this fails. |
| 85 | * The allocated memory will be filled with zeros. |
| 86 | * |
| 87 | * You must set \p pointer to \c NULL before calling this macro and |
| 88 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 89 | * |
| 90 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
| 91 | * This is usually what we want in tests since API functions are |
| 92 | * supposed to accept null pointers when a buffer size is zero. |
| 93 | * |
| 94 | * This macro expands to an instruction, not an expression. |
| 95 | * It may jump to the \c exit label. |
| 96 | * |
| 97 | * \param pointer An lvalue where the address of the allocated buffer |
| 98 | * will be stored. |
| 99 | * This expression may be evaluated multiple times. |
| 100 | * \param length Number of elements to allocate. |
| 101 | * This expression may be evaluated multiple times. |
| 102 | * |
| 103 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | #define ASSERT_ALLOC(pointer, length) \ |
| 105 | do { \ |
| 106 | TEST_ASSERT((pointer) == NULL); \ |
| 107 | if ((length) != 0) { \ |
| 108 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \ |
| 109 | TEST_ASSERT((pointer) != NULL); \ |
| 110 | } \ |
| 111 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 112 | |
| 113 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
| 114 | * |
| 115 | * This macro behaves like #ASSERT_ALLOC, except that if the allocation |
| 116 | * fails, it marks the test as skipped rather than failed. |
| 117 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 118 | #define ASSERT_ALLOC_WEAK(pointer, length) \ |
| 119 | do { \ |
| 120 | TEST_ASSERT((pointer) == NULL); \ |
| 121 | if ((length) != 0) { \ |
| 122 | (pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \ |
| 123 | TEST_ASSUME((pointer) != NULL); \ |
| 124 | } \ |
| 125 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 126 | |
| 127 | /** Compare two buffers and fail the test case if they differ. |
| 128 | * |
| 129 | * This macro expands to an instruction, not an expression. |
| 130 | * It may jump to the \c exit label. |
| 131 | * |
| 132 | * \param p1 Pointer to the start of the first buffer. |
| 133 | * \param size1 Size of the first buffer in bytes. |
| 134 | * This expression may be evaluated multiple times. |
| 135 | * \param p2 Pointer to the start of the second buffer. |
| 136 | * \param size2 Size of the second buffer in bytes. |
| 137 | * This expression may be evaluated multiple times. |
| 138 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 139 | #define ASSERT_COMPARE(p1, size1, p2, size2) \ |
| 140 | do { \ |
| 141 | TEST_ASSERT((size1) == (size2)); \ |
| 142 | if ((size1) != 0) \ |
| 143 | TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \ |
| 144 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * \brief This macro tests the expression passed to it and skips the |
| 148 | * running test if it doesn't evaluate to 'true'. |
| 149 | * |
| 150 | * \param TEST The test expression to be tested. |
| 151 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 152 | #define TEST_ASSUME(TEST) \ |
| 153 | do { \ |
| 154 | if (!(TEST)) { \ |
| 155 | mbedtls_test_skip(#TEST, __LINE__, __FILE__); \ |
| 156 | goto exit; \ |
| 157 | } \ |
| 158 | } while (0) |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 160 | #define TEST_HELPER_ASSERT(a) \ |
| 161 | if (!(a)) { \ |
| 162 | mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", __FILE__, \ |
| 163 | __LINE__, #a); \ |
| 164 | mbedtls_exit(1); \ |
| 165 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 166 | |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 167 | /** \def ARRAY_LENGTH |
| 168 | * Return the number of elements of a static or stack array. |
| 169 | * |
| 170 | * \param array A value of array (not pointer) type. |
| 171 | * |
| 172 | * \return The number of elements of the array. |
| 173 | */ |
| 174 | /* A correct implementation of ARRAY_LENGTH, but which silently gives |
| 175 | * a nonsensical result if called with a pointer rather than an array. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 176 | #define ARRAY_LENGTH_UNSAFE(array) (sizeof(array) / sizeof(*(array))) |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 177 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 178 | #if defined(__GNUC__) |
| 179 | /* Test if arg and &(arg)[0] have the same type. This is true if arg is |
| 180 | * an array but not if it's a pointer. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 181 | # define IS_ARRAY_NOT_POINTER(arg) \ |
| 182 | (!__builtin_types_compatible_p(__typeof__(arg), __typeof__(&(arg)[0]))) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 183 | /* A compile-time constant with the value 0. If `const_expr` is not a |
| 184 | * compile-time constant with a nonzero value, cause a compile-time error. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 185 | # define STATIC_ASSERT_EXPR(const_expr) \ |
| 186 | (0 && sizeof(struct { \ |
| 187 | unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); \ |
| 188 | })) |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 189 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 190 | /* Return the scalar value `value` (possibly promoted). This is a compile-time |
| 191 | * constant if `value` is. `condition` must be a compile-time constant. |
| 192 | * If `condition` is false, arrange to cause a compile-time error. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 193 | # define STATIC_ASSERT_THEN_RETURN(condition, value) \ |
| 194 | (STATIC_ASSERT_EXPR(condition) ? 0 : (value)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 195 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 196 | # define ARRAY_LENGTH(array) \ |
| 197 | (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \ |
| 198 | ARRAY_LENGTH_UNSAFE(array))) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 199 | |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 200 | #else |
| 201 | /* If we aren't sure the compiler supports our non-standard tricks, |
| 202 | * fall back to the unsafe implementation. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 203 | # define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array) |
Gilles Peskine | c86a165 | 2021-02-15 12:17:00 +0100 | [diff] [blame] | 204 | #endif |
| 205 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 206 | /** Return the smaller of two values. |
| 207 | * |
| 208 | * \param x An integer-valued expression without side effects. |
| 209 | * \param y An integer-valued expression without side effects. |
| 210 | * |
| 211 | * \return The smaller of \p x and \p y. |
| 212 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 213 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 214 | |
| 215 | /** Return the larger of two values. |
| 216 | * |
| 217 | * \param x An integer-valued expression without side effects. |
| 218 | * \param y An integer-valued expression without side effects. |
| 219 | * |
| 220 | * \return The larger of \p x and \p y. |
| 221 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 222 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * 32-bit integer manipulation macros (big endian) |
| 226 | */ |
| 227 | #ifndef GET_UINT32_BE |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 228 | # define GET_UINT32_BE(n, b, i) \ |
| 229 | { \ |
| 230 | (n) = ((uint32_t)(b)[(i)] << 24) | \ |
| 231 | ((uint32_t)(b)[(i) + 1] << 16) | \ |
| 232 | ((uint32_t)(b)[(i) + 2] << 8) | ((uint32_t)(b)[(i) + 3]); \ |
| 233 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 234 | #endif |
| 235 | |
| 236 | #ifndef PUT_UINT32_BE |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 237 | # define PUT_UINT32_BE(n, b, i) \ |
| 238 | { \ |
| 239 | (b)[(i)] = (unsigned char)((n) >> 24); \ |
| 240 | (b)[(i) + 1] = (unsigned char)((n) >> 16); \ |
| 241 | (b)[(i) + 2] = (unsigned char)((n) >> 8); \ |
| 242 | (b)[(i) + 3] = (unsigned char)((n)); \ |
| 243 | } |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 244 | #endif |
| 245 | |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 246 | #endif /* TEST_MACROS_H */ |