2 // KeychainEntitledTestRunner.m
3 // KeychainEntitledTestRunner
5 // Stolen from Mark Pauley / CDEntitledTestRunner who stole it from Drew Terry / MobileContainerManager
6 // Copyright 2016-2017 Apple. All rights reserved.
9 #import <Foundation/Foundation.h>
11 #import <XCTest/XCTest.h>
13 @interface TestRunner : NSObject <XCTestObservation> {
15 XCTestSuite *_testSuite;
18 - (instancetype)initWithBundlePath:(NSString *)path andTestNames:(NSArray *)names;
19 - (NSUInteger)runUnitTestSuite;
21 - (void)testLogWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
22 - (void)testLogWithFormat:(NSString *)format arguments:(va_list)arguments NS_FORMAT_FUNCTION(1,0);
26 @implementation TestRunner
27 - (instancetype)initWithBundlePath:(NSString *)path andTestNames:(NSArray *)names
29 if ((self = [super init])) {
32 _bundle = [NSBundle bundleWithPath:path];
34 [self testLogWithFormat:@"No bundle at location %@ (%s)\n", path, strerror(errno)];
37 if (![_bundle loadAndReturnError:&error]) {
38 [self testLogWithFormat:@"Test Bundle at %@ didn't load: %@\n", path, error];
43 XCTestSuite* testSuite = [[XCTestSuite alloc] initWithName:[[path lastPathComponent] stringByDeletingPathExtension]];
44 XCTestSuite* loadedSuite = [XCTestSuite testSuiteForBundlePath:path];
45 // Filter out only the tests that were named.
46 [loadedSuite.tests enumerateObjectsUsingBlock:^(__kindof XCTest * _Nonnull test, NSUInteger __unused idx, BOOL * __unused _Nonnull stop) {
47 [self testLogWithFormat:@"Checking test %@\n", test.name];
48 if([names containsObject:test.name]) {
49 [testSuite addTest:test];
52 _testSuite = testSuite;
55 _testSuite = [XCTestSuite testSuiteForBundlePath:path];
61 - (NSUInteger)runUnitTestSuite
63 [[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:self];
67 XCTestRun *testRun = [_testSuite testRun];
69 return testRun.totalFailureCount;
72 - (NSFileHandle *)logFileHandle
74 return [NSFileHandle fileHandleWithStandardOutput];
77 - (void)testLogWithFormat:(NSString *)format, ...
81 [self testLogWithFormat:format arguments:ap];
85 - (void)testLogWithFormat:(NSString *)format arguments:(va_list)arguments
87 NSString *message = [[NSString alloc] initWithFormat:format arguments:arguments];
88 [self.logFileHandle writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
91 - (NSDateFormatter *)dateFormatter
93 static NSDateFormatter *dateFormatter;
94 static dispatch_once_t onceToken;
95 dispatch_once(&onceToken, ^{
96 dateFormatter = [NSDateFormatter new];
97 dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
102 /* -testBundleWillStart: // exactly once per test bundle
103 * -testSuiteWillStart: // exactly once per test suite
104 * -testCaseWillStart: // exactly once per test case
105 * -testCase:didFailWithDescription:... // zero or more times per test case, any time between test case start and finish
106 * -testCaseDidFinish: // exactly once per test case
107 * -testSuite:didFailWithDescription:... // zero or more times per test suite, any time between test suite start and finish
108 * -testSuiteDidFinish: // exactly once per test suite
109 * -testBundleDidFinish: // exactly once per test bundle
112 - (void)testSuiteWillStart:(XCTestSuite *)testSuite
114 [self testLogWithFormat:@"Test Suite '%@' started at %@\n", testSuite.name, [self.dateFormatter stringFromDate:testSuite.testRun.startDate]];
117 - (void)testSuite:(XCTestSuite *)testSuite didRecordIssue:(XCTIssue *)issue {
118 [self testLogWithFormat:@"(%@)%@:%lu: error: %@", testSuite.name, issue.sourceCodeContext.location.fileURL,
119 issue.sourceCodeContext.location.lineNumber, issue.compactDescription];
122 - (void)testSuiteDidFinish:(XCTestSuite *)testSuite
124 XCTestRun *testRun = testSuite.testRun;
125 [self testLogWithFormat:@"Test Suite '%@' %s at %@.\n\t Executed %lu test%s, with %lu failure%s (%lu unexpected) in %.3f (%.3f) seconds\n",
127 (testRun.hasSucceeded ? "passed" : "failed"),
128 [self.dateFormatter stringFromDate:testRun.stopDate],
129 ((unsigned long)testRun.executionCount), (testRun.executionCount != 1 ? "s" : ""),
130 ((unsigned long)testRun.totalFailureCount), (testRun.totalFailureCount != 1 ? "s" : ""),
131 ((unsigned long)testRun.unexpectedExceptionCount),
132 testRun.testDuration,
133 testRun.totalDuration];
136 - (void)testCaseWillStart:(XCTestCase *)testCase
138 [self testLogWithFormat:@"Test Case '%@' started.\n", testCase.name];
141 - (void)testCase:(XCTestCase *)testCase didRecordIssue:(XCTIssue *)issue {
142 [self testLogWithFormat:@"(%@)%@:%lu error: %@\n%@", testCase.name, issue.sourceCodeContext.location.fileURL, issue.sourceCodeContext.location.lineNumber, issue.detailedDescription, issue.sourceCodeContext.callStack];
145 - (void)testCaseDidFinish:(XCTestCase *)testCase
147 [self testLogWithFormat:@"Test Case '%@' %s (%.3f seconds).\n", testCase.name, (testCase.testRun.hasSucceeded ? "passed" : "failed"), testCase.testRun.totalDuration];
155 static char* gTestBundleDir = "/AppleInternal/XCTests/com.apple.security";
156 static char* gTestBundleName = "CKKSCloudKitTests";
158 static NSMutableArray* gTestCaseNames = nil;
160 static const char* opt_str = "d:t:c:h";
162 static void usage(char*const binName, bool longUsage) {
163 fprintf(stderr, "Usage: %s [-d <test_dir>] -t test_bundle_name [(-c test_case_name)*]\n", binName);
165 fprintf(stderr, "-d: argument = path to directory where test bundles live\n");
166 fprintf(stderr, "-t: argument = name of test bundle to be run (without extension)\n");
167 fprintf(stderr, "-c: argument = name of test case to be run (multiple)\n");
171 static void getOptions(int argc, char *const *argv) {
173 while ( (ch = getopt(argc, argv, opt_str)) != -1 ) {
177 gTestBundleDir = optarg;
180 gTestBundleName = optarg;
183 if(!gTestCaseNames) {
184 gTestCaseNames = [NSMutableArray new];
186 [gTestCaseNames addObject:@(optarg)];
191 usage(argv[0], true);
198 int main (int argc, const char * argv[])
201 getOptions(argc, (char*const*)argv);
202 NSString *testBundleDir = [NSString stringWithCString:gTestBundleDir encoding:NSUTF8StringEncoding];
203 NSString *testBundleName = [NSString stringWithCString:gTestBundleName encoding:NSUTF8StringEncoding];
204 NSString *testBundlePath = [[testBundleDir stringByAppendingPathComponent:testBundleName] stringByAppendingPathExtension:@"xctest"];
206 printf("Running unit tests %s at: %s\n", gTestCaseNames?gTestCaseNames.description.UTF8String:"[All]", testBundlePath.UTF8String);
208 TestRunner *unitTest = [[TestRunner alloc] initWithBundlePath:testBundlePath andTestNames:gTestCaseNames];
210 fprintf(stderr, "Failed to load unit test runner at: %s\n", testBundlePath.UTF8String);
214 //runUnitTestSuite returns the number of failures. 0 = success, non-zero means failure. This complies with BATS testing standards.
215 return (int)[unitTest runUnitTestSuite];