forked from aws/s2n-tls
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths2n_test.h
284 lines (247 loc) · 11.8 KB
/
s2n_test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#pragma once
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/crypto.h>
#include "error/s2n_errno.h"
#include "utils/s2n_safety.h"
#include "utils/s2n_result.h"
#include "tls/s2n_alerts.h"
#include "tls/s2n_tls13.h"
int test_count;
/* Macro definitions for calls that occur within BEGIN_TEST() and END_TEST() to preserve the SKIPPED test behavior
* by ignoring the test_count, keeping it as 0 to indicate that a test was skipped. */
#define EXPECT_TRUE_WITHOUT_COUNT( condition ) do { if ( !(condition) ) { FAIL_MSG( #condition " is not true "); } } while(0)
#define EXPECT_FALSE_WITHOUT_COUNT( condition ) EXPECT_TRUE_WITHOUT_COUNT( !(condition) )
#define EXPECT_NOT_EQUAL_WITHOUT_COUNT( p1, p2 ) EXPECT_FALSE_WITHOUT_COUNT( (p1) == (p2) )
#define EXPECT_SUCCESS_WITHOUT_COUNT( function_call ) EXPECT_NOT_EQUAL_WITHOUT_COUNT( (function_call) , -1 )
#define END_TEST_PRINT() \
if (isatty(fileno(stdout))) { \
if (test_count) { \
fprintf(stdout, "\033[32;1mPASSED\033[0m %10d tests\n", test_count ); \
} \
else { \
fprintf(stdout, "\033[33;1mSKIPPED\033[0m ALL tests\n" ); \
} \
} \
else { \
if (test_count) { \
fprintf(stdout, "PASSED %10d tests\n", test_count ); \
} \
else { \
fprintf(stdout, "SKIPPED ALL tests\n" ); \
} \
}
/* Macros similar to BEGIN_TEST() and END_TEST() but for tests where s2n should
* not initialise at the start of the test. Useful for tests that e.g spawn a
* number of independent childs at the start of a unit test and where you want
* each child to have its own independently initialised s2n.
*
* BEGIN_TEST() prints unit test information to stdout. But this often gets
* buffered by the kernel and will then be flushed in each child spawned. The
* result is a number of repeated messages being send to stdout and, in turn,
* appear in the logs. At the moment, we think this is better than risking not
* having any printing at all.
*/
#define BEGIN_TEST_NO_INIT() \
do { \
test_count = 0; \
fprintf(stdout, "Running %-50s ... ", __FILE__); \
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_in_unit_test_set(true)); \
S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE(); \
} while(0)
#define END_TEST_NO_INIT() \
do { \
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_in_unit_test_set(false)); \
END_TEST_PRINT() \
return 0; \
} while(0)
/* This is a very basic, but functional unit testing framework. All testing
* should happen in main() and start with a BEGIN_TEST() and end with an
* END_TEST().
*/
#define BEGIN_TEST() \
do { \
BEGIN_TEST_NO_INIT(); \
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_init()); \
} while(0)
#define END_TEST() \
do { \
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_cleanup()); \
END_TEST_NO_INIT(); \
} while(0)
#define FAIL() FAIL_MSG("")
#define FAIL_MSG( msg ) do { \
FAIL_MSG_PRINT(msg); \
exit(1); \
} while(0)
#define FAIL_MSG_PRINT( msg ) do { \
s2n_print_stacktrace(stderr); \
/* isatty will overwrite errno on failure */ \
int real_errno = errno; \
if (isatty(fileno(stderr))) { \
errno = real_errno; \
fprintf(stderr, "\033[31;1mFAILED test %d\033[0m\n%s (%s:%d)\nError Message: '%s'\n Debug String: '%s'\n System Error: %s (%d)\n", test_count, (msg), __FILE__, __LINE__, s2n_strerror(s2n_errno, "EN"), s2n_debug_str, strerror(errno), errno); \
} \
else { \
errno = real_errno; \
fprintf(stderr, "FAILED test %d\n%s (%s:%d)\nError Message: '%s'\n Debug String: '%s'\n System Error: %s (%d)\n", test_count, (msg), __FILE__, __LINE__, s2n_strerror(s2n_errno, "EN"), s2n_debug_str, strerror(errno), errno); \
} \
} while(0)
#define RESET_ERRNO() \
do { \
s2n_errno = 0; \
s2n_debug_str = NULL; \
errno = 0; \
} while(0);
#define EXPECT_TRUE( condition ) do { test_count++; if ( !(condition) ) { FAIL_MSG( #condition " is not true "); } } while(0)
#define EXPECT_FALSE( condition ) EXPECT_TRUE( !(condition) )
#define EXPECT_EQUAL( p1, p2 ) EXPECT_TRUE( (p1) == (p2) )
#define EXPECT_NOT_EQUAL( p1, p2 ) EXPECT_FALSE( (p1) == (p2) )
#define EXPECT_NULL( ptr ) EXPECT_EQUAL( ptr, NULL )
#define EXPECT_NOT_NULL( ptr ) EXPECT_NOT_EQUAL( ptr, NULL )
#define EXPECT_FAILURE( function_call ) \
do { \
EXPECT_EQUAL( (function_call) , -1 ); \
EXPECT_NOT_EQUAL(s2n_errno, 0); \
EXPECT_NOT_NULL(s2n_debug_str); \
RESET_ERRNO(); \
} while(0)
#define EXPECT_ERROR( function_call ) \
do { \
EXPECT_TRUE( s2n_result_is_error(function_call) ); \
EXPECT_NOT_EQUAL(s2n_errno, 0); \
EXPECT_NOT_NULL(s2n_debug_str); \
RESET_ERRNO(); \
} while(0)
#define EXPECT_FAILURE_WITH_ERRNO_NO_RESET( function_call, err ) \
do { \
EXPECT_EQUAL( (function_call), -1 ); \
EXPECT_EQUAL(s2n_errno, err); \
EXPECT_NOT_NULL(s2n_debug_str); \
} while(0)
#define EXPECT_FAILURE_WITH_ERRNO( function_call, err ) \
do { \
EXPECT_FAILURE_WITH_ERRNO_NO_RESET( function_call, err ); \
RESET_ERRNO(); \
} while(0)
#define EXPECT_FAILURE_WITH_ALERT( function_call, err, alert ) \
do { \
EXPECT_FAILURE_WITH_ERRNO_NO_RESET(function_call, err); \
uint8_t _alert_for_failure = 0; \
EXPECT_SUCCESS(s2n_error_get_alert(s2n_errno, &_alert_for_failure)); \
EXPECT_EQUAL(_alert_for_failure, (alert)); \
RESET_ERRNO(); \
} while(0)
/* for use with S2N_RESULT */
#define EXPECT_ERROR_WITH_ERRNO_NO_RESET( function_call, err ) \
do { \
EXPECT_TRUE( s2n_result_is_error(function_call) ); \
EXPECT_EQUAL(s2n_errno, err); \
EXPECT_NOT_NULL(s2n_debug_str); \
} while(0)
/* for use with S2N_RESULT */
#define EXPECT_ERROR_WITH_ERRNO( function_call, err ) \
do { \
EXPECT_ERROR_WITH_ERRNO_NO_RESET( function_call, err ); \
RESET_ERRNO(); \
} while(0)
#define EXPECT_NULL_WITH_ERRNO_NO_RESET( function_call, err ) \
do { \
EXPECT_NULL( (function_call) ); \
EXPECT_EQUAL(s2n_errno, err); \
EXPECT_NOT_NULL(s2n_debug_str); \
} while(0)
#define EXPECT_NULL_WITH_ERRNO( function_call, err ) \
do { \
EXPECT_NULL_WITH_ERRNO_NO_RESET( function_call, err ); \
RESET_ERRNO(); \
} while(0)
#define EXPECT_SUCCESS( function_call ) EXPECT_NOT_EQUAL( (function_call) , -1 )
/* for use with S2N_RESULT */
#define EXPECT_OK( function_call ) EXPECT_TRUE( s2n_result_is_ok(function_call) )
#define EXPECT_BYTEARRAY_EQUAL( p1, p2, l ) EXPECT_EQUAL( memcmp( (p1), (p2), (l) ), 0 )
#define EXPECT_BYTEARRAY_NOT_EQUAL( p1, p2, l ) EXPECT_NOT_EQUAL( memcmp( (p1), (p2), (l) ), 0 )
#define EXPECT_STRING_EQUAL( p1, p2 ) EXPECT_EQUAL( strcmp( (p1), (p2) ), 0 )
#define EXPECT_STRING_NOT_EQUAL( p1, p2 ) EXPECT_NOT_EQUAL( strcmp( (p1), (p2) ), 0 )
#ifdef S2N_TEST_IN_FIPS_MODE
#include <openssl/err.h>
#define S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE() \
do { \
if (FIPS_mode_set(1) == 0) { \
unsigned long fips_rc = ERR_get_error(); \
char ssl_error_buf[256]; \
fprintf(stderr, "s2nd failed to enter FIPS mode with RC: %lu; String: %s\n", fips_rc, ERR_error_string(fips_rc, ssl_error_buf)); \
return 1; \
} \
printf("s2n entered FIPS mode\n"); \
} while (0)
#else
#define S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE()
#endif
/* Ensures fuzz test input length is greater than or equal to the minimum needed for the test */
#define S2N_FUZZ_ENSURE_MIN_LEN( len , min ) do {if ( (len) < (min) ) return S2N_SUCCESS;} while (0)
#define EXPECT_MEMCPY_SUCCESS(d, s, n) \
do { \
__typeof(n) __tmp_n = (n); \
if (__tmp_n) { \
if (memmove((d), (s), (__tmp_n)) == NULL) { \
FAIL_MSG(#d "is NULL, memmove() failed"); \
} \
} \
} while (0)
#if defined(S2N_TEST_DEBUG)
#define TEST_DEBUG_PRINT(...) \
do { \
(void) fprintf(stderr, __VA_ARGS__); \
} while (0)
#else
#define TEST_DEBUG_PRINT(...)
#endif
/* Creates a fuzz target */
#define S2N_FUZZ_TARGET(fuzz_init, fuzz_entry, fuzz_cleanup) \
void s2n_test__fuzz_cleanup() \
{ \
if (fuzz_cleanup) { \
((void (*)()) fuzz_cleanup)(); \
} \
s2n_cleanup(); \
} \
int LLVMFuzzerInitialize(int *argc, char **argv[]) \
{ \
S2N_TEST_OPTIONALLY_ENABLE_FIPS_MODE(); \
EXPECT_SUCCESS_WITHOUT_COUNT(s2n_init()); \
EXPECT_SUCCESS_WITHOUT_COUNT(atexit(s2n_test__fuzz_cleanup)); \
if (!fuzz_init) { \
return S2N_SUCCESS; \
} \
int result = ((int (*)(int *argc, char **argv[])) fuzz_init)(argc, argv); \
if (result != S2N_SUCCESS) { \
FAIL_MSG_PRINT(#fuzz_init " did not return S2N_SUCCESS"); \
} \
return result; \
} \
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) \
{ \
int result = fuzz_entry(buf, len); \
if (result != S2N_SUCCESS) { \
FAIL_MSG_PRINT(#fuzz_entry " did not return S2N_SUCCESS"); \
} \
return result; \
}