blob: 73459d992f565324ba6e15a5832279e8f6bdf08c [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)
40#define MBEDTLS_TEST_MUTEX_USAGE
41#endif
42
Ronald Cronf40529d2020-06-09 16:27:37 +020043#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020044
45#include <stddef.h>
46#include <stdint.h>
47
Gilles Peskineebc49e52021-06-11 14:13:53 +020048#if defined(MBEDTLS_BIGNUM_C)
49#include "mbedtls/bignum.h"
50#endif
51
Gilles Peskine571576f2022-09-20 21:37:56 +020052/** The type of test case arguments that contain binary data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010053typedef struct data_tag {
54 uint8_t *x;
Gilles Peskine571576f2022-09-20 21:37:56 +020055 uint32_t len;
56} data_t;
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000059 MBEDTLS_TEST_RESULT_SUCCESS = 0,
60 MBEDTLS_TEST_RESULT_FAILED,
61 MBEDTLS_TEST_RESULT_SKIPPED
62} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000063
Paul Elliott5c498f32023-10-31 16:38:56 +000064#define MBEDTLS_TEST_LINE_LENGTH 76
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000067 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000068 const char *test;
69 const char *filename;
70 int line_no;
71 unsigned long step;
Paul Elliott5c498f32023-10-31 16:38:56 +000072 char line1[MBEDTLS_TEST_LINE_LENGTH];
73 char line2[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010074#if defined(MBEDTLS_TEST_MUTEX_USAGE)
75 const char *mutex_usage_error;
76#endif
Paul Elliottc7a1e992023-11-03 18:44:57 +000077#if defined(MBEDTLS_BIGNUM_C)
78 unsigned case_uses_negative_0;
79#endif
Chris Jones9634bb12021-01-20 15:56:42 +000080}
Chris Jonese60e2ae2021-01-20 17:51:47 +000081mbedtls_test_info_t;
Paul Elliott4580d4d2023-10-27 18:41:02 +010082
83/**
84 * \brief Get the current test result status
85 *
86 * \return The current test result status
87 */
88mbedtls_test_result_t mbedtls_test_get_result(void);
89
90/**
91 * \brief Get the current test name/description
92 *
93 * \return The current test name/description
94 */
95const char *mbedtls_test_get_test(void);
96
97/**
98 * \brief Get the current test filename
99 *
100 * \return The current test filename
101 */
102const char *mbedtls_get_test_filename(void);
103
104/**
105 * \brief Get the current test file line number (for failure / skip)
106 *
107 * \return The current test file line number (for failure / skip)
108 */
109int mbedtls_test_get_line_no(void);
110
111/**
112 * \brief Increment the current test step.
113 */
114void mbedtls_test_increment_step(void);
115
116/**
117 * \brief Get the current test step
118 *
119 * \return The current test step
120 */
121unsigned long mbedtls_test_get_step(void);
122
123/**
124 * \brief Get the current test line buffer 1
125 *
Paul Elliott65064262023-11-27 17:29:05 +0000126 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
127 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100128 */
Paul Elliott65064262023-11-27 17:29:05 +0000129void mbedtls_test_get_line1(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100130
131/**
132 * \brief Get the current test line buffer 2
133 *
Paul Elliott65064262023-11-27 17:29:05 +0000134 * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH,
135 * which will have line buffer 1 copied to it.
Paul Elliott4580d4d2023-10-27 18:41:02 +0100136 */
Paul Elliott65064262023-11-27 17:29:05 +0000137void mbedtls_test_get_line2(char *line);
Paul Elliott4580d4d2023-10-27 18:41:02 +0100138
139#if defined(MBEDTLS_TEST_MUTEX_USAGE)
140/**
141 * \brief Get the current mutex usage error message
142 *
143 * \return The current mutex error message (may be NULL if no error)
144 */
145const char *mbedtls_test_get_mutex_usage_error(void);
146
147/**
148 * \brief Set the current mutex usage error message
149 *
150 * \note This will only set the mutex error message if one has not
151 * already been set, or if we are clearing the message (msg is
152 * NULL)
153 *
154 * \param msg Error message to set (can be NULL to clear)
155 */
156void mbedtls_test_set_mutex_usage_error(const char *msg);
157#endif
158
Paul Elliottc7a1e992023-11-03 18:44:57 +0000159#if defined(MBEDTLS_BIGNUM_C)
160
161/**
162 * \brief Get whether the current test is a bignum test that uses
163 * negative zero.
164 *
165 * \return non zero if the current test uses bignum negative zero.
166 */
167unsigned mbedtls_test_get_case_uses_negative_0(void);
168
169/**
170 * \brief Indicate that the current test uses bignum negative zero.
171 *
172 * \note This function is called if the current test case had an
173 * input parsed with mbedtls_test_read_mpi() that is a negative
174 * 0 (`"-"`, `"-0"`, `"-00"`, etc., constructing a result with
175 * the sign bit set to -1 and the value being all-limbs-0,
176 * which is not a valid representation in #mbedtls_mpi but is
177 * tested for robustness). *
178 */
179void mbedtls_test_increment_case_uses_negative_0(void);
180#endif
Chris Jones9634bb12021-01-20 15:56:42 +0000181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182int mbedtls_test_platform_setup(void);
183void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +0200184
Ronald Crona0c25392020-06-18 10:10:46 +0200185/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000186 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +0000187 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000188 * This function can be called directly however it is usually
189 * called via macros such as TEST_ASSERT, TEST_EQUAL,
190 * PSA_ASSERT, etc...
191 *
192 * \note If the test case was already marked as failed, calling
193 * `mbedtls_test_fail( )` again will not overwrite any
194 * previous information about the failure.
195 *
196 * \param test Description of the failure or assertion that failed. This
197 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +0000198 * \param line_no Line number where the failure originated.
199 * \param filename Filename where the failure originated.
200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +0000202
203/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000204 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000205 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000206 * This function can be called directly however it is usually
207 * called via the TEST_ASSUME macro.
208 *
209 * \param test Description of the assumption that caused the test case to
210 * be skipped. This MUST be a string literal.
211 * \param line_no Line number where the test case was skipped.
212 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000213 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000215
Chris Jones567e0ad2021-02-03 12:07:01 +0000216/**
217 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000218 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000219 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000220 * line number and file name if a test fails. Typically the "step
221 * number" is the index of a for loop but it can be whatever you
222 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000223 *
224 * \param step The step number to report.
225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000227
Chris Jones567e0ad2021-02-03 12:07:01 +0000228/**
229 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000232
Ronald Crona0c25392020-06-18 10:10:46 +0200233/**
Gilles Peskine89615ee2021-04-29 20:28:54 +0200234 * \brief Record the current test case as a failure if two integers
235 * have a different value.
236 *
237 * This function is usually called via the macro
238 * #TEST_EQUAL.
239 *
240 * \param test Description of the failure or assertion that failed. This
241 * MUST be a string literal. This normally has the form
242 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
243 * and EXPR2 has the value \p value2.
244 * \param line_no Line number where the failure originated.
245 * \param filename Filename where the failure originated.
246 * \param value1 The first value to compare.
247 * \param value2 The second value to compare.
248 *
249 * \return \c 1 if the values are equal, otherwise \c 0.
250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251int mbedtls_test_equal(const char *test, int line_no, const char *filename,
252 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200253
254/**
Gilles Peskined1465422022-04-13 23:59:52 +0200255 * \brief Record the current test case as a failure based
256 * on comparing two unsigned integers.
257 *
258 * This function is usually called via the macro
259 * #TEST_LE_U.
260 *
261 * \param test Description of the failure or assertion that failed. This
262 * MUST be a string literal. This normally has the form
263 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
264 * and EXPR2 has the value \p value2.
265 * \param line_no Line number where the failure originated.
266 * \param filename Filename where the failure originated.
267 * \param value1 The first value to compare.
268 * \param value2 The second value to compare.
269 *
270 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
273 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200274
275/**
276 * \brief Record the current test case as a failure based
277 * on comparing two signed integers.
278 *
279 * This function is usually called via the macro
280 * #TEST_LE_S.
281 *
282 * \param test Description of the failure or assertion that failed. This
283 * MUST be a string literal. This normally has the form
284 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
285 * and EXPR2 has the value \p value2.
286 * \param line_no Line number where the failure originated.
287 * \param filename Filename where the failure originated.
288 * \param value1 The first value to compare.
289 * \param value2 The second value to compare.
290 *
291 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
292 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
294 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200295
296/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200297 * \brief This function decodes the hexadecimal representation of
298 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200299 *
300 * \note The output buffer can be the same as the input buffer. For
301 * any other overlapping of the input and output buffers, the
302 * behavior is undefined.
303 *
304 * \param obuf Output buffer.
305 * \param obufmax Size in number of bytes of \p obuf.
306 * \param ibuf Input buffer.
307 * \param len The number of unsigned char written in \p obuf. This must
308 * not be \c NULL.
309 *
310 * \return \c 0 on success.
311 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200312 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200313 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
315 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317void mbedtls_test_hexify(unsigned char *obuf,
318 const unsigned char *ibuf,
319 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200320
321/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100322 * \brief Convert hexadecimal digit to an integer.
323 *
324 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
325 * `'a'` to `'f'`).
326 * \param[out] uc On success, the value of the digit (0 to 15).
327 *
328 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
329 */
330int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
331
332/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200333 * Allocate and zeroize a buffer.
334 *
335 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
336 *
337 * For convenience, dies if allocation fails.
338 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200340
341/**
342 * Allocate and fill a buffer from hex data.
343 *
344 * The buffer is sized exactly as needed. This allows to detect buffer
345 * overruns (including overreads) when running the test suite under valgrind.
346 *
347 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
348 *
349 * For convenience, dies if allocation fails.
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
354 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200355
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100356#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100357#include "test/fake_external_rng_for_test.h"
358#endif
359
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100360#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Paul Elliottf25d8312023-11-23 18:49:43 +0000361/**
362 * Activate the mutex usage verification framework. See threading_helpers.c for
363 * information.
364 * */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100366
Paul Elliottf25d8312023-11-23 18:49:43 +0000367/**
368 * Deactivate the mutex usage verification framework. See threading_helpers.c
369 * for information.
370 */
371void mbedtls_test_mutex_usage_end(void);
372
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100373/** Call this function after executing a test case to check for mutex usage
374 * errors. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100376#endif /* MBEDTLS_TEST_MUTEX_USAGE */
377
Chris Jones96ae73b2021-01-08 17:04:59 +0000378#if defined(MBEDTLS_TEST_HOOKS)
379/**
Chris Jones3f613c12021-03-31 09:34:22 +0100380 * \brief Check that only a pure high-level error code is being combined with
381 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000382 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100383 *
384 * \note Both high-level and low-level error codes cannot be greater than
385 * zero however can be zero. If one error code is zero then the
386 * other error code is returned even if both codes are zero.
387 *
388 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390void mbedtls_test_err_add_check(int high, int low,
391 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000392#endif
393
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200394#endif /* TEST_HELPERS_H */