blob: dd4a6a2b46909a6c56d62af5fae434b9d8908735 [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
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020010 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020023 */
24
25#ifndef TEST_HELPERS_H
26#define TEST_HELPERS_H
27
Mateusz Starzykb1982722021-05-27 14:46:48 +020028/* Most fields of publicly available structs are private and are wrapped with
29 * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
30 * directly (without using the MBEDTLS_PRIVATE wrapper). */
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020031#define MBEDTLS_ALLOW_PRIVATE_ACCESS
32
Bence Szépkútic662b362021-05-27 11:25:03 +020033#include "mbedtls/build_info.h"
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020034
Gilles Peskine2a4c5982021-01-29 21:18:09 +010035#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
36 defined(MBEDTLS_TEST_HOOKS)
37#define MBEDTLS_TEST_MUTEX_USAGE
38#endif
39
Ronald Cronf40529d2020-06-09 16:27:37 +020040#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020041
42#include <stddef.h>
43#include <stdint.h>
44
Gilles Peskineebc49e52021-06-11 14:13:53 +020045#if defined(MBEDTLS_BIGNUM_C)
46#include "mbedtls/bignum.h"
47#endif
48
Gilles Peskine571576f2022-09-20 21:37:56 +020049/** The type of test case arguments that contain binary data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010050typedef struct data_tag {
51 uint8_t *x;
Gilles Peskine571576f2022-09-20 21:37:56 +020052 uint32_t len;
53} data_t;
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000056 MBEDTLS_TEST_RESULT_SUCCESS = 0,
57 MBEDTLS_TEST_RESULT_FAILED,
58 MBEDTLS_TEST_RESULT_SKIPPED
59} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000060
Gilles Peskine449bd832023-01-11 14:50:10 +010061typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000062 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000063 const char *test;
64 const char *filename;
65 int line_no;
66 unsigned long step;
Gilles Peskine89615ee2021-04-29 20:28:54 +020067 char line1[76];
68 char line2[76];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010069#if defined(MBEDTLS_TEST_MUTEX_USAGE)
70 const char *mutex_usage_error;
71#endif
Chris Jones9634bb12021-01-20 15:56:42 +000072}
Chris Jonese60e2ae2021-01-20 17:51:47 +000073mbedtls_test_info_t;
74extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000075
Gilles Peskine449bd832023-01-11 14:50:10 +010076int mbedtls_test_platform_setup(void);
77void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +020078
Ronald Crona0c25392020-06-18 10:10:46 +020079/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000080 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000081 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000082 * This function can be called directly however it is usually
83 * called via macros such as TEST_ASSERT, TEST_EQUAL,
84 * PSA_ASSERT, etc...
85 *
86 * \note If the test case was already marked as failed, calling
87 * `mbedtls_test_fail( )` again will not overwrite any
88 * previous information about the failure.
89 *
90 * \param test Description of the failure or assertion that failed. This
91 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000092 * \param line_no Line number where the failure originated.
93 * \param filename Filename where the failure originated.
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +000096
97/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000098 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000099 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000100 * This function can be called directly however it is usually
101 * called via the TEST_ASSUME macro.
102 *
103 * \param test Description of the assumption that caused the test case to
104 * be skipped. This MUST be a string literal.
105 * \param line_no Line number where the test case was skipped.
106 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000107 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000109
Chris Jones567e0ad2021-02-03 12:07:01 +0000110/**
111 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000112 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000113 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000114 * line number and file name if a test fails. Typically the "step
115 * number" is the index of a for loop but it can be whatever you
116 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000117 *
118 * \param step The step number to report.
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000121
Chris Jones567e0ad2021-02-03 12:07:01 +0000122/**
123 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000126
Ronald Crona0c25392020-06-18 10:10:46 +0200127/**
Gilles Peskine89615ee2021-04-29 20:28:54 +0200128 * \brief Record the current test case as a failure if two integers
129 * have a different value.
130 *
131 * This function is usually called via the macro
132 * #TEST_EQUAL.
133 *
134 * \param test Description of the failure or assertion that failed. This
135 * MUST be a string literal. This normally has the form
136 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
137 * and EXPR2 has the value \p value2.
138 * \param line_no Line number where the failure originated.
139 * \param filename Filename where the failure originated.
140 * \param value1 The first value to compare.
141 * \param value2 The second value to compare.
142 *
143 * \return \c 1 if the values are equal, otherwise \c 0.
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145int mbedtls_test_equal(const char *test, int line_no, const char *filename,
146 unsigned long long value1, unsigned long long value2);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200147
148/**
Gilles Peskined1465422022-04-13 23:59:52 +0200149 * \brief Record the current test case as a failure based
150 * on comparing two unsigned integers.
151 *
152 * This function is usually called via the macro
153 * #TEST_LE_U.
154 *
155 * \param test Description of the failure or assertion that failed. This
156 * MUST be a string literal. This normally has the form
157 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
158 * and EXPR2 has the value \p value2.
159 * \param line_no Line number where the failure originated.
160 * \param filename Filename where the failure originated.
161 * \param value1 The first value to compare.
162 * \param value2 The second value to compare.
163 *
164 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
165 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
167 unsigned long long value1, unsigned long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200168
169/**
170 * \brief Record the current test case as a failure based
171 * on comparing two signed integers.
172 *
173 * This function is usually called via the macro
174 * #TEST_LE_S.
175 *
176 * \param test Description of the failure or assertion that failed. This
177 * MUST be a string literal. This normally has the form
178 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
179 * and EXPR2 has the value \p value2.
180 * \param line_no Line number where the failure originated.
181 * \param filename Filename where the failure originated.
182 * \param value1 The first value to compare.
183 * \param value2 The second value to compare.
184 *
185 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
188 long long value1, long long value2);
Gilles Peskined1465422022-04-13 23:59:52 +0200189
190/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200191 * \brief This function decodes the hexadecimal representation of
192 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200193 *
194 * \note The output buffer can be the same as the input buffer. For
195 * any other overlapping of the input and output buffers, the
196 * behavior is undefined.
197 *
198 * \param obuf Output buffer.
199 * \param obufmax Size in number of bytes of \p obuf.
200 * \param ibuf Input buffer.
201 * \param len The number of unsigned char written in \p obuf. This must
202 * not be \c NULL.
203 *
204 * \return \c 0 on success.
205 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200206 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200207 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
209 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211void mbedtls_test_hexify(unsigned char *obuf,
212 const unsigned char *ibuf,
213 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200214
215/**
Gilles Peskine881447d2022-12-08 15:24:52 +0100216 * \brief Convert hexadecimal digit to an integer.
217 *
218 * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or
219 * `'a'` to `'f'`).
220 * \param[out] uc On success, the value of the digit (0 to 15).
221 *
222 * \return 0 on success, -1 if \p c is not a hexadecimal digit.
223 */
224int mbedtls_test_ascii2uc(const char c, unsigned char *uc);
225
226/**
Ronald Cronf40529d2020-06-09 16:27:37 +0200227 * Allocate and zeroize a buffer.
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_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200234
235/**
236 * Allocate and fill a buffer from hex data.
237 *
238 * The buffer is sized exactly as needed. This allows to detect buffer
239 * overruns (including overreads) when running the test suite under valgrind.
240 *
241 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
242 *
243 * For convenience, dies if allocation fails.
244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
248 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200249
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100250#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100251#include "test/fake_external_rng_for_test.h"
252#endif
253
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100254#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100255/** Permanently activate the mutex usage verification framework. See
256 * threading_helpers.c for information. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100258
259/** Call this function after executing a test case to check for mutex usage
260 * errors. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100262#endif /* MBEDTLS_TEST_MUTEX_USAGE */
263
Chris Jones96ae73b2021-01-08 17:04:59 +0000264#if defined(MBEDTLS_TEST_HOOKS)
265/**
Chris Jones3f613c12021-03-31 09:34:22 +0100266 * \brief Check that only a pure high-level error code is being combined with
267 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000268 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100269 *
270 * \note Both high-level and low-level error codes cannot be greater than
271 * zero however can be zero. If one error code is zero then the
272 * other error code is returned even if both codes are zero.
273 *
274 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276void mbedtls_test_err_add_check(int high, int low,
277 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000278#endif
279
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200280#endif /* TEST_HELPERS_H */