helpers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "mbedtls/config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
  31. defined(MBEDTLS_TEST_HOOKS)
  32. #define MBEDTLS_TEST_MUTEX_USAGE
  33. #endif
  34. #if defined(MBEDTLS_PLATFORM_C)
  35. #include "mbedtls/platform.h"
  36. #else
  37. #include <stdio.h>
  38. #define mbedtls_fprintf fprintf
  39. #define mbedtls_snprintf snprintf
  40. #define mbedtls_calloc calloc
  41. #define mbedtls_free free
  42. #define mbedtls_exit exit
  43. #define mbedtls_time time
  44. #define mbedtls_time_t time_t
  45. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  46. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  47. #endif
  48. #include <stddef.h>
  49. #include <stdint.h>
  50. #if defined(MBEDTLS_BIGNUM_C)
  51. #include "mbedtls/bignum.h"
  52. #endif
  53. typedef enum
  54. {
  55. MBEDTLS_TEST_RESULT_SUCCESS = 0,
  56. MBEDTLS_TEST_RESULT_FAILED,
  57. MBEDTLS_TEST_RESULT_SKIPPED
  58. } mbedtls_test_result_t;
  59. typedef struct
  60. {
  61. mbedtls_test_result_t result;
  62. const char *test;
  63. const char *filename;
  64. int line_no;
  65. unsigned long step;
  66. char line1[76];
  67. char line2[76];
  68. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  69. const char *mutex_usage_error;
  70. #endif
  71. }
  72. mbedtls_test_info_t;
  73. extern mbedtls_test_info_t mbedtls_test_info;
  74. int mbedtls_test_platform_setup( void );
  75. void mbedtls_test_platform_teardown( void );
  76. /**
  77. * \brief Record the current test case as a failure.
  78. *
  79. * This function can be called directly however it is usually
  80. * called via macros such as TEST_ASSERT, TEST_EQUAL,
  81. * PSA_ASSERT, etc...
  82. *
  83. * \note If the test case was already marked as failed, calling
  84. * `mbedtls_test_fail( )` again will not overwrite any
  85. * previous information about the failure.
  86. *
  87. * \param test Description of the failure or assertion that failed. This
  88. * MUST be a string literal.
  89. * \param line_no Line number where the failure originated.
  90. * \param filename Filename where the failure originated.
  91. */
  92. void mbedtls_test_fail( const char *test, int line_no, const char* filename );
  93. /**
  94. * \brief Record the current test case as skipped.
  95. *
  96. * This function can be called directly however it is usually
  97. * called via the TEST_ASSUME macro.
  98. *
  99. * \param test Description of the assumption that caused the test case to
  100. * be skipped. This MUST be a string literal.
  101. * \param line_no Line number where the test case was skipped.
  102. * \param filename Filename where the test case was skipped.
  103. */
  104. void mbedtls_test_skip( const char *test, int line_no, const char* filename );
  105. /**
  106. * \brief Set the test step number for failure reports.
  107. *
  108. * Call this function to display "step NNN" in addition to the
  109. * line number and file name if a test fails. Typically the "step
  110. * number" is the index of a for loop but it can be whatever you
  111. * want.
  112. *
  113. * \param step The step number to report.
  114. */
  115. void mbedtls_test_set_step( unsigned long step );
  116. /**
  117. * \brief Reset mbedtls_test_info to a ready/starting state.
  118. */
  119. void mbedtls_test_info_reset( void );
  120. /**
  121. * \brief Record the current test case as a failure if two integers
  122. * have a different value.
  123. *
  124. * This function is usually called via the macro
  125. * #TEST_EQUAL.
  126. *
  127. * \param test Description of the failure or assertion that failed. This
  128. * MUST be a string literal. This normally has the form
  129. * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
  130. * and EXPR2 has the value \p value2.
  131. * \param line_no Line number where the failure originated.
  132. * \param filename Filename where the failure originated.
  133. * \param value1 The first value to compare.
  134. * \param value2 The second value to compare.
  135. *
  136. * \return \c 1 if the values are equal, otherwise \c 0.
  137. */
  138. int mbedtls_test_equal( const char *test, int line_no, const char* filename,
  139. unsigned long long value1, unsigned long long value2 );
  140. /**
  141. * \brief This function decodes the hexadecimal representation of
  142. * data.
  143. *
  144. * \note The output buffer can be the same as the input buffer. For
  145. * any other overlapping of the input and output buffers, the
  146. * behavior is undefined.
  147. *
  148. * \param obuf Output buffer.
  149. * \param obufmax Size in number of bytes of \p obuf.
  150. * \param ibuf Input buffer.
  151. * \param len The number of unsigned char written in \p obuf. This must
  152. * not be \c NULL.
  153. *
  154. * \return \c 0 on success.
  155. * \return \c -1 if the output buffer is too small or the input string
  156. * is not a valid hexadecimal representation.
  157. */
  158. int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
  159. const char *ibuf, size_t *len );
  160. void mbedtls_test_hexify( unsigned char *obuf,
  161. const unsigned char *ibuf,
  162. int len );
  163. /**
  164. * Allocate and zeroize a buffer.
  165. *
  166. * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
  167. *
  168. * For convenience, dies if allocation fails.
  169. */
  170. unsigned char *mbedtls_test_zero_alloc( size_t len );
  171. /**
  172. * Allocate and fill a buffer from hex data.
  173. *
  174. * The buffer is sized exactly as needed. This allows to detect buffer
  175. * overruns (including overreads) when running the test suite under valgrind.
  176. *
  177. * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
  178. *
  179. * For convenience, dies if allocation fails.
  180. */
  181. unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
  182. int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
  183. uint32_t a_len, uint32_t b_len );
  184. #if defined(MBEDTLS_CHECK_PARAMS)
  185. typedef struct
  186. {
  187. const char *failure_condition;
  188. const char *file;
  189. int line;
  190. }
  191. mbedtls_test_param_failed_location_record_t;
  192. /**
  193. * \brief Get the location record of the last call to
  194. * mbedtls_test_param_failed().
  195. *
  196. * \note The call expectation is set up and active until the next call to
  197. * mbedtls_test_param_failed_check_expected_call() or
  198. * mbedtls_param_failed() that cancels it.
  199. */
  200. void mbedtls_test_param_failed_get_location_record(
  201. mbedtls_test_param_failed_location_record_t *location_record );
  202. /**
  203. * \brief State that a call to mbedtls_param_failed() is expected.
  204. *
  205. * \note The call expectation is set up and active until the next call to
  206. * mbedtls_test_param_failed_check_expected_call() or
  207. * mbedtls_param_failed that cancel it.
  208. */
  209. void mbedtls_test_param_failed_expect_call( void );
  210. /**
  211. * \brief Check whether mbedtls_param_failed() has been called as expected.
  212. *
  213. * \note Check whether mbedtls_param_failed() has been called between the
  214. * last call to mbedtls_test_param_failed_expect_call() and the call
  215. * to this function.
  216. *
  217. * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
  218. * mbedtls_param_failed() has been called.
  219. * \c -1 Otherwise.
  220. */
  221. int mbedtls_test_param_failed_check_expected_call( void );
  222. /**
  223. * \brief Get the address of the object of type jmp_buf holding the execution
  224. * state information used by mbedtls_param_failed() to do a long jump.
  225. *
  226. * \note If a call to mbedtls_param_failed() is not expected in the sense
  227. * that there is no call to mbedtls_test_param_failed_expect_call()
  228. * preceding it, then mbedtls_param_failed() will try to restore the
  229. * execution to the state stored in the jmp_buf object whose address
  230. * is returned by the present function.
  231. *
  232. * \note This function is intended to provide the parameter of the
  233. * setjmp() function to set-up where mbedtls_param_failed() should
  234. * long-jump if it has to. It is foreseen to be used as:
  235. *
  236. * setjmp( mbedtls_test_param_failed_get_state_buf() ).
  237. *
  238. * \note The type of the returned value is not jmp_buf as jmp_buf is an
  239. * an array type (C specification) and a function cannot return an
  240. * array type.
  241. *
  242. * \note The type of the returned value is not jmp_buf* as then the return
  243. * value couldn't be used by setjmp(), as its parameter's type is
  244. * jmp_buf.
  245. *
  246. * \return Address of the object of type jmp_buf holding the execution state
  247. * information used by mbedtls_param_failed() to do a long jump.
  248. */
  249. void* mbedtls_test_param_failed_get_state_buf( void );
  250. /**
  251. * \brief Reset the execution state used by mbedtls_param_failed() to do a
  252. * long jump.
  253. *
  254. * \note If a call to mbedtls_param_failed() is not expected in the sense
  255. * that there is no call to mbedtls_test_param_failed_expect_call()
  256. * preceding it, then mbedtls_param_failed() will try to restore the
  257. * execution state that this function reset.
  258. *
  259. * \note It is recommended to reset the execution state when the state
  260. * is not relevant anymore. That way an unexpected call to
  261. * mbedtls_param_failed() will not trigger a long jump with
  262. * undefined behavior but rather a long jump that will rather fault.
  263. */
  264. void mbedtls_test_param_failed_reset_state( void );
  265. #endif /* MBEDTLS_CHECK_PARAMS */
  266. #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  267. #include "test/fake_external_rng_for_test.h"
  268. #endif
  269. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  270. /** Permanently activate the mutex usage verification framework. See
  271. * threading_helpers.c for information. */
  272. void mbedtls_test_mutex_usage_init( void );
  273. /** Call this function after executing a test case to check for mutex usage
  274. * errors. */
  275. void mbedtls_test_mutex_usage_check( void );
  276. #endif /* MBEDTLS_TEST_MUTEX_USAGE */
  277. #if defined(MBEDTLS_TEST_HOOKS)
  278. /**
  279. * \brief Check that only a pure high-level error code is being combined with
  280. * a pure low-level error code as otherwise the resultant error code
  281. * would be corrupted.
  282. *
  283. * \note Both high-level and low-level error codes cannot be greater than
  284. * zero however can be zero. If one error code is zero then the
  285. * other error code is returned even if both codes are zero.
  286. *
  287. * \note If the check fails, fail the test currently being run.
  288. */
  289. void mbedtls_test_err_add_check( int high, int low,
  290. const char *file, int line);
  291. #endif
  292. #if defined(MBEDTLS_BIGNUM_C)
  293. /** Read an MPI from a string.
  294. *
  295. * Like mbedtls_mpi_read_string(), but size the resulting bignum based
  296. * on the number of digits in the string. In particular, construct a
  297. * bignum with 0 limbs for an empty string, and a bignum with leading 0
  298. * limbs if the string has sufficiently many leading 0 digits.
  299. *
  300. * This is important so that the "0 (null)" and "0 (1 limb)" and
  301. * "leading zeros" test cases do what they claim.
  302. *
  303. * \param[out] X The MPI object to populate. It must be initialized.
  304. * \param radix The radix (2 to 16).
  305. * \param[in] s The null-terminated string to read from.
  306. *
  307. * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
  308. */
  309. /* Since the library has exactly the desired behavior, this is trivial. */
  310. int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s );
  311. #endif /* MBEDTLS_BIGNUM_C */
  312. #endif /* TEST_HELPERS_H */