blob: 4e59e209497ea408218eb3d717edafc694c08303 [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
Paul Elliotte053cb22024-02-06 14:57:43 +000026#if defined(__SANITIZE_THREAD__) /* gcc -fsanitize-thread */
27# define MBEDTLS_TEST_HAVE_TSAN
28#endif
29
Gilles Peskinefa8ec262023-11-22 17:55:43 +010030#if defined(__has_feature)
31# if __has_feature(address_sanitizer) /* clang -fsanitize=address */
32# define MBEDTLS_TEST_HAVE_ASAN
33# endif
34# if __has_feature(memory_sanitizer) /* clang -fsanitize=memory */
35# define MBEDTLS_TEST_HAVE_MSAN
36# endif
37# if __has_feature(thread_sanitizer) /* clang -fsanitize=thread */
38# define MBEDTLS_TEST_HAVE_TSAN
39# endif
40#endif
41
Paul Elliott17c119a2023-12-08 16:55:03 +000042#include "test/threading_helpers.h"
Paul Elliott54ad01e2024-02-09 14:33:58 +000043
44#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Paul Elliott3d2db892024-01-19 20:42:56 +000045#include "mbedtls/threading.h"
Gilles Peskine2a4c5982021-01-29 21:18:09 +010046#endif
47
Ronald Cronf40529d2020-06-09 16:27:37 +020048#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020049
50#include <stddef.h>
51#include <stdint.h>
52
Gilles Peskineebc49e52021-06-11 14:13:53 +020053#if defined(MBEDTLS_BIGNUM_C)
54#include "mbedtls/bignum.h"
55#endif
56
Gilles Peskine571576f2022-09-20 21:37:56 +020057/** The type of test case arguments that contain binary data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010058typedef struct data_tag {
59 uint8_t *x;
Gilles Peskine571576f2022-09-20 21:37:56 +020060 uint32_t len;
61} data_t;
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000064 MBEDTLS_TEST_RESULT_SUCCESS = 0,
65 MBEDTLS_TEST_RESULT_FAILED,
66 MBEDTLS_TEST_RESULT_SKIPPED
67} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000068
Paul Elliott5c498f32023-10-31 16:38:56 +000069#define MBEDTLS_TEST_LINE_LENGTH 76
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000072 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000073 const char *test;
74 const char *filename;
75 int line_no;
76 unsigned long step;
Paul Elliott5c498f32023-10-31 16:38:56 +000077 char line1[MBEDTLS_TEST_LINE_LENGTH];
78 char line2[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010079#if defined(MBEDTLS_TEST_MUTEX_USAGE)
80 const char *mutex_usage_error;
81#endif
Paul Elliottc7a1e992023-11-03 18:44:57 +000082#if defined(MBEDTLS_BIGNUM_C)
83 unsigned case_uses_negative_0;
84#endif
Chris Jones9634bb12021-01-20 15:56:42 +000085}
Chris Jonese60e2ae2021-01-20 17:51:47 +000086mbedtls_test_info_t;
Paul Elliott4580d4d2023-10-27 18:41:02 +010087
88/**
89 * \brief Get the current test result status
90 *
91 * \return The current test result status
92 */
93mbedtls_test_result_t mbedtls_test_get_result(void);
94
95/**
96 * \brief Get the current test name/description
97 *
98 * \return The current test name/description
99 */
100const char *mbedtls_test_get_test(void);
101
102/**
103 * \brief Get the current test filename
104 *
105 * \return The current test filename
106 */
107const char *mbedtls_get_test_filename(void);
108
109/**
110 * \brief Get the current test file line number (for failure / skip)
111 *
112 * \return The current test file line number (for failure / skip)
113 */
114int mbedtls_test_get_line_no(void);
115
116/**
117 * \brief Increment the current test step.
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000118 *
119 * \note Calling this function from within multiple threads at the
120 * same time is not recommended - whilst it is entirely thread
121 * safe, the order of calls to this function can obviously not
122 * be ensured, so unexpected results may occur.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100123 */
124void mbedtls_test_increment_step(void);
125
126/**
127 * \brief Get the current test step
128 *
129 * \return The current test step
130 */
131unsigned long mbedtls_test_get_step(void);
132
133/**
134 * \brief Get the current test line buffer 1
135 *
Paul Elliott65064262023-11-27 17:29:05 +0000136 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
137 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100138 */
Paul Elliott65064262023-11-27 17:29:05 +0000139void mbedtls_test_get_line1(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100140
141/**
142 * \brief Get the current test line buffer 2
143 *
Paul Elliott65064262023-11-27 17:29:05 +0000144 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
145 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100146 */
Paul Elliott65064262023-11-27 17:29:05 +0000147void mbedtls_test_get_line2(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100148
149#if defined(MBEDTLS_TEST_MUTEX_USAGE)
150/**
151 * \brief Get the current mutex usage error message
152 *
153 * \return The current mutex error message (may be NULL if no error)
154 */
155const char *mbedtls_test_get_mutex_usage_error(void);
156
157/**
158 * \brief Set the current mutex usage error message
159 *
160 * \note This will only set the mutex error message if one has not
161 * already been set, or if we are clearing the message (msg is
162 * NULL)
163 *
164 * \param msg Error message to set (can be NULL to clear)
165 */
166void mbedtls_test_set_mutex_usage_error(const char *msg);
167#endif
168
Paul Elliottc7a1e992023-11-03 18:44:57 +0000169#if defined(MBEDTLS_BIGNUM_C)
170
171/**
172 * \brief Get whether the current test is a bignum test that uses
173 * negative zero.
174 *
175 * \return non zero if the current test uses bignum negative zero.
176 */
177unsigned mbedtls_test_get_case_uses_negative_0(void);
178
179/**
180 * \brief Indicate that the current test uses bignum negative zero.
181 *
182 * \note This function is called if the current test case had an
183 * input parsed with mbedtls_test_read_mpi() that is a negative
184 * 0 (`"-"`, `"-0"`, `"-00"`, etc., constructing a result with
185 * the sign bit set to -1 and the value being all-limbs-0,
186 * which is not a valid representation in #mbedtls_mpi but is
187 * tested for robustness). *
188 */
189void mbedtls_test_increment_case_uses_negative_0(void);
190#endif
Chris Jones9634bb12021-01-20 15:56:42 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_test_platform_setup(void);
193void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +0200194
Ronald Crona0c25392020-06-18 10:10:46 +0200195/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000196 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +0000197 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000198 * This function can be called directly however it is usually
199 * called via macros such as TEST_ASSERT, TEST_EQUAL,
200 * PSA_ASSERT, etc...
201 *
202 * \note If the test case was already marked as failed, calling
203 * `mbedtls_test_fail( )` again will not overwrite any
204 * previous information about the failure.
205 *
206 * \param test Description of the failure or assertion that failed. This
207 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +0000208 * \param line_no Line number where the failure originated.
209 * \param filename Filename where the failure originated.
210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +0000212
213/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000214 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000215 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000216 * This function can be called directly however it is usually
217 * called via the TEST_ASSUME macro.
218 *
219 * \param test Description of the assumption that caused the test case to
220 * be skipped. This MUST be a string literal.
221 * \param line_no Line number where the test case was skipped.
222 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000225
Chris Jones567e0ad2021-02-03 12:07:01 +0000226/**
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000227 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000228 *
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000229 * Call this function to display "step NNN" in addition to the
230 * line number and file name if a test fails. Typically the
231 * "step number" is the index of a for loop but it can be
232 * whatever you want.
233 *
234 * \note Calling this function from a within multiple threads at the
235 * same time is not recommended - whilst it is entirely thread
236 * safe, the order of calls to this function can obviously not
237 * be ensured, so unexpected results may occur.
Chris Jones9634bb12021-01-20 15:56:42 +0000238 *
239 * \param step The step number to report.
240 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000242
Chris Jones567e0ad2021-02-03 12:07:01 +0000243/**
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000244 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000247
Paul Elliott3d2db892024-01-19 20:42:56 +0000248#ifdef MBEDTLS_TEST_MUTEX_USAGE
Ronald Crona0c25392020-06-18 10:10:46 +0200249/**
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000250 * \brief Get the test info data mutex.
Paul Elliott3d2db892024-01-19 20:42:56 +0000251 *
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000252 * \note This is designed only to be used by threading_helpers to
253 * avoid a deadlock, not for general access to this mutex.
Paul Elliott3d2db892024-01-19 20:42:56 +0000254 *
Paul Elliott79e2e5d2024-02-06 15:10:03 +0000255 * \return The test info data mutex.
Paul Elliott3d2db892024-01-19 20:42:56 +0000256 */
257mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void);
258
259#endif /* MBEDTLS_TEST_MUTEX_USAGE */
260
261/**
262 * \brief Record the current test case as a failure if two integers
Gilles Peskine89615ee2021-04-29 20:28:54 +0200263 * have a different value.
264 *
265 * This function is usually called via the macro
266 * #TEST_EQUAL.
267 *
268 * \param test Description of the failure or assertion that failed. This
269 * MUST be a string literal. This normally has the form
270 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
271 * and EXPR2 has the value \p value2.
272 * \param line_no Line number where the failure originated.
273 * \param filename Filename where the failure originated.
274 * \param value1 The first value to compare.
275 * \param value2 The second value to compare.
276 *
277 * \return \c 1 if the values are equal, otherwise \c 0.
278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279int mbedtls_test_equal(const char *test, int line_no, const char *filename,
280 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200281
282/**
Gilles Peskined1465422022-04-13 23:59:52 +0200283 * \brief Record the current test case as a failure based
284 * on comparing two unsigned integers.
285 *
286 * This function is usually called via the macro
287 * #TEST_LE_U.
288 *
289 * \param test Description of the failure or assertion that failed. This
290 * MUST be a string literal. This normally has the form
291 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
292 * and EXPR2 has the value \p value2.
293 * \param line_no Line number where the failure originated.
294 * \param filename Filename where the failure originated.
295 * \param value1 The first value to compare.
296 * \param value2 The second value to compare.
297 *
298 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
301 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200302
303/**
304 * \brief Record the current test case as a failure based
305 * on comparing two signed integers.
306 *
307 * This function is usually called via the macro
308 * #TEST_LE_S.
309 *
310 * \param test Description of the failure or assertion that failed. This
311 * MUST be a string literal. This normally has the form
312 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
313 * and EXPR2 has the value \p value2.
314 * \param line_no Line number where the failure originated.
315 * \param filename Filename where the failure originated.
316 * \param value1 The first value to compare.
317 * \param value2 The second value to compare.
318 *
319 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
322 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200323
324/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200325 * \brief This function decodes the hexadecimal representation of
326 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200327 *
328 * \note The output buffer can be the same as the input buffer. For
329 * any other overlapping of the input and output buffers, the
330 * behavior is undefined.
331 *
332 * \param obuf Output buffer.
333 * \param obufmax Size in number of bytes of \p obuf.
334 * \param ibuf Input buffer.
335 * \param len The number of unsigned char written in \p obuf. This must
336 * not be \c NULL.
337 *
338 * \return \c 0 on success.
339 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200340 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
343 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345void mbedtls_test_hexify(unsigned char *obuf,
346 const unsigned char *ibuf,
347 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200348
349/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100350 * \brief Convert hexadecimal digit to an integer.
351 *
352 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
353 * `'a'` to `'f'`).
354 * \param[out] uc On success, the value of the digit (0 to 15).
355 *
356 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
357 */
358int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
359
360/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200361 * Allocate and zeroize a buffer.
362 *
363 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
364 *
365 * For convenience, dies if allocation fails.
366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200368
369/**
370 * Allocate and fill a buffer from hex data.
371 *
372 * The buffer is sized exactly as needed. This allows to detect buffer
373 * overruns (including overreads) when running the test suite under valgrind.
374 *
375 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
376 *
377 * For convenience, dies if allocation fails.
378 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
382 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200383
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100384#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100385#include "test/fake_external_rng_for_test.h"
386#endif
387
Chris Jones96ae73b2021-01-08 17:04:59 +0000388#if defined(MBEDTLS_TEST_HOOKS)
389/**
Chris Jones3f613c12021-03-31 09:34:22 +0100390 * \brief Check that only a pure high-level error code is being combined with
391 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000392 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100393 *
394 * \note Both high-level and low-level error codes cannot be greater than
395 * zero however can be zero. If one error code is zero then the
396 * other error code is returned even if both codes are zero.
397 *
398 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000399 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400void mbedtls_test_err_add_check(int high, int low,
401 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000402#endif
403
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200404#endif /* TEST_HELPERS_H */