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