Debian Codegraph @

crt/s2n/tests/s2n_test.h

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