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 | |
| 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 28 | #include "mbedtls/config.h" |
| 29 | #else |
| 30 | #include MBEDTLS_CONFIG_FILE |
| 31 | #endif |
| 32 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | |
| 35 | #if defined(MBEDTLS_PLATFORM_C) |
| 36 | #include "mbedtls/platform.h" |
| 37 | #else |
| 38 | #include <stdio.h> |
| 39 | #define mbedtls_fprintf fprintf |
| 40 | #define mbedtls_snprintf snprintf |
| 41 | #define mbedtls_calloc calloc |
| 42 | #define mbedtls_free free |
| 43 | #define mbedtls_exit exit |
| 44 | #define mbedtls_time time |
| 45 | #define mbedtls_time_t time_t |
| 46 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 47 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 48 | #endif |
| 49 | |
| 50 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 51 | #include "mbedtls/memory_buffer_alloc.h" |
| 52 | #endif |
| 53 | |
Chris Jones | a6d155f | 2021-02-09 12:09:33 +0000 | [diff] [blame] | 54 | /** |
| 55 | * \brief This macro tests the expression passed to it as a test step or |
| 56 | * individual test in a test case. |
| 57 | * |
| 58 | * It allows a library function to return a value and return an error |
| 59 | * code that can be tested. |
| 60 | * |
| 61 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 62 | * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test |
| 63 | * failure. |
| 64 | * |
| 65 | * This macro is not suitable for negative parameter validation tests, |
| 66 | * as it assumes the test step will not create an error. |
| 67 | * |
| 68 | * Failing the test means: |
| 69 | * - Mark this test case as failed. |
| 70 | * - Print a message identifying the failure. |
| 71 | * - Jump to the \c exit label. |
| 72 | * |
| 73 | * This macro expands to an instruction, not an expression. |
| 74 | * It may jump to the \c exit label. |
| 75 | * |
| 76 | * \param TEST The test expression to be tested. |
| 77 | */ |
| 78 | #define TEST_ASSERT( TEST ) \ |
| 79 | do { \ |
| 80 | if( ! (TEST) ) \ |
| 81 | { \ |
| 82 | mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 83 | goto exit; \ |
| 84 | } \ |
| 85 | } while( 0 ) |
| 86 | |
| 87 | /** Evaluate two expressions and fail the test case if they have different |
| 88 | * values. |
| 89 | * |
| 90 | * \param expr1 An expression to evaluate. |
| 91 | * \param expr2 The expected value of \p expr1. This can be any |
| 92 | * expression, but it is typically a constant. |
| 93 | */ |
| 94 | #define TEST_EQUAL( expr1, expr2 ) \ |
| 95 | TEST_ASSERT( ( expr1 ) == ( expr2 ) ) |
| 96 | |
| 97 | /** Allocate memory dynamically and fail the test case if this fails. |
| 98 | * The allocated memory will be filled with zeros. |
| 99 | * |
| 100 | * You must set \p pointer to \c NULL before calling this macro and |
| 101 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 102 | * |
| 103 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
| 104 | * This is usually what we want in tests since API functions are |
| 105 | * supposed to accept null pointers when a buffer size is zero. |
| 106 | * |
| 107 | * This macro expands to an instruction, not an expression. |
| 108 | * It may jump to the \c exit label. |
| 109 | * |
| 110 | * \param pointer An lvalue where the address of the allocated buffer |
| 111 | * will be stored. |
| 112 | * This expression may be evaluated multiple times. |
| 113 | * \param length Number of elements to allocate. |
| 114 | * This expression may be evaluated multiple times. |
| 115 | * |
| 116 | */ |
| 117 | #define ASSERT_ALLOC( pointer, length ) \ |
| 118 | do \ |
| 119 | { \ |
| 120 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 121 | if( ( length ) != 0 ) \ |
| 122 | { \ |
| 123 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 124 | ( length ) ); \ |
| 125 | TEST_ASSERT( ( pointer ) != NULL ); \ |
| 126 | } \ |
| 127 | } \ |
| 128 | while( 0 ) |
| 129 | |
| 130 | /** Allocate memory dynamically. If the allocation fails, skip the test case. |
| 131 | * |
| 132 | * This macro behaves like #ASSERT_ALLOC, except that if the allocation |
| 133 | * fails, it marks the test as skipped rather than failed. |
| 134 | */ |
| 135 | #define ASSERT_ALLOC_WEAK( pointer, length ) \ |
| 136 | do \ |
| 137 | { \ |
| 138 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 139 | if( ( length ) != 0 ) \ |
| 140 | { \ |
| 141 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 142 | ( length ) ); \ |
| 143 | TEST_ASSUME( ( pointer ) != NULL ); \ |
| 144 | } \ |
| 145 | } \ |
| 146 | while( 0 ) |
| 147 | |
| 148 | /** Compare two buffers and fail the test case if they differ. |
| 149 | * |
| 150 | * This macro expands to an instruction, not an expression. |
| 151 | * It may jump to the \c exit label. |
| 152 | * |
| 153 | * \param p1 Pointer to the start of the first buffer. |
| 154 | * \param size1 Size of the first buffer in bytes. |
| 155 | * This expression may be evaluated multiple times. |
| 156 | * \param p2 Pointer to the start of the second buffer. |
| 157 | * \param size2 Size of the second buffer in bytes. |
| 158 | * This expression may be evaluated multiple times. |
| 159 | */ |
| 160 | #define ASSERT_COMPARE( p1, size1, p2, size2 ) \ |
| 161 | do \ |
| 162 | { \ |
| 163 | TEST_ASSERT( ( size1 ) == ( size2 ) ); \ |
| 164 | if( ( size1 ) != 0 ) \ |
| 165 | TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \ |
| 166 | } \ |
| 167 | while( 0 ) |
| 168 | |
| 169 | /** |
| 170 | * \brief This macro tests the expression passed to it and skips the |
| 171 | * running test if it doesn't evaluate to 'true'. |
| 172 | * |
| 173 | * \param TEST The test expression to be tested. |
| 174 | */ |
| 175 | #define TEST_ASSUME( TEST ) \ |
| 176 | do { \ |
| 177 | if( ! (TEST) ) \ |
| 178 | { \ |
| 179 | mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \ |
| 180 | goto exit; \ |
| 181 | } \ |
| 182 | } while( 0 ) |
| 183 | |
| 184 | #if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT) |
| 185 | /** |
| 186 | * \brief This macro tests the statement passed to it as a test step or |
| 187 | * individual test in a test case. The macro assumes the test will fail |
| 188 | * and will generate an error. |
| 189 | * |
| 190 | * It allows a library function to return a value and tests the return |
| 191 | * code on return to confirm the given error code was returned. |
| 192 | * |
| 193 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 194 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 195 | * expected failure, and the test will pass. |
| 196 | * |
| 197 | * This macro is intended for negative parameter validation tests, |
| 198 | * where the failing function may return an error value or call |
| 199 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
| 200 | * |
| 201 | * \param PARAM_ERROR_VALUE The expected error code. |
| 202 | * |
| 203 | * \param TEST The test expression to be tested. |
| 204 | */ |
| 205 | #define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \ |
| 206 | do { \ |
| 207 | mbedtls_test_param_failed_expect_call( ); \ |
| 208 | if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \ |
| 209 | ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \ |
| 210 | { \ |
| 211 | mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 212 | goto exit; \ |
| 213 | } \ |
| 214 | mbedtls_test_param_failed_check_expected_call( ); \ |
| 215 | } while( 0 ) |
| 216 | |
| 217 | /** |
| 218 | * \brief This macro tests the statement passed to it as a test step or |
| 219 | * individual test in a test case. The macro assumes the test will fail |
| 220 | * and will generate an error. |
| 221 | * |
| 222 | * It assumes the library function under test cannot return a value and |
| 223 | * assumes errors can only be indicated byt calls to |
| 224 | * MBEDTLS_PARAM_FAILED(). |
| 225 | * |
| 226 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 227 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 228 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 229 | * can be made. |
| 230 | * |
| 231 | * This macro is intended for negative parameter validation tests, |
| 232 | * where the failing function can only return an error by calling |
| 233 | * MBEDTLS_PARAM_FAILED() to indicate the error. |
| 234 | * |
| 235 | * \param TEST The test expression to be tested. |
| 236 | */ |
| 237 | #define TEST_INVALID_PARAM( TEST ) \ |
| 238 | do { \ |
| 239 | memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \ |
| 240 | sizeof( jmp_tmp ) ); \ |
| 241 | if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \ |
| 242 | { \ |
| 243 | TEST; \ |
| 244 | mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \ |
| 245 | goto exit; \ |
| 246 | } \ |
| 247 | mbedtls_test_param_failed_reset_state( ); \ |
| 248 | } while( 0 ) |
| 249 | #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */ |
| 250 | |
| 251 | /** |
| 252 | * \brief This macro tests the statement passed to it as a test step or |
| 253 | * individual test in a test case. The macro assumes the test will not fail. |
| 254 | * |
| 255 | * It assumes the library function under test cannot return a value and |
| 256 | * assumes errors can only be indicated by calls to |
| 257 | * MBEDTLS_PARAM_FAILED(). |
| 258 | * |
| 259 | * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure |
| 260 | * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the |
| 261 | * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test |
| 262 | * can be made. |
| 263 | * |
| 264 | * This macro is intended to test that functions returning void |
| 265 | * accept all of the parameter values they're supposed to accept - eg |
| 266 | * that they don't call MBEDTLS_PARAM_FAILED() when a parameter |
| 267 | * that's allowed to be NULL happens to be NULL. |
| 268 | * |
| 269 | * Note: for functions that return something other that void, |
| 270 | * checking that they accept all the parameters they're supposed to |
| 271 | * accept is best done by using TEST_ASSERT() and checking the return |
| 272 | * value as well. |
| 273 | * |
| 274 | * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is |
| 275 | * disabled, as it makes sense to check that the functions accept all |
| 276 | * legal values even if this option is disabled - only in that case, |
| 277 | * the test is more about whether the function segfaults than about |
| 278 | * whether it invokes MBEDTLS_PARAM_FAILED(). |
| 279 | * |
| 280 | * \param TEST The test expression to be tested. |
| 281 | */ |
| 282 | #define TEST_VALID_PARAM( TEST ) \ |
| 283 | TEST_ASSERT( ( TEST, 1 ) ); |
| 284 | |
| 285 | /** Allocate memory dynamically and fail the test case if this fails. |
| 286 | * |
| 287 | * You must set \p pointer to \c NULL before calling this macro and |
| 288 | * put `mbedtls_free( pointer )` in the test's cleanup code. |
| 289 | * |
| 290 | * If \p length is zero, the resulting \p pointer will be \c NULL. |
| 291 | * This is usually what we want in tests since API functions are |
| 292 | * supposed to accept null pointers when a buffer size is zero. |
| 293 | * |
| 294 | * This macro expands to an instruction, not an expression. |
| 295 | * It may jump to the \c exit label. |
| 296 | * |
| 297 | * \param pointer An lvalue where the address of the allocated buffer |
| 298 | * will be stored. |
| 299 | * This expression may be evaluated multiple times. |
| 300 | * \param length Number of elements to allocate. |
| 301 | * This expression may be evaluated multiple times. |
| 302 | * |
| 303 | */ |
| 304 | #define ASSERT_ALLOC( pointer, length ) \ |
| 305 | do \ |
| 306 | { \ |
| 307 | TEST_ASSERT( ( pointer ) == NULL ); \ |
| 308 | if( ( length ) != 0 ) \ |
| 309 | { \ |
| 310 | ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ |
| 311 | ( length ) ); \ |
| 312 | TEST_ASSERT( ( pointer ) != NULL ); \ |
| 313 | } \ |
| 314 | } \ |
| 315 | while( 0 ) |
| 316 | |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 317 | #define TEST_HELPER_ASSERT(a) if( !( a ) ) \ |
| 318 | { \ |
| 319 | mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ |
| 320 | __FILE__, __LINE__, #a ); \ |
| 321 | mbedtls_exit( 1 ); \ |
| 322 | } |
| 323 | |
| 324 | #if defined(__GNUC__) |
| 325 | /* Test if arg and &(arg)[0] have the same type. This is true if arg is |
| 326 | * an array but not if it's a pointer. */ |
| 327 | #define IS_ARRAY_NOT_POINTER( arg ) \ |
| 328 | ( ! __builtin_types_compatible_p( __typeof__( arg ), \ |
| 329 | __typeof__( &( arg )[0] ) ) ) |
| 330 | #else |
| 331 | /* On platforms where we don't know how to implement this check, |
| 332 | * omit it. Oh well, a non-portable check is better than nothing. */ |
| 333 | #define IS_ARRAY_NOT_POINTER( arg ) 1 |
| 334 | #endif |
| 335 | |
| 336 | /* A compile-time constant with the value 0. If `const_expr` is not a |
| 337 | * compile-time constant with a nonzero value, cause a compile-time error. */ |
| 338 | #define STATIC_ASSERT_EXPR( const_expr ) \ |
makise-homura | e74f372 | 2020-08-18 23:57:48 +0300 | [diff] [blame] | 339 | ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) ) |
Ronald Cron | 849930a | 2020-06-03 08:06:47 +0200 | [diff] [blame] | 340 | /* Return the scalar value `value` (possibly promoted). This is a compile-time |
| 341 | * constant if `value` is. `condition` must be a compile-time constant. |
| 342 | * If `condition` is false, arrange to cause a compile-time error. */ |
| 343 | #define STATIC_ASSERT_THEN_RETURN( condition, value ) \ |
| 344 | ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) ) |
| 345 | |
| 346 | #define ARRAY_LENGTH_UNSAFE( array ) \ |
| 347 | ( sizeof( array ) / sizeof( *( array ) ) ) |
| 348 | /** Return the number of elements of a static or stack array. |
| 349 | * |
| 350 | * \param array A value of array (not pointer) type. |
| 351 | * |
| 352 | * \return The number of elements of the array. |
| 353 | */ |
| 354 | #define ARRAY_LENGTH( array ) \ |
| 355 | ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \ |
| 356 | ARRAY_LENGTH_UNSAFE( array ) ) ) |
| 357 | |
| 358 | /** Return the smaller of two values. |
| 359 | * |
| 360 | * \param x An integer-valued expression without side effects. |
| 361 | * \param y An integer-valued expression without side effects. |
| 362 | * |
| 363 | * \return The smaller of \p x and \p y. |
| 364 | */ |
| 365 | #define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) ) |
| 366 | |
| 367 | /** Return the larger of two values. |
| 368 | * |
| 369 | * \param x An integer-valued expression without side effects. |
| 370 | * \param y An integer-valued expression without side effects. |
| 371 | * |
| 372 | * \return The larger of \p x and \p y. |
| 373 | */ |
| 374 | #define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) ) |
| 375 | |
| 376 | /* |
| 377 | * 32-bit integer manipulation macros (big endian) |
| 378 | */ |
| 379 | #ifndef GET_UINT32_BE |
| 380 | #define GET_UINT32_BE(n,b,i) \ |
| 381 | { \ |
| 382 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
| 383 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
| 384 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
| 385 | | ( (uint32_t) (b)[(i) + 3] ); \ |
| 386 | } |
| 387 | #endif |
| 388 | |
| 389 | #ifndef PUT_UINT32_BE |
| 390 | #define PUT_UINT32_BE(n,b,i) \ |
| 391 | { \ |
| 392 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 393 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 394 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 395 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 396 | } |
| 397 | #endif |
| 398 | |
Ronald Cron | 4b8b199 | 2020-06-09 13:52:23 +0200 | [diff] [blame] | 399 | #endif /* TEST_MACROS_H */ |