/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. #import "GTMServiceManagement.h"
  19. #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
  20. #import "GTMSenTestCase.h"
  21. #import <servers/bootstrap.h>
  22. #define STANDARD_JOB_LABEL "com.apple.launchctl.Background"
  23. #define OUR_JOB_LABEL "com.google.gtm.GTMServiceManagementTest.job"
  24. #define BAD_JOB_LABEL "com.google.gtm.GTMServiceManagementTest.badjob"
  25. #define TEST_HARNESS_LABEL "com.google.gtm.GTMServiceManagementTestHarness"
  26. #define GTM_MACH_PORT_NAME "GTMServiceManagementTestingHarnessMachPort"
  27. static NSString const *kGTMSocketKey
  28. = @"COM_GOOGLE_GTM_GTMSERVICEMANAGEMENT_TEST_SOCKET";
  29. static NSString const *kGTMSocketName
  30. = @"GTMServiceManagementTesting";
  31. @interface GTMServiceManagementTest : GTMTestCase
  32. @end
  33. @implementation GTMServiceManagementTest
  34. - (void)testDataConversion {
  35. const char *someData = "someData";
  36. NSDictionary *subDict
  37. = [NSDictionary dictionaryWithObjectsAndKeys:
  38. [NSNumber numberWithBool:1], @"BoolValue",
  39. [NSNumber numberWithInt:2], @"IntValue",
  40. [NSNumber numberWithDouble:0.3], @"DoubleValue",
  41. @"A String", @"StringValue",
  42. [NSData dataWithBytes:someData length:strlen(someData)], @"DataValue",
  43. nil];
  44. NSArray *subArray
  45. = [NSArray arrayWithObjects:@"1", [NSNumber numberWithInt:2], nil];
  46. NSDictionary *topDict = [NSDictionary dictionaryWithObjectsAndKeys:
  47. subDict, @"SubDict",
  48. subArray, @"SubArray",
  49. @"Random String", @"RandomString",
  50. nil];
  51. CFErrorRef error = NULL;
  52. launch_data_t launchDict = GTMLaunchDataCreateFromCFType(topDict, &error);
  53. STAssertNotNULL(launchDict, nil);
  54. STAssertNULL(error, @"Error: %@", error);
  55. NSDictionary *nsDict
  56. = GTMCFAutorelease(GTMCFTypeCreateFromLaunchData(launchDict,
  57. NO,
  58. &error));
  59. STAssertNotNil(nsDict, nil);
  60. STAssertNULL(error, @"Error: %@", error);
  61. STAssertEqualObjects(nsDict, topDict, @"");
  62. launch_data_free(launchDict);
  63. // Test a bad type
  64. NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  65. STAssertNotNil(url, nil);
  66. launchDict = GTMLaunchDataCreateFromCFType(url, &error);
  67. STAssertNULL(launchDict, nil);
  68. STAssertNotNULL(error, nil);
  69. STAssertEqualObjects((id)CFErrorGetDomain(error),
  70. (id)kCFErrorDomainPOSIX, nil);
  71. STAssertEquals(CFErrorGetCode(error), (CFIndex)EINVAL, nil);
  72. CFRelease(error);
  73. CFTypeRef cfType = GTMCFTypeCreateFromLaunchData(NULL, YES, &error);
  74. STAssertNULL(cfType, nil);
  75. STAssertNotNULL(error, nil);
  76. CFRelease(error);
  77. }
  78. - (void)testJobDictionaries {
  79. NSDictionary *jobs = GTMCFAutorelease(GTMSMCopyAllJobDictionaries());
  80. STAssertNotNil(jobs, nil);
  81. // A job that should always be around
  82. NSDictionary *job
  83. = GTMCFAutorelease(GTMSMJobCopyDictionary(CFSTR(STANDARD_JOB_LABEL)));
  84. STAssertNotNil(job, nil);
  85. // A job that should never be around
  86. CFTypeRef type = GTMSMJobCopyDictionary(CFSTR(BAD_JOB_LABEL));
  87. STAssertNULL(type, nil);
  88. }
  89. - (void)testLaunching {
  90. CFErrorRef error = NULL;
  91. Boolean isGood = GTMSMJobSubmit(NULL, &error);
  92. STAssertFalse(isGood, nil);
  93. STAssertNotNULL(error, nil);
  94. CFRelease(error);
  95. NSDictionary *empty = [NSDictionary dictionary];
  96. isGood = GTMSMJobSubmit((CFDictionaryRef)empty, &error);
  97. STAssertFalse(isGood, nil);
  98. STAssertNotNULL(error, nil);
  99. CFRelease(error);
  100. NSDictionary *alreadyThere
  101. = [NSDictionary dictionaryWithObject:@STANDARD_JOB_LABEL
  102. forKey:@LAUNCH_JOBKEY_LABEL];
  103. isGood = GTMSMJobSubmit((CFDictionaryRef)alreadyThere, &error);
  104. STAssertFalse(isGood, nil);
  105. STAssertEquals([(NSError *)error code], (NSInteger)EEXIST, nil);
  106. CFRelease(error);
  107. NSDictionary *goodJob
  108. = [NSDictionary dictionaryWithObjectsAndKeys:
  109. @OUR_JOB_LABEL, @LAUNCH_JOBKEY_LABEL,
  110. @"/bin/test", @LAUNCH_JOBKEY_PROGRAM,
  111. nil];
  112. isGood = GTMSMJobSubmit((CFDictionaryRef)goodJob, &error);
  113. STAssertTrue(isGood, nil);
  114. STAssertNULL(error, nil);
  115. isGood = GTMSMJobRemove(CFSTR(OUR_JOB_LABEL), &error);
  116. STAssertTrue(isGood,
  117. @"You may need to run launchctl remove %s", OUR_JOB_LABEL);
  118. STAssertNULL(error, nil);
  119. isGood = GTMSMJobRemove(CFSTR(OUR_JOB_LABEL), &error);
  120. STAssertFalse(isGood, nil);
  121. STAssertNotNULL(error, nil);
  122. CFRelease(error);
  123. }
  124. - (void)testCopyExports {
  125. CFDictionaryRef exports = GTMCopyLaunchdExports();
  126. STAssertNotNULL(exports, nil);
  127. NSString *user = [(NSDictionary *)exports objectForKey:@"USER"];
  128. STAssertEqualObjects(user, NSUserName(), nil);
  129. CFRelease(exports);
  130. }
  131. - (void)testCheckin {
  132. CFErrorRef error = NULL;
  133. // Can't check ourselves in
  134. NSDictionary *badTest
  135. = GTMCFAutorelease(GTMSMCopyJobCheckInDictionary(&error));
  136. STAssertNil(badTest, nil);
  137. STAssertNotNULL(error, nil);
  138. CFRelease(error);
  139. NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
  140. STAssertNotNil(testBundle, nil);
  141. NSString *testHarnessPath
  142. = [testBundle pathForResource:@"GTMServiceManagementTestingHarness"
  143. ofType:nil];
  144. STAssertNotNil(testHarnessPath, nil);
  145. NSDictionary *machServices
  146. = [NSDictionary dictionaryWithObjectsAndKeys:
  147. [NSNumber numberWithBool:YES], @GTM_MACH_PORT_NAME,
  148. nil];
  149. NSDictionary *socket
  150. = [NSDictionary dictionaryWithObjectsAndKeys:
  151. kGTMSocketKey,@LAUNCH_JOBSOCKETKEY_SECUREWITHKEY,
  152. nil];
  153. NSDictionary *sockets
  154. = [NSDictionary dictionaryWithObjectsAndKeys:
  155. socket, kGTMSocketName,
  156. nil];
  157. // LAUNCH_JOBKEY_WAITFORDEBUGGER left commented out
  158. // so that it can easily be reenabled for debugging.
  159. NSDictionary *job = [NSDictionary dictionaryWithObjectsAndKeys:
  160. @TEST_HARNESS_LABEL, @LAUNCH_JOBKEY_LABEL,
  161. testHarnessPath, @LAUNCH_JOBKEY_PROGRAM,
  162. [NSNumber numberWithBool:YES], @LAUNCH_JOBKEY_RUNATLOAD,
  163. [NSNumber numberWithBool:YES], @LAUNCH_JOBKEY_DEBUG,
  164. //[NSNumber numberWithBool:YES], @LAUNCH_JOBKEY_WAITFORDEBUGGER,
  165. machServices, @LAUNCH_JOBKEY_MACHSERVICES,
  166. sockets, @LAUNCH_JOBKEY_SOCKETS,
  167. nil];
  168. // This is allowed to fail.
  169. GTMSMJobRemove(CFSTR(TEST_HARNESS_LABEL), NULL);
  170. BOOL isGood = GTMSMJobSubmit((CFDictionaryRef)job, &error);
  171. STAssertTrue(isGood, @"Error %@", error);
  172. NSDictionary* exports = GTMCFAutorelease(GTMCopyLaunchdExports());
  173. STAssertNotNULL(exports, nil);
  174. NSString *socketPath = [exports objectForKey:kGTMSocketKey];
  175. STAssertNotNULL(socketPath, nil);
  176. STAssertEqualObjects([socketPath lastPathComponent], kGTMSocketName, nil);
  177. mach_port_t sp = 0;
  178. kern_return_t rt = bootstrap_look_up(bootstrap_port,
  179. (char*)GTM_MACH_PORT_NAME,
  180. &sp);
  181. STAssertNotEquals(sp, (mach_port_t)0, nil);
  182. STAssertEquals(rt, KERN_SUCCESS, nil);
  183. isGood = GTMSMJobRemove(CFSTR(TEST_HARNESS_LABEL), &error);
  184. STAssertTrue(isGood, @"Error %@", error);
  185. }
  186. @end
  187. #endif // if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4