blob: a1098c5558982c95a80785772e1db640059f2caf [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
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020033#if !defined(MBEDTLS_CONFIG_FILE)
34#include "mbedtls/config.h"
35#else
36#include MBEDTLS_CONFIG_FILE
37#endif
38
Gilles Peskine2a4c5982021-01-29 21:18:09 +010039#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
40 defined(MBEDTLS_TEST_HOOKS)
41#define MBEDTLS_TEST_MUTEX_USAGE
42#endif
43
Ronald Cronf40529d2020-06-09 16:27:37 +020044#if defined(MBEDTLS_PLATFORM_C)
45#include "mbedtls/platform.h"
46#else
47#include <stdio.h>
48#define mbedtls_fprintf fprintf
49#define mbedtls_snprintf snprintf
50#define mbedtls_calloc calloc
51#define mbedtls_free free
52#define mbedtls_exit exit
53#define mbedtls_time time
54#define mbedtls_time_t time_t
55#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
56#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
57#endif
58
59#include <stddef.h>
60#include <stdint.h>
61
Chris Jones9634bb12021-01-20 15:56:42 +000062typedef enum
63{
Chris Jonese60e2ae2021-01-20 17:51:47 +000064 MBEDTLS_TEST_RESULT_SUCCESS = 0,
65 MBEDTLS_TEST_RESULT_FAILED,
66 MBEDTLS_TEST_RESULT_SKIPPED
67} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000068
69typedef struct
70{
Chris Jonese60e2ae2021-01-20 17:51:47 +000071 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000072 const char *test;
73 const char *filename;
74 int line_no;
75 unsigned long step;
Gilles Peskine2a4c5982021-01-29 21:18:09 +010076#if defined(MBEDTLS_TEST_MUTEX_USAGE)
77 const char *mutex_usage_error;
78#endif
Chris Jones9634bb12021-01-20 15:56:42 +000079}
Chris Jonese60e2ae2021-01-20 17:51:47 +000080mbedtls_test_info_t;
81extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000082
Ronald Crone9c09f12020-06-08 16:44:58 +020083int mbedtls_test_platform_setup( void );
84void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020085
Ronald Crona0c25392020-06-18 10:10:46 +020086/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000087 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000088 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000089 * This function can be called directly however it is usually
90 * called via macros such as TEST_ASSERT, TEST_EQUAL,
91 * PSA_ASSERT, etc...
92 *
93 * \note If the test case was already marked as failed, calling
94 * `mbedtls_test_fail( )` again will not overwrite any
95 * previous information about the failure.
96 *
97 * \param test Description of the failure or assertion that failed. This
98 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000099 * \param line_no Line number where the failure originated.
100 * \param filename Filename where the failure originated.
101 */
Chris Jones9634bb12021-01-20 15:56:42 +0000102void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +0000103
104/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000105 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000106 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000107 * This function can be called directly however it is usually
108 * called via the TEST_ASSUME macro.
109 *
110 * \param test Description of the assumption that caused the test case to
111 * be skipped. This MUST be a string literal.
112 * \param line_no Line number where the test case was skipped.
113 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000114 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000115void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000116
Chris Jones567e0ad2021-02-03 12:07:01 +0000117/**
118 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000119 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000120 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000121 * line number and file name if a test fails. Typically the "step
122 * number" is the index of a for loop but it can be whatever you
123 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000124 *
125 * \param step The step number to report.
126 */
127void mbedtls_test_set_step( unsigned long step );
128
Chris Jones567e0ad2021-02-03 12:07:01 +0000129/**
130 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000131 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000132void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000133
Ronald Crona0c25392020-06-18 10:10:46 +0200134/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200135 * \brief This function decodes the hexadecimal representation of
136 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200137 *
138 * \note The output buffer can be the same as the input buffer. For
139 * any other overlapping of the input and output buffers, the
140 * behavior is undefined.
141 *
142 * \param obuf Output buffer.
143 * \param obufmax Size in number of bytes of \p obuf.
144 * \param ibuf Input buffer.
145 * \param len The number of unsigned char written in \p obuf. This must
146 * not be \c NULL.
147 *
148 * \return \c 0 on success.
149 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200150 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200151 */
152int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
153 const char *ibuf, size_t *len );
154
Ronald Cron72d628f2020-06-08 17:05:57 +0200155void mbedtls_test_hexify( unsigned char *obuf,
156 const unsigned char *ibuf,
157 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200158
159/**
160 * Allocate and zeroize a buffer.
161 *
162 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
163 *
164 * For convenience, dies if allocation fails.
165 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200166unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200167
168/**
169 * Allocate and fill a buffer from hex data.
170 *
171 * The buffer is sized exactly as needed. This allows to detect buffer
172 * overruns (including overreads) when running the test suite under valgrind.
173 *
174 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
175 *
176 * For convenience, dies if allocation fails.
177 */
Ronald Crona256c702020-06-10 10:53:11 +0200178unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200179
Ronald Cronde70b162020-06-10 11:03:08 +0200180int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
181 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200182
Ronald Crona1236142020-07-01 16:01:21 +0200183#if defined(MBEDTLS_CHECK_PARAMS)
184
185typedef struct
186{
187 const char *failure_condition;
188 const char *file;
189 int line;
190}
191mbedtls_test_param_failed_location_record_t;
192
193/**
194 * \brief Get the location record of the last call to
195 * mbedtls_test_param_failed().
196 *
197 * \note The call expectation is set up and active until the next call to
198 * mbedtls_test_param_failed_check_expected_call() or
199 * mbedtls_param_failed() that cancels it.
200 */
201void mbedtls_test_param_failed_get_location_record(
202 mbedtls_test_param_failed_location_record_t *location_record );
203
204/**
205 * \brief State that a call to mbedtls_param_failed() is expected.
206 *
207 * \note The call expectation is set up and active until the next call to
208 * mbedtls_test_param_failed_check_expected_call() or
209 * mbedtls_param_failed that cancel it.
210 */
211void mbedtls_test_param_failed_expect_call( void );
212
213/**
214 * \brief Check whether mbedtls_param_failed() has been called as expected.
215 *
216 * \note Check whether mbedtls_param_failed() has been called between the
217 * last call to mbedtls_test_param_failed_expect_call() and the call
218 * to this function.
219 *
220 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
221 * mbedtls_param_failed() has been called.
222 * \c -1 Otherwise.
223 */
224int mbedtls_test_param_failed_check_expected_call( void );
225
226/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200227 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200228 * state information used by mbedtls_param_failed() to do a long jump.
229 *
230 * \note If a call to mbedtls_param_failed() is not expected in the sense
231 * that there is no call to mbedtls_test_param_failed_expect_call()
232 * preceding it, then mbedtls_param_failed() will try to restore the
233 * execution to the state stored in the jmp_buf object whose address
234 * is returned by the present function.
235 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200236 * \note This function is intended to provide the parameter of the
237 * setjmp() function to set-up where mbedtls_param_failed() should
238 * long-jump if it has to. It is foreseen to be used as:
239 *
240 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
241 *
242 * \note The type of the returned value is not jmp_buf as jmp_buf is an
243 * an array type (C specification) and a function cannot return an
244 * array type.
245 *
246 * \note The type of the returned value is not jmp_buf* as then the return
247 * value couldn't be used by setjmp(), as its parameter's type is
248 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200249 *
250 * \return Address of the object of type jmp_buf holding the execution state
251 * information used by mbedtls_param_failed() to do a long jump.
252 */
253void* mbedtls_test_param_failed_get_state_buf( void );
254
255/**
256 * \brief Reset the execution state used by mbedtls_param_failed() to do a
257 * long jump.
258 *
259 * \note If a call to mbedtls_param_failed() is not expected in the sense
260 * that there is no call to mbedtls_test_param_failed_expect_call()
261 * preceding it, then mbedtls_param_failed() will try to restore the
262 * execution state that this function reset.
263 *
264 * \note It is recommended to reset the execution state when the state
265 * is not relevant anymore. That way an unexpected call to
266 * mbedtls_param_failed() will not trigger a long jump with
267 * undefined behavior but rather a long jump that will rather fault.
268 */
269void mbedtls_test_param_failed_reset_state( void );
270#endif /* MBEDTLS_CHECK_PARAMS */
271
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100272#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100273#include "test/fake_external_rng_for_test.h"
274#endif
275
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100276#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100277/** Permanently activate the mutex usage verification framework. See
278 * threading_helpers.c for information. */
279void mbedtls_test_mutex_usage_init( void );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100280
281/** Call this function after executing a test case to check for mutex usage
282 * errors. */
283void mbedtls_test_mutex_usage_check( void );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100284#endif /* MBEDTLS_TEST_MUTEX_USAGE */
285
Chris Jones96ae73b2021-01-08 17:04:59 +0000286#if defined(MBEDTLS_TEST_HOOKS)
287/**
Chris Jones3f613c12021-03-31 09:34:22 +0100288 * \brief Check that only a pure high-level error code is being combined with
289 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000290 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100291 *
292 * \note Both high-level and low-level error codes cannot be greater than
293 * zero however can be zero. If one error code is zero then the
294 * other error code is returned even if both codes are zero.
295 *
296 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000297 */
298void mbedtls_test_err_add_check( int high, int low,
299 const char *file, int line);
300#endif
301
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200302#endif /* TEST_HELPERS_H */