blob: 79a63fbd3a0bb748d0f6a9e25658a4b7f487ab85 [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/*
9 * Copyright (C) 2020, ARM Limited, All Rights Reserved
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.
23 *
24 * This file is part of mbed TLS (https://tls.mbed.org)
25 */
26
27#ifndef TEST_HELPERS_H
28#define TEST_HELPERS_H
29
30#if !defined(MBEDTLS_CONFIG_FILE)
31#include "mbedtls/config.h"
32#else
33#include MBEDTLS_CONFIG_FILE
34#endif
35
Ronald Cronf40529d2020-06-09 16:27:37 +020036#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdio.h>
40#define mbedtls_fprintf fprintf
41#define mbedtls_snprintf snprintf
42#define mbedtls_calloc calloc
43#define mbedtls_free free
44#define mbedtls_exit exit
45#define mbedtls_time time
46#define mbedtls_time_t time_t
47#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
48#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
49#endif
50
51#include <stddef.h>
52#include <stdint.h>
53
Ronald Crone9c09f12020-06-08 16:44:58 +020054int mbedtls_test_platform_setup( void );
55void mbedtls_test_platform_teardown( void );
Ronald Cronf40529d2020-06-09 16:27:37 +020056
Ronald Crona0c25392020-06-18 10:10:46 +020057/**
Ronald Cronab500cb2020-07-01 17:09:10 +020058 * \brief This function decodes the hexadecimal representation of
59 * data.
Ronald Crona0c25392020-06-18 10:10:46 +020060 *
61 * \note The output buffer can be the same as the input buffer. For
62 * any other overlapping of the input and output buffers, the
63 * behavior is undefined.
64 *
65 * \param obuf Output buffer.
66 * \param obufmax Size in number of bytes of \p obuf.
67 * \param ibuf Input buffer.
68 * \param len The number of unsigned char written in \p obuf. This must
69 * not be \c NULL.
70 *
71 * \return \c 0 on success.
72 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +020073 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +020074 */
75int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
76 const char *ibuf, size_t *len );
77
Ronald Cron72d628f2020-06-08 17:05:57 +020078void mbedtls_test_hexify( unsigned char *obuf,
79 const unsigned char *ibuf,
80 int len );
Ronald Cronf40529d2020-06-09 16:27:37 +020081
82/**
83 * Allocate and zeroize a buffer.
84 *
85 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
86 *
87 * For convenience, dies if allocation fails.
88 */
Ronald Cron690f3eb2020-06-10 10:42:18 +020089unsigned char *mbedtls_test_zero_alloc( size_t len );
Ronald Cronf40529d2020-06-09 16:27:37 +020090
91/**
92 * Allocate and fill a buffer from hex data.
93 *
94 * The buffer is sized exactly as needed. This allows to detect buffer
95 * overruns (including overreads) when running the test suite under valgrind.
96 *
97 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
98 *
99 * For convenience, dies if allocation fails.
100 */
Ronald Crona256c702020-06-10 10:53:11 +0200101unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
Ronald Cronf40529d2020-06-09 16:27:37 +0200102
Ronald Cronde70b162020-06-10 11:03:08 +0200103int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
104 uint32_t a_len, uint32_t b_len );
Ronald Cronf40529d2020-06-09 16:27:37 +0200105
Ronald Crona1236142020-07-01 16:01:21 +0200106#if defined(MBEDTLS_CHECK_PARAMS)
107
108typedef struct
109{
110 const char *failure_condition;
111 const char *file;
112 int line;
113}
114mbedtls_test_param_failed_location_record_t;
115
116/**
117 * \brief Get the location record of the last call to
118 * mbedtls_test_param_failed().
119 *
120 * \note The call expectation is set up and active until the next call to
121 * mbedtls_test_param_failed_check_expected_call() or
122 * mbedtls_param_failed() that cancels it.
123 */
124void mbedtls_test_param_failed_get_location_record(
125 mbedtls_test_param_failed_location_record_t *location_record );
126
127/**
128 * \brief State that a call to mbedtls_param_failed() is expected.
129 *
130 * \note The call expectation is set up and active until the next call to
131 * mbedtls_test_param_failed_check_expected_call() or
132 * mbedtls_param_failed that cancel it.
133 */
134void mbedtls_test_param_failed_expect_call( void );
135
136/**
137 * \brief Check whether mbedtls_param_failed() has been called as expected.
138 *
139 * \note Check whether mbedtls_param_failed() has been called between the
140 * last call to mbedtls_test_param_failed_expect_call() and the call
141 * to this function.
142 *
143 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
144 * mbedtls_param_failed() has been called.
145 * \c -1 Otherwise.
146 */
147int mbedtls_test_param_failed_check_expected_call( void );
148
149/**
150 * \brief Get a pointer to the object of type jmp_buf holding the execution
151 * state information used by mbedtls_param_failed() to do a long jump.
152 *
153 * \note If a call to mbedtls_param_failed() is not expected in the sense
154 * that there is no call to mbedtls_test_param_failed_expect_call()
155 * preceding it, then mbedtls_param_failed() will try to restore the
156 * execution to the state stored in the jmp_buf object whose address
157 * is returned by the present function.
158 *
159 * \note The returned pointer is of type void* as its type is opaque,
160 * implementation dependent (jmp_buf is an array type not the type of
161 * one element of an array).
162 *
163 * \return Address of the object of type jmp_buf holding the execution state
164 * information used by mbedtls_param_failed() to do a long jump.
165 */
166void* mbedtls_test_param_failed_get_state_buf( void );
167
168/**
169 * \brief Reset the execution state used by mbedtls_param_failed() to do a
170 * long jump.
171 *
172 * \note If a call to mbedtls_param_failed() is not expected in the sense
173 * that there is no call to mbedtls_test_param_failed_expect_call()
174 * preceding it, then mbedtls_param_failed() will try to restore the
175 * execution state that this function reset.
176 *
177 * \note It is recommended to reset the execution state when the state
178 * is not relevant anymore. That way an unexpected call to
179 * mbedtls_param_failed() will not trigger a long jump with
180 * undefined behavior but rather a long jump that will rather fault.
181 */
182void mbedtls_test_param_failed_reset_state( void );
183#endif /* MBEDTLS_CHECK_PARAMS */
184
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200185#endif /* TEST_HELPERS_H */