]> git.saurik.com Git - apple/dyld.git/blob - testing/lib/test_support.cpp
dyld-851.27.tar.gz
[apple/dyld.git] / testing / lib / test_support.cpp
1 #include <dlfcn.h>
2 #include <Block.h>
3 #include <spawn.h>
4 #include <stdio.h>
5 #include <assert.h>
6 #include <signal.h>
7 #include <stdarg.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <os/lock.h>
14 #include <sys/attr.h>
15 #include <sys/wait.h>
16 #include <mach/mach.h>
17 #include <mach-o/dyld.h>
18 #include <mach/mach_vm.h>
19 #include <sys/fsgetpath.h>
20 #include <mach-o/getsect.h>
21 #include <mach/vm_region.h>
22 #include <dispatch/private.h>
23 #include <dispatch/dispatch.h>
24
25
26 #include <atomic>
27 #include <utility>
28 #include <algorithm>
29
30 extern "C" {
31 #include "execserverServer.h"
32
33 union catch_mach_exc_request_reply {
34 union __RequestUnion__catch_mach_exc_subsystem request;
35 union __ReplyUnion__catch_mach_exc_subsystem reply;
36 };
37 };
38
39 #include "test_support.h"
40
41 extern const int NXArgc;
42 extern const char** NXArgv;
43 extern const char** environ;
44 extern char* __progname;
45 #if __x86_64__
46 static const cpu_type_t currentArch = CPU_TYPE_X86_64;
47 #elif __i386__
48 static const cpu_type_t currentArch = CPU_TYPE_I386;
49 #elif __arm64__
50 static const cpu_type_t currentArch = CPU_TYPE_ARM64;
51 #elif __arm__
52 static const cpu_type_t currentArch = CPU_TYPE_ARM;
53 #endif
54
55 namespace {
56 struct ScopedLock {
57 ScopedLock() : _lock(OS_UNFAIR_LOCK_INIT) {}
58 template<typename F>
59 void withLock(F f) {
60 os_unfair_lock_lock(&_lock);
61 f();
62 os_unfair_lock_unlock(&_lock);
63 }
64 private:
65 os_unfair_lock _lock;
66 };
67
68 template <typename T, int QUANT=4, int INIT=1>
69 class GrowableArray
70 {
71 public:
72
73 T& operator[](size_t idx) { assert(idx < _usedCount); return _elements[idx]; }
74 const T& operator[](size_t idx) const { assert(idx < _usedCount); return _elements[idx]; }
75 T& back() { assert(_usedCount > 0); return _elements[_usedCount-1]; }
76 uintptr_t count() const { return _usedCount; }
77 uintptr_t maxCount() const { return _allocCount; }
78 bool empty() const { return (_usedCount == 0); }
79 uintptr_t index(const T& element) { return &element - _elements; }
80 void push_back(const T& t) { verifySpace(1); _elements[_usedCount++] = t; }
81 void pop_back() { assert(_usedCount > 0); _usedCount--; }
82 T* begin() { return &_elements[0]; }
83 T* end() { return &_elements[_usedCount]; }
84 const T* begin() const { return &_elements[0]; }
85 const T* end() const { return &_elements[_usedCount]; }
86 bool contains(const T& targ) const { for (const T& a : *this) { if ( a == targ ) return true; } return false; }
87 void erase(T& targ);
88
89 protected:
90 void growTo(uintptr_t n);
91 void verifySpace(uintptr_t n) { if (this->_usedCount+n > this->_allocCount) growTo(this->_usedCount + n); }
92
93 private:
94 T* _elements = _initialAlloc;
95 uintptr_t _allocCount = INIT;
96 uintptr_t _usedCount = 0;
97 T _initialAlloc[INIT] = { };
98 };
99
100
101 template <typename T, int QUANT, int INIT>
102 inline void GrowableArray<T,QUANT,INIT>::growTo(uintptr_t n)
103 {
104 uintptr_t newCount = (n + QUANT - 1) & (-QUANT);
105 T* newArray = (T*)::malloc(sizeof(T)*newCount);
106 T* oldArray = this->_elements;
107 if ( this->_usedCount != 0 )
108 ::memcpy(newArray, oldArray, sizeof(T)*this->_usedCount);
109 this->_elements = newArray;
110 this->_allocCount = newCount;
111 if ( oldArray != this->_initialAlloc )
112 ::free(oldArray);
113 }
114
115 template <typename T, int QUANT, int INIT>
116 inline void GrowableArray<T,QUANT,INIT>::erase(T& targ)
117 {
118 intptr_t index = &targ - _elements;
119 assert(index >= 0);
120 assert(index < (intptr_t)_usedCount);
121 intptr_t moveCount = _usedCount-index-1;
122 if ( moveCount > 0 )
123 ::memcpy(&_elements[index], &_elements[index+1], moveCount*sizeof(T));
124 _usedCount -= 1;
125 }
126
127 struct TestState {
128 TestState();
129 static TestState* getState();
130 void _PASSV(const char* file, unsigned line, const char* format, va_list args) __attribute__ ((noreturn));
131 void _FAILV(const char* file, unsigned line, const char* format, va_list args) __attribute__ ((noreturn));
132 void _LOGV(const char* file, unsigned line, const char* format, va_list args);
133 GrowableArray<std::pair<mach_port_t, _dyld_test_crash_handler_t>>& getCrashHandlers();
134 private:
135 enum OutputStyle {
136 None,
137 BATS,
138 Console,
139 XCTest
140 };
141 void emitBegin();
142 void runLeaks();
143 void dumpLogs();
144 void getLogsString(char** buffer);
145 static uint8_t hexCharToUInt(const char hexByte, uint8_t* value);
146 static uint64_t hexToUInt64(const char* startHexByte, const char** endHexByte);
147
148 ScopedLock _IOlock;
149 GrowableArray<const char *> logs;
150 const char *testName;
151 bool logImmediate;
152 bool logOnSuccess;
153 bool checkForLeaks;
154 OutputStyle output;
155 GrowableArray<std::pair<mach_port_t, _dyld_test_crash_handler_t>> crashHandlers;
156 };
157
158 // Okay, this is tricky. We need something with roughly he semantics of a weak def, but without using weak defs as their presence
159 // may impact certain tests. Instead we do the following:
160 //
161 // 1. Embed a stuct containing a lock and a pointer to our global state object in each binary
162 // 2. Once per binary we walk the entire image list looking for the first entry that also has state data
163 // 3. If it has state we lock its initializaion lock, and if it is not initialized we initialize it
164 // 4. We then copy the initalized pointer into our own state, and unlock the initializer lock
165 //
166 // This should work because the image list forms a stable ordering. The one loose end is if an executable is running where logging
167 // is only used in dylibs that are all being dlopned() and dlclosed. Since many dylibs cannot be dlclosed that should be a non-issue
168 // in practice.
169 };
170
171 __attribute__((section("__DATA,__dyld_test")))
172 static std::atomic<TestState*> sState;
173
174 kern_return_t
175 catch_mach_exception_raise(mach_port_t exception_port,
176 mach_port_t thread,
177 mach_port_t task,
178 exception_type_t exception,
179 mach_exception_data_t code,
180 mach_msg_type_number_t codeCnt)
181 {
182 _dyld_test_crash_handler_t crashHandler = NULL;
183 for (const auto& handler : TestState::getState()->getCrashHandlers()) {
184 if (handler.first == exception_port) {
185 crashHandler = handler.second;
186 }
187 }
188 if (crashHandler) {
189 if (exception == EXC_CORPSE_NOTIFY) {
190 crashHandler(task);
191 } else {
192 return KERN_FAILURE;
193 }
194 }
195 return KERN_SUCCESS;
196 }
197
198 kern_return_t
199 catch_mach_exception_raise_state(mach_port_t exception_port,
200 exception_type_t exception,
201 const mach_exception_data_t code,
202 mach_msg_type_number_t codeCnt,
203 int * flavor,
204 const thread_state_t old_state,
205 mach_msg_type_number_t old_stateCnt,
206 thread_state_t new_state,
207 mach_msg_type_number_t * new_stateCnt)
208 {
209 return KERN_NOT_SUPPORTED;
210 }
211
212 kern_return_t
213 catch_mach_exception_raise_state_identity(mach_port_t exception_port,
214 mach_port_t thread,
215 mach_port_t task,
216 exception_type_t exception,
217 mach_exception_data_t code,
218 mach_msg_type_number_t codeCnt,
219 int * flavor,
220 thread_state_t old_state,
221 mach_msg_type_number_t old_stateCnt,
222 thread_state_t new_state,
223 mach_msg_type_number_t * new_stateCnt)
224 {
225 return KERN_NOT_SUPPORTED;
226 }
227
228 _process::_process() : executablePath(nullptr), args(nullptr), env(nullptr), stdoutHandler(nullptr), stderrHandler(nullptr),
229 crashHandler(nullptr), exitHandler(nullptr), pid(0), arch(currentArch), suspended(false) {}
230 _process::~_process() {
231 if (stdoutHandler) { Block_release(stdoutHandler);}
232 if (stderrHandler) { Block_release(stderrHandler);}
233 if (crashHandler) { Block_release(crashHandler);}
234 if (exitHandler) { Block_release(exitHandler);}
235 }
236
237 void _process::set_executable_path(const char* EP) { executablePath = EP; }
238 void _process::set_args(const char** A) { args = A; }
239 void _process::set_env(const char** E) { env = E; }
240 void _process::set_stdout_handler(_dyld_test_reader_t SOH) { stdoutHandler = Block_copy(SOH); };
241 void _process::set_stderr_handler(_dyld_test_reader_t SEH) { stderrHandler = Block_copy(SEH); }
242 void _process::set_exit_handler(_dyld_test_exit_handler_t EH) { exitHandler = Block_copy(EH); }
243 void _process::set_crash_handler(_dyld_test_crash_handler_t CH) { crashHandler = Block_copy(CH); }
244 void _process::set_launch_suspended(bool S) { suspended = S; }
245 void _process::set_launch_arch(cpu_type_t A) { arch = A; }
246
247 pid_t _process::launch() {
248 dispatch_queue_t queue = dispatch_queue_create("com.apple.dyld.test.launch", NULL);
249 posix_spawn_file_actions_t fileActions = NULL;
250 posix_spawnattr_t attr = NULL;
251 dispatch_source_t stdoutSource = NULL;
252 dispatch_source_t stderrSource = NULL;
253 int stdoutPipe[2];
254 int stderrPipe[2];
255
256 if (posix_spawn_file_actions_init(&fileActions) != 0) {
257 FAIL("Setting up spawn filea actions");
258 }
259 if (posix_spawnattr_init(&attr) != 0) { FAIL("Setting up spawn attr"); }
260 if (posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED) != 0) {
261 FAIL("Setting up spawn attr: POSIX_SPAWN_START_SUSPENDED");
262 }
263
264 if (pipe(stdoutPipe) != 0) { FAIL("Setting up pipe"); }
265 if (posix_spawn_file_actions_addclose(&fileActions, stdoutPipe[0]) != 0) { FAIL("Setting up pipe"); }
266 if (posix_spawn_file_actions_adddup2(&fileActions, stdoutPipe[1], STDOUT_FILENO) != 0) { FAIL("Setting up pipe"); }
267 if (posix_spawn_file_actions_addclose(&fileActions, stdoutPipe[1]) != 0) { FAIL("Setting up pipe"); }
268 fcntl((int)stdoutPipe[0], F_SETFL, O_NONBLOCK);
269 stdoutSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)stdoutPipe[0], 0, queue);
270 dispatch_source_set_event_handler(stdoutSource, ^{
271 int fd = (int)dispatch_source_get_handle(stdoutSource);
272 if (stdoutHandler) {
273 stdoutHandler(fd);
274 } else {
275 char buffer[16384];
276 ssize_t size = 0;
277 do {
278 size = read(fd, &buffer[0], 16384);
279 } while (size > 0);
280 }
281 });
282 dispatch_source_set_cancel_handler(stdoutSource, ^{
283 dispatch_release(stdoutSource);
284 });
285 dispatch_resume(stdoutSource);
286
287 if (pipe(stderrPipe) != 0) { FAIL("Setting up pipe"); }
288 if (posix_spawn_file_actions_addclose(&fileActions, stderrPipe[0]) != 0) { FAIL("Setting up pipe"); }
289 if (posix_spawn_file_actions_adddup2(&fileActions, stderrPipe[1], STDERR_FILENO) != 0) { FAIL("Setting up pipe"); }
290 if (posix_spawn_file_actions_addclose(&fileActions, stderrPipe[1]) != 0) { FAIL("Setting up pipe"); }
291 fcntl((int)stderrPipe[0], F_SETFL, O_NONBLOCK);
292 stderrSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)stderrPipe[0], 0, queue);
293 dispatch_source_set_event_handler(stderrSource, ^{
294 int fd = (int)dispatch_source_get_handle(stderrSource);
295 if (stderrHandler) {
296 stderrHandler(fd);
297 } else {
298 char buffer[16384];
299 ssize_t size = 0;
300 do {
301 size = read(fd, &buffer[0], 16384);
302 } while (size > 0);
303 }
304 });
305 dispatch_source_set_cancel_handler(stderrSource, ^{
306 dispatch_release(stderrSource);
307 });
308 dispatch_resume(stderrSource);
309
310 if (crashHandler) {
311 auto& crashHandlers = TestState::getState()->getCrashHandlers();
312 mach_port_t exceptionPort = MACH_PORT_NULL;
313 mach_port_options_t options = { .flags = MPO_CONTEXT_AS_GUARD | MPO_STRICT | MPO_INSERT_SEND_RIGHT, .mpl = { 1 }};
314 if ( mach_port_construct(mach_task_self(), &options, (mach_port_context_t)exceptionPort, &exceptionPort) != KERN_SUCCESS ) {
315 FAIL("Could not construct port");
316 }
317 if (posix_spawnattr_setexceptionports_np(&attr, EXC_MASK_CRASH | EXC_MASK_CORPSE_NOTIFY, exceptionPort,
318 EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, 0) != 0) {
319 FAIL("posix_spawnattr_setexceptionports_np failed");
320 }
321 crashHandlers.push_back(std::make_pair(exceptionPort, crashHandler));
322 dispatch_source_t crashSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, exceptionPort, 0, queue);
323 dispatch_source_set_event_handler(crashSource, ^{
324 dispatch_mig_server(crashSource, sizeof(union catch_mach_exc_request_reply), ::mach_exc_server);
325 });
326 dispatch_source_set_cancel_handler(crashSource, ^{
327 mach_port_destruct(mach_task_self(), exceptionPort, 0, (mach_port_context_t)exceptionPort);
328 });
329 dispatch_resume(crashSource);
330 }
331
332 pid_t pid;
333 uint32_t argc = 0;
334 if (args) {
335 for (argc = 0; args[argc] != NULL; ++argc) {}
336 }
337 ++argc;
338 const char *argv[argc+1];
339 argv[0] = executablePath;
340 for (uint32_t i = 1; i < argc; ++i) {
341 argv[i] = args[i-1];
342 }
343 argv[argc] = NULL;
344
345 int result = posix_spawn(&pid, executablePath, &fileActions, &attr, (char **)argv, (char **)env);
346 if ( result != 0 ) {
347 FAIL("posix_spawn(%s) failed, err=%d", executablePath, result);
348 }
349 dispatch_source_t exitSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, (pid_t)pid,
350 DISPATCH_PROC_EXIT, queue);
351 dispatch_source_set_event_handler(exitSource, ^{
352 if (exitHandler) {
353 exitHandler((pid_t)dispatch_source_get_handle(exitSource));
354 }
355 dispatch_source_cancel(exitSource);
356 if (stdoutSource) {
357 dispatch_source_cancel(stdoutSource);
358 }
359 if (stderrSource) {
360 dispatch_source_cancel(stderrSource);
361 }
362 dispatch_source_cancel(exitSource);
363 });
364 dispatch_resume(exitSource);
365
366 if (stdoutHandler) {
367 close(stdoutPipe[1]);
368 }
369 if (stderrHandler) {
370 close(stderrPipe[1]);
371 }
372 if (fileActions) {
373 posix_spawn_file_actions_destroy(&fileActions);
374 }
375 posix_spawnattr_destroy(&attr);
376 if (!suspended) {
377 kill(pid, SIGCONT);
378 }
379 dispatch_release(queue);
380 return pid;
381 }
382
383 void *_process::operator new(size_t size) {
384 return malloc(size);
385 }
386
387 void _process::operator delete(void *ptr) {
388 free(ptr);
389 }
390
391 // MARK: Private implementation details
392
393 template<typename F>
394 static
395 void forEachEnvVar(const char* envp[], F&& f) {
396 for (uint32_t i = 0; envp[i] != nullptr; ++i) {
397 const char* envBegin = envp[i];
398 const char* envEnd = strchr(envp[i], '=');
399 if (!envEnd) { continue; }
400 size_t envSize = (envEnd-envBegin)+1;
401 const char* valBegin = envEnd+1;
402 const char* valEnd = strchr(envp[i], '\0');
403 if (!valEnd) { continue; }
404 size_t valSize = (valEnd-valBegin)+1;
405 char env[envSize];
406 char val[valSize];
407 strlcpy(&env[0], envBegin, envSize);
408 strlcpy(&val[0], valBegin, valSize);
409 f(&env[0], &val[0]);
410 }
411 }
412
413 uint8_t TestState::hexCharToUInt(const char hexByte, uint8_t* value) {
414 if (hexByte >= '0' && hexByte <= '9') {
415 *value = hexByte - '0';
416 return true;
417 } else if (hexByte >= 'A' && hexByte <= 'F') {
418 *value = hexByte - 'A' + 10;
419 return true;
420 } else if (hexByte >= 'a' && hexByte <= 'f') {
421 *value = hexByte - 'a' + 10;
422 return true;
423 }
424
425 return false;
426 }
427
428 uint64_t TestState::hexToUInt64(const char* startHexByte, const char** endHexByte) {
429 const char* scratch;
430 if (endHexByte == NULL) {
431 endHexByte = &scratch;
432 }
433 if (startHexByte == NULL)
434 return 0;
435 uint64_t retval = 0;
436 if (startHexByte[0] == '0' && startHexByte[1] == 'x') {
437 startHexByte +=2;
438 }
439 *endHexByte = startHexByte + 16;
440
441 //FIXME overrun?
442 for (uint32_t i = 0; i < 16; ++i) {
443 uint8_t value;
444 if (!hexCharToUInt(startHexByte[i], &value)) {
445 *endHexByte = &startHexByte[i];
446 break;
447 }
448 retval = (retval << 4) + value;
449 }
450 return retval;
451 }
452
453 void TestState::getLogsString(char** buffer)
454 {
455 char *logBuf = NULL;
456 if ( logs.count() ) {
457 size_t idx = 0;
458 size_t bufSize = 0;
459 for (const auto& log : logs) {
460 size_t logSize = strlen(log);
461 bufSize += logSize + 2; // \t and \n
462 logBuf = (char*)realloc(logBuf, bufSize);
463 strncpy(logBuf+idx, "\t", 1);
464 idx++;
465 strncpy(logBuf+idx, log, logSize);
466 idx += logSize;
467 strncpy(logBuf+idx, "\n", 1);
468 idx++;
469 }
470 logBuf = (char*)realloc(logBuf, bufSize + 1);
471 logBuf[bufSize] = '\0';
472 *buffer = logBuf;
473 }
474 }
475
476 TestState::TestState() : testName(__progname), logImmediate(false), logOnSuccess(false), checkForLeaks(false), output(Console) {
477 forEachEnvVar(environ, [this](const char* env, const char* val) {
478 if (strcmp(env, "TEST_LOG_IMMEDIATE") == 0) {
479 logImmediate = true;
480 }
481 if (strcmp(env, "TEST_LOG_ON_SUCCESS") == 0) {
482 logOnSuccess = true;
483 }
484 if (strcmp(env, "MallocStackLogging") == 0) {
485 checkForLeaks = true;
486 }
487 if (strcmp(env, "TEST_OUTPUT") == 0) {
488 if (strcmp(val, "BATS") == 0) {
489 output = BATS;
490 } else if (strcmp(val, "XCTest") == 0) {
491 output = XCTest;
492 }
493 }
494 });
495 }
496
497 void TestState::emitBegin() {
498 if (output == BATS) {
499 printf("[BEGIN]");
500 if (checkForLeaks) {
501 printf(" MallocStackLogging=1 MallocDebugReport=none");
502 }
503 forEachEnvVar(environ, [this](const char* env, const char* val) {
504 if ((strncmp(env, "DYLD_", 5) == 0) || (strncmp(env, "TEST_", 5) == 0)) {
505 printf(" %s=%s", env, val);
506 }
507 });
508 printf(" %s", testName);
509 for (uint32_t i = 1; i < NXArgc; ++i) {
510 printf(" %s", NXArgv[i]);
511 }
512 printf("\n");
513 }
514 }
515
516 GrowableArray<std::pair<mach_port_t, _dyld_test_crash_handler_t>>& TestState::getCrashHandlers() {
517 return crashHandlers;
518 }
519
520 TestState* TestState::getState() {
521 if (!sState) {
522 uint32_t imageCnt = _dyld_image_count();
523 for (uint32_t i = 0; i < imageCnt; ++i) {
524 #if __LP64__
525 const struct mach_header_64* mh = (const struct mach_header_64*)_dyld_get_image_header(i);
526 #else
527 const struct mach_header* mh = _dyld_get_image_header(i);
528 #endif
529 if (mh->filetype != MH_EXECUTE) {
530 continue;
531 }
532 size_t size = 0;
533 auto state = (std::atomic<TestState*>*)getsectiondata(mh, "__DATA", "__dyld_test", &size);
534 // fprintf(stderr, "__dyld_test -> 0x%llx\n", state);
535 if (!state) {
536 fprintf(stderr, "Could not find test state in main executable TestState\n");
537 exit(0);
538 }
539 if (*state == nullptr) {
540 void *temp = malloc(sizeof(TestState));
541 auto newState = new (temp) TestState();
542 TestState* expected = nullptr;
543 if(!state->compare_exchange_strong(expected, newState)) {
544 newState->~TestState();
545 free(temp);
546 } else {
547 newState->emitBegin();
548 }
549 }
550 sState.store(*state);
551 }
552 }
553 assert(sState != nullptr);
554 return sState;
555 }
556
557 __attribute__((noreturn))
558 void TestState::runLeaks(void) {
559 auto testState = TestState::getState();
560 pid_t pid = getpid();
561 char pidString[32];
562 sprintf(&pidString[0], "%d", pid);
563 if (getuid() != 0) {
564 printf("Insufficient priviledges, skipping Leak check: %s\n", testState->testName);
565 exit(0);
566 }
567 const char *args[] = { pidString, NULL };
568 // We do this instead of using a dispatch_semaphore to prevent priority inversions
569 __block dispatch_data_t leaksOutput = NULL;
570 _process process;
571 process.set_executable_path("/usr/bin/leaks");
572 process.set_args(args);
573 process.set_stdout_handler(^(int fd) {
574 ssize_t size = 0;
575 do {
576 char buffer[16384];
577 size = read(fd, &buffer[0], 16384);
578 if (size == -1) { break; }
579 dispatch_data_t data = dispatch_data_create(&buffer[0], size, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
580 if (!leaksOutput) {
581 leaksOutput = data;
582 } else {
583 leaksOutput = dispatch_data_create_concat(leaksOutput, data);
584 }
585 } while (size > 0);
586 });
587 process.set_exit_handler(^(pid_t pid) {
588 int status = 0;
589 (void)waitpid(pid, &status, 0);
590
591 int exitStatus = WEXITSTATUS(status);
592 if (exitStatus == 0) {
593 PASS("No leaks");
594 } else {
595 if (leaksOutput) {
596 const void * buffer;
597 size_t size;
598 __unused dispatch_data_t map = dispatch_data_create_map(leaksOutput, &buffer, &size);
599 FAIL("Found Leaks:\n\n%s", buffer);
600 }
601 }
602 });
603
604 testState->checkForLeaks = false;
605 (void)process.launch();
606 exit(0);
607 }
608
609 void TestState::_PASSV(const char* file, unsigned line, const char* format, va_list args) {
610 if (output == None) {
611 exit(0);
612 }
613 if (checkForLeaks) {
614 runLeaks();
615 } else {
616 _IOlock.withLock([this,&format,&args,&file,&line](){
617 if (output == Console) {
618 printf("[\033[0;32mPASS\033[0m] %s: ", testName);
619 vprintf(format, args);
620 printf("\n");
621 if (logOnSuccess && logs.count()) {
622 printf("[\033[0;33mLOG\033[0m]\n");
623 for (const auto& log : logs) {
624 printf("\t%s\n", log);
625 }
626 }
627 } else if (output == BATS) {
628 printf("[PASS] %s: ", testName);
629 vprintf(format, args);
630 printf("\n");
631 if (logOnSuccess && logs.count()) {
632 printf("[LOG]\n");
633 for (const auto& log : logs) {
634 printf("\t%s\n", log);
635 }
636 }
637 } else if (output == XCTest) {
638 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
639 printf("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
640 printf("<plist version=\"1.0\">");
641 printf("<dict>");
642 printf("<key>PASS</key><true />");
643 if (logOnSuccess) {
644 char *logBuffer = NULL;
645 getLogsString(&logBuffer);
646 if ( logBuffer != NULL ) {
647 printf("<key>LOGS</key><string>%s</string>", logBuffer);
648 free(logBuffer);
649 }
650 }
651 printf("</dict>");
652 printf("</plist>");
653 }
654 exit(0);
655 });
656 }
657 __builtin_unreachable();
658 }
659
660 void _PASS(const char* file, unsigned line, const char* format, ...) {
661 va_list args;
662 va_start (args, format);
663 TestState::getState()->_PASSV(file, line, format, args);
664 va_end (args);
665 }
666
667 void TestState::_FAILV(const char* file, unsigned line, const char* format, va_list args) {
668 if (output == None) {
669 exit(0);
670 }
671 _IOlock.withLock([this,&format,&args,&file,&line](){
672 if (output == Console) {
673 printf("[\033[0;31mFAIL\033[0m] %s: ", testName);
674 vprintf(format, args);
675 printf("\n");
676 printf("[\033[0;33mLOG\033[0m]\n");
677 if (logs.count()) {
678 for (const auto& log : logs) {
679 printf("\t%s\n", log);
680 }
681 }
682 } else if (output == BATS) {
683 printf("[FAIL] %s: ", testName);
684 vprintf(format, args);
685 printf("\n");
686 if (logs.count()) {
687 printf("[LOG]\n");
688 for (const auto& log : logs) {
689 printf("\t%s\n", log);
690 }
691 }
692 } else if (output == XCTest) {
693 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
694 printf("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
695 printf("<plist version=\"1.0\">");
696 printf("<dict>");
697 printf("<key>PASS</key><false />");
698 printf("<key>FILE</key><string>%s</string>", file);
699 printf("<key>LINE</key><integer>%u</integer>", line);
700 char *buffer;
701 vasprintf(&buffer, format, args);
702 printf("<key>INFO</key><string>%s</string>", buffer);
703 free(buffer);
704 char *logBuffer = NULL;
705 getLogsString(&logBuffer);
706 if ( logBuffer != NULL ) {
707 printf("<key>LOGS</key><string>%s</string>", logBuffer);
708 free(logBuffer);
709 }
710 printf("</dict>");
711 printf("</plist>");
712 }
713 exit(0);
714 });
715 __builtin_unreachable();
716 }
717
718 void _FAIL(const char* file, unsigned line, const char* format, ...) {
719 va_list args;
720 va_start (args, format);
721 TestState::getState()->_FAILV(file, line, format, args);
722 va_end (args);
723 }
724
725 void TestState::_LOGV(const char* file, unsigned line, const char* format, va_list args) {
726 _IOlock.withLock([this,&format,&args](){
727 if (logImmediate) {
728 vprintf(format, args);
729 printf("\n");
730 } else {
731 char *str;
732 vasprintf(&str, format, args);
733 logs.push_back(str);
734 }
735 });
736 }
737
738 void _LOG(const char* file, unsigned line, const char* format, ...) {
739 va_list args;
740 va_start (args, format);
741 TestState::getState()->_LOGV(file, line, format, args);
742 va_end (args);
743 }
744
745 void _TIMEOUT(const char* file, unsigned line, uint64_t seconds) {
746 _LOG(file, line, "Registering %llu second test timeout", seconds);
747 dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, DISPATCH_TARGET_QUEUE_DEFAULT);
748 dispatch_time_t milestone = dispatch_time(DISPATCH_WALLTIME_NOW, seconds * NSEC_PER_SEC);
749 dispatch_source_set_timer(source, milestone, 0, 0);
750 dispatch_source_set_event_handler(source, ^{
751 FAIL("Test timed out");
752 });
753 dispatch_resume(source);
754 }