blob: 0f82a904061b0fd04215a1b4726e36421d6a3aa3 [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 Starzyk2c09c9b2021-05-14 22:20:10 +020028#define MBEDTLS_ALLOW_PRIVATE_ACCESS
29
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020030#if !defined(MBEDTLS_CONFIG_FILE)
31#include "mbedtls/config.h"
32#else
33#include MBEDTLS_CONFIG_FILE
34#endif
35
Gilles Peskine2a4c5982021-01-29 21:18:09 +010036#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
37 defined(MBEDTLS_TEST_HOOKS)
38#define MBEDTLS_TEST_MUTEX_USAGE
39#endif
40
Ronald Cronf40529d2020-06-09 16:27:37 +020041#if defined(MBEDTLS_PLATFORM_C)
42#include "mbedtls/platform.h"
43#else
44#include <stdio.h>
45#define mbedtls_fprintf fprintf
46#define mbedtls_snprintf snprintf
47#define mbedtls_calloc calloc
48#define mbedtls_free free
49#define mbedtls_exit exit
50#define mbedtls_time time
51#define mbedtls_time_t time_t
52#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
53#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
54#endif
55
56#include <stddef.h>
57#include <stdint.h>
58
Chris Jones9634bb12021-01-20 15:56:42 +000059typedef enum
60{
Chris Jonese60e2ae2021-01-20 17:51:47 +000061 MBEDTLS_TEST_RESULT_SUCCESS = 0,
62 MBEDTLS_TEST_RESULT_FAILED,
63 MBEDTLS_TEST_RESULT_SKIPPED
64} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000065
66typedef struct
67{
Chris Jonese60e2ae2021-01-20 17:51:47 +000068 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000069 const char *test;
70 const char *filename;
71 int line_no;
72 unsigned long step;
Gilles Peskine2a4c5982021-01-29 21:18:09 +010073#if defined(MBEDTLS_TEST_MUTEX_USAGE)
74 const char *mutex_usage_error;
75#endif
Chris Jones9634bb12021-01-20 15:56:42 +000076}
Chris Jonese60e2ae2021-01-20 17:51:47 +000077mbedtls_test_info_t;
78extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000079
Ronald Crone9c09f12020-06-08 16:44:58 +020080int mbedtls_test_platform_setup( void );
81void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020082
Ronald Crona0c25392020-06-18 10:10:46 +020083/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000084 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000085 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000086 * This function can be called directly however it is usually
87 * called via macros such as TEST_ASSERT, TEST_EQUAL,
88 * PSA_ASSERT, etc...
89 *
90 * \note If the test case was already marked as failed, calling
91 * `mbedtls_test_fail( )` again will not overwrite any
92 * previous information about the failure.
93 *
94 * \param test Description of the failure or assertion that failed. This
95 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000096 * \param line_no Line number where the failure originated.
97 * \param filename Filename where the failure originated.
98 */
Chris Jones9634bb12021-01-20 15:56:42 +000099void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +0000100
101/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000102 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000103 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000104 * This function can be called directly however it is usually
105 * called via the TEST_ASSUME macro.
106 *
107 * \param test Description of the assumption that caused the test case to
108 * be skipped. This MUST be a string literal.
109 * \param line_no Line number where the test case was skipped.
110 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000111 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000112void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000113
Chris Jones567e0ad2021-02-03 12:07:01 +0000114/**
115 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000116 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000117 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000118 * line number and file name if a test fails. Typically the "step
119 * number" is the index of a for loop but it can be whatever you
120 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000121 *
122 * \param step The step number to report.
123 */
124void mbedtls_test_set_step( unsigned long step );
125
Chris Jones567e0ad2021-02-03 12:07:01 +0000126/**
127 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000128 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000129void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000130
Ronald Crona0c25392020-06-18 10:10:46 +0200131/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200132 * \brief This function decodes the hexadecimal representation of
133 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200134 *
135 * \note The output buffer can be the same as the input buffer. For
136 * any other overlapping of the input and output buffers, the
137 * behavior is undefined.
138 *
139 * \param obuf Output buffer.
140 * \param obufmax Size in number of bytes of \p obuf.
141 * \param ibuf Input buffer.
142 * \param len The number of unsigned char written in \p obuf. This must
143 * not be \c NULL.
144 *
145 * \return \c 0 on success.
146 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200147 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200148 */
149int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
150 const char *ibuf, size_t *len );
151
Ronald Cron72d628f2020-06-08 17:05:57 +0200152void mbedtls_test_hexify( unsigned char *obuf,
153 const unsigned char *ibuf,
154 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200155
156/**
157 * Allocate and zeroize a buffer.
158 *
159 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
160 *
161 * For convenience, dies if allocation fails.
162 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200163unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200164
165/**
166 * Allocate and fill a buffer from hex data.
167 *
168 * The buffer is sized exactly as needed. This allows to detect buffer
169 * overruns (including overreads) when running the test suite under valgrind.
170 *
171 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
172 *
173 * For convenience, dies if allocation fails.
174 */
Ronald Crona256c702020-06-10 10:53:11 +0200175unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200176
Ronald Cronde70b162020-06-10 11:03:08 +0200177int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
178 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200179
Ronald Crona1236142020-07-01 16:01:21 +0200180#if defined(MBEDTLS_CHECK_PARAMS)
181
182typedef struct
183{
184 const char *failure_condition;
185 const char *file;
186 int line;
187}
188mbedtls_test_param_failed_location_record_t;
189
190/**
191 * \brief Get the location record of the last call to
192 * mbedtls_test_param_failed().
193 *
194 * \note The call expectation is set up and active until the next call to
195 * mbedtls_test_param_failed_check_expected_call() or
196 * mbedtls_param_failed() that cancels it.
197 */
198void mbedtls_test_param_failed_get_location_record(
199 mbedtls_test_param_failed_location_record_t *location_record );
200
201/**
202 * \brief State that a call to mbedtls_param_failed() is expected.
203 *
204 * \note The call expectation is set up and active until the next call to
205 * mbedtls_test_param_failed_check_expected_call() or
206 * mbedtls_param_failed that cancel it.
207 */
208void mbedtls_test_param_failed_expect_call( void );
209
210/**
211 * \brief Check whether mbedtls_param_failed() has been called as expected.
212 *
213 * \note Check whether mbedtls_param_failed() has been called between the
214 * last call to mbedtls_test_param_failed_expect_call() and the call
215 * to this function.
216 *
217 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
218 * mbedtls_param_failed() has been called.
219 * \c -1 Otherwise.
220 */
221int mbedtls_test_param_failed_check_expected_call( void );
222
223/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200224 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200225 * state information used by mbedtls_param_failed() to do a long jump.
226 *
227 * \note If a call to mbedtls_param_failed() is not expected in the sense
228 * that there is no call to mbedtls_test_param_failed_expect_call()
229 * preceding it, then mbedtls_param_failed() will try to restore the
230 * execution to the state stored in the jmp_buf object whose address
231 * is returned by the present function.
232 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200233 * \note This function is intended to provide the parameter of the
234 * setjmp() function to set-up where mbedtls_param_failed() should
235 * long-jump if it has to. It is foreseen to be used as:
236 *
237 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
238 *
239 * \note The type of the returned value is not jmp_buf as jmp_buf is an
240 * an array type (C specification) and a function cannot return an
241 * array type.
242 *
243 * \note The type of the returned value is not jmp_buf* as then the return
244 * value couldn't be used by setjmp(), as its parameter's type is
245 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200246 *
247 * \return Address of the object of type jmp_buf holding the execution state
248 * information used by mbedtls_param_failed() to do a long jump.
249 */
250void* mbedtls_test_param_failed_get_state_buf( void );
251
252/**
253 * \brief Reset the execution state used by mbedtls_param_failed() to do a
254 * long jump.
255 *
256 * \note If a call to mbedtls_param_failed() is not expected in the sense
257 * that there is no call to mbedtls_test_param_failed_expect_call()
258 * preceding it, then mbedtls_param_failed() will try to restore the
259 * execution state that this function reset.
260 *
261 * \note It is recommended to reset the execution state when the state
262 * is not relevant anymore. That way an unexpected call to
263 * mbedtls_param_failed() will not trigger a long jump with
264 * undefined behavior but rather a long jump that will rather fault.
265 */
266void mbedtls_test_param_failed_reset_state( void );
267#endif /* MBEDTLS_CHECK_PARAMS */
268
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100269#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100270#include "test/fake_external_rng_for_test.h"
271#endif
272
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100273#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100274/** Permanently activate the mutex usage verification framework. See
275 * threading_helpers.c for information. */
276void mbedtls_test_mutex_usage_init( void );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100277
278/** Call this function after executing a test case to check for mutex usage
279 * errors. */
280void mbedtls_test_mutex_usage_check( void );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100281#endif /* MBEDTLS_TEST_MUTEX_USAGE */
282
Chris Jones96ae73b2021-01-08 17:04:59 +0000283#if defined(MBEDTLS_TEST_HOOKS)
284/**
Chris Jones3f613c12021-03-31 09:34:22 +0100285 * \brief Check that only a pure high-level error code is being combined with
286 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000287 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100288 *
289 * \note Both high-level and low-level error codes cannot be greater than
290 * zero however can be zero. If one error code is zero then the
291 * other error code is returned even if both codes are zero.
292 *
293 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000294 */
295void mbedtls_test_err_add_check( int high, int low,
296 const char *file, int line);
297#endif
298
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200299#endif /* TEST_HELPERS_H */