macros.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * \file macros.h
  3. *
  4. * \brief This file contains generic macros for the purpose of testing.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef TEST_MACROS_H
  23. #define TEST_MACROS_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #include <stdlib.h>
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #include <stdio.h>
  34. #define mbedtls_fprintf fprintf
  35. #define mbedtls_snprintf snprintf
  36. #define mbedtls_calloc calloc
  37. #define mbedtls_free free
  38. #define mbedtls_exit exit
  39. #define mbedtls_time time
  40. #define mbedtls_time_t time_t
  41. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  42. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  43. #endif
  44. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  45. #include "mbedtls/memory_buffer_alloc.h"
  46. #endif
  47. /**
  48. * \brief This macro tests the expression passed to it as a test step or
  49. * individual test in a test case.
  50. *
  51. * It allows a library function to return a value and return an error
  52. * code that can be tested.
  53. *
  54. * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
  55. * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
  56. * failure.
  57. *
  58. * This macro is not suitable for negative parameter validation tests,
  59. * as it assumes the test step will not create an error.
  60. *
  61. * Failing the test means:
  62. * - Mark this test case as failed.
  63. * - Print a message identifying the failure.
  64. * - Jump to the \c exit label.
  65. *
  66. * This macro expands to an instruction, not an expression.
  67. * It may jump to the \c exit label.
  68. *
  69. * \param TEST The test expression to be tested.
  70. */
  71. #define TEST_ASSERT( TEST ) \
  72. do { \
  73. if( ! (TEST) ) \
  74. { \
  75. mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
  76. goto exit; \
  77. } \
  78. } while( 0 )
  79. /** Evaluate two integer expressions and fail the test case if they have
  80. * different values.
  81. *
  82. * The two expressions should have the same signedness, otherwise the
  83. * comparison is not meaningful if the signed value is negative.
  84. *
  85. * \param expr1 An integral-typed expression to evaluate.
  86. * \param expr2 Another integral-typed expression to evaluate.
  87. */
  88. #define TEST_EQUAL( expr1, expr2 ) \
  89. do { \
  90. if( ! mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
  91. expr1, expr2 ) ) \
  92. goto exit; \
  93. } while( 0 )
  94. /** Allocate memory dynamically and fail the test case if this fails.
  95. * The allocated memory will be filled with zeros.
  96. *
  97. * You must set \p pointer to \c NULL before calling this macro and
  98. * put `mbedtls_free( pointer )` in the test's cleanup code.
  99. *
  100. * If \p length is zero, the resulting \p pointer will be \c NULL.
  101. * This is usually what we want in tests since API functions are
  102. * supposed to accept null pointers when a buffer size is zero.
  103. *
  104. * This macro expands to an instruction, not an expression.
  105. * It may jump to the \c exit label.
  106. *
  107. * \param pointer An lvalue where the address of the allocated buffer
  108. * will be stored.
  109. * This expression may be evaluated multiple times.
  110. * \param length Number of elements to allocate.
  111. * This expression may be evaluated multiple times.
  112. *
  113. */
  114. #define ASSERT_ALLOC( pointer, length ) \
  115. do \
  116. { \
  117. TEST_ASSERT( ( pointer ) == NULL ); \
  118. if( ( length ) != 0 ) \
  119. { \
  120. ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
  121. ( length ) ); \
  122. TEST_ASSERT( ( pointer ) != NULL ); \
  123. } \
  124. } \
  125. while( 0 )
  126. /** Allocate memory dynamically. If the allocation fails, skip the test case.
  127. *
  128. * This macro behaves like #ASSERT_ALLOC, except that if the allocation
  129. * fails, it marks the test as skipped rather than failed.
  130. */
  131. #define ASSERT_ALLOC_WEAK( pointer, length ) \
  132. do \
  133. { \
  134. TEST_ASSERT( ( pointer ) == NULL ); \
  135. if( ( length ) != 0 ) \
  136. { \
  137. ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
  138. ( length ) ); \
  139. TEST_ASSUME( ( pointer ) != NULL ); \
  140. } \
  141. } \
  142. while( 0 )
  143. /** Compare two buffers and fail the test case if they differ.
  144. *
  145. * This macro expands to an instruction, not an expression.
  146. * It may jump to the \c exit label.
  147. *
  148. * \param p1 Pointer to the start of the first buffer.
  149. * \param size1 Size of the first buffer in bytes.
  150. * This expression may be evaluated multiple times.
  151. * \param p2 Pointer to the start of the second buffer.
  152. * \param size2 Size of the second buffer in bytes.
  153. * This expression may be evaluated multiple times.
  154. */
  155. #define ASSERT_COMPARE( p1, size1, p2, size2 ) \
  156. do \
  157. { \
  158. TEST_ASSERT( ( size1 ) == ( size2 ) ); \
  159. if( ( size1 ) != 0 ) \
  160. TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
  161. } \
  162. while( 0 )
  163. /**
  164. * \brief This macro tests the expression passed to it and skips the
  165. * running test if it doesn't evaluate to 'true'.
  166. *
  167. * \param TEST The test expression to be tested.
  168. */
  169. #define TEST_ASSUME( TEST ) \
  170. do { \
  171. if( ! (TEST) ) \
  172. { \
  173. mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
  174. goto exit; \
  175. } \
  176. } while( 0 )
  177. #if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
  178. /**
  179. * \brief This macro tests the statement passed to it as a test step or
  180. * individual test in a test case. The macro assumes the test will fail
  181. * and will generate an error.
  182. *
  183. * It allows a library function to return a value and tests the return
  184. * code on return to confirm the given error code was returned.
  185. *
  186. * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
  187. * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
  188. * expected failure, and the test will pass.
  189. *
  190. * This macro is intended for negative parameter validation tests,
  191. * where the failing function may return an error value or call
  192. * MBEDTLS_PARAM_FAILED() to indicate the error.
  193. *
  194. * \param PARAM_ERROR_VALUE The expected error code.
  195. *
  196. * \param TEST The test expression to be tested.
  197. */
  198. #define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
  199. do { \
  200. mbedtls_test_param_failed_expect_call( ); \
  201. if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
  202. ( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
  203. { \
  204. mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
  205. goto exit; \
  206. } \
  207. mbedtls_test_param_failed_check_expected_call( ); \
  208. } while( 0 )
  209. /**
  210. * \brief This macro tests the statement passed to it as a test step or
  211. * individual test in a test case. The macro assumes the test will fail
  212. * and will generate an error.
  213. *
  214. * It assumes the library function under test cannot return a value and
  215. * assumes errors can only be indicated byt calls to
  216. * MBEDTLS_PARAM_FAILED().
  217. *
  218. * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
  219. * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
  220. * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
  221. * can be made.
  222. *
  223. * This macro is intended for negative parameter validation tests,
  224. * where the failing function can only return an error by calling
  225. * MBEDTLS_PARAM_FAILED() to indicate the error.
  226. *
  227. * \param TEST The test expression to be tested.
  228. */
  229. #define TEST_INVALID_PARAM( TEST ) \
  230. do { \
  231. memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
  232. sizeof( jmp_tmp ) ); \
  233. if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
  234. { \
  235. TEST; \
  236. mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
  237. goto exit; \
  238. } \
  239. mbedtls_test_param_failed_reset_state( ); \
  240. } while( 0 )
  241. #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
  242. /**
  243. * \brief This macro tests the statement passed to it as a test step or
  244. * individual test in a test case. The macro assumes the test will not fail.
  245. *
  246. * It assumes the library function under test cannot return a value and
  247. * assumes errors can only be indicated by calls to
  248. * MBEDTLS_PARAM_FAILED().
  249. *
  250. * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
  251. * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
  252. * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
  253. * can be made.
  254. *
  255. * This macro is intended to test that functions returning void
  256. * accept all of the parameter values they're supposed to accept - eg
  257. * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
  258. * that's allowed to be NULL happens to be NULL.
  259. *
  260. * Note: for functions that return something other that void,
  261. * checking that they accept all the parameters they're supposed to
  262. * accept is best done by using TEST_ASSERT() and checking the return
  263. * value as well.
  264. *
  265. * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
  266. * disabled, as it makes sense to check that the functions accept all
  267. * legal values even if this option is disabled - only in that case,
  268. * the test is more about whether the function segfaults than about
  269. * whether it invokes MBEDTLS_PARAM_FAILED().
  270. *
  271. * \param TEST The test expression to be tested.
  272. */
  273. #define TEST_VALID_PARAM( TEST ) \
  274. TEST_ASSERT( ( TEST, 1 ) );
  275. #define TEST_HELPER_ASSERT(a) if( !( a ) ) \
  276. { \
  277. mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
  278. __FILE__, __LINE__, #a ); \
  279. mbedtls_exit( 1 ); \
  280. }
  281. /** \def ARRAY_LENGTH
  282. * Return the number of elements of a static or stack array.
  283. *
  284. * \param array A value of array (not pointer) type.
  285. *
  286. * \return The number of elements of the array.
  287. */
  288. /* A correct implementation of ARRAY_LENGTH, but which silently gives
  289. * a nonsensical result if called with a pointer rather than an array. */
  290. #define ARRAY_LENGTH_UNSAFE( array ) \
  291. ( sizeof( array ) / sizeof( *( array ) ) )
  292. #if defined(__GNUC__)
  293. /* Test if arg and &(arg)[0] have the same type. This is true if arg is
  294. * an array but not if it's a pointer. */
  295. #define IS_ARRAY_NOT_POINTER( arg ) \
  296. ( ! __builtin_types_compatible_p( __typeof__( arg ), \
  297. __typeof__( &( arg )[0] ) ) )
  298. /* A compile-time constant with the value 0. If `const_expr` is not a
  299. * compile-time constant with a nonzero value, cause a compile-time error. */
  300. #define STATIC_ASSERT_EXPR( const_expr ) \
  301. ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
  302. /* Return the scalar value `value` (possibly promoted). This is a compile-time
  303. * constant if `value` is. `condition` must be a compile-time constant.
  304. * If `condition` is false, arrange to cause a compile-time error. */
  305. #define STATIC_ASSERT_THEN_RETURN( condition, value ) \
  306. ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
  307. #define ARRAY_LENGTH( array ) \
  308. ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
  309. ARRAY_LENGTH_UNSAFE( array ) ) )
  310. #else
  311. /* If we aren't sure the compiler supports our non-standard tricks,
  312. * fall back to the unsafe implementation. */
  313. #define ARRAY_LENGTH( array ) ARRAY_LENGTH_UNSAFE( array )
  314. #endif
  315. /** Return the smaller of two values.
  316. *
  317. * \param x An integer-valued expression without side effects.
  318. * \param y An integer-valued expression without side effects.
  319. *
  320. * \return The smaller of \p x and \p y.
  321. */
  322. #define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
  323. /** Return the larger of two values.
  324. *
  325. * \param x An integer-valued expression without side effects.
  326. * \param y An integer-valued expression without side effects.
  327. *
  328. * \return The larger of \p x and \p y.
  329. */
  330. #define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
  331. /*
  332. * 32-bit integer manipulation macros (big endian)
  333. */
  334. #ifndef GET_UINT32_BE
  335. #define GET_UINT32_BE(n,b,i) \
  336. { \
  337. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  338. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  339. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  340. | ( (uint32_t) (b)[(i) + 3] ); \
  341. }
  342. #endif
  343. #ifndef PUT_UINT32_BE
  344. #define PUT_UINT32_BE(n,b,i) \
  345. { \
  346. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  347. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  348. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  349. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  350. }
  351. #endif
  352. #endif /* TEST_MACROS_H */