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