blob: 59260d9025e284cd90d79206aebbfc66cad5ca42 [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
Ronald Cronf40529d2020-06-09 16:27:37 +020034#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#include <stdio.h>
38#define mbedtls_fprintf fprintf
39#define mbedtls_snprintf snprintf
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#define mbedtls_exit exit
43#define mbedtls_time time
44#define mbedtls_time_t time_t
45#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
46#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
47#endif
48
49#include <stddef.h>
50#include <stdint.h>
51
Chris Jones9634bb12021-01-20 15:56:42 +000052typedef enum
53{
Chris Jonese60e2ae2021-01-20 17:51:47 +000054 MBEDTLS_TEST_RESULT_SUCCESS = 0,
55 MBEDTLS_TEST_RESULT_FAILED,
56 MBEDTLS_TEST_RESULT_SKIPPED
57} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000058
59typedef struct
60{
Chris Jonese60e2ae2021-01-20 17:51:47 +000061 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000062 const char *test;
63 const char *filename;
64 int line_no;
65 unsigned long step;
66}
Chris Jonese60e2ae2021-01-20 17:51:47 +000067mbedtls_test_info_t;
68extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000069
Ronald Crone9c09f12020-06-08 16:44:58 +020070int mbedtls_test_platform_setup( void );
71void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020072
Chris Jones567e0ad2021-02-03 12:07:01 +000073/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000074 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000075 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000076 * This function can be called directly however it is usually
77 * called via macros such as TEST_ASSERT, TEST_EQUAL,
78 * PSA_ASSERT, etc...
79 *
80 * \note If the test case was already marked as failed, calling
81 * `mbedtls_test_fail( )` again will not overwrite any
82 * previous information about the failure.
83 *
84 * \param test Description of the failure or assertion that failed. This
85 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000086 * \param line_no Line number where the failure originated.
87 * \param filename Filename where the failure originated.
88 */
Chris Jones9634bb12021-01-20 15:56:42 +000089void mbedtls_test_fail( const char *test, int line_no, const char* filename );
Chris Jones567e0ad2021-02-03 12:07:01 +000090
91/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000092 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000093 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000094 * This function can be called directly however it is usually
95 * called via the TEST_ASSUME macro.
96 *
97 * \param test Description of the assumption that caused the test case to
98 * be skipped. This MUST be a string literal.
99 * \param line_no Line number where the test case was skipped.
100 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +0000101 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000102void mbedtls_test_skip( const char *test, int line_no, const char* filename );
Chris Jones9634bb12021-01-20 15:56:42 +0000103
Chris Jones567e0ad2021-02-03 12:07:01 +0000104/**
105 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +0000106 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000107 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000108 * line number and file name if a test fails. Typically the "step
109 * number" is the index of a for loop but it can be whatever you
110 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000111 *
112 * \param step The step number to report.
113 */
114void mbedtls_test_set_step( unsigned long step );
115
Chris Jones567e0ad2021-02-03 12:07:01 +0000116/**
117 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000118 */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000119void mbedtls_test_info_reset( void );
Chris Jones9634bb12021-01-20 15:56:42 +0000120
Ronald Crona0c25392020-06-18 10:10:46 +0200121/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200122 * \brief This function decodes the hexadecimal representation of
123 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200124 *
125 * \note The output buffer can be the same as the input buffer. For
126 * any other overlapping of the input and output buffers, the
127 * behavior is undefined.
128 *
129 * \param obuf Output buffer.
130 * \param obufmax Size in number of bytes of \p obuf.
131 * \param ibuf Input buffer.
132 * \param len The number of unsigned char written in \p obuf. This must
133 * not be \c NULL.
134 *
135 * \return \c 0 on success.
136 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200137 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200138 */
139int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
140 const char *ibuf, size_t *len );
141
Ronald Cron72d628f2020-06-08 17:05:57 +0200142void mbedtls_test_hexify( unsigned char *obuf,
143 const unsigned char *ibuf,
144 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200145
146/**
147 * Allocate and zeroize a buffer.
148 *
149 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
150 *
151 * For convenience, dies if allocation fails.
152 */
Ronald Cron690f3eb2020-06-10 10:42:18 +0200153unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200154
155/**
156 * Allocate and fill a buffer from hex data.
157 *
158 * The buffer is sized exactly as needed. This allows to detect buffer
159 * overruns (including overreads) when running the test suite under valgrind.
160 *
161 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
162 *
163 * For convenience, dies if allocation fails.
164 */
Ronald Crona256c702020-06-10 10:53:11 +0200165unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200166
Ronald Cronde70b162020-06-10 11:03:08 +0200167int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
168 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200169
Ronald Crona1236142020-07-01 16:01:21 +0200170#if defined(MBEDTLS_CHECK_PARAMS)
171
172typedef struct
173{
174 const char *failure_condition;
175 const char *file;
176 int line;
177}
178mbedtls_test_param_failed_location_record_t;
179
180/**
181 * \brief Get the location record of the last call to
182 * mbedtls_test_param_failed().
183 *
184 * \note The call expectation is set up and active until the next call to
185 * mbedtls_test_param_failed_check_expected_call() or
186 * mbedtls_param_failed() that cancels it.
187 */
188void mbedtls_test_param_failed_get_location_record(
189 mbedtls_test_param_failed_location_record_t *location_record );
190
191/**
192 * \brief State that a call to mbedtls_param_failed() is expected.
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 cancel it.
197 */
198void mbedtls_test_param_failed_expect_call( void );
199
200/**
201 * \brief Check whether mbedtls_param_failed() has been called as expected.
202 *
203 * \note Check whether mbedtls_param_failed() has been called between the
204 * last call to mbedtls_test_param_failed_expect_call() and the call
205 * to this function.
206 *
207 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
208 * mbedtls_param_failed() has been called.
209 * \c -1 Otherwise.
210 */
211int mbedtls_test_param_failed_check_expected_call( void );
212
213/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200214 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200215 * state information used by mbedtls_param_failed() to do a long jump.
216 *
217 * \note If a call to mbedtls_param_failed() is not expected in the sense
218 * that there is no call to mbedtls_test_param_failed_expect_call()
219 * preceding it, then mbedtls_param_failed() will try to restore the
220 * execution to the state stored in the jmp_buf object whose address
221 * is returned by the present function.
222 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200223 * \note This function is intended to provide the parameter of the
224 * setjmp() function to set-up where mbedtls_param_failed() should
225 * long-jump if it has to. It is foreseen to be used as:
226 *
227 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
228 *
229 * \note The type of the returned value is not jmp_buf as jmp_buf is an
230 * an array type (C specification) and a function cannot return an
231 * array type.
232 *
233 * \note The type of the returned value is not jmp_buf* as then the return
234 * value couldn't be used by setjmp(), as its parameter's type is
235 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200236 *
237 * \return Address of the object of type jmp_buf holding the execution state
238 * information used by mbedtls_param_failed() to do a long jump.
239 */
240void* mbedtls_test_param_failed_get_state_buf( void );
241
242/**
243 * \brief Reset the execution state used by mbedtls_param_failed() to do a
244 * long jump.
245 *
246 * \note If a call to mbedtls_param_failed() is not expected in the sense
247 * that there is no call to mbedtls_test_param_failed_expect_call()
248 * preceding it, then mbedtls_param_failed() will try to restore the
249 * execution state that this function reset.
250 *
251 * \note It is recommended to reset the execution state when the state
252 * is not relevant anymore. That way an unexpected call to
253 * mbedtls_param_failed() will not trigger a long jump with
254 * undefined behavior but rather a long jump that will rather fault.
255 */
256void mbedtls_test_param_failed_reset_state( void );
257#endif /* MBEDTLS_CHECK_PARAMS */
258
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200259#endif /* TEST_HELPERS_H */