blob: 4708df1632c5f436fa189b315d9c720b1428f457 [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 Peskine2a4c5982021-01-29 21:18:09 +010023#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
24 defined(MBEDTLS_TEST_HOOKS)
25#define MBEDTLS_TEST_MUTEX_USAGE
26#endif
27
Ronald Cronf40529d2020-06-09 16:27:37 +020028#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020029
30#include <stddef.h>
31#include <stdint.h>
32
Gilles Peskineebc49e52021-06-11 14:13:53 +020033#if defined(MBEDTLS_BIGNUM_C)
34#include "mbedtls/bignum.h"
35#endif
36
Gilles Peskine571576f2022-09-20 21:37:56 +020037/** The type of test case arguments that contain binary data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010038typedef struct data_tag {
39 uint8_t *x;
Gilles Peskine571576f2022-09-20 21:37:56 +020040 uint32_t len;
41} data_t;
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000044 MBEDTLS_TEST_RESULT_SUCCESS = 0,
45 MBEDTLS_TEST_RESULT_FAILED,
46 MBEDTLS_TEST_RESULT_SKIPPED
47} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000048
Gilles Peskine449bd832023-01-11 14:50:10 +010049typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000050 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000051 const char *test;
52 const char *filename;
53 int line_no;
54 unsigned long step;
Gilles Peskine89615ee2021-04-29 20:28:54 +020055 char line1[76];
56 char line2[76];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010057#if defined(MBEDTLS_TEST_MUTEX_USAGE)
58 const char *mutex_usage_error;
59#endif
Chris Jones9634bb12021-01-20 15:56:42 +000060}
Chris Jonese60e2ae2021-01-20 17:51:47 +000061mbedtls_test_info_t;
62extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000063
Gilles Peskine449bd832023-01-11 14:50:10 +010064int mbedtls_test_platform_setup(void);
65void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +020066
Ronald Crona0c25392020-06-18 10:10:46 +020067/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000068 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000069 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000070 * This function can be called directly however it is usually
71 * called via macros such as TEST_ASSERT, TEST_EQUAL,
72 * PSA_ASSERT, etc...
73 *
74 * \note If the test case was already marked as failed, calling
75 * `mbedtls_test_fail( )` again will not overwrite any
76 * previous information about the failure.
77 *
78 * \param test Description of the failure or assertion that failed. This
79 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000080 * \param line_no Line number where the failure originated.
81 * \param filename Filename where the failure originated.
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +000084
85/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000086 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000087 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000088 * This function can be called directly however it is usually
89 * called via the TEST_ASSUME macro.
90 *
91 * \param test Description of the assumption that caused the test case to
92 * be skipped. This MUST be a string literal.
93 * \param line_no Line number where the test case was skipped.
94 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000095 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +000097
Chris Jones567e0ad2021-02-03 12:07:01 +000098/**
99 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000100 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000101 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000102 * line number and file name if a test fails. Typically the "step
103 * number" is the index of a for loop but it can be whatever you
104 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000105 *
106 * \param step The step number to report.
107 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000109
Chris Jones567e0ad2021-02-03 12:07:01 +0000110/**
111 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000114
Ronald Crona0c25392020-06-18 10:10:46 +0200115/**
Gilles Peskine89615ee2021-04-29 20:28:54 +0200116 * \brief Record the current test case as a failure if two integers
117 * have a different value.
118 *
119 * This function is usually called via the macro
120 * #TEST_EQUAL.
121 *
122 * \param test Description of the failure or assertion that failed. This
123 * MUST be a string literal. This normally has the form
124 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
125 * and EXPR2 has the value \p value2.
126 * \param line_no Line number where the failure originated.
127 * \param filename Filename where the failure originated.
128 * \param value1 The first value to compare.
129 * \param value2 The second value to compare.
130 *
131 * \return \c 1 if the values are equal, otherwise \c 0.
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133int mbedtls_test_equal(const char *test, int line_no, const char *filename,
134 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200135
136/**
Gilles Peskined1465422022-04-13 23:59:52 +0200137 * \brief Record the current test case as a failure based
138 * on comparing two unsigned integers.
139 *
140 * This function is usually called via the macro
141 * #TEST_LE_U.
142 *
143 * \param test Description of the failure or assertion that failed. This
144 * MUST be a string literal. This normally has the form
145 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
146 * and EXPR2 has the value \p value2.
147 * \param line_no Line number where the failure originated.
148 * \param filename Filename where the failure originated.
149 * \param value1 The first value to compare.
150 * \param value2 The second value to compare.
151 *
152 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
155 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200156
157/**
158 * \brief Record the current test case as a failure based
159 * on comparing two signed integers.
160 *
161 * This function is usually called via the macro
162 * #TEST_LE_S.
163 *
164 * \param test Description of the failure or assertion that failed. This
165 * MUST be a string literal. This normally has the form
166 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
167 * and EXPR2 has the value \p value2.
168 * \param line_no Line number where the failure originated.
169 * \param filename Filename where the failure originated.
170 * \param value1 The first value to compare.
171 * \param value2 The second value to compare.
172 *
173 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
176 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200177
178/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200179 * \brief This function decodes the hexadecimal representation of
180 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200181 *
182 * \note The output buffer can be the same as the input buffer. For
183 * any other overlapping of the input and output buffers, the
184 * behavior is undefined.
185 *
186 * \param obuf Output buffer.
187 * \param obufmax Size in number of bytes of \p obuf.
188 * \param ibuf Input buffer.
189 * \param len The number of unsigned char written in \p obuf. This must
190 * not be \c NULL.
191 *
192 * \return \c 0 on success.
193 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200194 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
197 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199void mbedtls_test_hexify(unsigned char *obuf,
200 const unsigned char *ibuf,
201 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200202
203/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100204 * \brief Convert hexadecimal digit to an integer.
205 *
206 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
207 * `'a'` to `'f'`).
208 * \param[out] uc On success, the value of the digit (0 to 15).
209 *
210 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
211 */
212int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
213
214/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200215 * Allocate and zeroize a buffer.
216 *
217 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
218 *
219 * For convenience, dies if allocation fails.
220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200222
223/**
224 * Allocate and fill a buffer from hex data.
225 *
226 * The buffer is sized exactly as needed. This allows to detect buffer
227 * overruns (including overreads) when running the test suite under valgrind.
228 *
229 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
230 *
231 * For convenience, dies if allocation fails.
232 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
236 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200237
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100238#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100239#include "test/fake_external_rng_for_test.h"
240#endif
241
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100242#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Paul Elliottf25d8312023-11-23 18:49:43 +0000243/**
244 * Activate the mutex usage verification framework. See threading_helpers.c for
245 * information.
246 * */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100248
Paul Elliottf25d8312023-11-23 18:49:43 +0000249/**
250 * Deactivate the mutex usage verification framework. See threading_helpers.c
251 * for information.
252 */
253void mbedtls_test_mutex_usage_end(void);
254
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100255/** Call this function after executing a test case to check for mutex usage
256 * errors. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100258#endif /* MBEDTLS_TEST_MUTEX_USAGE */
259
Chris Jones96ae73b2021-01-08 17:04:59 +0000260#if defined(MBEDTLS_TEST_HOOKS)
261/**
Chris Jones3f613c12021-03-31 09:34:22 +0100262 * \brief Check that only a pure high-level error code is being combined with
263 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000264 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100265 *
266 * \note Both high-level and low-level error codes cannot be greater than
267 * zero however can be zero. If one error code is zero then the
268 * other error code is returned even if both codes are zero.
269 *
270 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272void mbedtls_test_err_add_check(int high, int low,
273 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000274#endif
275
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200276#endif /* TEST_HELPERS_H */