1 /** 2 * \file macros.h 3 * 4 * \brief This file contains generic macros for the purpose of testing. 5 */ 6 7 /* 8 * Copyright The Mbed TLS Contributors 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. 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 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 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 integer expressions and fail the test case if they have 88 * different values. 89 * 90 * The two expressions should have the same signedness, otherwise the 91 * comparison is not meaningful if the signed value is negative. 92 * 93 * \param expr1 An integral-typed expression to evaluate. 94 * \param expr2 Another integral-typed expression to evaluate. 95 */ 96 #define TEST_EQUAL( expr1, expr2 ) \ 97 do { \ 98 if( ! mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \ 99 expr1, expr2 ) ) \ 100 goto exit; \ 101 } while( 0 ) 102 103 /** Allocate memory dynamically and fail the test case if this fails. 104 * The allocated memory will be filled with zeros. 105 * 106 * You must set \p pointer to \c NULL before calling this macro and 107 * put `mbedtls_free( pointer )` in the test's cleanup code. 108 * 109 * If \p length is zero, the resulting \p pointer will be \c NULL. 110 * This is usually what we want in tests since API functions are 111 * supposed to accept null pointers when a buffer size is zero. 112 * 113 * This macro expands to an instruction, not an expression. 114 * It may jump to the \c exit label. 115 * 116 * \param pointer An lvalue where the address of the allocated buffer 117 * will be stored. 118 * This expression may be evaluated multiple times. 119 * \param length Number of elements to allocate. 120 * This expression may be evaluated multiple times. 121 * 122 */ 123 #define ASSERT_ALLOC( pointer, length ) \ 124 do \ 125 { \ 126 TEST_ASSERT( ( pointer ) == NULL ); \ 127 if( ( length ) != 0 ) \ 128 { \ 129 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ 130 ( length ) ); \ 131 TEST_ASSERT( ( pointer ) != NULL ); \ 132 } \ 133 } \ 134 while( 0 ) 135 136 /** Allocate memory dynamically. If the allocation fails, skip the test case. 137 * 138 * This macro behaves like #ASSERT_ALLOC, except that if the allocation 139 * fails, it marks the test as skipped rather than failed. 140 */ 141 #define ASSERT_ALLOC_WEAK( pointer, length ) \ 142 do \ 143 { \ 144 TEST_ASSERT( ( pointer ) == NULL ); \ 145 if( ( length ) != 0 ) \ 146 { \ 147 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \ 148 ( length ) ); \ 149 TEST_ASSUME( ( pointer ) != NULL ); \ 150 } \ 151 } \ 152 while( 0 ) 153 154 /** Compare two buffers and fail the test case if they differ. 155 * 156 * This macro expands to an instruction, not an expression. 157 * It may jump to the \c exit label. 158 * 159 * \param p1 Pointer to the start of the first buffer. 160 * \param size1 Size of the first buffer in bytes. 161 * This expression may be evaluated multiple times. 162 * \param p2 Pointer to the start of the second buffer. 163 * \param size2 Size of the second buffer in bytes. 164 * This expression may be evaluated multiple times. 165 */ 166 #define ASSERT_COMPARE( p1, size1, p2, size2 ) \ 167 do \ 168 { \ 169 TEST_ASSERT( ( size1 ) == ( size2 ) ); \ 170 if( ( size1 ) != 0 ) \ 171 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \ 172 } \ 173 while( 0 ) 174 175 /** 176 * \brief This macro tests the expression passed to it and skips the 177 * running test if it doesn't evaluate to 'true'. 178 * 179 * \param TEST The test expression to be tested. 180 */ 181 #define TEST_ASSUME( TEST ) \ 182 do { \ 183 if( ! (TEST) ) \ 184 { \ 185 mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \ 186 goto exit; \ 187 } \ 188 } while( 0 ) 189 190 #if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT) 191 /** 192 * \brief This macro tests the statement passed to it as a test step or 193 * individual test in a test case. The macro assumes the test will fail 194 * and will generate an error. 195 * 196 * It allows a library function to return a value and tests the return 197 * code on return to confirm the given error code was returned. 198 * 199 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure 200 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the 201 * expected failure, and the test will pass. 202 * 203 * This macro is intended for negative parameter validation tests, 204 * where the failing function may return an error value or call 205 * MBEDTLS_PARAM_FAILED() to indicate the error. 206 * 207 * \param PARAM_ERROR_VALUE The expected error code. 208 * 209 * \param TEST The test expression to be tested. 210 */ 211 #define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \ 212 do { \ 213 mbedtls_test_param_failed_expect_call( ); \ 214 if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \ 215 ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \ 216 { \ 217 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \ 218 goto exit; \ 219 } \ 220 mbedtls_test_param_failed_check_expected_call( ); \ 221 } while( 0 ) 222 223 /** 224 * \brief This macro tests the statement passed to it as a test step or 225 * individual test in a test case. The macro assumes the test will fail 226 * and will generate an error. 227 * 228 * It assumes the library function under test cannot return a value and 229 * assumes errors can only be indicated byt calls to 230 * MBEDTLS_PARAM_FAILED(). 231 * 232 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure 233 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the 234 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test 235 * can be made. 236 * 237 * This macro is intended for negative parameter validation tests, 238 * where the failing function can only return an error by calling 239 * MBEDTLS_PARAM_FAILED() to indicate the error. 240 * 241 * \param TEST The test expression to be tested. 242 */ 243 #define TEST_INVALID_PARAM( TEST ) \ 244 do { \ 245 memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \ 246 sizeof( jmp_tmp ) ); \ 247 if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \ 248 { \ 249 TEST; \ 250 mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \ 251 goto exit; \ 252 } \ 253 mbedtls_test_param_failed_reset_state( ); \ 254 } while( 0 ) 255 #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */ 256 257 /** 258 * \brief This macro tests the statement passed to it as a test step or 259 * individual test in a test case. The macro assumes the test will not fail. 260 * 261 * It assumes the library function under test cannot return a value and 262 * assumes errors can only be indicated by calls to 263 * MBEDTLS_PARAM_FAILED(). 264 * 265 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure 266 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the 267 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test 268 * can be made. 269 * 270 * This macro is intended to test that functions returning void 271 * accept all of the parameter values they're supposed to accept - eg 272 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter 273 * that's allowed to be NULL happens to be NULL. 274 * 275 * Note: for functions that return something other that void, 276 * checking that they accept all the parameters they're supposed to 277 * accept is best done by using TEST_ASSERT() and checking the return 278 * value as well. 279 * 280 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is 281 * disabled, as it makes sense to check that the functions accept all 282 * legal values even if this option is disabled - only in that case, 283 * the test is more about whether the function segfaults than about 284 * whether it invokes MBEDTLS_PARAM_FAILED(). 285 * 286 * \param TEST The test expression to be tested. 287 */ 288 #define TEST_VALID_PARAM( TEST ) \ 289 TEST_ASSERT( ( TEST, 1 ) ); 290 291 #define TEST_HELPER_ASSERT(a) if( !( a ) ) \ 292 { \ 293 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ 294 __FILE__, __LINE__, #a ); \ 295 mbedtls_exit( 1 ); \ 296 } 297 298 /** \def ARRAY_LENGTH 299 * Return the number of elements of a static or stack array. 300 * 301 * \param array A value of array (not pointer) type. 302 * 303 * \return The number of elements of the array. 304 */ 305 /* A correct implementation of ARRAY_LENGTH, but which silently gives 306 * a nonsensical result if called with a pointer rather than an array. */ 307 #define ARRAY_LENGTH_UNSAFE( array ) \ 308 ( sizeof( array ) / sizeof( *( array ) ) ) 309 310 #if defined(__GNUC__) 311 /* Test if arg and &(arg)[0] have the same type. This is true if arg is 312 * an array but not if it's a pointer. */ 313 #define IS_ARRAY_NOT_POINTER( arg ) \ 314 ( ! __builtin_types_compatible_p( __typeof__( arg ), \ 315 __typeof__( &( arg )[0] ) ) ) 316 /* A compile-time constant with the value 0. If `const_expr` is not a 317 * compile-time constant with a nonzero value, cause a compile-time error. */ 318 #define STATIC_ASSERT_EXPR( const_expr ) \ 319 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) ) 320 321 /* Return the scalar value `value` (possibly promoted). This is a compile-time 322 * constant if `value` is. `condition` must be a compile-time constant. 323 * If `condition` is false, arrange to cause a compile-time error. */ 324 #define STATIC_ASSERT_THEN_RETURN( condition, value ) \ 325 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) ) 326 327 #define ARRAY_LENGTH( array ) \ 328 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \ 329 ARRAY_LENGTH_UNSAFE( array ) ) ) 330 331 #else 332 /* If we aren't sure the compiler supports our non-standard tricks, 333 * fall back to the unsafe implementation. */ 334 #define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array ) 335 #endif 336 337 /** Return the smaller of two values. 338 * 339 * \param x An integer-valued expression without side effects. 340 * \param y An integer-valued expression without side effects. 341 * 342 * \return The smaller of \p x and \p y. 343 */ 344 #define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) ) 345 346 /** Return the larger of two values. 347 * 348 * \param x An integer-valued expression without side effects. 349 * \param y An integer-valued expression without side effects. 350 * 351 * \return The larger of \p x and \p y. 352 */ 353 #define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) ) 354 355 /* 356 * 32-bit integer manipulation macros (big endian) 357 */ 358 #ifndef GET_UINT32_BE 359 #define GET_UINT32_BE(n,b,i) \ 360 { \ 361 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 362 | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 363 | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 364 | ( (uint32_t) (b)[(i) + 3] ); \ 365 } 366 #endif 367 368 #ifndef PUT_UINT32_BE 369 #define PUT_UINT32_BE(n,b,i) \ 370 { \ 371 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 372 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 373 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 374 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 375 } 376 #endif 377 378 #endif /* TEST_MACROS_H */ 379