PageRenderTime 51ms CodeModel.GetById 19ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMServiceManagementTest.m

http://macfuse.googlecode.com/
Objective C | 218 lines | 162 code | 31 blank | 25 comment | 0 complexity | 73b377a8e5815afaa71bf2e2c0b4a322 MD5 | raw file
  1//
  2//  GTMServiceManagementTest.m
  3//
  4//  Copyright 2010 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMServiceManagement.h"
 20
 21#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
 22
 23#import "GTMSenTestCase.h"
 24#import <servers/bootstrap.h>
 25
 26#define STANDARD_JOB_LABEL "com.apple.launchctl.Background"
 27#define OUR_JOB_LABEL "com.google.gtm.GTMServiceManagementTest.job"
 28#define BAD_JOB_LABEL "com.google.gtm.GTMServiceManagementTest.badjob"
 29#define TEST_HARNESS_LABEL "com.google.gtm.GTMServiceManagementTestHarness"
 30#define GTM_MACH_PORT_NAME "GTMServiceManagementTestingHarnessMachPort"
 31
 32static NSString const *kGTMSocketKey
 33  = @"COM_GOOGLE_GTM_GTMSERVICEMANAGEMENT_TEST_SOCKET";
 34static NSString const *kGTMSocketName
 35  = @"GTMServiceManagementTesting";
 36
 37@interface GTMServiceManagementTest : GTMTestCase
 38@end
 39
 40@implementation GTMServiceManagementTest
 41
 42- (void)testDataConversion {
 43  const char *someData = "someData";
 44  NSDictionary *subDict
 45    = [NSDictionary dictionaryWithObjectsAndKeys:
 46       [NSNumber numberWithBool:1], @"BoolValue",
 47       [NSNumber numberWithInt:2], @"IntValue",
 48       [NSNumber numberWithDouble:0.3], @"DoubleValue",
 49       @"A String", @"StringValue",
 50       [NSData dataWithBytes:someData length:strlen(someData)], @"DataValue",
 51       nil];
 52  NSArray *subArray
 53    = [NSArray arrayWithObjects:@"1", [NSNumber numberWithInt:2], nil];
 54  NSDictionary *topDict = [NSDictionary dictionaryWithObjectsAndKeys:
 55                           subDict, @"SubDict",
 56                           subArray, @"SubArray",
 57                           @"Random String", @"RandomString",
 58                           nil];
 59  CFErrorRef error = NULL;
 60  launch_data_t launchDict = GTMLaunchDataCreateFromCFType(topDict, &error);
 61  STAssertNotNULL(launchDict, nil);
 62  STAssertNULL(error, @"Error: %@", error);
 63  NSDictionary *nsDict
 64    = GTMCFAutorelease(GTMCFTypeCreateFromLaunchData(launchDict,
 65                                                     NO,
 66                                                     &error));
 67  STAssertNotNil(nsDict, nil);
 68  STAssertNULL(error, @"Error: %@", error);
 69  STAssertEqualObjects(nsDict, topDict, @"");
 70
 71  launch_data_free(launchDict);
 72
 73  // Test a bad type
 74  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
 75  STAssertNotNil(url, nil);
 76  launchDict = GTMLaunchDataCreateFromCFType(url, &error);
 77  STAssertNULL(launchDict, nil);
 78  STAssertNotNULL(error, nil);
 79  STAssertEqualObjects((id)CFErrorGetDomain(error),
 80                       (id)kCFErrorDomainPOSIX, nil);
 81  STAssertEquals(CFErrorGetCode(error), (CFIndex)EINVAL, nil);
 82  CFRelease(error);
 83
 84  CFTypeRef cfType = GTMCFTypeCreateFromLaunchData(NULL, YES, &error);
 85  STAssertNULL(cfType, nil);
 86  STAssertNotNULL(error, nil);
 87  CFRelease(error);
 88}
 89
 90- (void)testJobDictionaries {
 91  NSDictionary *jobs = GTMCFAutorelease(GTMSMCopyAllJobDictionaries());
 92  STAssertNotNil(jobs, nil);
 93  // A job that should always be around
 94  NSDictionary *job
 95    = GTMCFAutorelease(GTMSMJobCopyDictionary(CFSTR(STANDARD_JOB_LABEL)));
 96  STAssertNotNil(job, nil);
 97
 98  // A job that should never be around
 99  CFTypeRef type = GTMSMJobCopyDictionary(CFSTR(BAD_JOB_LABEL));
100  STAssertNULL(type, nil);
101}
102
103- (void)testLaunching {
104  CFErrorRef error = NULL;
105  Boolean isGood = GTMSMJobSubmit(NULL, &error);
106  STAssertFalse(isGood, nil);
107  STAssertNotNULL(error, nil);
108  CFRelease(error);
109
110  NSDictionary *empty = [NSDictionary dictionary];
111  isGood = GTMSMJobSubmit((CFDictionaryRef)empty, &error);
112  STAssertFalse(isGood, nil);
113  STAssertNotNULL(error, nil);
114  CFRelease(error);
115
116  NSDictionary *alreadyThere
117    = [NSDictionary dictionaryWithObject:@STANDARD_JOB_LABEL
118                                  forKey:@LAUNCH_JOBKEY_LABEL];
119  isGood = GTMSMJobSubmit((CFDictionaryRef)alreadyThere, &error);
120  STAssertFalse(isGood, nil);
121  STAssertEquals([(NSError *)error code], (NSInteger)EEXIST, nil);
122  CFRelease(error);
123
124  NSDictionary *goodJob
125    = [NSDictionary dictionaryWithObjectsAndKeys:
126       @OUR_JOB_LABEL, @LAUNCH_JOBKEY_LABEL,
127       @"/bin/test", @LAUNCH_JOBKEY_PROGRAM,
128       nil];
129  isGood = GTMSMJobSubmit((CFDictionaryRef)goodJob, &error);
130  STAssertTrue(isGood, nil);
131  STAssertNULL(error, nil);
132
133  isGood = GTMSMJobRemove(CFSTR(OUR_JOB_LABEL), &error);
134  STAssertTrue(isGood,
135               @"You may need to run launchctl remove %s", OUR_JOB_LABEL);
136  STAssertNULL(error, nil);
137
138  isGood = GTMSMJobRemove(CFSTR(OUR_JOB_LABEL), &error);
139  STAssertFalse(isGood, nil);
140  STAssertNotNULL(error, nil);
141  CFRelease(error);
142}
143
144- (void)testCopyExports {
145  CFDictionaryRef exports = GTMCopyLaunchdExports();
146  STAssertNotNULL(exports, nil);
147  NSString *user = [(NSDictionary *)exports objectForKey:@"USER"];
148  STAssertEqualObjects(user, NSUserName(), nil);
149  CFRelease(exports);
150}
151
152- (void)testCheckin {
153  CFErrorRef error = NULL;
154  // Can't check ourselves in
155  NSDictionary *badTest
156    = GTMCFAutorelease(GTMSMCopyJobCheckInDictionary(&error));
157  STAssertNil(badTest, nil);
158  STAssertNotNULL(error, nil);
159  CFRelease(error);
160
161  NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
162  STAssertNotNil(testBundle, nil);
163  NSString *testHarnessPath
164    = [testBundle pathForResource:@"GTMServiceManagementTestingHarness"
165                           ofType:nil];
166  STAssertNotNil(testHarnessPath, nil);
167  NSDictionary *machServices
168    = [NSDictionary dictionaryWithObjectsAndKeys:
169        [NSNumber numberWithBool:YES], @GTM_MACH_PORT_NAME,
170        nil];
171
172  NSDictionary *socket
173    = [NSDictionary dictionaryWithObjectsAndKeys:
174       kGTMSocketKey,@LAUNCH_JOBSOCKETKEY_SECUREWITHKEY,
175       nil];
176
177  NSDictionary *sockets
178    = [NSDictionary dictionaryWithObjectsAndKeys:
179       socket, kGTMSocketName,
180       nil];
181
182  // LAUNCH_JOBKEY_WAITFORDEBUGGER left commented out
183  // so that it can easily be reenabled for debugging.
184  NSDictionary *job = [NSDictionary dictionaryWithObjectsAndKeys:
185    @TEST_HARNESS_LABEL, @LAUNCH_JOBKEY_LABEL,
186    testHarnessPath, @LAUNCH_JOBKEY_PROGRAM,
187    [NSNumber numberWithBool:YES], @LAUNCH_JOBKEY_RUNATLOAD,
188    [NSNumber numberWithBool:YES], @LAUNCH_JOBKEY_DEBUG,
189    //[NSNumber numberWithBool:YES], @LAUNCH_JOBKEY_WAITFORDEBUGGER,
190    machServices, @LAUNCH_JOBKEY_MACHSERVICES,
191    sockets, @LAUNCH_JOBKEY_SOCKETS,
192    nil];
193
194  // This is allowed to fail.
195  GTMSMJobRemove(CFSTR(TEST_HARNESS_LABEL), NULL);
196
197  BOOL isGood = GTMSMJobSubmit((CFDictionaryRef)job, &error);
198  STAssertTrue(isGood, @"Error %@", error);
199
200  NSDictionary* exports = GTMCFAutorelease(GTMCopyLaunchdExports());
201  STAssertNotNULL(exports, nil);
202  NSString *socketPath = [exports objectForKey:kGTMSocketKey];
203  STAssertNotNULL(socketPath, nil);
204  STAssertEqualObjects([socketPath lastPathComponent], kGTMSocketName, nil);
205
206  mach_port_t sp = 0;
207  kern_return_t rt = bootstrap_look_up(bootstrap_port,
208                                       (char*)GTM_MACH_PORT_NAME,
209                                       &sp);
210  STAssertNotEquals(sp, (mach_port_t)0, nil);
211  STAssertEquals(rt, KERN_SUCCESS, nil);
212  isGood = GTMSMJobRemove(CFSTR(TEST_HARNESS_LABEL), &error);
213  STAssertTrue(isGood, @"Error %@", error);
214}
215
216@end
217
218#endif //  if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4