| 1 |
#ifndef AWS_TESTING_AWS_TEST_HARNESS_H |
| 2 |
#define AWS_TESTING_AWS_TEST_HARNESS_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <aws/common/common.h> |
| 9 |
#include <aws/common/error.h> |
| 10 |
#include <aws/common/logging.h> |
| 11 |
#include <aws/common/mutex.h> |
| 12 |
#include <aws/common/system_info.h> |
| 13 |
|
| 14 |
#include <stdarg.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#define AWS_OP_SKIP (-2) |
| 23 |
|
| 24 |
#ifndef AWS_UNSTABLE_TESTING_API |
| 25 |
# error The AWS Test Fixture is designed only for use by AWS owned libraries for the AWS C99 SDK. You are welcome to use it, \ |
| 26 |
but you should be aware we make no promises on the stability of this API. To enable use of the aws test fixtures, set \ |
| 27 |
the AWS_UNSTABLE_TESTING_API compiler flag |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifndef AWS_TESTING_REPORT_FD |
| 31 |
# define AWS_TESTING_REPORT_FD stderr |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef _MSC_VER |
| 35 |
# pragma warning(disable : 4221) |
| 36 |
# pragma warning(disable : 4204) |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(__clang__) |
| 40 |
# pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static int s_cunit_failure_message0( |
| 48 |
const char *prefix, |
| 49 |
const char *function, |
| 50 |
const char *file, |
| 51 |
int line, |
| 52 |
const char *format, |
| 53 |
...) { |
| 54 |
if (!format) { |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
fprintf(AWS_TESTING_REPORT_FD, "%s", prefix); |
| 59 |
|
| 60 |
va_list ap; |
| 61 |
va_start(ap, format); |
| 62 |
vfprintf(AWS_TESTING_REPORT_FD, format, ap); |
| 63 |
va_end(ap); |
| 64 |
|
| 65 |
if (function && file) { |
| 66 |
fprintf(AWS_TESTING_REPORT_FD, " [%s(): %s:%d]\n", function, file, line); |
| 67 |
} else { |
| 68 |
fprintf(AWS_TESTING_REPORT_FD, "\n"); |
| 69 |
} |
| 70 |
|
| 71 |
return 1; |
| 72 |
} |
| 73 |
|
| 74 |
#define FAIL_PREFIX "***FAILURE*** " |
| 75 |
#define CUNIT_FAILURE_MESSAGE(func, file, line, format, ...) \ |
| 76 |
s_cunit_failure_message0(FAIL_PREFIX, func, file, line, format, #__VA_ARGS__) |
| 77 |
|
| 78 |
#define SUCCESS (0) |
| 79 |
#define FAILURE (-1) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define SKIP (103) |
| 85 |
|
| 86 |
#define RETURN_SUCCESS(format, ...) \ |
| 87 |
do { \ |
| 88 |
printf(format, ##__VA_ARGS__); \ |
| 89 |
printf("\n"); \ |
| 90 |
return SUCCESS; \ |
| 91 |
} while (0) |
| 92 |
|
| 93 |
#define PRINT_FAIL_INTERNAL(...) CUNIT_FAILURE_MESSAGE(__func__, __FILE__, __LINE__, ##__VA_ARGS__, (const char *)NULL) |
| 94 |
|
| 95 |
#define PRINT_FAIL_INTERNAL0(...) \ |
| 96 |
s_cunit_failure_message0(FAIL_PREFIX, __func__, __FILE__, __LINE__, ##__VA_ARGS__, (const char *)NULL) |
| 97 |
|
| 98 |
#define PRINT_FAIL_WITHOUT_LOCATION(...) \ |
| 99 |
s_cunit_failure_message0(FAIL_PREFIX, NULL, NULL, __LINE__, ##__VA_ARGS__, (const char *)NULL) |
| 100 |
|
| 101 |
#define POSTFAIL_INTERNAL() \ |
| 102 |
do { \ |
| 103 |
return FAILURE; \ |
| 104 |
} while (0) |
| 105 |
|
| 106 |
#define FAIL(...) \ |
| 107 |
do { \ |
| 108 |
PRINT_FAIL_INTERNAL0(__VA_ARGS__); \ |
| 109 |
POSTFAIL_INTERNAL(); \ |
| 110 |
} while (0) |
| 111 |
|
| 112 |
#define ASSERT_TRUE(condition, ...) \ |
| 113 |
do { \ |
| 114 |
if (!(condition)) { \ |
| 115 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 116 |
PRINT_FAIL_INTERNAL0("Expected condition to be true: " #condition); \ |
| 117 |
} \ |
| 118 |
POSTFAIL_INTERNAL(); \ |
| 119 |
} \ |
| 120 |
} while (0) |
| 121 |
|
| 122 |
#define ASSERT_FALSE(condition, ...) \ |
| 123 |
do { \ |
| 124 |
if ((condition)) { \ |
| 125 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 126 |
PRINT_FAIL_INTERNAL0("Expected condition to be false: " #condition); \ |
| 127 |
} \ |
| 128 |
POSTFAIL_INTERNAL(); \ |
| 129 |
} \ |
| 130 |
} while (0) |
| 131 |
|
| 132 |
#define ASSERT_SUCCESS(condition, ...) \ |
| 133 |
do { \ |
| 134 |
int assert_rv = (condition); \ |
| 135 |
if (assert_rv != AWS_OP_SUCCESS) { \ |
| 136 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 137 |
PRINT_FAIL_INTERNAL0( \ |
| 138 |
"Expected success at %s; got return value %d with last error %d\n", \ |
| 139 |
#condition, \ |
| 140 |
assert_rv, \ |
| 141 |
aws_last_error()); \ |
| 142 |
} \ |
| 143 |
POSTFAIL_INTERNAL(); \ |
| 144 |
} \ |
| 145 |
} while (0) |
| 146 |
|
| 147 |
#define ASSERT_FAILS(condition, ...) \ |
| 148 |
do { \ |
| 149 |
int assert_rv = (condition); \ |
| 150 |
if (assert_rv != AWS_OP_ERR) { \ |
| 151 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 152 |
PRINT_FAIL_INTERNAL0( \ |
| 153 |
"Expected failure at %s; got return value %d with last error %d\n", \ |
| 154 |
#condition, \ |
| 155 |
assert_rv, \ |
| 156 |
aws_last_error()); \ |
| 157 |
} \ |
| 158 |
POSTFAIL_INTERNAL(); \ |
| 159 |
} \ |
| 160 |
} while (0) |
| 161 |
|
| 162 |
#define ASSERT_ERROR(error, condition, ...) \ |
| 163 |
do { \ |
| 164 |
int assert_rv = (condition); \ |
| 165 |
int assert_err = aws_last_error(); \ |
| 166 |
int assert_err_expect = (error); \ |
| 167 |
if (assert_rv != AWS_OP_ERR) { \ |
| 168 |
fprintf( \ |
| 169 |
AWS_TESTING_REPORT_FD, \ |
| 170 |
"%sExpected error but no error occurred; rv=%d, aws_last_error=%d (expected %d): ", \ |
| 171 |
FAIL_PREFIX, \ |
| 172 |
assert_rv, \ |
| 173 |
assert_err, \ |
| 174 |
assert_err_expect); \ |
| 175 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 176 |
PRINT_FAIL_INTERNAL0("%s", #condition); \ |
| 177 |
} \ |
| 178 |
POSTFAIL_INTERNAL(); \ |
| 179 |
} \ |
| 180 |
if (assert_err != assert_err_expect) { \ |
| 181 |
fprintf( \ |
| 182 |
AWS_TESTING_REPORT_FD, \ |
| 183 |
"%sIncorrect error code; aws_last_error=%d (expected %d): ", \ |
| 184 |
FAIL_PREFIX, \ |
| 185 |
assert_err, \ |
| 186 |
assert_err_expect); \ |
| 187 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 188 |
PRINT_FAIL_INTERNAL0("%s", #condition); \ |
| 189 |
} \ |
| 190 |
POSTFAIL_INTERNAL(); \ |
| 191 |
} \ |
| 192 |
} while (0) |
| 193 |
|
| 194 |
#define ASSERT_NULL(ptr, ...) \ |
| 195 |
do { \ |
| 196 |
\ |
| 197 |
void *assert_p = (void *)(uintptr_t)(ptr); \ |
| 198 |
if (assert_p) { \ |
| 199 |
fprintf(AWS_TESTING_REPORT_FD, "%sExpected null but got %p: ", FAIL_PREFIX, assert_p); \ |
| 200 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 201 |
PRINT_FAIL_INTERNAL0("%s", #ptr); \ |
| 202 |
} \ |
| 203 |
POSTFAIL_INTERNAL(); \ |
| 204 |
} \ |
| 205 |
} while (0) |
| 206 |
|
| 207 |
#define ASSERT_NOT_NULL(ptr, ...) \ |
| 208 |
do { \ |
| 209 |
\ |
| 210 |
void *assert_p = (void *)(uintptr_t)(ptr); \ |
| 211 |
if (!assert_p) { \ |
| 212 |
fprintf(AWS_TESTING_REPORT_FD, "%sExpected non-null but got null: ", FAIL_PREFIX); \ |
| 213 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 214 |
PRINT_FAIL_INTERNAL0("%s", #ptr); \ |
| 215 |
} \ |
| 216 |
POSTFAIL_INTERNAL(); \ |
| 217 |
} \ |
| 218 |
} while (0) |
| 219 |
|
| 220 |
#define ASSERT_TYP_EQUALS(type, formatarg, expected, got, ...) \ |
| 221 |
do { \ |
| 222 |
type assert_expected = (expected); \ |
| 223 |
type assert_actual = (got); \ |
| 224 |
if (assert_expected != assert_actual) { \ |
| 225 |
fprintf( \ |
| 226 |
AWS_TESTING_REPORT_FD, \ |
| 227 |
"%s" formatarg " != " formatarg ": ", \ |
| 228 |
FAIL_PREFIX, \ |
| 229 |
assert_expected, \ |
| 230 |
assert_actual); \ |
| 231 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 232 |
PRINT_FAIL_INTERNAL0("%s != %s", #expected, #got); \ |
| 233 |
} \ |
| 234 |
POSTFAIL_INTERNAL(); \ |
| 235 |
} \ |
| 236 |
} while (0) |
| 237 |
|
| 238 |
#ifdef _MSC_VER |
| 239 |
# define ASSERT_INT_EQUALS(expected, got, ...) ASSERT_TYP_EQUALS(intmax_t, "%lld", expected, got, __VA_ARGS__) |
| 240 |
# define ASSERT_UINT_EQUALS(expected, got, ...) ASSERT_TYP_EQUALS(uintmax_t, "%llu", expected, got, __VA_ARGS__) |
| 241 |
#else |
| 242 |
|
| 243 |
# define ASSERT_INT_EQUALS(expected, got, ...) ASSERT_TYP_EQUALS(intmax_t, "%jd", expected, got, __VA_ARGS__) |
| 244 |
|
| 245 |
# define ASSERT_UINT_EQUALS(expected, got, ...) ASSERT_TYP_EQUALS(uintmax_t, "%ju", expected, got, __VA_ARGS__) |
| 246 |
#endif |
| 247 |
|
| 248 |
#define ASSERT_PTR_EQUALS(expected, got, ...) \ |
| 249 |
do { \ |
| 250 |
void *assert_expected = (void *)(uintptr_t)(expected); \ |
| 251 |
void *assert_actual = (void *)(uintptr_t)(got); \ |
| 252 |
if (assert_expected != assert_actual) { \ |
| 253 |
fprintf(AWS_TESTING_REPORT_FD, "%s%p != %p: ", FAIL_PREFIX, assert_expected, assert_actual); \ |
| 254 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 255 |
PRINT_FAIL_INTERNAL0("%s != %s", #expected, #got); \ |
| 256 |
} \ |
| 257 |
POSTFAIL_INTERNAL(); \ |
| 258 |
} \ |
| 259 |
} while (0) |
| 260 |
|
| 261 |
|
| 262 |
#define ASSERT_BYTE_HEX_EQUALS(expected, got, ...) ASSERT_TYP_EQUALS(uint8_t, "%02X", expected, got, __VA_ARGS__) |
| 263 |
#define ASSERT_HEX_EQUALS(expected, got, ...) ASSERT_TYP_EQUALS(unsigned long long, "%llX", expected, got, __VA_ARGS__) |
| 264 |
|
| 265 |
#define ASSERT_STR_EQUALS(expected, got, ...) \ |
| 266 |
do { \ |
| 267 |
const char *assert_expected = (expected); \ |
| 268 |
const char *assert_got = (got); \ |
| 269 |
ASSERT_NOT_NULL(assert_expected); \ |
| 270 |
ASSERT_NOT_NULL(assert_got); \ |
| 271 |
if (strcmp(assert_expected, assert_got) != 0) { \ |
| 272 |
fprintf( \ |
| 273 |
AWS_TESTING_REPORT_FD, "%sExpected: \"%s\"; got: \"%s\": ", FAIL_PREFIX, assert_expected, assert_got); \ |
| 274 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 275 |
PRINT_FAIL_INTERNAL0("ASSERT_STR_EQUALS(%s, %s)", #expected, #got); \ |
| 276 |
} \ |
| 277 |
POSTFAIL_INTERNAL(); \ |
| 278 |
} \ |
| 279 |
} while (0) |
| 280 |
|
| 281 |
#define ASSERT_BIN_ARRAYS_EQUALS(expected, expected_size, got, got_size, ...) \ |
| 282 |
do { \ |
| 283 |
const uint8_t *assert_ex_p = (const uint8_t *)(expected); \ |
| 284 |
size_t assert_ex_s = (expected_size); \ |
| 285 |
const uint8_t *assert_got_p = (const uint8_t *)(got); \ |
| 286 |
size_t assert_got_s = (got_size); \ |
| 287 |
if (assert_ex_s == 0 && assert_got_s == 0) { \ |
| 288 |
break; \ |
| 289 |
} \ |
| 290 |
if (assert_ex_s != assert_got_s) { \ |
| 291 |
fprintf(AWS_TESTING_REPORT_FD, "%sSize mismatch: %zu != %zu: ", FAIL_PREFIX, assert_ex_s, assert_got_s); \ |
| 292 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 293 |
PRINT_FAIL_INTERNAL0( \ |
| 294 |
"ASSERT_BIN_ARRAYS_EQUALS(%s, %s, %s, %s)", #expected, #expected_size, #got, #got_size); \ |
| 295 |
} \ |
| 296 |
POSTFAIL_INTERNAL(); \ |
| 297 |
} \ |
| 298 |
if (memcmp(assert_ex_p, assert_got_p, assert_got_s) != 0) { \ |
| 299 |
if (assert_got_s <= 1024) { \ |
| 300 |
for (size_t assert_i = 0; assert_i < assert_ex_s; ++assert_i) { \ |
| 301 |
if (assert_ex_p[assert_i] != assert_got_p[assert_i]) { \ |
| 302 |
fprintf( \ |
| 303 |
AWS_TESTING_REPORT_FD, \ |
| 304 |
"%sMismatch at byte[%zu]: 0x%02X != 0x%02X: ", \ |
| 305 |
FAIL_PREFIX, \ |
| 306 |
assert_i, \ |
| 307 |
assert_ex_p[assert_i], \ |
| 308 |
assert_got_p[assert_i]); \ |
| 309 |
break; \ |
| 310 |
} \ |
| 311 |
} \ |
| 312 |
} else { \ |
| 313 |
fprintf(AWS_TESTING_REPORT_FD, "%sData mismatch: ", FAIL_PREFIX); \ |
| 314 |
} \ |
| 315 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 316 |
PRINT_FAIL_INTERNAL0( \ |
| 317 |
"ASSERT_BIN_ARRAYS_EQUALS(%s, %s, %s, %s)", #expected, #expected_size, #got, #got_size); \ |
| 318 |
} \ |
| 319 |
POSTFAIL_INTERNAL(); \ |
| 320 |
} \ |
| 321 |
} while (0) |
| 322 |
|
| 323 |
#define ASSERT_CURSOR_VALUE_CSTRING_EQUALS(cursor, cstring, ...) \ |
| 324 |
do { \ |
| 325 |
const uint8_t *assert_ex_p = (const uint8_t *)((cursor).ptr); \ |
| 326 |
size_t assert_ex_s = (cursor).len; \ |
| 327 |
const uint8_t *assert_got_p = (const uint8_t *)cstring; \ |
| 328 |
size_t assert_got_s = strlen(cstring); \ |
| 329 |
if (assert_ex_s == 0 && assert_got_s == 0) { \ |
| 330 |
break; \ |
| 331 |
} \ |
| 332 |
if (assert_ex_s != assert_got_s) { \ |
| 333 |
fprintf(AWS_TESTING_REPORT_FD, "%sSize mismatch: %zu != %zu: \n", FAIL_PREFIX, assert_ex_s, assert_got_s); \ |
| 334 |
fprintf( \ |
| 335 |
AWS_TESTING_REPORT_FD, \ |
| 336 |
"%sGot: \"" PRInSTR "\"; Expected: \"%s\" \n", \ |
| 337 |
FAIL_PREFIX, \ |
| 338 |
AWS_BYTE_CURSOR_PRI(cursor), \ |
| 339 |
cstring); \ |
| 340 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 341 |
PRINT_FAIL_INTERNAL0("ASSERT_CURSOR_VALUE_STRING_EQUALS(%s, %s)", #cursor, #cstring); \ |
| 342 |
} \ |
| 343 |
POSTFAIL_INTERNAL(); \ |
| 344 |
} \ |
| 345 |
if (memcmp(assert_ex_p, assert_got_p, assert_got_s) != 0) { \ |
| 346 |
fprintf( \ |
| 347 |
AWS_TESTING_REPORT_FD, \ |
| 348 |
"%sData mismatch; Got: \"" PRInSTR "\"; Expected: \"%s\" \n", \ |
| 349 |
FAIL_PREFIX, \ |
| 350 |
AWS_BYTE_CURSOR_PRI(cursor), \ |
| 351 |
cstring); \ |
| 352 |
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \ |
| 353 |
PRINT_FAIL_INTERNAL0("ASSERT_CURSOR_VALUE_STRING_EQUALS(%s, %s)", #cursor, #cstring); \ |
| 354 |
} \ |
| 355 |
POSTFAIL_INTERNAL(); \ |
| 356 |
} \ |
| 357 |
} while (0) |
| 358 |
|
| 359 |
#define ASSERT_CURSOR_VALUE_STRING_EQUALS(cursor, string, ...) \ |
| 360 |
ASSERT_CURSOR_VALUE_CSTRING_EQUALS(cursor, aws_string_c_str(string)); |
| 361 |
|
| 362 |
typedef int(aws_test_before_fn)(struct aws_allocator *allocator, void *ctx); |
| 363 |
typedef int(aws_test_run_fn)(struct aws_allocator *allocator, void *ctx); |
| 364 |
typedef int(aws_test_after_fn)(struct aws_allocator *allocator, int setup_result, void *ctx); |
| 365 |
|
| 366 |
struct aws_test_harness { |
| 367 |
aws_test_before_fn *on_before; |
| 368 |
aws_test_run_fn *run; |
| 369 |
aws_test_after_fn *on_after; |
| 370 |
void *ctx; |
| 371 |
const char *test_name; |
| 372 |
int suppress_memcheck; |
| 373 |
}; |
| 374 |
|
| 375 |
#if defined(_WIN32) |
| 376 |
# include <windows.h> |
| 377 |
static LONG WINAPI s_test_print_stack_trace(struct _EXCEPTION_POINTERS *exception_pointers) { |
| 378 |
# if !defined(AWS_HEADER_CHECKER) |
| 379 |
aws_backtrace_print(stderr, exception_pointers); |
| 380 |
# endif |
| 381 |
return EXCEPTION_EXECUTE_HANDLER; |
| 382 |
} |
| 383 |
#elif defined(AWS_HAVE_EXECINFO) |
| 384 |
# include <signal.h> |
| 385 |
static void s_print_stack_trace(int sig, siginfo_t *sig_info, void *user_data) { |
| 386 |
(void)sig; |
| 387 |
(void)sig_info; |
| 388 |
(void)user_data; |
| 389 |
# if !defined(AWS_HEADER_CHECKER) |
| 390 |
aws_backtrace_print(stderr, sig_info); |
| 391 |
# endif |
| 392 |
exit(-1); |
| 393 |
} |
| 394 |
#endif |
| 395 |
|
| 396 |
static inline int s_aws_run_test_case(struct aws_test_harness *harness) { |
| 397 |
AWS_ASSERT(harness->run); |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#ifdef _MSC_VER |
| 403 |
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); |
| 404 |
#endif |
| 405 |
|
| 406 |
#if defined(_WIN32) |
| 407 |
SetUnhandledExceptionFilter(s_test_print_stack_trace); |
| 408 |
|
| 409 |
char cwd[512]; |
| 410 |
DWORD len = GetModuleFileNameA(NULL, cwd, sizeof(cwd)); |
| 411 |
DWORD idx = len - 1; |
| 412 |
while (idx && cwd[idx] != '\\') { |
| 413 |
idx--; |
| 414 |
} |
| 415 |
cwd[idx] = 0; |
| 416 |
SetCurrentDirectory(cwd); |
| 417 |
#elif defined(AWS_HAVE_EXECINFO) |
| 418 |
struct sigaction sa; |
| 419 |
memset(&sa, 0, sizeof(struct sigaction)); |
| 420 |
sigemptyset(&sa.sa_mask); |
| 421 |
|
| 422 |
sa.sa_flags = SA_NODEFER; |
| 423 |
sa.sa_sigaction = s_print_stack_trace; |
| 424 |
|
| 425 |
sigaction(SIGSEGV, &sa, NULL); |
| 426 |
#endif |
| 427 |
|
| 428 |
|
| 429 |
struct aws_allocator *allocator = NULL; |
| 430 |
if (harness->suppress_memcheck) { |
| 431 |
allocator = aws_default_allocator(); |
| 432 |
} else { |
| 433 |
allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_STACKS, 8); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
struct aws_logger err_logger; |
| 438 |
struct aws_logger_standard_options options; |
| 439 |
options.file = AWS_TESTING_REPORT_FD; |
| 440 |
options.level = AWS_LL_TRACE; |
| 441 |
options.filename = NULL; |
| 442 |
aws_logger_init_standard(&err_logger, aws_default_allocator(), &options); |
| 443 |
aws_logger_set(&err_logger); |
| 444 |
|
| 445 |
int test_res = AWS_OP_ERR; |
| 446 |
int setup_res = AWS_OP_SUCCESS; |
| 447 |
if (harness->on_before) { |
| 448 |
setup_res = harness->on_before(allocator, harness->ctx); |
| 449 |
} |
| 450 |
|
| 451 |
if (!setup_res) { |
| 452 |
test_res = harness->run(allocator, harness->ctx); |
| 453 |
} |
| 454 |
|
| 455 |
if (harness->on_after) { |
| 456 |
test_res |= harness->on_after(allocator, setup_res, harness->ctx); |
| 457 |
} |
| 458 |
|
| 459 |
if (test_res != AWS_OP_SUCCESS) { |
| 460 |
goto fail; |
| 461 |
} |
| 462 |
|
| 463 |
if (!harness->suppress_memcheck) { |
| 464 |
|
| 465 |
|
| 466 |
aws_logger_set(&err_logger); |
| 467 |
|
| 468 |
const size_t leaked_allocations = aws_mem_tracer_count(allocator); |
| 469 |
const size_t leaked_bytes = aws_mem_tracer_bytes(allocator); |
| 470 |
if (leaked_bytes) { |
| 471 |
aws_mem_tracer_dump(allocator); |
| 472 |
PRINT_FAIL_WITHOUT_LOCATION( |
| 473 |
"Test leaked memory: %zu bytes %zu allocations", leaked_bytes, leaked_allocations); |
| 474 |
goto fail; |
| 475 |
} |
| 476 |
|
| 477 |
aws_mem_tracer_destroy(allocator); |
| 478 |
} |
| 479 |
|
| 480 |
aws_logger_set(NULL); |
| 481 |
aws_logger_clean_up(&err_logger); |
| 482 |
|
| 483 |
RETURN_SUCCESS("%s [ \033[32mOK\033[0m ]", harness->test_name); |
| 484 |
|
| 485 |
fail: |
| 486 |
if (test_res == AWS_OP_SKIP) { |
| 487 |
fprintf(AWS_TESTING_REPORT_FD, "%s [ \033[32mSKIP\033[0m ]\n", harness->test_name); |
| 488 |
} else { |
| 489 |
PRINT_FAIL_WITHOUT_LOCATION("%s [ \033[31mFAILED\033[0m ]", harness->test_name); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
fflush(AWS_TESTING_REPORT_FD); |
| 495 |
fflush(stdout); |
| 496 |
fflush(stderr); |
| 497 |
_Exit(test_res == AWS_OP_SKIP ? SKIP : FAILURE); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
#ifdef _WIN32 |
| 503 |
|
| 504 |
# include <windows.h> |
| 505 |
|
| 506 |
# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 507 |
# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 |
| 508 |
# endif |
| 509 |
|
| 510 |
static inline int enable_vt_mode(void) { |
| 511 |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 512 |
if (hOut == INVALID_HANDLE_VALUE) { |
| 513 |
return AWS_OP_ERR; |
| 514 |
} |
| 515 |
|
| 516 |
DWORD dwMode = 0; |
| 517 |
if (!GetConsoleMode(hOut, &dwMode)) { |
| 518 |
return AWS_OP_ERR; |
| 519 |
} |
| 520 |
|
| 521 |
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; |
| 522 |
if (!SetConsoleMode(hOut, dwMode)) { |
| 523 |
return AWS_OP_ERR; |
| 524 |
} |
| 525 |
return AWS_OP_SUCCESS; |
| 526 |
} |
| 527 |
|
| 528 |
#else |
| 529 |
|
| 530 |
static inline int enable_vt_mode(void) { |
| 531 |
return AWS_OP_ERR; |
| 532 |
} |
| 533 |
|
| 534 |
#endif |
| 535 |
|
| 536 |
#define AWS_TEST_CASE_SUPRESSION(name, fn, s) \ |
| 537 |
static int fn(struct aws_allocator *allocator, void *ctx); \ |
| 538 |
static struct aws_test_harness name##_test = { \ |
| 539 |
NULL, \ |
| 540 |
fn, \ |
| 541 |
NULL, \ |
| 542 |
NULL, \ |
| 543 |
#name, \ |
| 544 |
s, \ |
| 545 |
}; \ |
| 546 |
int name(int argc, char *argv[]) { \ |
| 547 |
(void)argc, (void)argv; \ |
| 548 |
return s_aws_run_test_case(&name##_test); \ |
| 549 |
} |
| 550 |
|
| 551 |
#define AWS_TEST_CASE_FIXTURE_SUPPRESSION(name, b, fn, af, c, s) \ |
| 552 |
static int b(struct aws_allocator *allocator, void *ctx); \ |
| 553 |
static int fn(struct aws_allocator *allocator, void *ctx); \ |
| 554 |
static int af(struct aws_allocator *allocator, int setup_result, void *ctx); \ |
| 555 |
static struct aws_test_harness name##_test = { \ |
| 556 |
b, \ |
| 557 |
fn, \ |
| 558 |
af, \ |
| 559 |
c, \ |
| 560 |
#name, \ |
| 561 |
s, \ |
| 562 |
}; \ |
| 563 |
int name(int argc, char *argv[]) { \ |
| 564 |
(void)argc; \ |
| 565 |
(void)argv; \ |
| 566 |
return s_aws_run_test_case(&name##_test); \ |
| 567 |
} |
| 568 |
|
| 569 |
#define AWS_TEST_CASE(name, fn) AWS_TEST_CASE_SUPRESSION(name, fn, 0) |
| 570 |
#define AWS_TEST_CASE_FIXTURE(name, b, fn, af, c) AWS_TEST_CASE_FIXTURE_SUPPRESSION(name, b, fn, af, c, 0) |
| 571 |
|
| 572 |
#endif |