blob: 9bfd7e047893e1ddb18a01067a85f527d8a22358 [file] [log] [blame]
Bence Szépkúti86974652020-06-15 11:59:37 +02001/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02002 * Copyright The Mbed TLS Contributors
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020016 */
17
18#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020019#include <test/macros.h>
20#include <string.h>
21
Ronald Crona1236142020-07-01 16:01:21 +020022#if defined(MBEDTLS_CHECK_PARAMS)
23#include <setjmp.h>
24#endif
25
26/*----------------------------------------------------------------------------*/
27/* Static global variables */
28
29#if defined(MBEDTLS_CHECK_PARAMS)
30typedef struct
31{
32 uint8_t expected_call;
33 uint8_t expected_call_happened;
34
35 jmp_buf state;
36
37 mbedtls_test_param_failed_location_record_t location_record;
38}
39param_failed_ctx_t;
40static param_failed_ctx_t param_failed_ctx;
41#endif
42
Ronald Cronf40529d2020-06-09 16:27:37 +020043#if defined(MBEDTLS_PLATFORM_C)
44static mbedtls_platform_context platform_ctx;
45#endif
46
Chris Jonese60e2ae2021-01-20 17:51:47 +000047mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000048
Ronald Crona1236142020-07-01 16:01:21 +020049/*----------------------------------------------------------------------------*/
50/* Helper Functions */
51
Ronald Crone9c09f12020-06-08 16:44:58 +020052int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020053{
54 int ret = 0;
55#if defined(MBEDTLS_PLATFORM_C)
56 ret = mbedtls_platform_setup( &platform_ctx );
57#endif /* MBEDTLS_PLATFORM_C */
58 return( ret );
59}
60
Ronald Crone9c09f12020-06-08 16:44:58 +020061void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020062{
63#if defined(MBEDTLS_PLATFORM_C)
64 mbedtls_platform_teardown( &platform_ctx );
65#endif /* MBEDTLS_PLATFORM_C */
66}
67
Ronald Crona0c25392020-06-18 10:10:46 +020068static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020069{
Ronald Crona0c25392020-06-18 10:10:46 +020070 if( ( c >= '0' ) && ( c <= '9' ) )
71 *uc = c - '0';
72 else if( ( c >= 'a' ) && ( c <= 'f' ) )
73 *uc = c - 'a' + 10;
74 else if( ( c >= 'A' ) && ( c <= 'F' ) )
75 *uc = c - 'A' + 10;
76 else
77 return( -1 );
78
79 return( 0 );
80}
81
Chris Jones9634bb12021-01-20 15:56:42 +000082void mbedtls_test_fail( const char *test, int line_no, const char* filename )
83{
Chris Jonese60e2ae2021-01-20 17:51:47 +000084 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
Chris Jones9634bb12021-01-20 15:56:42 +000085 {
86 /* We've already recorded the test as having failed. Don't
87 * overwrite any previous information about the failure. */
88 return;
89 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000090 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
91 mbedtls_test_info.test = test;
92 mbedtls_test_info.line_no = line_no;
93 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000094}
95
96void mbedtls_test_set_step( unsigned long step )
97{
Chris Jonese60e2ae2021-01-20 17:51:47 +000098 mbedtls_test_info.step = step;
Chris Jones9634bb12021-01-20 15:56:42 +000099}
100
101void mbedtls_test_skip( const char *test, int line_no, const char* filename )
102{
Chris Jonese60e2ae2021-01-20 17:51:47 +0000103 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
104 mbedtls_test_info.test = test;
105 mbedtls_test_info.line_no = line_no;
106 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000107}
108
Ronald Crona0c25392020-06-18 10:10:46 +0200109int mbedtls_test_unhexify( unsigned char *obuf,
110 size_t obufmax,
111 const char *ibuf,
112 size_t *len )
113{
114 unsigned char uc, uc2;
115
116 *len = strlen( ibuf );
117
118 /* Must be even number of bytes. */
119 if ( ( *len ) & 1 )
120 return( -1 );
121 *len /= 2;
122
123 if ( (*len) > obufmax )
124 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200125
126 while( *ibuf != 0 )
127 {
Ronald Crona0c25392020-06-18 10:10:46 +0200128 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
129 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200130
Ronald Crona0c25392020-06-18 10:10:46 +0200131 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
132 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200133
Ronald Crona0c25392020-06-18 10:10:46 +0200134 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200135 }
136
Ronald Crona0c25392020-06-18 10:10:46 +0200137 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200138}
139
Ronald Cron72d628f2020-06-08 17:05:57 +0200140void mbedtls_test_hexify( unsigned char *obuf,
141 const unsigned char *ibuf,
142 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200143{
144 unsigned char l, h;
145
146 while( len != 0 )
147 {
148 h = *ibuf / 16;
149 l = *ibuf % 16;
150
151 if( h < 10 )
152 *obuf++ = '0' + h;
153 else
154 *obuf++ = 'a' + h - 10;
155
156 if( l < 10 )
157 *obuf++ = '0' + l;
158 else
159 *obuf++ = 'a' + l - 10;
160
161 ++ibuf;
162 len--;
163 }
164}
165
Ronald Cron690f3eb2020-06-10 10:42:18 +0200166unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200167{
168 void *p;
169 size_t actual_len = ( len != 0 ) ? len : 1;
170
171 p = mbedtls_calloc( 1, actual_len );
172 TEST_HELPER_ASSERT( p != NULL );
173
174 memset( p, 0x00, actual_len );
175
176 return( p );
177}
178
Ronald Crona256c702020-06-10 10:53:11 +0200179unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200180{
181 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200182 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200183
184 *olen = strlen( ibuf ) / 2;
185
186 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200187 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200188
189 obuf = mbedtls_calloc( 1, *olen );
190 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200191 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200192
193 return( obuf );
194}
195
Ronald Cronde70b162020-06-10 11:03:08 +0200196int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
197 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200198{
199 int ret = 0;
200 uint32_t i = 0;
201
202 if( a_len != b_len )
203 return( -1 );
204
205 for( i = 0; i < a_len; i++ )
206 {
207 if( a[i] != b[i] )
208 {
209 ret = -1;
210 break;
211 }
212 }
213 return ret;
214}
Ronald Crona1236142020-07-01 16:01:21 +0200215
216#if defined(MBEDTLS_CHECK_PARAMS)
217void mbedtls_test_param_failed_get_location_record(
218 mbedtls_test_param_failed_location_record_t *location_record )
219{
220 *location_record = param_failed_ctx.location_record;
221}
222
223void mbedtls_test_param_failed_expect_call( void )
224{
225 param_failed_ctx.expected_call_happened = 0;
226 param_failed_ctx.expected_call = 1;
227}
228
229int mbedtls_test_param_failed_check_expected_call( void )
230{
231 param_failed_ctx.expected_call = 0;
232
233 if( param_failed_ctx.expected_call_happened != 0 )
234 return( 0 );
235
236 return( -1 );
237}
238
239void* mbedtls_test_param_failed_get_state_buf( void )
240{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200241 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200242}
243
244void mbedtls_test_param_failed_reset_state( void )
245{
246 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
247}
248
249void mbedtls_param_failed( const char *failure_condition,
250 const char *file,
251 int line )
252{
253 /* Record the location of the failure */
254 param_failed_ctx.location_record.failure_condition = failure_condition;
255 param_failed_ctx.location_record.file = file;
256 param_failed_ctx.location_record.line = line;
257
258 /* If we are testing the callback function... */
259 if( param_failed_ctx.expected_call != 0 )
260 {
261 param_failed_ctx.expected_call = 0;
262 param_failed_ctx.expected_call_happened = 1;
263 }
264 else
265 {
266 /* ...else try a long jump. If the execution state has not been set-up
267 * or reset then the long jump buffer is all zero's and the call will
268 * with high probability fault, emphasizing there is something to look
269 * at.
270 */
271
272 longjmp( param_failed_ctx.state, 1 );
273 }
274}
275#endif /* MBEDTLS_CHECK_PARAMS */