helpers.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * \file helpers.h
  3. *
  4. * \brief This file contains the prototypes of helper functions for the
  5. * purpose of testing.
  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. #ifndef TEST_HELPERS_H
  24. #define TEST_HELPERS_H
  25. /* Most fields of publicly available structs are private and are wrapped with
  26. * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
  27. * directly (without using the MBEDTLS_PRIVATE wrapper). */
  28. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  29. #include "mbedtls/build_info.h"
  30. #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
  31. defined(MBEDTLS_TEST_HOOKS)
  32. #define MBEDTLS_TEST_MUTEX_USAGE
  33. #endif
  34. #include "mbedtls/platform.h"
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #if defined(MBEDTLS_BIGNUM_C)
  38. #include "mbedtls/bignum.h"
  39. #endif
  40. /** The type of test case arguments that contain binary data. */
  41. typedef struct data_tag {
  42. uint8_t *x;
  43. uint32_t len;
  44. } data_t;
  45. typedef enum {
  46. MBEDTLS_TEST_RESULT_SUCCESS = 0,
  47. MBEDTLS_TEST_RESULT_FAILED,
  48. MBEDTLS_TEST_RESULT_SKIPPED
  49. } mbedtls_test_result_t;
  50. typedef struct {
  51. mbedtls_test_result_t result;
  52. const char *test;
  53. const char *filename;
  54. int line_no;
  55. unsigned long step;
  56. char line1[76];
  57. char line2[76];
  58. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  59. const char *mutex_usage_error;
  60. #endif
  61. }
  62. mbedtls_test_info_t;
  63. extern mbedtls_test_info_t mbedtls_test_info;
  64. int mbedtls_test_platform_setup(void);
  65. void mbedtls_test_platform_teardown(void);
  66. /**
  67. * \brief Record the current test case as a failure.
  68. *
  69. * This function can be called directly however it is usually
  70. * called via macros such as TEST_ASSERT, TEST_EQUAL,
  71. * PSA_ASSERT, etc...
  72. *
  73. * \note If the test case was already marked as failed, calling
  74. * `mbedtls_test_fail( )` again will not overwrite any
  75. * previous information about the failure.
  76. *
  77. * \param test Description of the failure or assertion that failed. This
  78. * MUST be a string literal.
  79. * \param line_no Line number where the failure originated.
  80. * \param filename Filename where the failure originated.
  81. */
  82. void mbedtls_test_fail(const char *test, int line_no, const char *filename);
  83. /**
  84. * \brief Record the current test case as skipped.
  85. *
  86. * This function can be called directly however it is usually
  87. * called via the TEST_ASSUME macro.
  88. *
  89. * \param test Description of the assumption that caused the test case to
  90. * be skipped. This MUST be a string literal.
  91. * \param line_no Line number where the test case was skipped.
  92. * \param filename Filename where the test case was skipped.
  93. */
  94. void mbedtls_test_skip(const char *test, int line_no, const char *filename);
  95. /**
  96. * \brief Set the test step number for failure reports.
  97. *
  98. * Call this function to display "step NNN" in addition to the
  99. * line number and file name if a test fails. Typically the "step
  100. * number" is the index of a for loop but it can be whatever you
  101. * want.
  102. *
  103. * \param step The step number to report.
  104. */
  105. void mbedtls_test_set_step(unsigned long step);
  106. /**
  107. * \brief Reset mbedtls_test_info to a ready/starting state.
  108. */
  109. void mbedtls_test_info_reset(void);
  110. /**
  111. * \brief Record the current test case as a failure if two integers
  112. * have a different value.
  113. *
  114. * This function is usually called via the macro
  115. * #TEST_EQUAL.
  116. *
  117. * \param test Description of the failure or assertion that failed. This
  118. * MUST be a string literal. This normally has the form
  119. * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
  120. * and EXPR2 has the value \p value2.
  121. * \param line_no Line number where the failure originated.
  122. * \param filename Filename where the failure originated.
  123. * \param value1 The first value to compare.
  124. * \param value2 The second value to compare.
  125. *
  126. * \return \c 1 if the values are equal, otherwise \c 0.
  127. */
  128. int mbedtls_test_equal(const char *test, int line_no, const char *filename,
  129. unsigned long long value1, unsigned long long value2);
  130. /**
  131. * \brief Record the current test case as a failure based
  132. * on comparing two unsigned integers.
  133. *
  134. * This function is usually called via the macro
  135. * #TEST_LE_U.
  136. *
  137. * \param test Description of the failure or assertion that failed. This
  138. * MUST be a string literal. This normally has the form
  139. * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
  140. * and EXPR2 has the value \p value2.
  141. * \param line_no Line number where the failure originated.
  142. * \param filename Filename where the failure originated.
  143. * \param value1 The first value to compare.
  144. * \param value2 The second value to compare.
  145. *
  146. * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
  147. */
  148. int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
  149. unsigned long long value1, unsigned long long value2);
  150. /**
  151. * \brief Record the current test case as a failure based
  152. * on comparing two signed integers.
  153. *
  154. * This function is usually called via the macro
  155. * #TEST_LE_S.
  156. *
  157. * \param test Description of the failure or assertion that failed. This
  158. * MUST be a string literal. This normally has the form
  159. * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
  160. * and EXPR2 has the value \p value2.
  161. * \param line_no Line number where the failure originated.
  162. * \param filename Filename where the failure originated.
  163. * \param value1 The first value to compare.
  164. * \param value2 The second value to compare.
  165. *
  166. * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
  167. */
  168. int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
  169. long long value1, long long value2);
  170. /**
  171. * \brief This function decodes the hexadecimal representation of
  172. * data.
  173. *
  174. * \note The output buffer can be the same as the input buffer. For
  175. * any other overlapping of the input and output buffers, the
  176. * behavior is undefined.
  177. *
  178. * \param obuf Output buffer.
  179. * \param obufmax Size in number of bytes of \p obuf.
  180. * \param ibuf Input buffer.
  181. * \param len The number of unsigned char written in \p obuf. This must
  182. * not be \c NULL.
  183. *
  184. * \return \c 0 on success.
  185. * \return \c -1 if the output buffer is too small or the input string
  186. * is not a valid hexadecimal representation.
  187. */
  188. int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
  189. const char *ibuf, size_t *len);
  190. void mbedtls_test_hexify(unsigned char *obuf,
  191. const unsigned char *ibuf,
  192. int len);
  193. /**
  194. * \brief Convert hexadecimal digit to an integer.
  195. *
  196. * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
  197. * `'a'` to `'f'`).
  198. * \param[out] uc On success, the value of the digit (0 to 15).
  199. *
  200. * \return 0 on success, -1 if \p c is not a hexadecimal digit.
  201. */
  202. int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
  203. /**
  204. * Allocate and zeroize a buffer.
  205. *
  206. * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
  207. *
  208. * For convenience, dies if allocation fails.
  209. */
  210. unsigned char *mbedtls_test_zero_alloc(size_t len);
  211. /**
  212. * Allocate and fill a buffer from hex data.
  213. *
  214. * The buffer is sized exactly as needed. This allows to detect buffer
  215. * overruns (including overreads) when running the test suite under valgrind.
  216. *
  217. * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
  218. *
  219. * For convenience, dies if allocation fails.
  220. */
  221. unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
  222. int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
  223. uint32_t a_len, uint32_t b_len);
  224. #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  225. #include "test/fake_external_rng_for_test.h"
  226. #endif
  227. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  228. /** Permanently activate the mutex usage verification framework. See
  229. * threading_helpers.c for information. */
  230. void mbedtls_test_mutex_usage_init(void);
  231. /** Call this function after executing a test case to check for mutex usage
  232. * errors. */
  233. void mbedtls_test_mutex_usage_check(void);
  234. #endif /* MBEDTLS_TEST_MUTEX_USAGE */
  235. #if defined(MBEDTLS_TEST_HOOKS)
  236. /**
  237. * \brief Check that only a pure high-level error code is being combined with
  238. * a pure low-level error code as otherwise the resultant error code
  239. * would be corrupted.
  240. *
  241. * \note Both high-level and low-level error codes cannot be greater than
  242. * zero however can be zero. If one error code is zero then the
  243. * other error code is returned even if both codes are zero.
  244. *
  245. * \note If the check fails, fail the test currently being run.
  246. */
  247. void mbedtls_test_err_add_check(int high, int low,
  248. const char *file, int line);
  249. #endif
  250. #endif /* TEST_HELPERS_H */