blob: 0d97e80e55aa2c442743641e0c21e69f291ace78 [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 Peskineb4366492021-04-29 20:28:54 +020075 char line1[76];
76 char line2[76];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010077#if defined(MBEDTLS_TEST_MUTEX_USAGE)
78 const char *mutex_usage_error;
79#endif
Chris Jones9634bb12021-01-20 15:56:42 +000080}
Chris Jonese60e2ae2021-01-20 17:51:47 +000081mbedtls_test_info_t;
82extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000083
Ronald Crone9c09f12020-06-08 16:44:58 +020084int mbedtls_test_platform_setup( void );
85void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020086
Ronald Crona0c25392020-06-18 10:10:46 +020087/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000088 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000089 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000090 * This function can be called directly however it is usually
91 * called via macros such as TEST_ASSERT, TEST_EQUAL,
92 * PSA_ASSERT, etc...
93 *
94 * \note If the test case was already marked as failed, calling
95 * `mbedtls_test_fail( )` again will not overwrite any
96 * previous information about the failure.
97 *
98 * \param test Description of the failure or assertion that failed. This
99 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +0000100 * \param line_no Line number where the failure originated.
101 * \param filename Filename where the failure originated.
102 */
Chris Jones9634bb12021-01-20 15:56:42 +0000103void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +0000104
105/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000106 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000107 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000108 * This function can be called directly however it is usually
109 * called via the TEST_ASSUME macro.
110 *
111 * \param test Description of the assumption that caused the test case to
112 * be skipped. This MUST be a string literal.
113 * \param line_no Line number where the test case was skipped.
114 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000115 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000116void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000117
Chris Jones567e0ad2021-02-03 12:07:01 +0000118/**
119 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000120 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000121 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000122 * line number and file name if a test fails. Typically the "step
123 * number" is the index of a for loop but it can be whatever you
124 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000125 *
126 * \param step The step number to report.
127 */
128void mbedtls_test_set_step( unsigned long step );
129
Chris Jones567e0ad2021-02-03 12:07:01 +0000130/**
131 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000132 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000133void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000134
Ronald Crona0c25392020-06-18 10:10:46 +0200135/**
Gilles Peskineb4366492021-04-29 20:28:54 +0200136 * \brief Record the current test case as a failure if two integers
137 * have a different value.
138 *
139 * This function is usually called via the macro
140 * #TEST_EQUAL.
141 *
142 * \param test Description of the failure or assertion that failed. This
143 * MUST be a string literal. This normally has the form
144 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
145 * and EXPR2 has the value \p value2.
146 * \param line_no Line number where the failure originated.
147 * \param filename Filename where the failure originated.
148 * \param value1 The first value to compare.
149 * \param value2 The second value to compare.
150 *
151 * \return \c 1 if the values are equal, otherwise \c 0.
152 */
153int mbedtls_test_equal( const char *test, int line_no, const char* filename,
154 unsigned long long value1, unsigned long long value2 );
155
156/**
Gilles Peskine063700d2022-04-13 23:59:52 +0200157 * \brief Record the current test case as a failure based
158 * on comparing two unsigned integers.
159 *
160 * This function is usually called via the macro
161 * #TEST_LE_U.
162 *
163 * \param test Description of the failure or assertion that failed. This
164 * MUST be a string literal. This normally has the form
165 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
166 * and EXPR2 has the value \p value2.
167 * \param line_no Line number where the failure originated.
168 * \param filename Filename where the failure originated.
169 * \param value1 The first value to compare.
170 * \param value2 The second value to compare.
171 *
172 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
173 */
174int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
175 unsigned long long value1, unsigned long long value2 );
176
177/**
178 * \brief Record the current test case as a failure based
179 * on comparing two signed integers.
180 *
181 * This function is usually called via the macro
182 * #TEST_LE_S.
183 *
184 * \param test Description of the failure or assertion that failed. This
185 * MUST be a string literal. This normally has the form
186 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
187 * and EXPR2 has the value \p value2.
188 * \param line_no Line number where the failure originated.
189 * \param filename Filename where the failure originated.
190 * \param value1 The first value to compare.
191 * \param value2 The second value to compare.
192 *
193 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
194 */
195int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
196 long long value1, long long value2 );
197
198/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200199 * \brief This function decodes the hexadecimal representation of
200 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200201 *
202 * \note The output buffer can be the same as the input buffer. For
203 * any other overlapping of the input and output buffers, the
204 * behavior is undefined.
205 *
206 * \param obuf Output buffer.
207 * \param obufmax Size in number of bytes of \p obuf.
208 * \param ibuf Input buffer.
209 * \param len The number of unsigned char written in \p obuf. This must
210 * not be \c NULL.
211 *
212 * \return \c 0 on success.
213 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200214 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200215 */
216int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
217 const char *ibuf, size_t *len );
218
Ronald Cron72d628f2020-06-08 17:05:57 +0200219void mbedtls_test_hexify( unsigned char *obuf,
220 const unsigned char *ibuf,
221 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200222
223/**
224 * Allocate and zeroize a buffer.
225 *
226 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
227 *
228 * For convenience, dies if allocation fails.
229 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200230unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200231
232/**
233 * Allocate and fill a buffer from hex data.
234 *
235 * The buffer is sized exactly as needed. This allows to detect buffer
236 * overruns (including overreads) when running the test suite under valgrind.
237 *
238 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
239 *
240 * For convenience, dies if allocation fails.
241 */
Ronald Crona256c702020-06-10 10:53:11 +0200242unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200243
Ronald Cronde70b162020-06-10 11:03:08 +0200244int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
245 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200246
Ronald Crona1236142020-07-01 16:01:21 +0200247#if defined(MBEDTLS_CHECK_PARAMS)
248
249typedef struct
250{
251 const char *failure_condition;
252 const char *file;
253 int line;
254}
255mbedtls_test_param_failed_location_record_t;
256
257/**
258 * \brief Get the location record of the last call to
259 * mbedtls_test_param_failed().
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 cancels it.
264 */
265void mbedtls_test_param_failed_get_location_record(
266 mbedtls_test_param_failed_location_record_t *location_record );
267
268/**
269 * \brief State that a call to mbedtls_param_failed() is expected.
270 *
271 * \note The call expectation is set up and active until the next call to
272 * mbedtls_test_param_failed_check_expected_call() or
273 * mbedtls_param_failed that cancel it.
274 */
275void mbedtls_test_param_failed_expect_call( void );
276
277/**
278 * \brief Check whether mbedtls_param_failed() has been called as expected.
279 *
280 * \note Check whether mbedtls_param_failed() has been called between the
281 * last call to mbedtls_test_param_failed_expect_call() and the call
282 * to this function.
283 *
284 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
285 * mbedtls_param_failed() has been called.
286 * \c -1 Otherwise.
287 */
288int mbedtls_test_param_failed_check_expected_call( void );
289
290/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200291 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200292 * state information used by mbedtls_param_failed() to do a long jump.
293 *
294 * \note If a call to mbedtls_param_failed() is not expected in the sense
295 * that there is no call to mbedtls_test_param_failed_expect_call()
296 * preceding it, then mbedtls_param_failed() will try to restore the
297 * execution to the state stored in the jmp_buf object whose address
298 * is returned by the present function.
299 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200300 * \note This function is intended to provide the parameter of the
301 * setjmp() function to set-up where mbedtls_param_failed() should
302 * long-jump if it has to. It is foreseen to be used as:
303 *
304 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
305 *
306 * \note The type of the returned value is not jmp_buf as jmp_buf is an
307 * an array type (C specification) and a function cannot return an
308 * array type.
309 *
310 * \note The type of the returned value is not jmp_buf* as then the return
311 * value couldn't be used by setjmp(), as its parameter's type is
312 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200313 *
314 * \return Address of the object of type jmp_buf holding the execution state
315 * information used by mbedtls_param_failed() to do a long jump.
316 */
317void* mbedtls_test_param_failed_get_state_buf( void );
318
319/**
320 * \brief Reset the execution state used by mbedtls_param_failed() to do a
321 * long jump.
322 *
323 * \note If a call to mbedtls_param_failed() is not expected in the sense
324 * that there is no call to mbedtls_test_param_failed_expect_call()
325 * preceding it, then mbedtls_param_failed() will try to restore the
326 * execution state that this function reset.
327 *
328 * \note It is recommended to reset the execution state when the state
329 * is not relevant anymore. That way an unexpected call to
330 * mbedtls_param_failed() will not trigger a long jump with
331 * undefined behavior but rather a long jump that will rather fault.
332 */
333void mbedtls_test_param_failed_reset_state( void );
334#endif /* MBEDTLS_CHECK_PARAMS */
335
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100336#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100337#include "test/fake_external_rng_for_test.h"
338#endif
339
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100340#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100341/** Permanently activate the mutex usage verification framework. See
342 * threading_helpers.c for information. */
343void mbedtls_test_mutex_usage_init( void );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100344
345/** Call this function after executing a test case to check for mutex usage
346 * errors. */
347void mbedtls_test_mutex_usage_check( void );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100348#endif /* MBEDTLS_TEST_MUTEX_USAGE */
349
Chris Jones96ae73b2021-01-08 17:04:59 +0000350#if defined(MBEDTLS_TEST_HOOKS)
351/**
Chris Jones3f613c12021-03-31 09:34:22 +0100352 * \brief Check that only a pure high-level error code is being combined with
353 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000354 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100355 *
356 * \note Both high-level and low-level error codes cannot be greater than
357 * zero however can be zero. If one error code is zero then the
358 * other error code is returned even if both codes are zero.
359 *
360 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000361 */
362void mbedtls_test_err_add_check( int high, int low,
363 const char *file, int line);
364#endif
365
Gilles Peskinedb479712021-06-11 14:13:53 +0200366#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100367/** Read an MPI from a hexadecimal string.
Gilles Peskinedb479712021-06-11 14:13:53 +0200368 *
369 * Like mbedtls_mpi_read_string(), but size the resulting bignum based
370 * on the number of digits in the string. In particular, construct a
371 * bignum with 0 limbs for an empty string, and a bignum with leading 0
372 * limbs if the string has sufficiently many leading 0 digits.
373 *
374 * This is important so that the "0 (null)" and "0 (1 limb)" and
375 * "leading zeros" test cases do what they claim.
376 *
377 * \param[out] X The MPI object to populate. It must be initialized.
Werner Lewis24b60782022-07-07 15:08:17 +0100378 * \param[in] s The null-terminated hexadecimal string to read from.
Gilles Peskinedb479712021-06-11 14:13:53 +0200379 *
380 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
381 */
382/* Since the library has exactly the desired behavior, this is trivial. */
Werner Lewis24b60782022-07-07 15:08:17 +0100383int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s );
Gilles Peskinedb479712021-06-11 14:13:53 +0200384#endif /* MBEDTLS_BIGNUM_C */
385
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200386#endif /* TEST_HELPERS_H */