blob: f2fb62d935de3a08356e75e443d77b7e0226a6f4 [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.
114 */
115void mbedtls_test_increment_step(void);
116
117/**
118 * \brief Get the current test step
119 *
120 * \return The current test step
121 */
122unsigned long mbedtls_test_get_step(void);
123
124/**
125 * \brief Get the current test line buffer 1
126 *
Paul Elliott65064262023-11-27 17:29:05 +0000127 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
128 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100129 */
Paul Elliott65064262023-11-27 17:29:05 +0000130void mbedtls_test_get_line1(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100131
132/**
133 * \brief Get the current test line buffer 2
134 *
Paul Elliott65064262023-11-27 17:29:05 +0000135 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
136 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100137 */
Paul Elliott65064262023-11-27 17:29:05 +0000138void mbedtls_test_get_line2(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100139
140#if defined(MBEDTLS_TEST_MUTEX_USAGE)
141/**
142 * \brief Get the current mutex usage error message
143 *
144 * \return The current mutex error message (may be NULL if no error)
145 */
146const char *mbedtls_test_get_mutex_usage_error(void);
147
148/**
149 * \brief Set the current mutex usage error message
150 *
151 * \note This will only set the mutex error message if one has not
152 * already been set, or if we are clearing the message (msg is
153 * NULL)
154 *
155 * \param msg Error message to set (can be NULL to clear)
156 */
157void mbedtls_test_set_mutex_usage_error(const char *msg);
158#endif
159
Paul Elliottc7a1e992023-11-03 18:44:57 +0000160#if defined(MBEDTLS_BIGNUM_C)
161
162/**
163 * \brief Get whether the current test is a bignum test that uses
164 * negative zero.
165 *
166 * \return non zero if the current test uses bignum negative zero.
167 */
168unsigned mbedtls_test_get_case_uses_negative_0(void);
169
170/**
171 * \brief Indicate that the current test uses bignum negative zero.
172 *
173 * \note This function is called if the current test case had an
174 * input parsed with mbedtls_test_read_mpi() that is a negative
175 * 0 (`"-"`, `"-0"`, `"-00"`, etc., constructing a result with
176 * the sign bit set to -1 and the value being all-limbs-0,
177 * which is not a valid representation in #mbedtls_mpi but is
178 * tested for robustness). *
179 */
180void mbedtls_test_increment_case_uses_negative_0(void);
181#endif
Chris Jones9634bb12021-01-20 15:56:42 +0000182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183int mbedtls_test_platform_setup(void);
184void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +0200185
Ronald Crona0c25392020-06-18 10:10:46 +0200186/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000187 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +0000188 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000189 * This function can be called directly however it is usually
190 * called via macros such as TEST_ASSERT, TEST_EQUAL,
191 * PSA_ASSERT, etc...
192 *
193 * \note If the test case was already marked as failed, calling
194 * `mbedtls_test_fail( )` again will not overwrite any
195 * previous information about the failure.
196 *
197 * \param test Description of the failure or assertion that failed. This
198 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +0000199 * \param line_no Line number where the failure originated.
200 * \param filename Filename where the failure originated.
201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +0000203
204/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000205 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000206 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000207 * This function can be called directly however it is usually
208 * called via the TEST_ASSUME macro.
209 *
210 * \param test Description of the assumption that caused the test case to
211 * be skipped. This MUST be a string literal.
212 * \param line_no Line number where the test case was skipped.
213 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000216
Chris Jones567e0ad2021-02-03 12:07:01 +0000217/**
218 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000219 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000220 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000221 * line number and file name if a test fails. Typically the "step
222 * number" is the index of a for loop but it can be whatever you
223 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000224 *
225 * \param step The step number to report.
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000228
Chris Jones567e0ad2021-02-03 12:07:01 +0000229/**
230 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000233
Paul Elliott3d2db892024-01-19 20:42:56 +0000234#ifdef MBEDTLS_TEST_MUTEX_USAGE
Ronald Crona0c25392020-06-18 10:10:46 +0200235/**
Paul Elliott3d2db892024-01-19 20:42:56 +0000236 * \brief Get the test info data mutex.
237 *
238 * \note This is designed only to be used by threading_helpers to avoid a
239 * deadlock, not for general access to this mutex.
240 *
241 * \return The test info data mutex.
242 */
243mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void);
244
245#endif /* MBEDTLS_TEST_MUTEX_USAGE */
246
247/**
248 * \brief Record the current test case as a failure if two integers
Gilles Peskine89615ee2021-04-29 20:28:54 +0200249 * have a different value.
250 *
251 * This function is usually called via the macro
252 * #TEST_EQUAL.
253 *
254 * \param test Description of the failure or assertion that failed. This
255 * MUST be a string literal. This normally has the form
256 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
257 * and EXPR2 has the value \p value2.
258 * \param line_no Line number where the failure originated.
259 * \param filename Filename where the failure originated.
260 * \param value1 The first value to compare.
261 * \param value2 The second value to compare.
262 *
263 * \return \c 1 if the values are equal, otherwise \c 0.
264 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265int mbedtls_test_equal(const char *test, int line_no, const char *filename,
266 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200267
268/**
Gilles Peskined1465422022-04-13 23:59:52 +0200269 * \brief Record the current test case as a failure based
270 * on comparing two unsigned integers.
271 *
272 * This function is usually called via the macro
273 * #TEST_LE_U.
274 *
275 * \param test Description of the failure or assertion that failed. This
276 * MUST be a string literal. This normally has the form
277 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
278 * and EXPR2 has the value \p value2.
279 * \param line_no Line number where the failure originated.
280 * \param filename Filename where the failure originated.
281 * \param value1 The first value to compare.
282 * \param value2 The second value to compare.
283 *
284 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
287 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200288
289/**
290 * \brief Record the current test case as a failure based
291 * on comparing two signed integers.
292 *
293 * This function is usually called via the macro
294 * #TEST_LE_S.
295 *
296 * \param test Description of the failure or assertion that failed. This
297 * MUST be a string literal. This normally has the form
298 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
299 * and EXPR2 has the value \p value2.
300 * \param line_no Line number where the failure originated.
301 * \param filename Filename where the failure originated.
302 * \param value1 The first value to compare.
303 * \param value2 The second value to compare.
304 *
305 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
308 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200309
310/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200311 * \brief This function decodes the hexadecimal representation of
312 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200313 *
314 * \note The output buffer can be the same as the input buffer. For
315 * any other overlapping of the input and output buffers, the
316 * behavior is undefined.
317 *
318 * \param obuf Output buffer.
319 * \param obufmax Size in number of bytes of \p obuf.
320 * \param ibuf Input buffer.
321 * \param len The number of unsigned char written in \p obuf. This must
322 * not be \c NULL.
323 *
324 * \return \c 0 on success.
325 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200326 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200327 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
329 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331void mbedtls_test_hexify(unsigned char *obuf,
332 const unsigned char *ibuf,
333 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200334
335/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100336 * \brief Convert hexadecimal digit to an integer.
337 *
338 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
339 * `'a'` to `'f'`).
340 * \param[out] uc On success, the value of the digit (0 to 15).
341 *
342 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
343 */
344int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
345
346/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200347 * Allocate and zeroize a buffer.
348 *
349 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
350 *
351 * For convenience, dies if allocation fails.
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200354
355/**
356 * Allocate and fill a buffer from hex data.
357 *
358 * The buffer is sized exactly as needed. This allows to detect buffer
359 * overruns (including overreads) when running the test suite under valgrind.
360 *
361 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
362 *
363 * For convenience, dies if allocation fails.
364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
368 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200369
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100370#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100371#include "test/fake_external_rng_for_test.h"
372#endif
373
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100374#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Paul Elliottf25d8312023-11-23 18:49:43 +0000375/**
376 * Activate the mutex usage verification framework. See threading_helpers.c for
377 * information.
378 * */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100380
Paul Elliottf25d8312023-11-23 18:49:43 +0000381/**
382 * Deactivate the mutex usage verification framework. See threading_helpers.c
383 * for information.
384 */
385void mbedtls_test_mutex_usage_end(void);
386
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100387/** Call this function after executing a test case to check for mutex usage
388 * errors. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100390#endif /* MBEDTLS_TEST_MUTEX_USAGE */
391
Chris Jones96ae73b2021-01-08 17:04:59 +0000392#if defined(MBEDTLS_TEST_HOOKS)
393/**
Chris Jones3f613c12021-03-31 09:34:22 +0100394 * \brief Check that only a pure high-level error code is being combined with
395 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000396 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100397 *
398 * \note Both high-level and low-level error codes cannot be greater than
399 * zero however can be zero. If one error code is zero then the
400 * other error code is returned even if both codes are zero.
401 *
402 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404void mbedtls_test_err_add_check(int high, int low,
405 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000406#endif
407
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200408#endif /* TEST_HELPERS_H */