PageRenderTime 249ms CodeModel.GetById 15ms app.highlight 227ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 949 lines | 722 code | 129 blank | 98 comment | 11 complexity | 73d7def073dfd18ece6ec10055845e97 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 "KSTicket.h"
 17#import "KSExistenceChecker.h"
 18
 19
 20@interface KSTicketTest : SenTestCase
 21@end
 22
 23
 24@implementation KSTicketTest
 25
 26- (void)testTicket {
 27  KSTicket *t = nil;
 28
 29  t = [[KSTicket alloc] init];
 30  STAssertNil(t, nil);
 31
 32  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
 33  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
 34
 35  // Make sure tickets created with the convenience, and the init, are sane.
 36  KSTicket *t1 = [KSTicket ticketWithProductID:@"{GUID}"
 37                                       version:@"1.1"
 38                              existenceChecker:xc
 39                                     serverURL:url];
 40  STAssertNotNil(t1, nil);
 41  KSTicket *t2 = [[KSTicket alloc] initWithProductID:@"{GUID}"
 42                                             version:@"1.1"
 43                                    existenceChecker:xc
 44                                           serverURL:url];
 45  STAssertNotNil(t2, nil);
 46
 47  NSArray *tickets = [NSArray arrayWithObjects:t1, t2, nil];
 48  NSEnumerator *enumerator = [tickets objectEnumerator];
 49  while ((t = [enumerator nextObject])) {
 50    STAssertEqualObjects([t productID], @"{GUID}", nil);
 51    STAssertEqualObjects([t version], @"1.1", nil);
 52    STAssertEqualObjects([t existenceChecker], xc, nil);
 53    STAssertEqualObjects([t serverURL], url, nil);
 54    STAssertNil([t trustedTesterToken], nil);
 55    STAssertNil([t tag], nil);
 56    STAssertTrue([[t creationDate] timeIntervalSinceNow] < 0, nil);
 57    STAssertTrue(-[[t creationDate] timeIntervalSinceNow] < 0.5, nil);
 58    STAssertTrue([[t description] length] > 1, nil);
 59  }
 60}
 61
 62- (void)testTicketEquality {
 63  KSTicket *t1 = nil;
 64  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
 65  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
 66  NSDate *cd = [NSDate dateWithTimeIntervalSinceNow:12345.67];
 67  NSMutableDictionary *args =
 68    [NSMutableDictionary dictionaryWithObjectsAndKeys:
 69                         @"{GUID}", KSTicketProductIDKey,
 70                         @"1.1", KSTicketVersionKey,
 71                         xc, KSTicketExistenceCheckerKey,
 72                         url, KSTicketServerURLKey,
 73                         cd, KSTicketCreationDateKey,
 74                         @"tttoken", KSTicketTrustedTesterTokenKey,
 75                         @"ttaggen", KSTicketTagKey,
 76                         @"path", KSTicketTagPathKey,
 77                         @"key", KSTicketTagKeyKey,
 78                         @"brandpath", KSTicketBrandPathKey,
 79                         @"brandkey", KSTicketBrandKeyKey,
 80                         @"versionpath", KSTicketVersionPathKey,
 81                         @"versionkey", KSTicketVersionKeyKey,
 82                         nil];
 83
 84  t1 = [KSTicket ticketWithParameters:args];
 85
 86  STAssertNotNil(t1, nil);
 87  STAssertTrue([t1 isEqual:t1], nil);
 88  STAssertTrue([t1 isEqualToTicket:t1], nil);
 89  STAssertFalse([t1 isEqual:@"blah"], nil);
 90
 91  // "copy" t1 by archiving it then unarchiving it. This simulates adding the
 92  // ticket to the ticket store, then retrieving it.
 93  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:t1];
 94  KSTicket *t2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
 95  STAssertNotNil(t2, nil);
 96
 97  // Make sure t2 has everything it should.
 98  STAssertEqualObjects([t2 productID], @"{GUID}", nil);
 99  STAssertEqualObjects([t2 version], @"1.1", nil);
100  STAssertEqualObjects([t2 existenceChecker], xc, nil);
101  STAssertEqualObjects([t2 serverURL], url, nil);
102  STAssertEqualObjects([t2 creationDate], cd, nil);
103  STAssertEqualObjects([t2 trustedTesterToken], @"tttoken", nil);
104  STAssertEqualObjects([t2 tag], @"ttaggen", nil);
105  STAssertEqualObjects([t2 tagPath], @"path", nil);
106  STAssertEqualObjects([t2 tagKey], @"key", nil);
107  STAssertEqualObjects([t2 brandPath], @"brandpath", nil);
108  STAssertEqualObjects([t2 brandKey], @"brandkey", nil);
109  STAssertEqualObjects([t2 versionPath], @"versionpath", nil);
110  STAssertEqualObjects([t2 versionKey], @"versionkey", nil);
111
112  STAssertTrue(t1 != t2, nil);
113  STAssertTrue([t1 isEqual:t2], nil);
114  STAssertTrue([t1 isEqualToTicket:t2], nil);
115  STAssertEqualObjects(t1, t2, nil);
116  STAssertEquals([t1 hash], [t2 hash], nil);
117
118  t2 = [KSTicket ticketWithProductID:@"{GUID}"
119                        version:@"1.1"
120               existenceChecker:xc
121                      serverURL:url];
122  STAssertNotNil(t2, nil);
123  STAssertFalse([t1 isEqual:t2], nil);
124
125  KSTicket *t3 = nil;
126  t3 = [KSTicket ticketWithProductID:@"{GUID}!"
127                        version:@"1.1"
128               existenceChecker:xc
129                      serverURL:url];
130  STAssertFalse([t1 isEqual:t3], nil);
131
132  t3 = [KSTicket ticketWithProductID:@"{GUID}"
133                        version:@"1.1!"
134               existenceChecker:xc
135                      serverURL:url];
136  STAssertFalse([t1 isEqual:t3], nil);
137
138  t3 = [KSTicket ticketWithProductID:@"{GUID}"
139                        version:@"1.1"
140               existenceChecker:xc
141                      serverURL:[NSURL URLWithString:@"http://unixjunkie.net"]];
142  STAssertFalse([t1 isEqual:t3], nil);
143
144  KSExistenceChecker *xchecker =
145    [KSPathExistenceChecker checkerWithPath:@"/tmp"];
146  t3 = [KSTicket ticketWithProductID:@"{GUID}"
147                        version:@"1.1"
148               existenceChecker:xchecker
149                      serverURL:url];
150  STAssertFalse([t1 isEqual:t3], nil);
151
152  // Make sure changing one of TTT, tag, tag path, or tag key renders
153  // the tickets not equal.
154  [args setObject:@"not-tttoken" forKey:KSTicketTrustedTesterTokenKey];
155  t3 = [KSTicket ticketWithParameters:args];
156  STAssertFalse([t1 isEqual:t3], nil);
157
158  [args setObject:@"tttoken" forKey:KSTicketTrustedTesterTokenKey];
159  [args setObject:@"not-ttaggen" forKey:KSTicketTagKey];
160  t3 = [KSTicket ticketWithParameters:args];
161  STAssertFalse([t1 isEqual:t3], nil);
162
163  [args setObject:@"ttaggen" forKey:KSTicketTagKey];
164  [args setObject:@"not-path" forKey:KSTicketTagPathKey];
165  t3 = [KSTicket ticketWithParameters:args];
166  STAssertFalse([t1 isEqual:t3], nil);
167
168  [args setObject:@"path" forKey:KSTicketTagPathKey];
169  [args setObject:@"not-key" forKey:KSTicketTagKeyKey];
170  t3 = [KSTicket ticketWithParameters:args];
171  STAssertFalse([t1 isEqual:t3], nil);
172
173  [args setObject:@"key" forKey:KSTicketTagKeyKey];
174  [args setObject:@"not-brandpath" forKey:KSTicketBrandPathKey];
175  t3 = [KSTicket ticketWithParameters:args];
176  STAssertFalse([t1 isEqual:t3], nil);
177
178  [args setObject:@"brandpath" forKey:KSTicketBrandPathKey];
179  [args setObject:@"not-brandkey" forKey:KSTicketBrandKeyKey];
180  t3 = [KSTicket ticketWithParameters:args];
181  STAssertFalse([t1 isEqual:t3], nil);
182
183  [args setObject:@"brandkey" forKey:KSTicketBrandKeyKey];
184  [args setObject:@"not-versionpath" forKey:KSTicketVersionPathKey];
185  t3 = [KSTicket ticketWithParameters:args];
186  STAssertFalse([t1 isEqual:t3], nil);
187
188  [args setObject:@"versionpath" forKey:KSTicketVersionPathKey];
189  [args setObject:@"not-versionkey" forKey:KSTicketVersionKeyKey];
190  t3 = [KSTicket ticketWithParameters:args];
191  STAssertFalse([t1 isEqual:t3], nil);
192}
193
194- (void)testNilArgs {
195  KSTicket *t = nil;
196
197  t = [KSTicket ticketWithProductID:nil version:nil
198              existenceChecker:nil serverURL:nil];
199  STAssertNil(t, nil);
200
201  t = [KSTicket ticketWithProductID:@"hi" version:nil
202              existenceChecker:nil serverURL:nil];
203  STAssertNil(t, nil);
204
205  t = [KSTicket ticketWithProductID:nil  version:nil
206              existenceChecker:nil serverURL:nil];
207  STAssertNil(t, nil);
208
209  t = [KSTicket ticketWithProductID:nil version:@"hi"
210              existenceChecker:nil serverURL:nil];
211  STAssertNil(t, nil);
212
213  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
214  t = [KSTicket ticketWithProductID:nil version:nil
215              existenceChecker:xc serverURL:nil];
216  STAssertNil(t, nil);
217
218  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
219  t = [KSTicket ticketWithProductID:nil version:nil
220              existenceChecker:nil serverURL:url];
221  STAssertNil(t, nil);
222
223  t = [KSTicket ticketWithProductID:@"hi" version:@"hi"
224              existenceChecker:xc serverURL:url];
225  STAssertNotNil(t, nil);
226}
227
228- (void)testTTToken {
229  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
230
231  // basics: make sure tttoken works
232  KSTicket *t = [KSTicket ticketWithProductID:@"{GUID}"
233                                      version:@"1.1"
234                             existenceChecker:[KSExistenceChecker falseChecker]
235                                    serverURL:url
236                           trustedTesterToken:@"tttoken"];
237  STAssertNotNil(t, nil);
238  STAssertEqualObjects([t trustedTesterToken], @"tttoken", nil);
239
240  // basics: make sure different tttoken works
241  KSTicket *u = [KSTicket ticketWithProductID:@"{GUID}"
242                                      version:@"1.1"
243                             existenceChecker:[KSExistenceChecker falseChecker]
244                                    serverURL:url
245                           trustedTesterToken:@"hi_mark"];
246  STAssertNotNil(u, nil);
247  STAssertEqualObjects([u trustedTesterToken], @"hi_mark", nil);
248
249  // hash not changed by tttoken
250  STAssertEquals([t hash], [u hash], nil);
251
252  // Same as 'u' but different version; make sure tttoken doens't mess
253  // up equality
254  KSTicket *v = [KSTicket ticketWithProductID:@"{GUID}"
255                                      version:@"1.2"
256                             existenceChecker:[KSExistenceChecker falseChecker]
257                                    serverURL:url
258                           trustedTesterToken:@"hi_mark"];
259  STAssertNotNil(v, nil);
260  STAssertFalse([u isEqual:v], nil);
261
262  STAssertTrue([[v description] length] > 1, nil);
263  STAssertTrue([[v description] rangeOfString:@"hi_mark"].length > 0,
264               nil);
265}
266
267- (void)testCreateDate {
268  KSTicket *t = nil;
269  KSExistenceChecker *xc = [KSExistenceChecker trueChecker];
270  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
271  NSDate *pastDate = [NSDate dateWithTimeIntervalSinceNow:-1234567.8];
272  t = [KSTicket ticketWithProductID:@"{GUID}"
273                            version:@"1.3"
274                   existenceChecker:xc
275                          serverURL:url
276                 trustedTesterToken:nil
277                       creationDate:pastDate];
278  STAssertEqualObjects(pastDate, [t creationDate], nil);
279
280  t = [KSTicket ticketWithProductID:@"{GUID}"
281                            version:@"1.3"
282                   existenceChecker:xc
283                          serverURL:url
284                 trustedTesterToken:nil
285                       creationDate:nil];
286  NSDate *now = [NSDate date];
287  // We should get "now".  Allow a minute slop to check.
288  STAssertTrue(fabs([now timeIntervalSinceDate:[t creationDate]]) < 60, nil);
289}
290
291- (void)testTag {
292  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
293
294  // basics: make sure tag works
295  KSTicket *t = [KSTicket ticketWithProductID:@"{GUID}"
296                                      version:@"1.1"
297                             existenceChecker:[KSExistenceChecker falseChecker]
298                                    serverURL:url
299                           trustedTesterToken:nil
300                                 creationDate:nil
301                                          tag:@"hi_greg"];
302  STAssertNotNil(t, nil);
303  STAssertEqualObjects([t tag], @"hi_greg", nil);
304
305  // basics: make sure different tag works
306  KSTicket *u = [KSTicket ticketWithProductID:@"{GUID}"
307                                      version:@"1.1"
308                             existenceChecker:[KSExistenceChecker falseChecker]
309                                    serverURL:url
310                           trustedTesterToken:nil
311                                 creationDate:nil
312                                          tag:@"snork"];
313  STAssertNotNil(u, nil);
314  STAssertEqualObjects([u tag], @"snork", nil);
315
316  // hash not changed by tag
317  STAssertEquals([t hash], [u hash], nil);
318
319  // Same as 'u' but different version; make sure tag doens't mess
320  // up equality
321  KSTicket *v = [KSTicket ticketWithProductID:@"{GUID}"
322                                      version:@"1.2"
323                             existenceChecker:[KSExistenceChecker falseChecker]
324                                    serverURL:url
325                           trustedTesterToken:nil
326                                 creationDate:nil
327                                          tag:@"hi_mom"];
328  STAssertNotNil(v, nil);
329  STAssertFalse([u isEqual:v], nil);
330
331  STAssertTrue([[v description] length] > 1, nil);
332  STAssertTrue([[v description] rangeOfString:@"hi_mom"].length > 0,
333               nil);
334}
335
336- (void)testTicketParameterCreation {
337  KSTicket *t;
338  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
339  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
340  NSDictionary *args;
341
342  // Make sure insufficient data results in no ticket.
343  t = [KSTicket ticketWithParameters:nil];
344  STAssertNil(t, nil);
345  t = [KSTicket ticketWithParameters:[NSDictionary dictionary]];
346  STAssertNil(t, nil);
347  t = [[[KSTicket alloc] initWithParameters:nil] autorelease];
348  STAssertNil(t, nil);
349  t = [[[KSTicket alloc] initWithParameters:[NSDictionary dictionary]]
350        autorelease];
351  STAssertNil(t, nil);
352
353  // -testNilArgs covers the combination of required args.  Make sure
354  // that a sampling of missing required args result in a nil object.
355  args = [NSDictionary dictionaryWithObjectsAndKeys:
356                       @"com.hassel.hoff", KSTicketProductIDKey,
357                       @"3.14.15", KSTicketVersionKey,
358                       nil];
359  t = [KSTicket ticketWithParameters:args];
360  STAssertNil(t, nil);
361
362  args = [NSDictionary dictionaryWithObjectsAndKeys:
363                       xc, KSTicketExistenceCheckerKey,
364                       url, KSTicketServerURLKey,
365                       nil];
366  t = [KSTicket ticketWithParameters:args];
367  STAssertNil(t, nil);
368
369  args = [NSDictionary dictionaryWithObjectsAndKeys:
370                       @"bubbles", KSTicketTrustedTesterTokenKey,
371                       [NSDate date], KSTicketCreationDateKey,
372                       @"tag", KSTicketTagKey,
373                       @"path", KSTicketTagPathKey,
374                       @"baby", KSTicketTagKeyKey,
375                       @"brandpath", KSTicketBrandPathKey,
376                       @"brandkey", KSTicketBrandKeyKey,
377                       nil];
378  t = [KSTicket ticketWithParameters:args];
379  STAssertNil(t, nil);
380
381  // Make sure everything set makes it through.
382  NSDate *now = [NSDate date];
383  args = [NSDictionary dictionaryWithObjectsAndKeys:
384                       @"com.hassel.hoff", KSTicketProductIDKey,
385                       @"3.14.15", KSTicketVersionKey,
386                       xc, KSTicketExistenceCheckerKey,
387                       url, KSTicketServerURLKey,
388                       @"ttt", KSTicketTrustedTesterTokenKey,
389                       now, KSTicketCreationDateKey,
390                       @"tagge", KSTicketTagKey,
391                       @"pathe", KSTicketTagPathKey,
392                       @"taggekeye", KSTicketTagKeyKey,
393                       @"brandpathe", KSTicketBrandPathKey,
394                       @"brandekeye", KSTicketBrandKeyKey,
395                       nil];
396  t = [KSTicket ticketWithParameters:args];
397  STAssertNotNil(t, nil);
398  STAssertEqualObjects([t productID], @"com.hassel.hoff", nil);
399  STAssertEqualObjects([t version], @"3.14.15", nil);
400  STAssertEqualObjects([t existenceChecker], xc, nil);
401  STAssertEqualObjects([t serverURL], url, nil);
402  STAssertEqualObjects([t trustedTesterToken], @"ttt", nil);
403  STAssertEqualObjects([t creationDate], now, nil);
404  STAssertEqualObjects([t tag], @"tagge", nil);
405  STAssertEqualObjects([t tagPath], @"pathe", nil);
406  STAssertEqualObjects([t tagKey], @"taggekeye", nil);
407  STAssertEqualObjects([t brandPath], @"brandpathe", nil);
408  STAssertEqualObjects([t brandKey], @"brandekeye", nil);
409}
410
411- (void)testTagPathAccessors {
412  KSTicket *t;
413  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
414  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
415  NSDictionary *args;
416
417  args = [NSDictionary dictionaryWithObjectsAndKeys:
418                       @"com.hassel.hoff", KSTicketProductIDKey,
419                       @"3.14.15", KSTicketVersionKey,
420                       xc, KSTicketExistenceCheckerKey,
421                       url, KSTicketServerURLKey,
422                       @"path", KSTicketTagPathKey,
423                       @"key", KSTicketTagKeyKey,
424                       nil];
425  t = [KSTicket ticketWithParameters:args];
426  STAssertEqualObjects([t tagPath], @"path", nil);
427  STAssertEqualObjects([t tagKey], @"key", nil);
428}
429
430- (void)testBrandPathAccessors {
431  KSTicket *t;
432  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
433  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
434  NSDictionary *args;
435
436  args = [NSDictionary dictionaryWithObjectsAndKeys:
437                       @"com.hassel.hoff", KSTicketProductIDKey,
438                       @"3.14.15", KSTicketVersionKey,
439                       xc, KSTicketExistenceCheckerKey,
440                       url, KSTicketServerURLKey,
441                       @"tagpath", KSTicketTagPathKey,
442                       @"tagkey", KSTicketTagKeyKey,
443                       @"brandpath", KSTicketBrandPathKey,
444                       @"brandkey", KSTicketBrandKeyKey,
445                       nil];
446  t = [KSTicket ticketWithParameters:args];
447  STAssertEqualObjects([t brandPath], @"brandpath", nil);
448  STAssertEqualObjects([t brandKey], @"brandkey", nil);
449}
450
451- (KSTicket *)ticketWithTagPath:(NSString *)tagPath
452                         tagKey:(NSString *)tagKey
453                            tag:(NSString *)tag
454                      brandPath:(NSString *)brandPath
455                       brandKey:(NSString *)brandKey
456                    versionPath:(NSString *)versionPath
457                     versionKey:(NSString *)versionKey
458                        version:(NSString *)version {
459  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
460  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
461
462  NSMutableDictionary *args
463    = [NSMutableDictionary dictionaryWithObjectsAndKeys:
464                           @"com.hassel.hoff", KSTicketProductIDKey,
465                           xc, KSTicketExistenceCheckerKey,
466                           url, KSTicketServerURLKey,
467                           @"3.14.15", KSTicketVersionKey,
468                           nil];
469  if (tagPath) [args setObject:tagPath forKey:KSTicketTagPathKey];
470  if (tagKey) [args setObject:tagKey forKey: KSTicketTagKeyKey];
471  if (tag) [args setObject:tag forKey:KSTicketTagKey];
472  if (brandPath) [args setObject:brandPath forKey:KSTicketBrandPathKey];
473  if (brandKey) [args setObject:brandKey forKey:KSTicketBrandKeyKey];
474  if (versionPath) [args setObject:versionPath forKey:KSTicketVersionPathKey];
475  if (versionKey) [args setObject:versionKey forKey:KSTicketVersionKeyKey];
476  if (version) [args setObject:version forKey:KSTicketVersionKey];
477
478  KSTicket *t = [KSTicket ticketWithParameters:args];
479  STAssertNotNil(t, nil);
480
481  return t;
482}
483
484- (KSTicket *)ticketWithTagPath:(NSString *)tagPath
485                         tagKey:(NSString *)tagKey
486                            tag:(NSString *)tag {
487  return [self ticketWithTagPath:tagPath
488                          tagKey:tagKey
489                             tag:tag
490                       brandPath:@"noBrandPath"
491                        brandKey:@"noBrandKey"
492                     versionPath:nil
493                      versionKey:nil
494                         version:nil];
495}
496
497- (KSTicket *)ticketWithVersionPath:(NSString *)versionPath
498                         versionKey:(NSString *)versionKey
499                            version:(NSString *)version {
500  return [self ticketWithTagPath:nil
501                          tagKey:nil
502                             tag:nil
503                       brandPath:nil
504                        brandKey:nil
505                     versionPath:versionPath
506                      versionKey:versionKey
507                         version:version];
508}
509
510- (KSTicket *)ticketWithBrandPath:(NSString *)brandPath
511                         brandKey:(NSString *)brandKey {
512  return [self ticketWithTagPath:nil
513                          tagKey:nil
514                             tag:nil
515                       brandPath:brandPath
516                        brandKey:brandKey
517                     versionPath:nil
518                      versionKey:nil
519                         version:nil];
520}
521
522- (KSTicket *)ticketForResourceName:(NSString *)name
523                               type:(NSString *)type
524                             tagKey:(NSString *)tagKey
525                                tag:(NSString *)tag {
526  NSBundle *mainBundle = [NSBundle bundleForClass:[self class]];
527  NSString *path = [mainBundle pathForResource:name ofType:type];
528  STAssertNotNil(path, nil);
529
530  return [self ticketWithTagPath:path
531                          tagKey:tagKey
532                             tag:tag
533                       brandPath:nil
534                        brandKey:nil
535                     versionPath:nil
536                      versionKey:nil
537                         version:nil];
538}
539
540- (KSTicket *)ticketForResourceName:(NSString *)name
541                               type:(NSString *)type
542                         versionKey:(NSString *)versionKey
543                            version:(NSString *)version {
544  NSBundle *mainBundle = [NSBundle bundleForClass:[self class]];
545  NSString *path = [mainBundle pathForResource:name ofType:type];
546  STAssertNotNil(path, nil);
547
548  return [self ticketWithTagPath:nil
549                          tagKey:nil
550                             tag:nil
551                       brandPath:nil
552                        brandKey:nil
553                     versionPath:path
554                      versionKey:versionKey
555                         version:version];
556}
557
558- (void)testDetermineTagPath {
559  KSTicket *t;
560  NSString *tag;
561
562  // Legitimate file tests - these are all readable plist files, so
563  // the -determinedTag should either be a value from the file, or nil.
564
565  t = [self ticketForResourceName:@"TagPath-success"
566                             type:@"plist"
567                           tagKey:@"Awesomeness"
568                              tag:@"blargle"];
569  tag = [t determineTag];
570  STAssertEqualObjects(tag, @"CowsGoMoo", nil);
571
572  // Binary-format plist should work the same.
573  t = [self ticketForResourceName:@"TagPath-binary-success"
574                             type:@"plist"
575                           tagKey:@"Awesomeness"
576                              tag:@"blargle"];
577  tag = [t determineTag];
578  STAssertEqualObjects(tag, @"CowsGoMoo", nil);
579
580  // This file does not have the Awesomeness2 key, so should evaulate to nil.
581  t = [self ticketForResourceName:@"TagPath-success"
582                             type:@"plist"
583                           tagKey:@"Awesomeness2"
584                              tag:@"blargle"];
585  tag = [t determineTag];
586  STAssertNil(tag, nil);
587
588  // This tag is huge, > 1K, so should evaluate to nil.
589  t = [self ticketForResourceName:@"TagPath-success"
590                             type:@"plist"
591                           tagKey:@"Hugeitude"
592                              tag:@"blargle"];
593  tag = [t determineTag];
594  STAssertNil(tag, nil);
595
596  // This tag is empty, so the returned tag should be nil.
597  // Empty string == no tag.
598  t = [self ticketForResourceName:@"TagPath-success"
599                             type:@"plist"
600                           tagKey:@"Groovyness"
601                              tag:@"blargle"];
602  tag = [t determineTag];
603  STAssertNil(tag, nil);
604
605  // ServerFailure.plist has an array under "Rules" rather than a
606  // string.  Since the file exists and is a legit plist file, the tag
607  // should not be returned.  The determined tag will be nil because
608  // the key doesn't point to a legitimate value.
609  t = [self ticketForResourceName:@"ServerFailure"
610                             type:@"plist"
611                           tagKey:@"Rules"
612                              tag:@"blargle"];
613  tag = [t determineTag];
614  STAssertNil(tag, nil);
615
616
617  // Invalid files, so -determineTag should use the existing tag value.
618
619  // No file there.
620  t = [self ticketWithTagPath:@"/flongwaffle"
621                       tagKey:@"notthere"
622                          tag:@"blargle"];
623  tag = [t determineTag];
624  STAssertEqualObjects(tag, @"blargle", nil);
625
626
627  // This file is too big (10 meg on leopard, 18 on Sneaux Leopard),
628  // and should be rejected.
629  t = [self ticketWithTagPath:@"/mach_kernel"
630                       tagKey:@"notthere"
631                          tag:@"blargle"];
632  tag = [t determineTag];
633  STAssertEqualObjects(tag, @"blargle", nil);
634
635  // This file is malformed (it's binary, but not even a plist), and
636  // should be rejected without anybody crashing or getting hassled.
637  // The tag should evaluate to "blargle" since the file is bad.
638  t = [self ticketForResourceName:@"TagPath-malformed-failure"
639                             type:@"plist"
640                           tagKey:@"Awesomeness"
641                              tag:@"blargle"];
642  tag = [t determineTag];
643  STAssertEqualObjects(tag, @"blargle", nil);
644
645  t = [self ticketForResourceName:@"ServerFailure"
646                             type:@"plist"
647                           tagKey:@"Rules"
648                              tag:nil];
649  tag = [t determineTag];
650  STAssertNil(tag, nil);
651
652  t = [self ticketWithTagPath:@"/flongwaffle"
653                       tagKey:@"notthere"
654                          tag:nil];
655  tag = [t determineTag];
656  STAssertNil(tag, nil);
657
658  t = [self ticketForResourceName:@"TagPath-malformed-failure"
659                             type:@"plist"
660                           tagKey:@"Awesomeness"
661                              tag:nil];
662  tag = [t determineTag];
663  STAssertNil(tag, nil);
664}
665
666- (void)testTagPathHomeExpansion {
667  // Copy a file to $HOME and make sure tilde expansion works.
668  NSBundle *mainBundle = [NSBundle bundleForClass:[self class]];
669  NSString *path = [mainBundle pathForResource:@"TagPath-success"
670                                        ofType:@"plist"];
671  STAssertNotNil(path, nil);
672  NSFileManager *fm = [NSFileManager defaultManager];
673  NSString *destPath =
674    [NSHomeDirectory()stringByAppendingPathComponent:@"TagPath-success.plist"];
675  [fm copyPath:path toPath:destPath handler:nil];
676
677  if (![fm fileExistsAtPath:destPath]) {
678    // Don't know if Pulse will choke on this.  If so, make a not of it and
679    // then bail out.
680    NSLog(@"Could not copy file to home directory.");
681    return;
682  }
683
684  KSTicket *t = [self ticketWithTagPath:@"~/TagPath-success.plist"
685                                 tagKey:@"Awesomeness"
686                                    tag:@"blargle"];
687  NSString *tag = [t determineTag];
688  STAssertEqualObjects(tag, @"CowsGoMoo", nil);
689
690  [fm removeFileAtPath:destPath handler:nil];
691}
692
693- (KSTicket *)ticketForResourceName:(NSString *)name
694                               type:(NSString *)type
695                           brandKey:(NSString *)brandKey {
696  NSBundle *mainBundle = [NSBundle bundleForClass:[self class]];
697  NSString *path = [mainBundle pathForResource:name ofType:type];
698  STAssertNotNil(path, nil);
699
700  return [self ticketWithTagPath:nil
701                          tagKey:nil
702                             tag:nil
703                       brandPath:path
704                        brandKey:brandKey
705                     versionPath:nil
706                      versionKey:nil
707                         version:nil];
708}
709
710- (void)testDetermineBrandPath {
711  KSTicket *t;
712  NSString *brand;
713
714  // Legitimate file tests - these are all readable plist files, so
715  // the -determinedBrand should either be a value from the file, or nil.
716  // Use the test files from TagPath, since tags and brands use similar
717  // implementations.
718
719  t = [self ticketForResourceName:@"TagPath-success"
720                             type:@"plist"
721                         brandKey:@"Awesomeness"];
722  brand = [t determineBrand];
723  STAssertEqualObjects(brand, @"CowsGoMoo", nil);
724
725  // Binary-format plist should work the same.
726  t = [self ticketForResourceName:@"TagPath-binary-success"
727                             type:@"plist"
728                         brandKey:@"Awesomeness"];
729  brand = [t determineBrand];
730  STAssertEqualObjects(brand, @"CowsGoMoo", nil);
731
732  // This file does not have the Awesomeness2 key, so should evaulate to nil.
733  t = [self ticketForResourceName:@"TagPath-success"
734                             type:@"plist"
735                         brandKey:@"Awesomeness2"];
736  brand = [t determineBrand];
737  STAssertNil(brand, nil);
738
739  // This brand is huge, > 1K, so should evaluate to nil.
740  t = [self ticketForResourceName:@"TagPath-success"
741                             type:@"plist"
742                         brandKey:@"Hugeitude"];
743  brand = [t determineBrand];
744  STAssertNil(brand, nil);
745
746  // This brand is empty, so the returned brand should be nil.
747  // Empty string == no brand.
748  t = [self ticketForResourceName:@"TagPath-success"
749                             type:@"plist"
750                         brandKey:@"Groovyness"];
751  brand = [t determineBrand];
752  STAssertNil(brand, nil);
753
754  // ServerFailure.plist has an array under "Rules" rather than a
755  // string.  Since the file exists and is a legit plist file, the brand
756  // should not be returned.  The determined brand will be nil because
757  // the key doesn't point to a legitimate value.
758  t = [self ticketForResourceName:@"ServerFailure"
759                             type:@"plist"
760                         brandKey:@"Rules"];
761  brand = [t determineBrand];
762  STAssertNil(brand, nil);
763
764
765  // Invalid files, so -determineBrand should return nil
766
767  // No file there.
768  t = [self ticketWithBrandPath:@"/flongwaffle"
769                       brandKey:@"notthere"];
770  brand = [t determineBrand];
771  STAssertNil(brand, nil);
772
773
774  // This file is too big (10 meg on leopard, 18 on Sneaux Leopard),
775  // and should be rejected.
776  t = [self ticketWithBrandPath:@"/mach_kernel"
777                       brandKey:@"notthere"];
778  brand = [t determineBrand];
779  STAssertNil(brand, nil);
780
781  // This file is malformed (it's binary, but not even a plist), and
782  // should be rejected without anybody crashing or getting hassled.
783  // The brand should evaluate to "blargle" since the file is bad.
784  t = [self ticketForResourceName:@"TagPath-malformed-failure"
785                             type:@"plist"
786                         brandKey:@"Awesomeness"];
787  brand = [t determineBrand];
788  STAssertNil(brand, nil);
789}
790
791- (void)testDetermineVersionPath {
792  KSTicket *t;
793  NSString *version;
794
795  // Legitimate file tests - these are all readable plist files, so
796  // the -determinedVersion should either be a value from the file, or nil.
797
798  t = [self ticketForResourceName:@"TagPath-success"
799                             type:@"plist"
800                       versionKey:@"CFBundleShortVersionString"
801                          version:@"blargle"];
802  version = [t determineVersion];
803  STAssertEqualObjects(version, @"4.0.249.30", nil);
804
805  // Binary-format plist should work the same.
806  t = [self ticketForResourceName:@"TagPath-binary-success"
807                             type:@"plist"
808                       versionKey:@"CFBundleShortVersionString"
809                          version:@"blargle"];
810  version = [t determineVersion];
811  STAssertEqualObjects(version, @"4.0.249.30", nil);
812
813  // This file does not have the Awesomeness2 key, so should evaulate to nil.
814  t = [self ticketForResourceName:@"TagPath-success"
815                             type:@"plist"
816                       versionKey:@"Awesomeness2"
817                          version:@"blargle"];
818  version = [t determineVersion];
819  STAssertEqualObjects(version, @"blargle", nil);
820
821  // This version is huge, > 1K, so should evaluate to nil.
822  t = [self ticketForResourceName:@"TagPath-success"
823                             type:@"plist"
824                       versionKey:@"Hugeitude"
825                          version:@"blargle"];
826  version = [t determineVersion];
827  STAssertEqualObjects(version, @"blargle", nil);
828
829  // This version is empty, so the returned version should be nil.
830  // Empty string == no version.
831  t = [self ticketForResourceName:@"TagPath-success"
832                             type:@"plist"
833                       versionKey:@"Groovyness"
834                          version:@"blargle"];
835  version = [t determineVersion];
836  STAssertEqualObjects(version, @"blargle", nil);
837
838  // ServerFailure.plist has an array under "Rules" rather than a
839  // string.  Since the file exists and is a legit plist file, the version
840  // should not be returned.  The determined version will be nil because
841  // the key doesn't point to a legitimate value.
842  t = [self ticketForResourceName:@"ServerFailure"
843                             type:@"plist"
844                       versionKey:@"Rules"
845                          version:@"blargle"];
846  version = [t determineVersion];
847  STAssertEqualObjects(version, @"blargle", nil);
848
849
850  // Invalid files, so -determineVersion should use the existing version value.
851
852  // No file there.
853  t = [self ticketWithVersionPath:@"/flongwaffle"
854                       versionKey:@"notthere"
855                          version:@"1.2.3.4"];
856  version = [t determineVersion];
857  STAssertEqualObjects(version, @"1.2.3.4", nil);
858
859
860  // This file is too big (10 meg on leopard, 18 on Sneaux Leopard),
861  // and should be rejected.
862  t = [self ticketWithVersionPath:@"/mach_kernel"
863                       versionKey:@"notthere"
864                          version:@"1.2.3.4"];
865  version = [t determineVersion];
866  STAssertEqualObjects(version, @"1.2.3.4", nil);
867
868  // This file is malformed (it's binary, but not even a plist), and
869  // should be rejected without anybody crashing or getting hassled.
870  // The version should evaluate to "blargle" since the file is bad.
871  t = [self ticketForResourceName:@"TagPath-malformed-failure"
872                             type:@"plist"
873                       versionKey:@"CFBundleShortVersionString"
874                          version:@"1.2.3.4"];
875  version = [t determineVersion];
876  STAssertEqualObjects(version, @"1.2.3.4", nil);
877
878  t = [self ticketForResourceName:@"ServerFailure"
879                             type:@"plist"
880                       versionKey:@"Rules"
881                          version:@"1.2.3.4"];
882  version = [t determineVersion];
883  STAssertEqualObjects(version, @"1.2.3.4", nil);
884
885  t = [self ticketWithVersionPath:@"/flongwaffle"
886                       versionKey:@"notthere"
887                          version:@"1.2.3.4"];
888  version = [t determineVersion];
889  STAssertEqualObjects(version, @"1.2.3.4", nil);
890
891  t = [self ticketForResourceName:@"TagPath-malformed-failure"
892                             type:@"plist"
893                       versionKey:@"Awesomeness"
894                          version:@"1.2.3.4"];
895  version = [t determineVersion];
896  STAssertEqualObjects(version, @"1.2.3.4", nil);
897}
898
899const char *templateDescription = "<KSTicket:%p\n"
900"	productID=com.hassel.hoff\n"
901"	version=3.14.15\n"
902"	xc=<KSPathExistenceChecker:%p path=/attack/of/the/clowns>\n"
903"	url=http://www.google.com\n"
904"	creationDate=%@\n"
905"	trustedTesterToken=monkeys\n"
906"	tag=tag\n"
907"	tagPath=path\n"
908"	tagKey=key\n"
909"	brandPath=brandpath\n"
910"	brandKey=brandkey\n"
911"	versionPath=versionpath\n"
912"	versionKey=versionkey\n"
913">";
914
915- (void)testDescriptionStability {
916  // The ticket's -description is easily parsable.  Keep the format mostly
917  // stable for clients that do so.
918  KSExistenceChecker *xc =
919    [KSPathExistenceChecker checkerWithPath:@"/attack/of/the/clowns"];
920  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
921  NSDate *creationDate = [NSDate date];
922  NSDictionary *args =
923    [NSDictionary dictionaryWithObjectsAndKeys:
924                  @"com.hassel.hoff", KSTicketProductIDKey,
925                  @"3.14.15", KSTicketVersionKey,
926                  xc, KSTicketExistenceCheckerKey,
927                  url, KSTicketServerURLKey,
928                  @"monkeys", KSTicketTrustedTesterTokenKey,
929                  creationDate, KSTicketCreationDateKey,
930                  @"tag", KSTicketTagKey,
931                  @"path", KSTicketTagPathKey,
932                  @"key", KSTicketTagKeyKey,
933                  @"brandpath", KSTicketBrandPathKey,
934                  @"brandkey", KSTicketBrandKeyKey,
935                  @"versionpath", KSTicketVersionPathKey,
936                  @"versionkey", KSTicketVersionKeyKey,
937                  nil];
938  KSTicket *ticket = [KSTicket ticketWithParameters:args];
939  NSString *description = [ticket description];
940  NSString *format = [NSString stringWithUTF8String:templateDescription];
941  // Date description changes based on time zone.  Known clients depending
942  // on the description are not using the creation date.  The ticket and
943  // xc addresses are also not stable, so plug those into the template too.
944  NSString *expected = [NSString stringWithFormat:format,
945                                 ticket, xc,creationDate];
946  STAssertEqualObjects(description, expected, nil);
947}
948
949@end