blob: b2b07cfa8bdcac240351763de3a6ccb793a83a1c [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
Paul Elliott17c119a2023-12-08 16:55:03 +000038#include "test/threading_helpers.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020039#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020040
41#include <stddef.h>
42#include <stdint.h>
43
Gilles Peskineebc49e52021-06-11 14:13:53 +020044#if defined(MBEDTLS_BIGNUM_C)
45#include "mbedtls/bignum.h"
46#endif
47
Gilles Peskine571576f2022-09-20 21:37:56 +020048/** The type of test case arguments that contain binary data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010049typedef struct data_tag {
50 uint8_t *x;
Gilles Peskine571576f2022-09-20 21:37:56 +020051 uint32_t len;
52} data_t;
53
Gilles Peskine449bd832023-01-11 14:50:10 +010054typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000055 MBEDTLS_TEST_RESULT_SUCCESS = 0,
56 MBEDTLS_TEST_RESULT_FAILED,
57 MBEDTLS_TEST_RESULT_SKIPPED
58} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000059
Gilles Peskine449bd832023-01-11 14:50:10 +010060typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000061 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000062 const char *test;
63 const char *filename;
64 int line_no;
65 unsigned long step;
Gilles Peskine89615ee2021-04-29 20:28:54 +020066 char line1[76];
67 char line2[76];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010068#if defined(MBEDTLS_TEST_MUTEX_USAGE)
69 const char *mutex_usage_error;
70#endif
Chris Jones9634bb12021-01-20 15:56:42 +000071}
Chris Jonese60e2ae2021-01-20 17:51:47 +000072mbedtls_test_info_t;
73extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000074
Gilles Peskine449bd832023-01-11 14:50:10 +010075int mbedtls_test_platform_setup(void);
76void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +020077
Ronald Crona0c25392020-06-18 10:10:46 +020078/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000079 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000080 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000081 * This function can be called directly however it is usually
82 * called via macros such as TEST_ASSERT, TEST_EQUAL,
83 * PSA_ASSERT, etc...
84 *
85 * \note If the test case was already marked as failed, calling
86 * `mbedtls_test_fail( )` again will not overwrite any
87 * previous information about the failure.
88 *
89 * \param test Description of the failure or assertion that failed. This
90 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000091 * \param line_no Line number where the failure originated.
92 * \param filename Filename where the failure originated.
93 */
Gilles Peskine449bd832023-01-11 14:50:10 +010094void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +000095
96/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000097 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000098 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000099 * This function can be called directly however it is usually
100 * called via the TEST_ASSUME macro.
101 *
102 * \param test Description of the assumption that caused the test case to
103 * be skipped. This MUST be a string literal.
104 * \param line_no Line number where the test case was skipped.
105 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000108
Chris Jones567e0ad2021-02-03 12:07:01 +0000109/**
110 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000111 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000112 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000113 * line number and file name if a test fails. Typically the "step
114 * number" is the index of a for loop but it can be whatever you
115 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000116 *
117 * \param step The step number to report.
118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000120
Chris Jones567e0ad2021-02-03 12:07:01 +0000121/**
122 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000125
Ronald Crona0c25392020-06-18 10:10:46 +0200126/**
Gilles Peskine89615ee2021-04-29 20:28:54 +0200127 * \brief Record the current test case as a failure if two integers
128 * have a different value.
129 *
130 * This function is usually called via the macro
131 * #TEST_EQUAL.
132 *
133 * \param test Description of the failure or assertion that failed. This
134 * MUST be a string literal. This normally has the form
135 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
136 * and EXPR2 has the value \p value2.
137 * \param line_no Line number where the failure originated.
138 * \param filename Filename where the failure originated.
139 * \param value1 The first value to compare.
140 * \param value2 The second value to compare.
141 *
142 * \return \c 1 if the values are equal, otherwise \c 0.
143 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144int mbedtls_test_equal(const char *test, int line_no, const char *filename,
145 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200146
147/**
Gilles Peskined1465422022-04-13 23:59:52 +0200148 * \brief Record the current test case as a failure based
149 * on comparing two unsigned integers.
150 *
151 * This function is usually called via the macro
152 * #TEST_LE_U.
153 *
154 * \param test Description of the failure or assertion that failed. This
155 * MUST be a string literal. This normally has the form
156 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
157 * and EXPR2 has the value \p value2.
158 * \param line_no Line number where the failure originated.
159 * \param filename Filename where the failure originated.
160 * \param value1 The first value to compare.
161 * \param value2 The second value to compare.
162 *
163 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
164 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
166 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200167
168/**
169 * \brief Record the current test case as a failure based
170 * on comparing two signed integers.
171 *
172 * This function is usually called via the macro
173 * #TEST_LE_S.
174 *
175 * \param test Description of the failure or assertion that failed. This
176 * MUST be a string literal. This normally has the form
177 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
178 * and EXPR2 has the value \p value2.
179 * \param line_no Line number where the failure originated.
180 * \param filename Filename where the failure originated.
181 * \param value1 The first value to compare.
182 * \param value2 The second value to compare.
183 *
184 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
187 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200188
189/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200190 * \brief This function decodes the hexadecimal representation of
191 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200192 *
193 * \note The output buffer can be the same as the input buffer. For
194 * any other overlapping of the input and output buffers, the
195 * behavior is undefined.
196 *
197 * \param obuf Output buffer.
198 * \param obufmax Size in number of bytes of \p obuf.
199 * \param ibuf Input buffer.
200 * \param len The number of unsigned char written in \p obuf. This must
201 * not be \c NULL.
202 *
203 * \return \c 0 on success.
204 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200205 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
208 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210void mbedtls_test_hexify(unsigned char *obuf,
211 const unsigned char *ibuf,
212 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200213
214/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100215 * \brief Convert hexadecimal digit to an integer.
216 *
217 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
218 * `'a'` to `'f'`).
219 * \param[out] uc On success, the value of the digit (0 to 15).
220 *
221 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
222 */
223int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
224
225/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200226 * Allocate and zeroize a buffer.
227 *
228 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
229 *
230 * For convenience, dies if allocation fails.
231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200233
234/**
235 * Allocate and fill a buffer from hex data.
236 *
237 * The buffer is sized exactly as needed. This allows to detect buffer
238 * overruns (including overreads) when running the test suite under valgrind.
239 *
240 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
241 *
242 * For convenience, dies if allocation fails.
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
247 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200248
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100249#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100250#include "test/fake_external_rng_for_test.h"
251#endif
252
Chris Jones96ae73b2021-01-08 17:04:59 +0000253#if defined(MBEDTLS_TEST_HOOKS)
254/**
Chris Jones3f613c12021-03-31 09:34:22 +0100255 * \brief Check that only a pure high-level error code is being combined with
256 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000257 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100258 *
259 * \note Both high-level and low-level error codes cannot be greater than
260 * zero however can be zero. If one error code is zero then the
261 * other error code is returned even if both codes are zero.
262 *
263 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000264 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265void mbedtls_test_err_add_check(int high, int low,
266 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000267#endif
268
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200269#endif /* TEST_HELPERS_H */