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
33 _bundle = [NSBundle bundleWithPath:path];
35 [self testLogWithFormat:@"No bundle at location %@ (%s)\n", path, strerror(errno)];
38 if (![_bundle loadAndReturnError:&error]) {
39 [self testLogWithFormat:@"Test Bundle at %@ didn't load: %@\n", path, error];
44 XCTestSuite* testSuite = [[XCTestSuite alloc] initWithName:[[path lastPathComponent] stringByDeletingPathExtension]];
45 XCTestSuite* loadedSuite = [XCTestSuite testSuiteForBundlePath:path];
46 // Filter out only the tests that were named.
47 [loadedSuite.tests enumerateObjectsUsingBlock:^(__kindof XCTest * _Nonnull test, NSUInteger __unused idx, BOOL * __unused _Nonnull stop) {
48 [self testLogWithFormat:@"Checking test %@\n", test.name];
49 if([names containsObject:test.name]) {
50 [testSuite addTest:test];
53 _testSuite = testSuite;
56 _testSuite = [XCTestSuite testSuiteForBundlePath:path];
62 - (NSUInteger)runUnitTestSuite
64 [[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:self];
68 XCTestRun *testRun = [_testSuite testRun];
70 return testRun.totalFailureCount;
73 - (NSFileHandle *)logFileHandle
75 return [NSFileHandle fileHandleWithStandardOutput];
78 - (void)testLogWithFormat:(NSString *)format, ...
82 [self testLogWithFormat:format arguments:ap];
86 - (void)testLogWithFormat:(NSString *)format arguments:(va_list)arguments
88 NSString *message = [[NSString alloc] initWithFormat:format arguments:arguments];
89 [self.logFileHandle writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
92 - (NSDateFormatter *)dateFormatter
94 static NSDateFormatter *dateFormatter;
95 static dispatch_once_t onceToken;
96 dispatch_once(&onceToken, ^{
97 dateFormatter = [NSDateFormatter new];
98 dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
100 return dateFormatter;
103 /* -testBundleWillStart: // exactly once per test bundle
104 * -testSuiteWillStart: // exactly once per test suite
105 * -testCaseWillStart: // exactly once per test case
106 * -testCase:didFailWithDescription:... // zero or more times per test case, any time between test case start and finish
107 * -testCaseDidFinish: // exactly once per test case
108 * -testSuite:didFailWithDescription:... // zero or more times per test suite, any time between test suite start and finish
109 * -testSuiteDidFinish: // exactly once per test suite
110 * -testBundleDidFinish: // exactly once per test bundle
113 - (void)testSuiteWillStart:(XCTestSuite *)testSuite
115 [self testLogWithFormat:@"Test Suite '%@' started at %@\n", testSuite.name, [self.dateFormatter stringFromDate:testSuite.testRun.startDate]];
118 - (void)testSuite:(XCTestSuite *)testSuite didFailWithDescription:(NSString *)description inFile:(nullable NSString *)filePath atLine:(NSUInteger)lineNumber
120 [self testLogWithFormat:@"%@:%lu: error: %@ : %@\n", ((nil != filePath) ? filePath : @"<unknown>"), ((unsigned long)((nil != filePath) ? lineNumber : 0)), testSuite.name, description];
123 - (void)testSuiteDidFinish:(XCTestSuite *)testSuite
125 XCTestRun *testRun = testSuite.testRun;
126 [self testLogWithFormat:@"Test Suite '%@' %s at %@.\n\t Executed %lu test%s, with %lu failure%s (%lu unexpected) in %.3f (%.3f) seconds\n",
128 (testRun.hasSucceeded ? "passed" : "failed"),
129 [self.dateFormatter stringFromDate:testRun.stopDate],
130 ((unsigned long)testRun.executionCount), (testRun.executionCount != 1 ? "s" : ""),
131 ((unsigned long)testRun.totalFailureCount), (testRun.totalFailureCount != 1 ? "s" : ""),
132 ((unsigned long)testRun.unexpectedExceptionCount),
133 testRun.testDuration,
134 testRun.totalDuration];
137 - (void)testCaseWillStart:(XCTestCase *)testCase
139 [self testLogWithFormat:@"Test Case '%@' started.\n", testCase.name];
142 - (void)testCase:(XCTestCase *)testCase didFailWithDescription:(NSString *)description inFile:(nullable NSString *)filePath atLine:(NSUInteger)lineNumber
144 [self testLogWithFormat:@"%@:%lu: error: %@ : %@\n", ((nil != filePath) ? filePath : @"<unknown>"), ((unsigned long)((nil != filePath) ? lineNumber : 0)), testCase.name, description];
147 - (void)testCaseDidFinish:(XCTestCase *)testCase
149 [self testLogWithFormat:@"Test Case '%@' %s (%.3f seconds).\n", testCase.name, (testCase.testRun.hasSucceeded ? "passed" : "failed"), testCase.testRun.totalDuration];
157 static char* gTestBundleDir = "/AppleInternal/XCTests/com.apple.security";
158 static char* gTestBundleName = "CKKSCloudKitTests";
160 static NSMutableArray* gTestCaseNames = nil;
162 static const char* opt_str = "d:t:c:h";
164 static void usage(char*const binName, bool longUsage) {
165 fprintf(stderr, "Usage: %s [-d <test_dir>] -t test_bundle_name [(-c test_case_name)*]\n", binName);
167 fprintf(stderr, "-d: argument = path to directory where test bundles live\n");
168 fprintf(stderr, "-t: argument = name of test bundle to be run (without extension)\n");
169 fprintf(stderr, "-c: argument = name of test case to be run (multiple)\n");
173 static void getOptions(int argc, char *const *argv) {
175 while ( (ch = getopt(argc, argv, opt_str)) != -1 ) {
179 gTestBundleDir = optarg;
182 gTestBundleName = optarg;
185 if(!gTestCaseNames) {
186 gTestCaseNames = [NSMutableArray new];
188 [gTestCaseNames addObject:@(optarg)];
193 usage(argv[0], true);
200 int main (int argc, const char * argv[])
203 getOptions(argc, (char*const*)argv);
204 NSString *testBundleDir = [NSString stringWithCString:gTestBundleDir encoding:NSUTF8StringEncoding];
205 NSString *testBundleName = [NSString stringWithCString:gTestBundleName encoding:NSUTF8StringEncoding];
206 NSString *testBundlePath = [[testBundleDir stringByAppendingPathComponent:testBundleName] stringByAppendingPathExtension:@"xctest"];
208 printf("Running unit tests %s at: %s\n", gTestCaseNames?gTestCaseNames.description.UTF8String:"[All]", testBundlePath.UTF8String);
210 TestRunner *unitTest = [[TestRunner alloc] initWithBundlePath:testBundlePath andTestNames:gTestCaseNames];
212 fprintf(stderr, "Failed to load unit test runner at: %s\n", testBundlePath.UTF8String);
216 //runUnitTestSuite returns the number of failures. 0 = success, non-zero means failure. This complies with BATS testing standards.
217 return (int)[unitTest runUnitTestSuite];