blob: c0d2c007240f5d33c3db9b5a039c603e30fe5be0 [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
Gilles Peskineebc49e52021-06-11 14:13:53 +020062#if defined(MBEDTLS_BIGNUM_C)
63#include "mbedtls/bignum.h"
64#endif
65
Chris Jones9634bb12021-01-20 15:56:42 +000066typedef enum
67{
Chris Jonese60e2ae2021-01-20 17:51:47 +000068 MBEDTLS_TEST_RESULT_SUCCESS = 0,
69 MBEDTLS_TEST_RESULT_FAILED,
70 MBEDTLS_TEST_RESULT_SKIPPED
71} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000072
73typedef struct
74{
Chris Jonese60e2ae2021-01-20 17:51:47 +000075 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000076 const char *test;
77 const char *filename;
78 int line_no;
79 unsigned long step;
Gilles Peskine2a4c5982021-01-29 21:18:09 +010080#if defined(MBEDTLS_TEST_MUTEX_USAGE)
81 const char *mutex_usage_error;
82#endif
Chris Jones9634bb12021-01-20 15:56:42 +000083}
Chris Jonese60e2ae2021-01-20 17:51:47 +000084mbedtls_test_info_t;
85extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000086
Ronald Crone9c09f12020-06-08 16:44:58 +020087int mbedtls_test_platform_setup( void );
88void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020089
Ronald Crona0c25392020-06-18 10:10:46 +020090/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000091 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000092 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000093 * This function can be called directly however it is usually
94 * called via macros such as TEST_ASSERT, TEST_EQUAL,
95 * PSA_ASSERT, etc...
96 *
97 * \note If the test case was already marked as failed, calling
98 * `mbedtls_test_fail( )` again will not overwrite any
99 * previous information about the failure.
100 *
101 * \param test Description of the failure or assertion that failed. This
102 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +0000103 * \param line_no Line number where the failure originated.
104 * \param filename Filename where the failure originated.
105 */
Chris Jones9634bb12021-01-20 15:56:42 +0000106void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +0000107
108/**
Chris Jones39ddb0a2021-02-03 16:15:00 +0000109 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000110 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000111 * This function can be called directly however it is usually
112 * called via the TEST_ASSUME macro.
113 *
114 * \param test Description of the assumption that caused the test case to
115 * be skipped. This MUST be a string literal.
116 * \param line_no Line number where the test case was skipped.
117 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000118 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000119void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000120
Chris Jones567e0ad2021-02-03 12:07:01 +0000121/**
122 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000123 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000124 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000125 * line number and file name if a test fails. Typically the "step
126 * number" is the index of a for loop but it can be whatever you
127 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000128 *
129 * \param step The step number to report.
130 */
131void mbedtls_test_set_step( unsigned long step );
132
Chris Jones567e0ad2021-02-03 12:07:01 +0000133/**
134 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000135 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000136void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000137
Ronald Crona0c25392020-06-18 10:10:46 +0200138/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200139 * \brief This function decodes the hexadecimal representation of
140 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200141 *
142 * \note The output buffer can be the same as the input buffer. For
143 * any other overlapping of the input and output buffers, the
144 * behavior is undefined.
145 *
146 * \param obuf Output buffer.
147 * \param obufmax Size in number of bytes of \p obuf.
148 * \param ibuf Input buffer.
149 * \param len The number of unsigned char written in \p obuf. This must
150 * not be \c NULL.
151 *
152 * \return \c 0 on success.
153 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200154 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200155 */
156int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
157 const char *ibuf, size_t *len );
158
Ronald Cron72d628f2020-06-08 17:05:57 +0200159void mbedtls_test_hexify( unsigned char *obuf,
160 const unsigned char *ibuf,
161 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200162
163/**
164 * Allocate and zeroize a buffer.
165 *
166 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
167 *
168 * For convenience, dies if allocation fails.
169 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200170unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200171
172/**
173 * Allocate and fill a buffer from hex data.
174 *
175 * The buffer is sized exactly as needed. This allows to detect buffer
176 * overruns (including overreads) when running the test suite under valgrind.
177 *
178 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
179 *
180 * For convenience, dies if allocation fails.
181 */
Ronald Crona256c702020-06-10 10:53:11 +0200182unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200183
Ronald Cronde70b162020-06-10 11:03:08 +0200184int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
185 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200186
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100187#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100188#include "test/fake_external_rng_for_test.h"
189#endif
190
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100191#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100192/** Permanently activate the mutex usage verification framework. See
193 * threading_helpers.c for information. */
194void mbedtls_test_mutex_usage_init( void );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100195
196/** Call this function after executing a test case to check for mutex usage
197 * errors. */
198void mbedtls_test_mutex_usage_check( void );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100199#endif /* MBEDTLS_TEST_MUTEX_USAGE */
200
Chris Jones96ae73b2021-01-08 17:04:59 +0000201#if defined(MBEDTLS_TEST_HOOKS)
202/**
Chris Jones3f613c12021-03-31 09:34:22 +0100203 * \brief Check that only a pure high-level error code is being combined with
204 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000205 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100206 *
207 * \note Both high-level and low-level error codes cannot be greater than
208 * zero however can be zero. If one error code is zero then the
209 * other error code is returned even if both codes are zero.
210 *
211 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000212 */
213void mbedtls_test_err_add_check( int high, int low,
214 const char *file, int line);
215#endif
216
Gilles Peskineebc49e52021-06-11 14:13:53 +0200217#if defined(MBEDTLS_BIGNUM_C)
218/** Read an MPI from a string.
219 *
220 * Like mbedtls_mpi_read_string(), but size the resulting bignum based
221 * on the number of digits in the string. In particular, construct a
222 * bignum with 0 limbs for an empty string, and a bignum with leading 0
223 * limbs if the string has sufficiently many leading 0 digits.
224 *
225 * This is important so that the "0 (null)" and "0 (1 limb)" and
226 * "leading zeros" test cases do what they claim.
227 *
228 * \param[out] X The MPI object to populate. It must be initialized.
229 * \param radix The radix (2 to 16).
230 * \param[in] s The null-terminated string to read from.
231 *
232 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
233 */
234/* Since the library has exactly the desired behavior, this is trivial. */
235int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s );
236#endif /* MBEDTLS_BIGNUM_C */
237
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200238#endif /* TEST_HELPERS_H */