blob: a939b1c0e0e0162389d8c802842e23c2a4fbb303 [file] [log] [blame]
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02001/**
2 * \file helpers.h
3 *
4 * \brief This file contains the prototypes of helper functions for the
5 * purpose of testing.
6 */
7
Bence Szépkúti86974652020-06-15 11:59:37 +02008/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020011 */
12
13#ifndef TEST_HELPERS_H
14#define TEST_HELPERS_H
15
Mateusz Starzykb1982722021-05-27 14:46:48 +020016/* Most fields of publicly available structs are private and are wrapped with
17 * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
18 * directly (without using the MBEDTLS_PRIVATE wrapper). */
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020019#define MBEDTLS_ALLOW_PRIVATE_ACCESS
20
Bence Szépkútic662b362021-05-27 11:25:03 +020021#include "mbedtls/build_info.h"
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020022
Gilles Peskinefa8ec262023-11-22 17:55:43 +010023#if defined(__SANITIZE_ADDRESS__) /* gcc -fsanitize=address */
24# define MBEDTLS_TEST_HAVE_ASAN
25#endif
26#if defined(__has_feature)
27# if __has_feature(address_sanitizer) /* clang -fsanitize=address */
28# define MBEDTLS_TEST_HAVE_ASAN
29# endif
30# if __has_feature(memory_sanitizer) /* clang -fsanitize=memory */
31# define MBEDTLS_TEST_HAVE_MSAN
32# endif
33# if __has_feature(thread_sanitizer) /* clang -fsanitize=thread */
34# define MBEDTLS_TEST_HAVE_TSAN
35# endif
36#endif
37
Gilles Peskine2a4c5982021-01-29 21:18:09 +010038#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
39 defined(MBEDTLS_TEST_HOOKS)
Paul Elliott3d2db892024-01-19 20:42:56 +000040#include "mbedtls/threading.h"
Gilles Peskine2a4c5982021-01-29 21:18:09 +010041#define MBEDTLS_TEST_MUTEX_USAGE
42#endif
43
Ronald Cronf40529d2020-06-09 16:27:37 +020044#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020045
46#include <stddef.h>
47#include <stdint.h>
48
Gilles Peskineebc49e52021-06-11 14:13:53 +020049#if defined(MBEDTLS_BIGNUM_C)
50#include "mbedtls/bignum.h"
51#endif
52
Gilles Peskine571576f2022-09-20 21:37:56 +020053/** The type of test case arguments that contain binary data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010054typedef struct data_tag {
55 uint8_t *x;
Gilles Peskine571576f2022-09-20 21:37:56 +020056 uint32_t len;
57} data_t;
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000060 MBEDTLS_TEST_RESULT_SUCCESS = 0,
61 MBEDTLS_TEST_RESULT_FAILED,
62 MBEDTLS_TEST_RESULT_SKIPPED
63} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000064
Paul Elliott5c498f32023-10-31 16:38:56 +000065#define MBEDTLS_TEST_LINE_LENGTH 76
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000068 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000069 const char *test;
70 const char *filename;
71 int line_no;
72 unsigned long step;
Paul Elliott5c498f32023-10-31 16:38:56 +000073 char line1[MBEDTLS_TEST_LINE_LENGTH];
74 char line2[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010075#if defined(MBEDTLS_TEST_MUTEX_USAGE)
76 const char *mutex_usage_error;
77#endif
Paul Elliottc7a1e992023-11-03 18:44:57 +000078#if defined(MBEDTLS_BIGNUM_C)
79 unsigned case_uses_negative_0;
80#endif
Chris Jones9634bb12021-01-20 15:56:42 +000081}
Chris Jonese60e2ae2021-01-20 17:51:47 +000082mbedtls_test_info_t;
Paul Elliott4580d4d2023-10-27 18:41:02 +010083
84/**
85 * \brief Get the current test result status
86 *
87 * \return The current test result status
88 */
89mbedtls_test_result_t mbedtls_test_get_result(void);
90
91/**
92 * \brief Get the current test name/description
93 *
94 * \return The current test name/description
95 */
96const char *mbedtls_test_get_test(void);
97
98/**
99 * \brief Get the current test filename
100 *
101 * \return The current test filename
102 */
103const char *mbedtls_get_test_filename(void);
104
105/**
106 * \brief Get the current test file line number (for failure / skip)
107 *
108 * \return The current test file line number (for failure / skip)
109 */
110int mbedtls_test_get_line_no(void);
111
112/**
113 * \brief Increment the current test step.
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000114 *
115 * \note Calling this function from within multiple threads at the
116 * same time is not recommended - whilst it is entirely thread
117 * safe, the order of calls to this function can obviously not
118 * be ensured, so unexpected results may occur.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100119 */
120void mbedtls_test_increment_step(void);
121
122/**
123 * \brief Get the current test step
124 *
125 * \return The current test step
126 */
127unsigned long mbedtls_test_get_step(void);
128
129/**
130 * \brief Get the current test line buffer 1
131 *
Paul Elliott65064262023-11-27 17:29:05 +0000132 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
133 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100134 */
Paul Elliott65064262023-11-27 17:29:05 +0000135void mbedtls_test_get_line1(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100136
137/**
138 * \brief Get the current test line buffer 2
139 *
Paul Elliott65064262023-11-27 17:29:05 +0000140 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
141 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100142 */
Paul Elliott65064262023-11-27 17:29:05 +0000143void mbedtls_test_get_line2(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100144
145#if defined(MBEDTLS_TEST_MUTEX_USAGE)
146/**
147 * \brief Get the current mutex usage error message
148 *
149 * \return The current mutex error message (may be NULL if no error)
150 */
151const char *mbedtls_test_get_mutex_usage_error(void);
152
153/**
154 * \brief Set the current mutex usage error message
155 *
156 * \note This will only set the mutex error message if one has not
157 * already been set, or if we are clearing the message (msg is
158 * NULL)
159 *
160 * \param msg Error message to set (can be NULL to clear)
161 */
162void mbedtls_test_set_mutex_usage_error(const char *msg);
163#endif
164
Paul Elliottc7a1e992023-11-03 18:44:57 +0000165#if defined(MBEDTLS_BIGNUM_C)
166
167/**
168 * \brief Get whether the current test is a bignum test that uses
169 * negative zero.
170 *
171 * \return non zero if the current test uses bignum negative zero.
172 */
173unsigned mbedtls_test_get_case_uses_negative_0(void);
174
175/**
176 * \brief Indicate that the current test uses bignum negative zero.
177 *
178 * \note This function is called if the current test case had an
179 * input parsed with mbedtls_test_read_mpi() that is a negative
180 * 0 (`"-"`, `"-0"`, `"-00"`, etc., constructing a result with
181 * the sign bit set to -1 and the value being all-limbs-0,
182 * which is not a valid representation in #mbedtls_mpi but is
183 * tested for robustness). *
184 */
185void mbedtls_test_increment_case_uses_negative_0(void);
186#endif
Chris Jones9634bb12021-01-20 15:56:42 +0000187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188int mbedtls_test_platform_setup(void);
189void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +0200190
Ronald Crona0c25392020-06-18 10:10:46 +0200191/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000192 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +0000193 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000194 * This function can be called directly however it is usually
195 * called via macros such as TEST_ASSERT, TEST_EQUAL,
196 * PSA_ASSERT, etc...
197 *
198 * \note If the test case was already marked as failed, calling
199 * `mbedtls_test_fail( )` again will not overwrite any
200 * previous information about the failure.
201 *
202 * \param test Description of the failure or assertion that failed. This
203 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +0000204 * \param line_no Line number where the failure originated.
205 * \param filename Filename where the failure originated.
206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +0000208
209/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000210 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000211 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000212 * This function can be called directly however it is usually
213 * called via the TEST_ASSUME macro.
214 *
215 * \param test Description of the assumption that caused the test case to
216 * be skipped. This MUST be a string literal.
217 * \param line_no Line number where the test case was skipped.
218 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000219 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000221
Chris Jones567e0ad2021-02-03 12:07:01 +0000222/**
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000223 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000224 *
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000225 * Call this function to display "step NNN" in addition to the
226 * line number and file name if a test fails. Typically the
227 * "step number" is the index of a for loop but it can be
228 * whatever you want.
229 *
230 * \note Calling this function from a within multiple threads at the
231 * same time is not recommended - whilst it is entirely thread
232 * safe, the order of calls to this function can obviously not
233 * be ensured, so unexpected results may occur.
Chris Jones9634bb12021-01-20 15:56:42 +0000234 *
235 * \param step The step number to report.
236 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000238
Chris Jones567e0ad2021-02-03 12:07:01 +0000239/**
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000240 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000243
Paul Elliott3d2db892024-01-19 20:42:56 +0000244#ifdef MBEDTLS_TEST_MUTEX_USAGE
Ronald Crona0c25392020-06-18 10:10:46 +0200245/**
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000246 * \brief Get the test info data mutex.
Paul Elliott3d2db892024-01-19 20:42:56 +0000247 *
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000248 * \note This is designed only to be used by threading_helpers to
249 * avoid a deadlock, not for general access to this mutex.
Paul Elliott3d2db892024-01-19 20:42:56 +0000250 *
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000251 * \return The test info data mutex.
Paul Elliott3d2db892024-01-19 20:42:56 +0000252 */
253mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void);
254
255#endif /* MBEDTLS_TEST_MUTEX_USAGE */
256
257/**
258 * \brief Record the current test case as a failure if two integers
Gilles Peskine89615ee2021-04-29 20:28:54 +0200259 * have a different value.
260 *
261 * This function is usually called via the macro
262 * #TEST_EQUAL.
263 *
264 * \param test Description of the failure or assertion that failed. This
265 * MUST be a string literal. This normally has the form
266 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
267 * and EXPR2 has the value \p value2.
268 * \param line_no Line number where the failure originated.
269 * \param filename Filename where the failure originated.
270 * \param value1 The first value to compare.
271 * \param value2 The second value to compare.
272 *
273 * \return \c 1 if the values are equal, otherwise \c 0.
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275int mbedtls_test_equal(const char *test, int line_no, const char *filename,
276 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200277
278/**
Gilles Peskined1465422022-04-13 23:59:52 +0200279 * \brief Record the current test case as a failure based
280 * on comparing two unsigned integers.
281 *
282 * This function is usually called via the macro
283 * #TEST_LE_U.
284 *
285 * \param test Description of the failure or assertion that failed. This
286 * MUST be a string literal. This normally has the form
287 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
288 * and EXPR2 has the value \p value2.
289 * \param line_no Line number where the failure originated.
290 * \param filename Filename where the failure originated.
291 * \param value1 The first value to compare.
292 * \param value2 The second value to compare.
293 *
294 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
295 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
297 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200298
299/**
300 * \brief Record the current test case as a failure based
301 * on comparing two signed integers.
302 *
303 * This function is usually called via the macro
304 * #TEST_LE_S.
305 *
306 * \param test Description of the failure or assertion that failed. This
307 * MUST be a string literal. This normally has the form
308 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
309 * and EXPR2 has the value \p value2.
310 * \param line_no Line number where the failure originated.
311 * \param filename Filename where the failure originated.
312 * \param value1 The first value to compare.
313 * \param value2 The second value to compare.
314 *
315 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
318 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200319
320/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200321 * \brief This function decodes the hexadecimal representation of
322 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200323 *
324 * \note The output buffer can be the same as the input buffer. For
325 * any other overlapping of the input and output buffers, the
326 * behavior is undefined.
327 *
328 * \param obuf Output buffer.
329 * \param obufmax Size in number of bytes of \p obuf.
330 * \param ibuf Input buffer.
331 * \param len The number of unsigned char written in \p obuf. This must
332 * not be \c NULL.
333 *
334 * \return \c 0 on success.
335 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200336 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200337 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
339 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341void mbedtls_test_hexify(unsigned char *obuf,
342 const unsigned char *ibuf,
343 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200344
345/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100346 * \brief Convert hexadecimal digit to an integer.
347 *
348 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
349 * `'a'` to `'f'`).
350 * \param[out] uc On success, the value of the digit (0 to 15).
351 *
352 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
353 */
354int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
355
356/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200357 * Allocate and zeroize a buffer.
358 *
359 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
360 *
361 * For convenience, dies if allocation fails.
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200364
365/**
366 * Allocate and fill a buffer from hex data.
367 *
368 * The buffer is sized exactly as needed. This allows to detect buffer
369 * overruns (including overreads) when running the test suite under valgrind.
370 *
371 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
372 *
373 * For convenience, dies if allocation fails.
374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
378 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200379
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100380#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100381#include "test/fake_external_rng_for_test.h"
382#endif
383
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100384#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Paul Elliottf25d8312023-11-23 18:49:43 +0000385/**
386 * Activate the mutex usage verification framework. See threading_helpers.c for
387 * information.
388 * */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100390
Paul Elliottf25d8312023-11-23 18:49:43 +0000391/**
392 * Deactivate the mutex usage verification framework. See threading_helpers.c
393 * for information.
394 */
395void mbedtls_test_mutex_usage_end(void);
396
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100397/** Call this function after executing a test case to check for mutex usage
398 * errors. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100400#endif /* MBEDTLS_TEST_MUTEX_USAGE */
401
Chris Jones96ae73b2021-01-08 17:04:59 +0000402#if defined(MBEDTLS_TEST_HOOKS)
403/**
Chris Jones3f613c12021-03-31 09:34:22 +0100404 * \brief Check that only a pure high-level error code is being combined with
405 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000406 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100407 *
408 * \note Both high-level and low-level error codes cannot be greater than
409 * zero however can be zero. If one error code is zero then the
410 * other error code is returned even if both codes are zero.
411 *
412 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000413 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414void mbedtls_test_err_add_check(int high, int low,
415 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000416#endif
417
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200418#endif /* TEST_HELPERS_H */