PageRenderTime 158ms CodeModel.GetById 8ms app.highlight 143ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 748 lines | 614 code | 62 blank | 72 comment | 1 complexity | b116d3a5e7fce694421bacf430b2c2c6 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 "KSPlistServer.h"
 17#import "KSUpdateInfo.h"
 18#import "KSTicket.h"
 19#import "KSExistenceChecker.h"
 20
 21
 22@interface KSPlistServerTest : SenTestCase {
 23 @private
 24  KSPlistServer *server_;
 25  NSArray *tickets_;
 26}
 27@end
 28
 29
 30//
 31// Static plist strings
 32//
 33// The easiest way to get these plist strings in here is to write them in a 
 34// regular file, then paste the output of the following command:
 35//
 36//   $ cat rule.plist | sed 's/"/\\"/g' | sed -E 's/^(.*)$/@"\1"/g'
 37//
 38
 39//
 40// ====== 1 Rule ======
 41//
 42
 43static NSString *const kPlist1Rule_1 = 
 44@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 45@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
 46@"<plist version=\"1.0\">"
 47@"<dict>"
 48@"  <key>Rules</key>"
 49@"  <array>"
 50@"    <dict>"
 51@"      <key>ProductID</key>"
 52@"      <string>com.google.Foo</string>"
 53@"      <key>Predicate</key>"
 54@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
 55@"      <key>Codebase</key>"
 56@"      <string>https://www.google.com/engine/</string>"
 57@"      <key>Hash</key>"
 58@"      <string>somehash=</string>"
 59@"      <key>Size</key>"
 60@"      <string>123456</string>"
 61@"    </dict>"
 62@"  </array>"
 63@"</dict>"
 64@"</plist>"
 65;
 66
 67static NSString *const kPlist1Rule_2 = 
 68@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 69@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
 70@"<plist version=\"1.0\">"
 71@"<dict>"
 72@"  <key>Rules</key>"
 73@"  <array>"
 74@"    <dict>"
 75@"      <key>ProductID</key>"
 76@"      <string>com.google.Foo</string>"
 77@"      <key>Predicate</key>"
 78@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
 79@"      <key>Codebase</key>"
 80@"      <string>https://www.google.com/engine/</string>"
 81@"      <key>Hash</key>"
 82@"      <string>somehash=</string>"
 83@"      <key>Size</key>"
 84@"      <string>123456</string>"
 85@"    </dict>"
 86@"  </array>"
 87@"</dict>"
 88@"</plist>"
 89;
 90
 91static NSString *const kPlist1Rule_3 = 
 92@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 93@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
 94@"<plist version=\"1.0\">"
 95@"<dict>"
 96@"  <key>Rules</key>"
 97@"  <array>"
 98@"    <dict>"
 99@"      <key>ProductID</key>"
100@"      <string>com.google.Foo</string>"
101@"      <key>Predicate</key>"
102@"      <string>SystemVersion.ProductVersion beginswith '10.' AND BLAHBLAHBLAH </string>"
103@"      <key>Codebase</key>"
104@"      <string>https://www.google.com/engine/</string>"
105@"      <key>Hash</key>"
106@"      <string>somehash=</string>"
107@"      <key>Size</key>"
108@"      <string>123456</string>"
109@"    </dict>"
110@"  </array>"
111@"</dict>"
112@"</plist>"
113;
114
115static NSString *const kPlist1Rule_4 = 
116@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
117@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
118@"<plist version=\"1.0\">"
119@"<dict>"
120@"  <key>Rules</key>"
121@"  <array>"
122@"    <dict>"
123@"      <key>ProductID</key>"
124@"      <string>com.google.DoesNotExist</string>"
125@"      <key>Predicate</key>"
126@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
127@"      <key>Codebase</key>"
128@"      <string>https://www.google.com/engine/</string>"
129@"      <key>Hash</key>"
130@"      <string>somehash=</string>"
131@"      <key>Size</key>"
132@"      <string>123456</string>"
133@"    </dict>"
134@"  </array>"
135@"</dict>"
136@"</plist>"
137;
138
139static NSString *const kPlist1Rule_5 = 
140@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
141@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
142@"<plist version=\"1.0\">"
143@"<dict>"
144@"  <key>Rules</key>"
145@"  <array>"
146@"    <dict>"
147@"      <key>ProductID</key>"
148@"      <string>com.google.Foo</string>"
149@"      <key>Predicate</key>"
150@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
151@"      <key>Codebase</key>"
152@"      <string>https://www.google.com/engine/</string>"
153@"      <key>Hash</key>"
154@"      <string>somehash=</string>"
155@"      <key>Size</key>"
156@"      <string>123456</string>"
157// ... truncated XML
158;
159
160static NSString *const kPlist1Rule_6 = 
161@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
162@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
163@"<plist version=\"1.0\">"
164@"<dict>"
165@"  <key>Rules</key>"
166@"  <array>"
167@"    <dict>"
168@"      <key>ProductID</key>"
169@"      <string>com.google.Foo</string>"
170@"      <key>Predicate</key>"
171@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
172@"      <key>Codebase</key>"
173@"      <string>https://www.google.com/engine/</string>"
174// Missing hash
175@"      <key>Size</key>"
176@"      <string>123456</string>"
177@"    </dict>"
178@"  </array>"
179@"</dict>"
180@"</plist>"
181;
182
183static NSString *const kPlist1Rule_7 = 
184@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
185@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
186@"<plist version=\"1.0\">"
187@"<dict>"
188@"  <key>Rules</key>"
189@"  <array>"
190@"    <dict>"
191@"      <key>ProductID</key>"
192@"      <string>com.google.Foo</string>"
193@"      <key>Predicate</key>"
194@"      <string>Ticket.version == '1.1'</string>"
195@"      <key>Codebase</key>"
196@"      <string>https://www.google.com/engine/</string>"
197@"      <key>Hash</key>"
198@"      <string>somehash=</string>"
199@"      <key>Size</key>"
200@"      <integer>123456</integer>"
201@"    </dict>"
202@"  </array>"
203@"</dict>"
204@"</plist>"
205;
206
207static NSString *const kPlist1Rule_8 = 
208@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
209@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
210@"<plist version=\"1.0\">"
211@"<dict>"
212@"  <key>Rules</key>"
213@"  <array>"
214// No rules
215@"  </array>"
216@"</dict>"
217@"</plist>"
218;
219
220static NSString *const kPlist1Rule_9 = 
221@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
222@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
223@"<plist version=\"1.0\">"
224@"<dict>"
225// Empty plist
226@"</dict>"
227@"</plist>"
228;
229
230static NSString *const kPlist1Rule_10 = 
231@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
232@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
233@"<plist version=\"1.0\">"
234@"<dict>"
235@"  <key>Rules</key>"
236@"  <array>"
237@"    <dict>"
238// Missing ProductID
239@"      <key>Predicate</key>"
240@"      <string>Ticket.version == '1.1'</string>"
241@"      <key>Codebase</key>"
242@"      <string>https://www.google.com/engine/</string>"
243@"      <key>Hash</key>"
244@"      <string>somehash=</string>"
245@"      <key>Size</key>"
246@"      <string>123456</string>"
247@"    </dict>"
248@"  </array>"
249@"</dict>"
250@"</plist>"
251;
252
253static NSString *const kPlist1Rule_11 = 
254@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
255@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
256@"<plist version=\"1.0\">"
257@"<dict>"
258@"  <key>Rules</key>"
259@"  <array>"
260@"    <dict>"
261@"      <key>ProductID</key>"
262@"      <string>com.google.Foo</string>"
263@"      <key>Predicate</key>"
264@"      <string>Foo.version == '1.1'</string>"
265@"      <key>Codebase</key>"
266@"      <string>https://www.google.com/engine/</string>"
267@"      <key>Hash</key>"
268@"      <string>somehash=</string>"
269@"      <key>Size</key>"
270@"      <string>123456</string>"
271@"    </dict>"
272@"  </array>"
273@"</dict>"
274@"</plist>"
275;
276
277//
278// ====== Multiple Rules ======
279//
280
281static NSString *const kPlistNRules_1 = 
282@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
283@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
284@"<plist version=\"1.0\">"
285@"<dict>"
286@"  <key>Rules</key>"
287@"  <array>"
288@"    <dict>"
289@"      <key>ProductID</key>"
290@"      <string>com.google.Foo</string>"
291@"      <key>Predicate</key>"
292@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
293@"      <key>Codebase</key>"
294@"      <string>https://www.google.com/engine/foo</string>"
295@"      <key>Hash</key>"
296@"      <string>foohash=</string>"
297@"      <key>Size</key>"
298@"      <string>123456</string>"
299@"    </dict>"
300@"    <dict>"
301@"      <key>ProductID</key>"
302@"      <string>com.google.Bar</string>"
303@"      <key>Predicate</key>"
304@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
305@"      <key>Codebase</key>"
306@"      <string>https://www.google.com/engine/bar</string>"
307@"      <key>Hash</key>"
308@"      <string>barhash=</string>"
309@"      <key>Size</key>"
310@"      <string>123456</string>"
311@"    </dict>"
312@"    <dict>"
313@"      <key>ProductID</key>"
314@"      <string>com.google.Baz</string>"
315@"      <key>Predicate</key>"
316@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
317@"      <key>Codebase</key>"
318@"      <string>https://www.google.com/engine/baz</string>"
319@"      <key>Hash</key>"
320@"      <string>bazhash=</string>"
321@"      <key>Size</key>"
322@"      <string>123456</string>"
323@"    </dict>"
324@"  </array>"
325@"</dict>"
326@"</plist>"
327;
328
329static NSString *const kPlistNRules_2 = 
330@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
331@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
332@"<plist version=\"1.0\">"
333@"<dict>"
334@"  <key>Rules</key>"
335@"  <array>"
336@"    <dict>"
337@"      <key>ProductID</key>"
338@"      <string>com.google.Foo</string>"
339@"      <key>Predicate</key>"
340@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
341@"      <key>Codebase</key>"
342@"      <string>https://www.google.com/engine/foo</string>"
343@"      <key>Hash</key>"
344@"      <string>foohash=</string>"
345@"      <key>Size</key>"
346@"      <string>123456</string>"
347@"    </dict>"
348@"    <dict>"
349@"      <key>ProductID</key>"
350@"      <string>com.google.Bar</string>"
351@"      <key>Predicate</key>"
352@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
353@"      <key>Codebase</key>"
354@"      <string>https://www.google.com/engine/bar</string>"
355@"      <key>Hash</key>"
356@"      <string>barhash=</string>"
357@"      <key>Size</key>"
358@"      <string>123456</string>"
359@"    </dict>"
360@"    <dict>"
361@"      <key>ProductID</key>"
362@"      <string>com.google.Baz</string>"
363@"      <key>Predicate</key>"
364@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
365@"      <key>Codebase</key>"
366@"      <string>https://www.google.com/engine/baz</string>"
367@"      <key>Hash</key>"
368@"      <string>bazhash=</string>"
369@"      <key>Size</key>"
370@"      <string>123456</string>"
371@"    </dict>"
372@"  </array>"
373@"</dict>"
374@"</plist>"
375;
376
377static NSString *const kPlistNRules_3 = 
378@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
379@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
380@"<plist version=\"1.0\">"
381@"<dict>"
382@"  <key>Rules</key>"
383@"  <array>"
384@"    <dict>"
385@"      <key>ProductID</key>"
386@"      <string>com.google.Foo</string>"
387@"      <key>Predicate</key>"
388@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
389@"      <key>Codebase</key>"
390@"      <string>https://www.google.com/engine/foo</string>"
391@"      <key>Hash</key>"
392@"      <string>foohash=</string>"
393@"      <key>Size</key>"
394@"      <string>123456</string>"
395@"    </dict>"
396@"    <dict>"
397@"      <key>ProductID</key>"
398@"      <string>com.google.Bar</string>"
399@"      <key>Predicate</key>"
400@"      <string>BLAHHHHHHHHHHH INVALID PREDICATE FORMAT</string>"
401@"      <key>Codebase</key>"
402@"      <string>https://www.google.com/engine/bar</string>"
403@"      <key>Hash</key>"
404@"      <string>barhash=</string>"
405@"      <key>Size</key>"
406@"      <string>123456</string>"
407@"    </dict>"
408@"    <dict>"
409@"      <key>ProductID</key>"
410@"      <string>com.google.Baz</string>"
411@"      <key>Predicate</key>"
412@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.1'</string>"
413@"      <key>Codebase</key>"
414@"      <string>https://www.google.com/engine/baz</string>"
415@"      <key>Hash</key>"
416@"      <string>bazhash=</string>"
417@"      <key>Size</key>"
418@"      <string>123456</string>"
419@"    </dict>"
420@"  </array>"
421@"</dict>"
422@"</plist>"
423;
424
425static NSString *const kPlistNRules_4 = 
426@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
427@"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
428@"<plist version=\"1.0\">"
429@"<dict>"
430@"  <key>Rules</key>"
431@"  <array>"
432@"    <dict>"
433@"      <key>ProductID</key>"
434@"      <string>com.google.Foo</string>"
435@"      <key>Predicate</key>"
436@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
437@"      <key>Codebase</key>"
438@"      <string>https://www.google.com/engine/foo</string>"
439@"      <key>Hash</key>"
440@"      <string>foohash=</string>"
441@"      <key>Size</key>"
442@"      <string>123456</string>"
443@"    </dict>"
444@"    <dict>"
445@"      <key>ProductID</key>"
446@"      <string>com.google.Bar</string>"
447@"      <key>Predicate</key>"
448@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
449@"      <key>Codebase</key>"
450@"      <string>https://www.google.com/engine/bar</string>"
451@"      <key>Hash</key>"
452@"      <string>barhash=</string>"
453@"      <key>Size</key>"
454@"      <string>123456</string>"
455@"    </dict>"
456@"    <dict>"
457@"      <key>ProductID</key>"
458@"      <string>com.google.Baz</string>"
459@"      <key>Predicate</key>"
460@"      <string>SystemVersion.ProductVersion beginswith '10.' AND Ticket.version == '1.0'</string>"
461@"      <key>Codebase</key>"
462@"      <string>https://www.google.com/engine/baz</string>"
463@"      <key>Hash</key>"
464@"      <string>bazhash=</string>"
465@"      <key>Size</key>"
466@"      <string>123456</string>"
467@"    </dict>"
468@"  </array>"
469@"</dict>"
470@"</plist>"
471;
472
473@implementation KSPlistServerTest
474
475// Simple helper function to make sure a KSUpdateInfo contains the correct info
476- (void)assertUpdateInfo:(KSUpdateInfo *)ui
477        matchesProductID:(NSString *)productID
478                    hash:(NSString *)hash
479                    size:(int)size
480                     url:(NSString *)url {
481  STAssertEqualObjects([ui productID], productID, nil);
482  STAssertEqualObjects([ui codeHash], hash, nil);
483  STAssertEqualObjects([ui codeSize], [NSNumber numberWithInt:size], nil);
484  STAssertEqualObjects([ui codebaseURL], [NSURL URLWithString:url], nil);
485}
486
487
488- (void)setUp {
489  NSURL *url = [NSURL URLWithString:@"https://engine.google.com"];
490  server_ = [[KSPlistServer alloc] initWithURL:url];
491  STAssertNotNil(server_, nil);
492  
493  KSExistenceChecker *xc = [KSExistenceChecker falseChecker];
494  KSTicket *t1 = [KSTicket ticketWithProductID:@"com.google.Foo"
495                                       version:@"1.1"
496                              existenceChecker:xc
497                                     serverURL:url];
498  KSTicket *t2 = [KSTicket ticketWithProductID:@"com.google.Bar"
499                                       version:@"1.1"
500                              existenceChecker:xc
501                                     serverURL:url];
502  KSTicket *t3 = [KSTicket ticketWithProductID:@"com.google.Baz"
503                                       version:@"1.1"
504                              existenceChecker:xc
505                                     serverURL:url];
506  
507  tickets_ = [[NSArray alloc] initWithObjects:t1, t2, t3, nil];
508  STAssertNotNil(tickets_, nil);
509}
510
511- (void)tearDown {
512  [server_ release];
513  [tickets_ release];
514}
515
516- (void)testCreation {
517  KSPlistServer *server = [[KSPlistServer alloc] init];
518  STAssertNil(server, nil);
519  
520  server = [KSPlistServer serverWithURL:nil];
521  STAssertNil(server, nil);
522  
523  NSURL *url = [NSURL URLWithString:@"http://non-ssl.google.com"];
524  server = [KSPlistServer serverWithURL:url];
525  STAssertNotNil(server, nil);
526  
527  url = [NSURL URLWithString:@"https://engine.google.com"];
528  server = [KSPlistServer serverWithURL:url];
529  STAssertNotNil(server, nil);
530}
531
532- (void)testRequests {
533  // Make sure we always get a valid array with exactly one object (request) in
534  // it, regardless of the tickets that we pass.
535  NSArray *requests = [server_ requestsForTickets:nil];
536  STAssertNotNil(requests, nil);
537  STAssertEquals([requests count], 1U, nil);
538  STAssertEqualObjects([server_ tickets], nil, nil);
539  
540  requests = [server_ requestsForTickets:[NSArray array]];
541  STAssertNotNil(requests, nil);
542  STAssertEquals([requests count], 1U, nil);
543  STAssertEqualObjects([server_ tickets], [NSArray array], nil);
544  
545  requests = [server_ requestsForTickets:(NSArray *)@"not nil"];
546  STAssertNotNil(requests, nil);
547  STAssertEquals([requests count], 1U, nil);
548  STAssertEqualObjects([server_ tickets], @"not nil", nil);
549}
550
551- (void)testUpdateInfosForResponsesSingleRule {
552  // Need to call this so the KSPlistServer can get our list of tickets
553  [server_ requestsForTickets:tickets_];
554  
555  // Make sure passing nil data returns nil
556  STAssertNil([server_ updateInfosForResponse:nil data:nil outOfBandData:NULL],
557              nil);
558  
559  // *** kPlist1Rule_1 ***
560  // Expect: 1 update
561  NSArray *updateInfos = nil;
562  updateInfos = [server_ updateInfosForResponse:nil data:
563                 [kPlist1Rule_1 dataUsingEncoding:NSUTF8StringEncoding]
564                                  outOfBandData:NULL];
565  
566  STAssertNotNil(updateInfos, nil);
567  STAssertEquals([updateInfos count], 1U, nil);
568  
569  // Make sure the one returned KSUpdateInfo is OK
570  KSUpdateInfo *ui = [updateInfos lastObject];
571  [self assertUpdateInfo:ui
572        matchesProductID:@"com.google.Foo"
573                    hash:@"somehash="
574                    size:123456
575                     url:@"https://www.google.com/engine/"];
576  
577  // *** kPlist1Rule_2 ***
578  // Expect: No updates (due to predicate's ticket version)
579  updateInfos = [server_ updateInfosForResponse:nil data:
580                 [kPlist1Rule_2 dataUsingEncoding:NSUTF8StringEncoding]
581                                  outOfBandData:NULL];
582  STAssertNil(updateInfos, nil);
583  
584  // *** kPlist1Rule_3 ***
585  // Expect: No updates (due to invalid predicate)
586  updateInfos = [server_ updateInfosForResponse:nil data:
587                 [kPlist1Rule_3 dataUsingEncoding:NSUTF8StringEncoding]
588                                  outOfBandData:NULL];
589  STAssertNil(updateInfos, nil);
590  
591  // *** kPlist1Rule_4 ***
592  // Expect: No updates (due to unmatched product ID)
593  updateInfos = [server_ updateInfosForResponse:nil data:
594                 [kPlist1Rule_4 dataUsingEncoding:NSUTF8StringEncoding]
595                                  outOfBandData:NULL];
596  STAssertNil(updateInfos, nil);
597  
598  // *** kPlist1Rule_5 ***
599  // Expect: No updates (due to badly formed XML)
600  updateInfos = [server_ updateInfosForResponse:nil data:
601                 [kPlist1Rule_5 dataUsingEncoding:NSUTF8StringEncoding]
602                                  outOfBandData:NULL];
603  STAssertNil(updateInfos, nil);
604  
605  // *** kPlist1Rule_6 ***
606  // Expect: No updates (due to missing code hash)
607  updateInfos = [server_ updateInfosForResponse:nil data:
608                 [kPlist1Rule_6 dataUsingEncoding:NSUTF8StringEncoding]
609                                  outOfBandData:NULL];
610  STAssertNil(updateInfos, nil);
611  
612  // *** kPlist1Rule_7 ***
613  // Expect: 1 update (w/ the size specified as an integer)
614  updateInfos = [server_ updateInfosForResponse:nil data:
615                 [kPlist1Rule_7 dataUsingEncoding:NSUTF8StringEncoding]
616                                  outOfBandData:NULL];
617  STAssertNotNil(updateInfos, nil);
618  ui = [updateInfos lastObject];
619  [self assertUpdateInfo:ui
620        matchesProductID:@"com.google.Foo"
621                    hash:@"somehash="
622                    size:123456
623                     url:@"https://www.google.com/engine/"];
624  
625  // *** kPlist1Rule_8 ***
626  // Expect: No updates (no rules)
627  updateInfos = [server_ updateInfosForResponse:nil data:
628                 [kPlist1Rule_8 dataUsingEncoding:NSUTF8StringEncoding]
629                                  outOfBandData:NULL];
630  STAssertNil(updateInfos, nil);
631  
632  // *** kPlist1Rule_9 ***
633  // Expect: No updates (empty plist)
634  updateInfos = [server_ updateInfosForResponse:nil data:
635                 [kPlist1Rule_9 dataUsingEncoding:NSUTF8StringEncoding]
636                                  outOfBandData:NULL];
637  STAssertNil(updateInfos, nil);
638  
639  // *** kPlist1Rule_10 ***
640  // Expect: No updates (empty plist)
641  updateInfos = [server_ updateInfosForResponse:nil data:
642                 [kPlist1Rule_10 dataUsingEncoding:NSUTF8StringEncoding]
643                                  outOfBandData:NULL];
644  STAssertNil(updateInfos, nil);
645  
646  // *** kPlist1Rule_11 ***
647  // Expect: No updates (empty plist)
648  updateInfos = [server_ updateInfosForResponse:nil data:
649                 [kPlist1Rule_11 dataUsingEncoding:NSUTF8StringEncoding]
650                                  outOfBandData:NULL];
651  STAssertNil(updateInfos, nil);
652}
653
654- (void)testUpdateInfosForResponsesMultipleRules {
655  // Need to call this so the KSPlistServer can get our list of tickets
656  [server_ requestsForTickets:tickets_];
657  
658  // *** kPlistNRules_1 ***
659  // Expect: 3 updates
660  NSArray *updateInfos = nil;
661  updateInfos = [server_ updateInfosForResponse:nil data:
662                 [kPlistNRules_1 dataUsingEncoding:NSUTF8StringEncoding]
663                                  outOfBandData:NULL];
664  
665  STAssertNotNil(updateInfos, nil);
666  STAssertEquals([updateInfos count], 3U, nil);
667  
668  // Make sure the one returned KSUpdateInfo is OK
669  KSUpdateInfo *ui = [updateInfos objectAtIndex:0];
670  [self assertUpdateInfo:ui
671        matchesProductID:@"com.google.Foo"
672                    hash:@"foohash="
673                    size:123456
674                     url:@"https://www.google.com/engine/foo"];
675  ui = [updateInfos objectAtIndex:1];
676  [self assertUpdateInfo:ui
677        matchesProductID:@"com.google.Bar"
678                    hash:@"barhash="
679                    size:123456
680                     url:@"https://www.google.com/engine/bar"];
681  ui = [updateInfos objectAtIndex:2];
682  [self assertUpdateInfo:ui
683        matchesProductID:@"com.google.Baz"
684                    hash:@"bazhash="
685                    size:123456
686                     url:@"https://www.google.com/engine/baz"];
687  
688  // *** kPlistNRules_2 ***
689  // Expect: 1 Update (other predicates fail)
690  updateInfos = [server_ updateInfosForResponse:nil data:
691                 [kPlistNRules_2 dataUsingEncoding:NSUTF8StringEncoding]
692                                  outOfBandData:NULL];
693  STAssertNotNil(updateInfos, nil);
694  STAssertEquals([updateInfos count], 1U, nil);
695  ui = [updateInfos objectAtIndex:0];
696  [self assertUpdateInfo:ui
697        matchesProductID:@"com.google.Baz"
698                    hash:@"bazhash="
699                    size:123456
700                     url:@"https://www.google.com/engine/baz"];
701  
702  // *** kPlistNRules_3 ***
703  // Expect: 1 Update (despite the exception that will be thrown for rule 2)
704  updateInfos = [server_ updateInfosForResponse:nil data:
705                 [kPlistNRules_3 dataUsingEncoding:NSUTF8StringEncoding]
706                                  outOfBandData:NULL];
707  STAssertNotNil(updateInfos, nil);
708  STAssertEquals([updateInfos count], 1U, nil);
709  ui = [updateInfos objectAtIndex:0];
710  [self assertUpdateInfo:ui
711        matchesProductID:@"com.google.Baz"
712                    hash:@"bazhash="
713                    size:123456
714                     url:@"https://www.google.com/engine/baz"];
715  
716  // *** kPlistNRules_4 ***
717  // Expect: No Updates (All predicates fail)
718  updateInfos = [server_ updateInfosForResponse:nil data:
719                 [kPlistNRules_4 dataUsingEncoding:NSUTF8StringEncoding]
720                                  outOfBandData:NULL];
721  STAssertNil(updateInfos, nil);
722}
723
724- (void)testPrettyPrinting {
725  // Note that the pretty printing doesn't require the data to be plist data.
726  // The pretty printing simply converts the given data into a UTF-8 NSString.
727  NSArray *expectedStrings = [NSArray arrayWithObjects:
728                              @"",
729                              @" ",
730                              @"   ",
731                              @"a",
732                              @"abc",
733                              @"a\nb\nc",
734                              @"<foo>bar</foo>",
735                              @"<foo>bar</bar>",  // invalid xml
736                              nil];
737  
738  
739  NSString *expectedString = nil;
740  NSEnumerator *stringEnumerator = [expectedStrings objectEnumerator];
741  while ((expectedString = [stringEnumerator nextObject])) {
742    NSData *data = [expectedString dataUsingEncoding:NSUTF8StringEncoding];
743    NSString *pretty = [server_ prettyPrintResponse:nil data:data];
744    STAssertEqualObjects(pretty, expectedString, nil);
745  }
746}
747
748@end