/core/externals/update-engine/Core/KSExistenceCheckerTest.m

http://macfuse.googlecode.com/ · Objective C · 299 lines · 178 code · 72 blank · 49 comment · 0 complexity · d5d166db5b39a032c76c077472b4dec6 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <SenTestingKit/SenTestingKit.h>
  15. #import "KSExistenceChecker.h"
  16. @interface KSExistenceCheckerTest : SenTestCase
  17. @end
  18. @implementation KSExistenceCheckerTest
  19. - (void)testNullChecker {
  20. KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
  21. STAssertNotNil(xc, nil);
  22. STAssertFalse([xc exists], nil);
  23. STAssertTrue([[xc description] length] > 1, nil);
  24. }
  25. - (void)testTrueChecker {
  26. KSExistenceChecker *xc = [KSExistenceChecker trueChecker];
  27. STAssertNotNil(xc, nil);
  28. STAssertTrue([xc exists], nil);
  29. STAssertTrue([[xc description] length] > 1, nil);
  30. }
  31. - (void)testPathChecker {
  32. KSPathExistenceChecker *xc = nil;
  33. xc = [[[KSPathExistenceChecker alloc] init] autorelease];
  34. STAssertNil(xc, nil);
  35. //
  36. // Should exist
  37. //
  38. xc = [KSPathExistenceChecker checkerWithPath:@"/"];
  39. STAssertNotNil(xc, nil);
  40. STAssertTrue([xc exists], nil);
  41. STAssertEqualObjects([xc path], @"/", nil);
  42. xc = [KSPathExistenceChecker checkerWithPath:@"/etc/passwd"];
  43. STAssertNotNil(xc, nil);
  44. STAssertTrue([xc exists], nil);
  45. STAssertEqualObjects([xc path], @"/etc/passwd", nil);
  46. xc = [KSPathExistenceChecker checkerWithPath:@"/../../.."];
  47. STAssertNotNil(xc, nil);
  48. STAssertTrue([xc exists], nil);
  49. STAssertEqualObjects([xc path], @"/../../..", nil);
  50. xc = [KSPathExistenceChecker checkerWithPath:@"/tmp"];
  51. STAssertNotNil(xc, nil);
  52. STAssertTrue([xc exists], nil);
  53. STAssertEqualObjects([xc path], @"/tmp", nil);
  54. xc = [KSPathExistenceChecker checkerWithPath:@"/tmp/."];
  55. STAssertNotNil(xc, nil);
  56. STAssertTrue([xc exists], nil);
  57. STAssertEqualObjects([xc path], @"/tmp/.", nil);
  58. xc = [KSPathExistenceChecker checkerWithPath:@"/Library/Application Support"];
  59. STAssertNotNil(xc, nil);
  60. STAssertTrue([xc exists], nil);
  61. STAssertEqualObjects([xc path], @"/Library/Application Support", nil);
  62. //
  63. // Should NOT exist
  64. //
  65. xc = [KSPathExistenceChecker checkerWithPath:@"/fake/path/to/quuuuuuuux"];
  66. STAssertNotNil(xc, nil);
  67. STAssertFalse([xc exists], nil);
  68. STAssertEqualObjects([xc path], @"/fake/path/to/quuuuuuuux", nil);
  69. xc = [KSPathExistenceChecker checkerWithPath:@"http://www.google.com"];
  70. STAssertNotNil(xc, nil);
  71. STAssertFalse([xc exists], nil);
  72. STAssertEqualObjects([xc path], @"http://www.google.com", nil);
  73. xc = [KSPathExistenceChecker checkerWithPath:@":etc:passwd"];
  74. STAssertNotNil(xc, nil);
  75. STAssertFalse([xc exists], nil);
  76. STAssertEqualObjects([xc path], @":etc:passwd", nil);
  77. xc = [KSPathExistenceChecker checkerWithPath:nil];
  78. STAssertNil(xc, nil);
  79. xc = [KSPathExistenceChecker checkerWithPath:@""];
  80. STAssertNotNil(xc, nil);
  81. STAssertFalse([xc exists], nil);
  82. STAssertEqualObjects([xc path], @"", nil);
  83. xc = [KSPathExistenceChecker checkerWithPath:@" "];
  84. STAssertNotNil(xc, nil);
  85. STAssertFalse([xc exists], nil);
  86. STAssertEqualObjects([xc path], @" ", nil);
  87. //
  88. // Test equality
  89. //
  90. KSExistenceChecker *xc1 = nil, *xc2 = nil;
  91. xc1 = [KSPathExistenceChecker checkerWithPath:@"/tmp"];
  92. xc2 = [KSPathExistenceChecker checkerWithPath:@"/tmp"];
  93. STAssertNotNil(xc1, nil);
  94. STAssertNotNil(xc2, nil);
  95. STAssertTrue([xc1 isEqual:xc1], nil);
  96. STAssertTrue([xc2 isEqual:xc2], nil);
  97. STAssertFalse([xc1 isEqual:@"blah"], nil);
  98. STAssertFalse([xc2 isEqual:@"blah"], nil);
  99. STAssertEqualObjects(xc1, xc2, nil);
  100. STAssertEquals([xc1 hash], [xc2 hash], nil);
  101. //
  102. // Test inequality
  103. //
  104. xc1 = [KSPathExistenceChecker checkerWithPath:@"/etc"];
  105. xc2 = [KSPathExistenceChecker checkerWithPath:@"/etc/"];
  106. STAssertNotNil(xc1, nil);
  107. STAssertNotNil(xc2, nil);
  108. STAssertFalse([xc1 isEqual:xc2], nil);
  109. STAssertTrue([[xc description] length] > 1, nil);
  110. }
  111. - (void)testLaunchServicesChecker {
  112. KSExistenceChecker *xc = nil;
  113. xc = [[[KSLaunchServicesExistenceChecker alloc] init] autorelease];
  114. STAssertNil(xc, nil);
  115. //
  116. // Should exist
  117. //
  118. xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"com.apple.TextEdit"];
  119. STAssertNotNil(xc, nil);
  120. STAssertTrue([xc exists], nil);
  121. xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"com.apple.Safari"];
  122. STAssertNotNil(xc, nil);
  123. STAssertTrue([xc exists], nil);
  124. //
  125. // Should NOT exist
  126. //
  127. xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c.d.e"];
  128. STAssertNotNil(xc, nil);
  129. STAssertFalse([xc exists], nil);
  130. xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@""];
  131. STAssertNotNil(xc, nil);
  132. STAssertFalse([xc exists], nil);
  133. xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:nil];
  134. STAssertNil(xc, nil);
  135. //
  136. // Test equality
  137. //
  138. KSExistenceChecker *xc1 = nil, *xc2 = nil;
  139. xc1 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"];
  140. xc2 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"];
  141. STAssertNotNil(xc1, nil);
  142. STAssertNotNil(xc2, nil);
  143. STAssertEqualObjects(xc1, xc2, nil);
  144. STAssertEquals([xc1 hash], [xc2 hash], nil);
  145. //
  146. // Test inequality
  147. //
  148. xc1 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"];
  149. xc2 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.d"];
  150. STAssertNotNil(xc1, nil);
  151. STAssertNotNil(xc2, nil);
  152. STAssertTrue([xc1 isEqual:xc1], nil);
  153. STAssertTrue([xc2 isEqual:xc2], nil);
  154. STAssertFalse([xc1 isEqual:@"blah"], nil);
  155. STAssertFalse([xc2 isEqual:@"blah"], nil);
  156. STAssertFalse([xc1 isEqual:xc2], nil);
  157. STAssertTrue([[xc1 description] length] > 1, nil);
  158. }
  159. - (void)testSpotlightChecker {
  160. KSExistenceChecker *xc = nil;
  161. xc = [[[KSSpotlightExistenceChecker alloc] init] autorelease];
  162. STAssertNil(xc, nil);
  163. //
  164. // Should exist
  165. //
  166. xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'TextEdit'"];
  167. STAssertNotNil(xc, nil);
  168. STAssertTrue([xc exists], nil);
  169. xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'Safari'"];
  170. STAssertNotNil(xc, nil);
  171. STAssertTrue([xc exists], nil);
  172. //
  173. // Should NOT exist
  174. //
  175. xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];
  176. STAssertNotNil(xc, nil);
  177. STAssertFalse([xc exists], nil);
  178. xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'RabbitFood'"];
  179. STAssertNotNil(xc, nil);
  180. STAssertFalse([xc exists], nil);
  181. xc = [KSSpotlightExistenceChecker checkerWithQuery:@""];
  182. STAssertNotNil(xc, nil);
  183. STAssertFalse([xc exists], nil);
  184. xc = [KSSpotlightExistenceChecker checkerWithQuery:@" "];
  185. STAssertNotNil(xc, nil);
  186. STAssertFalse([xc exists], nil);
  187. xc = [KSSpotlightExistenceChecker checkerWithQuery:@"invalid query"];
  188. STAssertNotNil(xc, nil);
  189. STAssertFalse([xc exists], nil);
  190. xc = [KSSpotlightExistenceChecker checkerWithQuery:nil];
  191. STAssertNil(xc, nil);
  192. //
  193. // Test equality
  194. //
  195. KSExistenceChecker *xc1 = nil, *xc2 = nil;
  196. xc1 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];
  197. xc2 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];
  198. STAssertNotNil(xc1, nil);
  199. STAssertNotNil(xc2, nil);
  200. STAssertTrue([xc1 isEqual:xc1], nil);
  201. STAssertTrue([xc2 isEqual:xc2], nil);
  202. STAssertFalse([xc1 isEqual:@"blah"], nil);
  203. STAssertFalse([xc2 isEqual:@"blah"], nil);
  204. STAssertEqualObjects(xc1, xc2, nil);
  205. STAssertEquals([xc1 hash], [xc2 hash], nil);
  206. //
  207. // Test inequality
  208. //
  209. xc1 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'TextEdit'"];
  210. xc2 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];
  211. STAssertNotNil(xc1, nil);
  212. STAssertNotNil(xc2, nil);
  213. STAssertFalse([xc1 isEqual:xc2], nil);
  214. STAssertTrue([[xc1 description] length] > 1, nil);
  215. }
  216. - (void)testNSCoding {
  217. NSArray *checkers = [NSArray arrayWithObjects:
  218. [KSExistenceChecker falseChecker],
  219. [KSPathExistenceChecker checkerWithPath:@"/Library/Application Support"],
  220. [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"],
  221. [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'TextEdit'"],
  222. nil];
  223. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:checkers];
  224. NSArray *unarchivedCheckers = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  225. STAssertEqualObjects(checkers, unarchivedCheckers, nil);
  226. }
  227. @end