PageRenderTime 126ms CodeModel.GetById 17ms app.highlight 105ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 15#import <SenTestingKit/SenTestingKit.h>
 16#import "KSExistenceChecker.h"
 17
 18
 19@interface KSExistenceCheckerTest : SenTestCase
 20@end
 21
 22
 23@implementation KSExistenceCheckerTest
 24
 25- (void)testNullChecker {
 26  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
 27  STAssertNotNil(xc, nil);
 28  STAssertFalse([xc exists], nil);
 29  STAssertTrue([[xc description] length] > 1, nil);
 30}
 31
 32- (void)testTrueChecker {
 33  KSExistenceChecker *xc = [KSExistenceChecker trueChecker];
 34  STAssertNotNil(xc, nil);
 35  STAssertTrue([xc exists], nil);
 36  STAssertTrue([[xc description] length] > 1, nil);
 37}
 38
 39- (void)testPathChecker {
 40  KSPathExistenceChecker *xc = nil;
 41  
 42  xc = [[[KSPathExistenceChecker alloc] init] autorelease];
 43  STAssertNil(xc, nil);
 44  
 45  //
 46  // Should exist
 47  //
 48  
 49  xc = [KSPathExistenceChecker checkerWithPath:@"/"];
 50  STAssertNotNil(xc, nil);
 51  STAssertTrue([xc exists], nil);
 52  STAssertEqualObjects([xc path], @"/", nil);
 53  
 54  xc = [KSPathExistenceChecker checkerWithPath:@"/etc/passwd"];
 55  STAssertNotNil(xc, nil);
 56  STAssertTrue([xc exists], nil);
 57  STAssertEqualObjects([xc path], @"/etc/passwd", nil);
 58  
 59  xc = [KSPathExistenceChecker checkerWithPath:@"/../../.."];
 60  STAssertNotNil(xc, nil);
 61  STAssertTrue([xc exists], nil);
 62  STAssertEqualObjects([xc path], @"/../../..", nil);
 63  
 64  xc = [KSPathExistenceChecker checkerWithPath:@"/tmp"];
 65  STAssertNotNil(xc, nil);
 66  STAssertTrue([xc exists], nil);
 67  STAssertEqualObjects([xc path], @"/tmp", nil);
 68  
 69  xc = [KSPathExistenceChecker checkerWithPath:@"/tmp/."];
 70  STAssertNotNil(xc, nil);
 71  STAssertTrue([xc exists], nil);
 72  STAssertEqualObjects([xc path], @"/tmp/.", nil);
 73  
 74  xc = [KSPathExistenceChecker checkerWithPath:@"/Library/Application Support"];
 75  STAssertNotNil(xc, nil);
 76  STAssertTrue([xc exists], nil);
 77  STAssertEqualObjects([xc path], @"/Library/Application Support", nil);
 78  
 79  //
 80  // Should NOT exist
 81  //
 82  
 83  xc = [KSPathExistenceChecker checkerWithPath:@"/fake/path/to/quuuuuuuux"];
 84  STAssertNotNil(xc, nil);
 85  STAssertFalse([xc exists], nil);
 86  STAssertEqualObjects([xc path], @"/fake/path/to/quuuuuuuux", nil);
 87  
 88  xc = [KSPathExistenceChecker checkerWithPath:@"http://www.google.com"];
 89  STAssertNotNil(xc, nil);
 90  STAssertFalse([xc exists], nil);
 91  STAssertEqualObjects([xc path], @"http://www.google.com", nil);
 92
 93  xc = [KSPathExistenceChecker checkerWithPath:@":etc:passwd"];
 94  STAssertNotNil(xc, nil);
 95  STAssertFalse([xc exists], nil);
 96  STAssertEqualObjects([xc path], @":etc:passwd", nil);
 97  
 98  xc = [KSPathExistenceChecker checkerWithPath:nil];
 99  STAssertNil(xc, nil);
100  
101  xc = [KSPathExistenceChecker checkerWithPath:@""];
102  STAssertNotNil(xc, nil);
103  STAssertFalse([xc exists], nil);
104  STAssertEqualObjects([xc path], @"", nil);
105  
106  xc = [KSPathExistenceChecker checkerWithPath:@" "];
107  STAssertNotNil(xc, nil);
108  STAssertFalse([xc exists], nil);
109  STAssertEqualObjects([xc path], @" ", nil);
110  
111  //
112  // Test equality
113  //
114  
115  KSExistenceChecker *xc1 = nil, *xc2 = nil;
116  xc1 = [KSPathExistenceChecker checkerWithPath:@"/tmp"];  
117  xc2 = [KSPathExistenceChecker checkerWithPath:@"/tmp"];
118  STAssertNotNil(xc1, nil);
119  STAssertNotNil(xc2, nil);
120  
121  STAssertTrue([xc1 isEqual:xc1], nil);
122  STAssertTrue([xc2 isEqual:xc2], nil);
123  STAssertFalse([xc1 isEqual:@"blah"], nil);
124  STAssertFalse([xc2 isEqual:@"blah"], nil);
125  
126  STAssertEqualObjects(xc1, xc2, nil);
127  STAssertEquals([xc1 hash], [xc2 hash], nil);
128  
129  //
130  // Test inequality
131  //
132  
133  xc1 = [KSPathExistenceChecker checkerWithPath:@"/etc"];  
134  xc2 = [KSPathExistenceChecker checkerWithPath:@"/etc/"];
135  STAssertNotNil(xc1, nil);
136  STAssertNotNil(xc2, nil);
137  
138  STAssertFalse([xc1 isEqual:xc2], nil);
139  
140  STAssertTrue([[xc description] length] > 1, nil);
141}
142
143- (void)testLaunchServicesChecker {
144  KSExistenceChecker *xc = nil;
145  
146  xc = [[[KSLaunchServicesExistenceChecker alloc] init] autorelease];
147  STAssertNil(xc, nil);
148  
149  //
150  // Should exist
151  //
152  
153  xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"com.apple.TextEdit"];
154  STAssertNotNil(xc, nil);
155  STAssertTrue([xc exists], nil);
156  
157  xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"com.apple.Safari"];
158  STAssertNotNil(xc, nil);
159  STAssertTrue([xc exists], nil);
160  
161  //
162  // Should NOT exist
163  //
164  
165  xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c.d.e"];
166  STAssertNotNil(xc, nil);
167  STAssertFalse([xc exists], nil);
168  
169  xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:@""];
170  STAssertNotNil(xc, nil);
171  STAssertFalse([xc exists], nil);
172  
173  xc = [KSLaunchServicesExistenceChecker checkerWithBundleID:nil];
174  STAssertNil(xc, nil);
175  
176  //
177  // Test equality
178  //
179  
180  KSExistenceChecker *xc1 = nil, *xc2 = nil;
181  xc1 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"];  
182  xc2 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"];
183  STAssertNotNil(xc1, nil);
184  STAssertNotNil(xc2, nil);
185  
186  STAssertEqualObjects(xc1, xc2, nil);
187  STAssertEquals([xc1 hash], [xc2 hash], nil);
188  
189  //
190  // Test inequality
191  //
192  
193  xc1 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"];  
194  xc2 = [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.d"];
195  STAssertNotNil(xc1, nil);
196  STAssertNotNil(xc2, nil);
197  
198  STAssertTrue([xc1 isEqual:xc1], nil);
199  STAssertTrue([xc2 isEqual:xc2], nil);
200  STAssertFalse([xc1 isEqual:@"blah"], nil);
201  STAssertFalse([xc2 isEqual:@"blah"], nil);
202  
203  STAssertFalse([xc1 isEqual:xc2], nil);
204  
205  STAssertTrue([[xc1 description] length] > 1, nil);
206}
207
208- (void)testSpotlightChecker {
209  KSExistenceChecker *xc = nil;
210  
211  xc = [[[KSSpotlightExistenceChecker alloc] init] autorelease];
212  STAssertNil(xc, nil);
213  
214  //
215  // Should exist
216  //
217  
218  xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'TextEdit'"];
219  STAssertNotNil(xc, nil);
220  STAssertTrue([xc exists], nil);
221  
222  xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'Safari'"];
223  STAssertNotNil(xc, nil);
224  STAssertTrue([xc exists], nil);
225  
226  //
227  // Should NOT exist
228  //
229  
230  xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];
231  STAssertNotNil(xc, nil);
232  STAssertFalse([xc exists], nil);
233  
234  xc = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'RabbitFood'"];
235  STAssertNotNil(xc, nil);
236  STAssertFalse([xc exists], nil);
237  
238  xc = [KSSpotlightExistenceChecker checkerWithQuery:@""];
239  STAssertNotNil(xc, nil);
240  STAssertFalse([xc exists], nil);
241  
242  xc = [KSSpotlightExistenceChecker checkerWithQuery:@" "];
243  STAssertNotNil(xc, nil);
244  STAssertFalse([xc exists], nil);
245  
246  xc = [KSSpotlightExistenceChecker checkerWithQuery:@"invalid query"];
247  STAssertNotNil(xc, nil);
248  STAssertFalse([xc exists], nil);
249  
250  xc = [KSSpotlightExistenceChecker checkerWithQuery:nil];
251  STAssertNil(xc, nil);
252  
253  //
254  // Test equality
255  //
256  
257  KSExistenceChecker *xc1 = nil, *xc2 = nil;
258  xc1 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];  
259  xc2 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];  
260  STAssertNotNil(xc1, nil);
261  STAssertNotNil(xc2, nil);
262  
263  STAssertTrue([xc1 isEqual:xc1], nil);
264  STAssertTrue([xc2 isEqual:xc2], nil);
265  STAssertFalse([xc1 isEqual:@"blah"], nil);
266  STAssertFalse([xc2 isEqual:@"blah"], nil);
267  
268  STAssertEqualObjects(xc1, xc2, nil);
269  STAssertEquals([xc1 hash], [xc2 hash], nil);
270  
271  //
272  // Test inequality
273  //
274  
275  xc1 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'TextEdit'"];  
276  xc2 = [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'DoesNotExist'"];  
277  STAssertNotNil(xc1, nil);
278  STAssertNotNil(xc2, nil);
279  
280  STAssertFalse([xc1 isEqual:xc2], nil);
281  
282  STAssertTrue([[xc1 description] length] > 1, nil);
283}
284
285- (void)testNSCoding {
286  NSArray *checkers = [NSArray arrayWithObjects:
287    [KSExistenceChecker falseChecker],               
288    [KSPathExistenceChecker checkerWithPath:@"/Library/Application Support"],                       
289    [KSLaunchServicesExistenceChecker checkerWithBundleID:@"a.b.c"],                      
290    [KSSpotlightExistenceChecker checkerWithQuery:@"kMDItemDisplayName == 'TextEdit'"],
291    nil];
292  
293  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:checkers];
294  NSArray *unarchivedCheckers = [NSKeyedUnarchiver unarchiveObjectWithData:data];
295  
296  STAssertEqualObjects(checkers, unarchivedCheckers, nil);
297}
298
299@end