blob: 7a87c5b84dfe7e8007bc21efeb4643018dd0e7d3 [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. */
49typedef struct data_tag
50{
51 uint8_t * x;
52 uint32_t len;
53} data_t;
54
Chris Jones9634bb12021-01-20 15:56:42 +000055typedef enum
56{
Chris Jonese60e2ae2021-01-20 17:51:47 +000057 MBEDTLS_TEST_RESULT_SUCCESS = 0,
58 MBEDTLS_TEST_RESULT_FAILED,
59 MBEDTLS_TEST_RESULT_SKIPPED
60} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000061
62typedef struct
63{
Chris Jonese60e2ae2021-01-20 17:51:47 +000064 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000065 const char *test;
66 const char *filename;
67 int line_no;
68 unsigned long step;
Gilles Peskineb4366492021-04-29 20:28:54 +020069 char line1[76];
70 char line2[76];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010071#if defined(MBEDTLS_TEST_MUTEX_USAGE)
72 const char *mutex_usage_error;
73#endif
Chris Jones9634bb12021-01-20 15:56:42 +000074}
Chris Jonese60e2ae2021-01-20 17:51:47 +000075mbedtls_test_info_t;
76extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000077
Ronald Crone9c09f12020-06-08 16:44:58 +020078int mbedtls_test_platform_setup( void );
79void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020080
Ronald Crona0c25392020-06-18 10:10:46 +020081/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000082 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000083 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000084 * This function can be called directly however it is usually
85 * called via macros such as TEST_ASSERT, TEST_EQUAL,
86 * PSA_ASSERT, etc...
87 *
88 * \note If the test case was already marked as failed, calling
89 * `mbedtls_test_fail( )` again will not overwrite any
90 * previous information about the failure.
91 *
92 * \param test Description of the failure or assertion that failed. This
93 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000094 * \param line_no Line number where the failure originated.
95 * \param filename Filename where the failure originated.
96 */
Chris Jones9634bb12021-01-20 15:56:42 +000097void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +000098
99/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000100 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000101 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000102 * This function can be called directly however it is usually
103 * called via the TEST_ASSUME macro.
104 *
105 * \param test Description of the assumption that caused the test case to
106 * be skipped. This MUST be a string literal.
107 * \param line_no Line number where the test case was skipped.
108 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000109 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000110void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000111
Chris Jones567e0ad2021-02-03 12:07:01 +0000112/**
113 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000114 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000115 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000116 * line number and file name if a test fails. Typically the "step
117 * number" is the index of a for loop but it can be whatever you
118 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000119 *
120 * \param step The step number to report.
121 */
122void mbedtls_test_set_step( unsigned long step );
123
Chris Jones567e0ad2021-02-03 12:07:01 +0000124/**
125 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000126 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000127void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000128
Ronald Crona0c25392020-06-18 10:10:46 +0200129/**
Gilles Peskineb4366492021-04-29 20:28:54 +0200130 * \brief Record the current test case as a failure if two integers
131 * have a different value.
132 *
133 * This function is usually called via the macro
134 * #TEST_EQUAL.
135 *
136 * \param test Description of the failure or assertion that failed. This
137 * MUST be a string literal. This normally has the form
138 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
139 * and EXPR2 has the value \p value2.
140 * \param line_no Line number where the failure originated.
141 * \param filename Filename where the failure originated.
142 * \param value1 The first value to compare.
143 * \param value2 The second value to compare.
144 *
145 * \return \c 1 if the values are equal, otherwise \c 0.
146 */
147int mbedtls_test_equal( const char *test, int line_no, const char* filename,
148 unsigned long long value1, unsigned long long value2 );
149
150/**
Gilles Peskine063700d2022-04-13 23:59:52 +0200151 * \brief Record the current test case as a failure based
152 * on comparing two unsigned integers.
153 *
154 * This function is usually called via the macro
155 * #TEST_LE_U.
156 *
157 * \param test Description of the failure or assertion that failed. This
158 * MUST be a string literal. This normally has the form
159 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
160 * and EXPR2 has the value \p value2.
161 * \param line_no Line number where the failure originated.
162 * \param filename Filename where the failure originated.
163 * \param value1 The first value to compare.
164 * \param value2 The second value to compare.
165 *
166 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
167 */
168int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
169 unsigned long long value1, unsigned long long value2 );
170
171/**
172 * \brief Record the current test case as a failure based
173 * on comparing two signed integers.
174 *
175 * This function is usually called via the macro
176 * #TEST_LE_S.
177 *
178 * \param test Description of the failure or assertion that failed. This
179 * MUST be a string literal. This normally has the form
180 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
181 * and EXPR2 has the value \p value2.
182 * \param line_no Line number where the failure originated.
183 * \param filename Filename where the failure originated.
184 * \param value1 The first value to compare.
185 * \param value2 The second value to compare.
186 *
187 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
188 */
189int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
190 long long value1, long long value2 );
191
192/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200193 * \brief This function decodes the hexadecimal representation of
194 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200195 *
196 * \note The output buffer can be the same as the input buffer. For
197 * any other overlapping of the input and output buffers, the
198 * behavior is undefined.
199 *
200 * \param obuf Output buffer.
201 * \param obufmax Size in number of bytes of \p obuf.
202 * \param ibuf Input buffer.
203 * \param len The number of unsigned char written in \p obuf. This must
204 * not be \c NULL.
205 *
206 * \return \c 0 on success.
207 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200208 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200209 */
210int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
211 const char *ibuf, size_t *len );
212
Ronald Cron72d628f2020-06-08 17:05:57 +0200213void mbedtls_test_hexify( unsigned char *obuf,
214 const unsigned char *ibuf,
215 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200216
217/**
218 * Allocate and zeroize a buffer.
219 *
220 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
221 *
222 * For convenience, dies if allocation fails.
223 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200224unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200225
226/**
227 * Allocate and fill a buffer from hex data.
228 *
229 * The buffer is sized exactly as needed. This allows to detect buffer
230 * overruns (including overreads) when running the test suite under valgrind.
231 *
232 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
233 *
234 * For convenience, dies if allocation fails.
235 */
Ronald Crona256c702020-06-10 10:53:11 +0200236unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200237
Ronald Cronde70b162020-06-10 11:03:08 +0200238int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
239 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200240
Ronald Crona1236142020-07-01 16:01:21 +0200241#if defined(MBEDTLS_CHECK_PARAMS)
242
243typedef struct
244{
245 const char *failure_condition;
246 const char *file;
247 int line;
248}
249mbedtls_test_param_failed_location_record_t;
250
251/**
252 * \brief Get the location record of the last call to
253 * mbedtls_test_param_failed().
254 *
255 * \note The call expectation is set up and active until the next call to
256 * mbedtls_test_param_failed_check_expected_call() or
257 * mbedtls_param_failed() that cancels it.
258 */
259void mbedtls_test_param_failed_get_location_record(
260 mbedtls_test_param_failed_location_record_t *location_record );
261
262/**
263 * \brief State that a call to mbedtls_param_failed() is expected.
264 *
265 * \note The call expectation is set up and active until the next call to
266 * mbedtls_test_param_failed_check_expected_call() or
267 * mbedtls_param_failed that cancel it.
268 */
269void mbedtls_test_param_failed_expect_call( void );
270
271/**
272 * \brief Check whether mbedtls_param_failed() has been called as expected.
273 *
274 * \note Check whether mbedtls_param_failed() has been called between the
275 * last call to mbedtls_test_param_failed_expect_call() and the call
276 * to this function.
277 *
278 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
279 * mbedtls_param_failed() has been called.
280 * \c -1 Otherwise.
281 */
282int mbedtls_test_param_failed_check_expected_call( void );
283
284/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200285 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200286 * state information used by mbedtls_param_failed() to do a long jump.
287 *
288 * \note If a call to mbedtls_param_failed() is not expected in the sense
289 * that there is no call to mbedtls_test_param_failed_expect_call()
290 * preceding it, then mbedtls_param_failed() will try to restore the
291 * execution to the state stored in the jmp_buf object whose address
292 * is returned by the present function.
293 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200294 * \note This function is intended to provide the parameter of the
295 * setjmp() function to set-up where mbedtls_param_failed() should
296 * long-jump if it has to. It is foreseen to be used as:
297 *
298 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
299 *
300 * \note The type of the returned value is not jmp_buf as jmp_buf is an
301 * an array type (C specification) and a function cannot return an
302 * array type.
303 *
304 * \note The type of the returned value is not jmp_buf* as then the return
305 * value couldn't be used by setjmp(), as its parameter's type is
306 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200307 *
308 * \return Address of the object of type jmp_buf holding the execution state
309 * information used by mbedtls_param_failed() to do a long jump.
310 */
311void* mbedtls_test_param_failed_get_state_buf( void );
312
313/**
314 * \brief Reset the execution state used by mbedtls_param_failed() to do a
315 * long jump.
316 *
317 * \note If a call to mbedtls_param_failed() is not expected in the sense
318 * that there is no call to mbedtls_test_param_failed_expect_call()
319 * preceding it, then mbedtls_param_failed() will try to restore the
320 * execution state that this function reset.
321 *
322 * \note It is recommended to reset the execution state when the state
323 * is not relevant anymore. That way an unexpected call to
324 * mbedtls_param_failed() will not trigger a long jump with
325 * undefined behavior but rather a long jump that will rather fault.
326 */
327void mbedtls_test_param_failed_reset_state( void );
328#endif /* MBEDTLS_CHECK_PARAMS */
329
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100330#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100331#include "test/fake_external_rng_for_test.h"
332#endif
333
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100334#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100335/** Permanently activate the mutex usage verification framework. See
336 * threading_helpers.c for information. */
337void mbedtls_test_mutex_usage_init( void );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100338
339/** Call this function after executing a test case to check for mutex usage
340 * errors. */
341void mbedtls_test_mutex_usage_check( void );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100342#endif /* MBEDTLS_TEST_MUTEX_USAGE */
343
Chris Jones96ae73b2021-01-08 17:04:59 +0000344#if defined(MBEDTLS_TEST_HOOKS)
345/**
Chris Jones3f613c12021-03-31 09:34:22 +0100346 * \brief Check that only a pure high-level error code is being combined with
347 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000348 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100349 *
350 * \note Both high-level and low-level error codes cannot be greater than
351 * zero however can be zero. If one error code is zero then the
352 * other error code is returned even if both codes are zero.
353 *
354 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000355 */
356void mbedtls_test_err_add_check( int high, int low,
357 const char *file, int line);
358#endif
359
Gilles Peskinedb479712021-06-11 14:13:53 +0200360#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100361/** Read an MPI from a hexadecimal string.
Gilles Peskinedb479712021-06-11 14:13:53 +0200362 *
Gilles Peskine53a72062022-11-09 21:08:44 +0100363 * Like mbedtls_mpi_read_string(), but with tighter guarantees around
364 * edge cases.
Gilles Peskinedb479712021-06-11 14:13:53 +0200365 *
Gilles Peskine53a72062022-11-09 21:08:44 +0100366 * - This function guarantees that if \p s begins with '-' then the sign
367 * bit of the result will be negative, even if the value is 0.
368 * When this function encounters such a "negative 0", it
Gilles Peskined64123a2022-11-11 15:59:51 +0100369 * increments #mbedtls_test_case_uses_negative_0.
Gilles Peskine53a72062022-11-09 21:08:44 +0100370 * - The size of the result is exactly the minimum number of limbs needed
371 * to fit the digits in the input. In particular, this function constructs
372 * a bignum with 0 limbs for an empty string, and a bignum with leading 0
373 * limbs if the string has sufficiently many leading 0 digits.
374 * This is important so that the "0 (null)" and "0 (1 limb)" and
375 * "leading zeros" test cases do what they claim.
Gilles Peskinedb479712021-06-11 14:13:53 +0200376 *
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 */
Werner Lewis24b60782022-07-07 15:08:17 +0100382int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s );
Gilles Peskine53a72062022-11-09 21:08:44 +0100383
384/** Nonzero if the current test case had an input parsed with
385 * mbedtls_test_read_mpi() that is a negative 0 (`"-"`, `"-0"`, `"-00"`, etc.,
386 * constructing a result with the sign bit set to -1 and the value being
387 * all-limbs-0, which is not a valid representation in #mbedtls_mpi but is
388 * tested for robustness).
389 */
390extern unsigned mbedtls_test_case_uses_negative_0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200391#endif /* MBEDTLS_BIGNUM_C */
392
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200393#endif /* TEST_HELPERS_H */