blob: 9d60c2034278b1152d00eac32030ef04b982c717 [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
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "mbedtls/config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Gilles Peskine2a4c5982021-01-29 21:18:09 +010034#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
35 defined(MBEDTLS_TEST_HOOKS)
36#define MBEDTLS_TEST_MUTEX_USAGE
37#endif
38
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 Peskinedb479712021-06-11 14:13:53 +020044#if defined(MBEDTLS_BIGNUM_C)
45#include "mbedtls/bignum.h"
46#endif
47
Gilles Peskine34cb4622022-09-20 21:37:56 +020048/** The type of test case arguments that contain binary data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049typedef struct data_tag {
50 uint8_t *x;
Gilles Peskine34cb4622022-09-20 21:37:56 +020051 uint32_t len;
52} data_t;
53
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskineb4366492021-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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100124void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000125
Ronald Crona0c25392020-06-18 10:10:46 +0200126/**
Gilles Peskineb4366492021-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 Peskine1b6c09a2023-01-11 14:52:35 +0100144int mbedtls_test_equal(const char *test, int line_no, const char *filename,
145 unsigned long long value1, unsigned long long value2);
Gilles Peskineb4366492021-04-29 20:28:54 +0200146
147/**
Gilles Peskine063700d2022-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 Peskine1b6c09a2023-01-11 14:52:35 +0100165int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
166 unsigned long long value1, unsigned long long value2);
Gilles Peskine063700d2022-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 Peskine1b6c09a2023-01-11 14:52:35 +0100186int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
187 long long value1, long long value2);
Gilles Peskine063700d2022-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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100210void mbedtls_test_hexify(unsigned char *obuf,
211 const unsigned char *ibuf,
212 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200213
214/**
215 * 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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100233unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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
Ronald Crona1236142020-07-01 16:01:21 +0200238#if defined(MBEDTLS_CHECK_PARAMS)
239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240typedef struct {
Ronald Crona1236142020-07-01 16:01:21 +0200241 const char *failure_condition;
242 const char *file;
243 int line;
244}
245mbedtls_test_param_failed_location_record_t;
246
247/**
248 * \brief Get the location record of the last call to
249 * mbedtls_test_param_failed().
250 *
251 * \note The call expectation is set up and active until the next call to
252 * mbedtls_test_param_failed_check_expected_call() or
253 * mbedtls_param_failed() that cancels it.
254 */
255void mbedtls_test_param_failed_get_location_record(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 mbedtls_test_param_failed_location_record_t *location_record);
Ronald Crona1236142020-07-01 16:01:21 +0200257
258/**
259 * \brief State that a call to mbedtls_param_failed() is expected.
260 *
261 * \note The call expectation is set up and active until the next call to
262 * mbedtls_test_param_failed_check_expected_call() or
263 * mbedtls_param_failed that cancel it.
264 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265void mbedtls_test_param_failed_expect_call(void);
Ronald Crona1236142020-07-01 16:01:21 +0200266
267/**
268 * \brief Check whether mbedtls_param_failed() has been called as expected.
269 *
270 * \note Check whether mbedtls_param_failed() has been called between the
271 * last call to mbedtls_test_param_failed_expect_call() and the call
272 * to this function.
273 *
274 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
275 * mbedtls_param_failed() has been called.
276 * \c -1 Otherwise.
277 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278int mbedtls_test_param_failed_check_expected_call(void);
Ronald Crona1236142020-07-01 16:01:21 +0200279
280/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200281 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200282 * state information used by mbedtls_param_failed() to do a long jump.
283 *
284 * \note If a call to mbedtls_param_failed() is not expected in the sense
285 * that there is no call to mbedtls_test_param_failed_expect_call()
286 * preceding it, then mbedtls_param_failed() will try to restore the
287 * execution to the state stored in the jmp_buf object whose address
288 * is returned by the present function.
289 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200290 * \note This function is intended to provide the parameter of the
291 * setjmp() function to set-up where mbedtls_param_failed() should
292 * long-jump if it has to. It is foreseen to be used as:
293 *
294 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
295 *
296 * \note The type of the returned value is not jmp_buf as jmp_buf is an
297 * an array type (C specification) and a function cannot return an
298 * array type.
299 *
300 * \note The type of the returned value is not jmp_buf* as then the return
301 * value couldn't be used by setjmp(), as its parameter's type is
302 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200303 *
304 * \return Address of the object of type jmp_buf holding the execution state
305 * information used by mbedtls_param_failed() to do a long jump.
306 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307void *mbedtls_test_param_failed_get_state_buf(void);
Ronald Crona1236142020-07-01 16:01:21 +0200308
309/**
310 * \brief Reset the execution state used by mbedtls_param_failed() to do a
311 * long jump.
312 *
313 * \note If a call to mbedtls_param_failed() is not expected in the sense
314 * that there is no call to mbedtls_test_param_failed_expect_call()
315 * preceding it, then mbedtls_param_failed() will try to restore the
316 * execution state that this function reset.
317 *
318 * \note It is recommended to reset the execution state when the state
319 * is not relevant anymore. That way an unexpected call to
320 * mbedtls_param_failed() will not trigger a long jump with
321 * undefined behavior but rather a long jump that will rather fault.
322 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323void mbedtls_test_param_failed_reset_state(void);
Ronald Crona1236142020-07-01 16:01:21 +0200324#endif /* MBEDTLS_CHECK_PARAMS */
325
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100326#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100327#include "test/fake_external_rng_for_test.h"
328#endif
329
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100330#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100331/** Permanently activate the mutex usage verification framework. See
332 * threading_helpers.c for information. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100334
335/** Call this function after executing a test case to check for mutex usage
336 * errors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100338#endif /* MBEDTLS_TEST_MUTEX_USAGE */
339
Chris Jones96ae73b2021-01-08 17:04:59 +0000340#if defined(MBEDTLS_TEST_HOOKS)
341/**
Chris Jones3f613c12021-03-31 09:34:22 +0100342 * \brief Check that only a pure high-level error code is being combined with
343 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000344 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100345 *
346 * \note Both high-level and low-level error codes cannot be greater than
347 * zero however can be zero. If one error code is zero then the
348 * other error code is returned even if both codes are zero.
349 *
350 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000351 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352void mbedtls_test_err_add_check(int high, int low,
353 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000354#endif
355
Gilles Peskinedb479712021-06-11 14:13:53 +0200356#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100357/** Read an MPI from a hexadecimal string.
Gilles Peskinedb479712021-06-11 14:13:53 +0200358 *
Gilles Peskine53a72062022-11-09 21:08:44 +0100359 * Like mbedtls_mpi_read_string(), but with tighter guarantees around
360 * edge cases.
Gilles Peskinedb479712021-06-11 14:13:53 +0200361 *
Gilles Peskine53a72062022-11-09 21:08:44 +0100362 * - This function guarantees that if \p s begins with '-' then the sign
363 * bit of the result will be negative, even if the value is 0.
364 * When this function encounters such a "negative 0", it
Gilles Peskined64123a2022-11-11 15:59:51 +0100365 * increments #mbedtls_test_case_uses_negative_0.
Gilles Peskine53a72062022-11-09 21:08:44 +0100366 * - The size of the result is exactly the minimum number of limbs needed
367 * to fit the digits in the input. In particular, this function constructs
368 * a bignum with 0 limbs for an empty string, and a bignum with leading 0
369 * limbs if the string has sufficiently many leading 0 digits.
370 * This is important so that the "0 (null)" and "0 (1 limb)" and
371 * "leading zeros" test cases do what they claim.
Gilles Peskinedb479712021-06-11 14:13:53 +0200372 *
373 * \param[out] X The MPI object to populate. It must be initialized.
Werner Lewis24b60782022-07-07 15:08:17 +0100374 * \param[in] s The null-terminated hexadecimal string to read from.
Gilles Peskinedb479712021-06-11 14:13:53 +0200375 *
376 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
377 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s);
Gilles Peskine53a72062022-11-09 21:08:44 +0100379
380/** Nonzero if the current test case had an input parsed with
381 * mbedtls_test_read_mpi() that is a negative 0 (`"-"`, `"-0"`, `"-00"`, etc.,
382 * constructing a result with the sign bit set to -1 and the value being
383 * all-limbs-0, which is not a valid representation in #mbedtls_mpi but is
384 * tested for robustness).
385 */
386extern unsigned mbedtls_test_case_uses_negative_0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200387#endif /* MBEDTLS_BIGNUM_C */
388
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200389#endif /* TEST_HELPERS_H */