blob: dfc09cdbb93c69db7d2abbe49d49d6a21e02b96a [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#if defined(MBEDTLS_PLATFORM_C)
40#include "mbedtls/platform.h"
41#else
42#include <stdio.h>
43#define mbedtls_fprintf fprintf
44#define mbedtls_snprintf snprintf
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#define mbedtls_exit exit
48#define mbedtls_time time
49#define mbedtls_time_t time_t
50#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
51#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
52#endif
53
54#include <stddef.h>
55#include <stdint.h>
56
Gilles Peskinedb479712021-06-11 14:13:53 +020057#if defined(MBEDTLS_BIGNUM_C)
58#include "mbedtls/bignum.h"
59#endif
60
Chris Jones9634bb12021-01-20 15:56:42 +000061typedef enum
62{
Chris Jonese60e2ae2021-01-20 17:51:47 +000063 MBEDTLS_TEST_RESULT_SUCCESS = 0,
64 MBEDTLS_TEST_RESULT_FAILED,
65 MBEDTLS_TEST_RESULT_SKIPPED
66} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000067
68typedef struct
69{
Chris Jonese60e2ae2021-01-20 17:51:47 +000070 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000071 const char *test;
72 const char *filename;
73 int line_no;
74 unsigned long step;
Gilles Peskine2a4c5982021-01-29 21:18:09 +010075#if defined(MBEDTLS_TEST_MUTEX_USAGE)
76 const char *mutex_usage_error;
77#endif
Chris Jones9634bb12021-01-20 15:56:42 +000078}
Chris Jonese60e2ae2021-01-20 17:51:47 +000079mbedtls_test_info_t;
80extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000081
Ronald Crone9c09f12020-06-08 16:44:58 +020082int mbedtls_test_platform_setup( void );
83void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020084
Ronald Crona0c25392020-06-18 10:10:46 +020085/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000086 * \brief Record the current test case as a failure.
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 macros such as TEST_ASSERT, TEST_EQUAL,
90 * PSA_ASSERT, etc...
91 *
92 * \note If the test case was already marked as failed, calling
93 * `mbedtls_test_fail( )` again will not overwrite any
94 * previous information about the failure.
95 *
96 * \param test Description of the failure or assertion that failed. This
97 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000098 * \param line_no Line number where the failure originated.
99 * \param filename Filename where the failure originated.
100 */
Chris Jones9634bb12021-01-20 15:56:42 +0000101void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +0000102
103/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000104 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000105 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000106 * This function can be called directly however it is usually
107 * called via the TEST_ASSUME macro.
108 *
109 * \param test Description of the assumption that caused the test case to
110 * be skipped. This MUST be a string literal.
111 * \param line_no Line number where the test case was skipped.
112 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000113 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000114void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000115
Chris Jones567e0ad2021-02-03 12:07:01 +0000116/**
117 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000118 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000119 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000120 * line number and file name if a test fails. Typically the "step
121 * number" is the index of a for loop but it can be whatever you
122 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000123 *
124 * \param step The step number to report.
125 */
126void mbedtls_test_set_step( unsigned long step );
127
Chris Jones567e0ad2021-02-03 12:07:01 +0000128/**
129 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000130 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000131void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000132
Ronald Crona0c25392020-06-18 10:10:46 +0200133/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200134 * \brief This function decodes the hexadecimal representation of
135 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200136 *
137 * \note The output buffer can be the same as the input buffer. For
138 * any other overlapping of the input and output buffers, the
139 * behavior is undefined.
140 *
141 * \param obuf Output buffer.
142 * \param obufmax Size in number of bytes of \p obuf.
143 * \param ibuf Input buffer.
144 * \param len The number of unsigned char written in \p obuf. This must
145 * not be \c NULL.
146 *
147 * \return \c 0 on success.
148 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200149 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200150 */
151int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
152 const char *ibuf, size_t *len );
153
Ronald Cron72d628f2020-06-08 17:05:57 +0200154void mbedtls_test_hexify( unsigned char *obuf,
155 const unsigned char *ibuf,
156 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200157
158/**
159 * Allocate and zeroize a buffer.
160 *
161 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
162 *
163 * For convenience, dies if allocation fails.
164 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200165unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200166
167/**
168 * Allocate and fill a buffer from hex data.
169 *
170 * The buffer is sized exactly as needed. This allows to detect buffer
171 * overruns (including overreads) when running the test suite under valgrind.
172 *
173 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
174 *
175 * For convenience, dies if allocation fails.
176 */
Ronald Crona256c702020-06-10 10:53:11 +0200177unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200178
Ronald Cronde70b162020-06-10 11:03:08 +0200179int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
180 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200181
Ronald Crona1236142020-07-01 16:01:21 +0200182#if defined(MBEDTLS_CHECK_PARAMS)
183
184typedef struct
185{
186 const char *failure_condition;
187 const char *file;
188 int line;
189}
190mbedtls_test_param_failed_location_record_t;
191
192/**
193 * \brief Get the location record of the last call to
194 * mbedtls_test_param_failed().
195 *
196 * \note The call expectation is set up and active until the next call to
197 * mbedtls_test_param_failed_check_expected_call() or
198 * mbedtls_param_failed() that cancels it.
199 */
200void mbedtls_test_param_failed_get_location_record(
201 mbedtls_test_param_failed_location_record_t *location_record );
202
203/**
204 * \brief State that a call to mbedtls_param_failed() is expected.
205 *
206 * \note The call expectation is set up and active until the next call to
207 * mbedtls_test_param_failed_check_expected_call() or
208 * mbedtls_param_failed that cancel it.
209 */
210void mbedtls_test_param_failed_expect_call( void );
211
212/**
213 * \brief Check whether mbedtls_param_failed() has been called as expected.
214 *
215 * \note Check whether mbedtls_param_failed() has been called between the
216 * last call to mbedtls_test_param_failed_expect_call() and the call
217 * to this function.
218 *
219 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
220 * mbedtls_param_failed() has been called.
221 * \c -1 Otherwise.
222 */
223int mbedtls_test_param_failed_check_expected_call( void );
224
225/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200226 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200227 * state information used by mbedtls_param_failed() to do a long jump.
228 *
229 * \note If a call to mbedtls_param_failed() is not expected in the sense
230 * that there is no call to mbedtls_test_param_failed_expect_call()
231 * preceding it, then mbedtls_param_failed() will try to restore the
232 * execution to the state stored in the jmp_buf object whose address
233 * is returned by the present function.
234 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200235 * \note This function is intended to provide the parameter of the
236 * setjmp() function to set-up where mbedtls_param_failed() should
237 * long-jump if it has to. It is foreseen to be used as:
238 *
239 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
240 *
241 * \note The type of the returned value is not jmp_buf as jmp_buf is an
242 * an array type (C specification) and a function cannot return an
243 * array type.
244 *
245 * \note The type of the returned value is not jmp_buf* as then the return
246 * value couldn't be used by setjmp(), as its parameter's type is
247 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200248 *
249 * \return Address of the object of type jmp_buf holding the execution state
250 * information used by mbedtls_param_failed() to do a long jump.
251 */
252void* mbedtls_test_param_failed_get_state_buf( void );
253
254/**
255 * \brief Reset the execution state used by mbedtls_param_failed() to do a
256 * long jump.
257 *
258 * \note If a call to mbedtls_param_failed() is not expected in the sense
259 * that there is no call to mbedtls_test_param_failed_expect_call()
260 * preceding it, then mbedtls_param_failed() will try to restore the
261 * execution state that this function reset.
262 *
263 * \note It is recommended to reset the execution state when the state
264 * is not relevant anymore. That way an unexpected call to
265 * mbedtls_param_failed() will not trigger a long jump with
266 * undefined behavior but rather a long jump that will rather fault.
267 */
268void mbedtls_test_param_failed_reset_state( void );
269#endif /* MBEDTLS_CHECK_PARAMS */
270
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100271#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100272#include "test/fake_external_rng_for_test.h"
273#endif
274
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100275#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100276/** Permanently activate the mutex usage verification framework. See
277 * threading_helpers.c for information. */
278void mbedtls_test_mutex_usage_init( void );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100279
280/** Call this function after executing a test case to check for mutex usage
281 * errors. */
282void mbedtls_test_mutex_usage_check( void );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100283#endif /* MBEDTLS_TEST_MUTEX_USAGE */
284
Chris Jones96ae73b2021-01-08 17:04:59 +0000285#if defined(MBEDTLS_TEST_HOOKS)
286/**
Chris Jones3f613c12021-03-31 09:34:22 +0100287 * \brief Check that only a pure high-level error code is being combined with
288 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000289 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100290 *
291 * \note Both high-level and low-level error codes cannot be greater than
292 * zero however can be zero. If one error code is zero then the
293 * other error code is returned even if both codes are zero.
294 *
295 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000296 */
297void mbedtls_test_err_add_check( int high, int low,
298 const char *file, int line);
299#endif
300
Gilles Peskinedb479712021-06-11 14:13:53 +0200301#if defined(MBEDTLS_BIGNUM_C)
302/** Read an MPI from a string.
303 *
304 * Like mbedtls_mpi_read_string(), but size the resulting bignum based
305 * on the number of digits in the string. In particular, construct a
306 * bignum with 0 limbs for an empty string, and a bignum with leading 0
307 * limbs if the string has sufficiently many leading 0 digits.
308 *
309 * This is important so that the "0 (null)" and "0 (1 limb)" and
310 * "leading zeros" test cases do what they claim.
311 *
312 * \param[out] X The MPI object to populate. It must be initialized.
313 * \param radix The radix (2 to 16).
314 * \param[in] s The null-terminated string to read from.
315 *
316 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
317 */
318/* Since the library has exactly the desired behavior, this is trivial. */
319int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s );
320#endif /* MBEDTLS_BIGNUM_C */
321
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200322#endif /* TEST_HELPERS_H */