macros.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "mbedtls/build_info.h"
  25. #include <stdlib.h>
  26. #include "mbedtls/platform.h"
  27. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  28. #include "mbedtls/memory_buffer_alloc.h"
  29. #endif
  30. /**
  31. * \brief This macro tests the expression passed to it as a test step or
  32. * individual test in a test case.
  33. *
  34. * It allows a library function to return a value and return an error
  35. * code that can be tested.
  36. *
  37. * Failing the test means:
  38. * - Mark this test case as failed.
  39. * - Print a message identifying the failure.
  40. * - Jump to the \c exit label.
  41. *
  42. * This macro expands to an instruction, not an expression.
  43. * It may jump to the \c exit label.
  44. *
  45. * \param TEST The test expression to be tested.
  46. */
  47. #define TEST_ASSERT(TEST) \
  48. do { \
  49. if (!(TEST)) \
  50. { \
  51. mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
  52. goto exit; \
  53. } \
  54. } while (0)
  55. /** Evaluate two integer expressions and fail the test case if they have
  56. * different values.
  57. *
  58. * The two expressions should have the same signedness, otherwise the
  59. * comparison is not meaningful if the signed value is negative.
  60. *
  61. * \param expr1 An integral-typed expression to evaluate.
  62. * \param expr2 Another integral-typed expression to evaluate.
  63. */
  64. #define TEST_EQUAL(expr1, expr2) \
  65. do { \
  66. if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
  67. expr1, expr2)) \
  68. goto exit; \
  69. } while (0)
  70. /** Evaluate two unsigned integer expressions and fail the test case
  71. * if they are not in increasing order (left <= right).
  72. *
  73. * \param expr1 An integral-typed expression to evaluate.
  74. * \param expr2 Another integral-typed expression to evaluate.
  75. */
  76. #define TEST_LE_U(expr1, expr2) \
  77. do { \
  78. if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
  79. expr1, expr2)) \
  80. goto exit; \
  81. } while (0)
  82. /** Evaluate two signed integer expressions and fail the test case
  83. * if they are not in increasing order (left <= right).
  84. *
  85. * \param expr1 An integral-typed expression to evaluate.
  86. * \param expr2 Another integral-typed expression to evaluate.
  87. */
  88. #define TEST_LE_S(expr1, expr2) \
  89. do { \
  90. if (!mbedtls_test_le_s( #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_EQUAL((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. #define TEST_HELPER_ASSERT(a) if (!(a)) \
  178. { \
  179. mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
  180. __FILE__, __LINE__, #a); \
  181. mbedtls_exit(1); \
  182. }
  183. /** \def ARRAY_LENGTH
  184. * Return the number of elements of a static or stack array.
  185. *
  186. * \param array A value of array (not pointer) type.
  187. *
  188. * \return The number of elements of the array.
  189. */
  190. /* A correct implementation of ARRAY_LENGTH, but which silently gives
  191. * a nonsensical result if called with a pointer rather than an array. */
  192. #define ARRAY_LENGTH_UNSAFE(array) \
  193. (sizeof(array) / sizeof(*(array)))
  194. #if defined(__GNUC__)
  195. /* Test if arg and &(arg)[0] have the same type. This is true if arg is
  196. * an array but not if it's a pointer. */
  197. #define IS_ARRAY_NOT_POINTER(arg) \
  198. (!__builtin_types_compatible_p(__typeof__(arg), \
  199. __typeof__(&(arg)[0])))
  200. /* A compile-time constant with the value 0. If `const_expr` is not a
  201. * compile-time constant with a nonzero value, cause a compile-time error. */
  202. #define STATIC_ASSERT_EXPR(const_expr) \
  203. (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
  204. /* Return the scalar value `value` (possibly promoted). This is a compile-time
  205. * constant if `value` is. `condition` must be a compile-time constant.
  206. * If `condition` is false, arrange to cause a compile-time error. */
  207. #define STATIC_ASSERT_THEN_RETURN(condition, value) \
  208. (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
  209. #define ARRAY_LENGTH(array) \
  210. (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
  211. ARRAY_LENGTH_UNSAFE(array)))
  212. #else
  213. /* If we aren't sure the compiler supports our non-standard tricks,
  214. * fall back to the unsafe implementation. */
  215. #define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
  216. #endif
  217. /** Return the smaller of two values.
  218. *
  219. * \param x An integer-valued expression without side effects.
  220. * \param y An integer-valued expression without side effects.
  221. *
  222. * \return The smaller of \p x and \p y.
  223. */
  224. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  225. /** Return the larger of two values.
  226. *
  227. * \param x An integer-valued expression without side effects.
  228. * \param y An integer-valued expression without side effects.
  229. *
  230. * \return The larger of \p x and \p y.
  231. */
  232. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  233. #endif /* TEST_MACROS_H */