FIRStorageIntegrationTests.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Copyright 2017 Google
  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 <FirebaseStorage/FIRStorageMetadata.h>
  15. #import <XCTest/XCTest.h>
  16. #import <math.h>
  17. #import "FirebaseStorage.h"
  18. #import <FirebaseCore/FIRApp.h>
  19. #import <FirebaseCore/FIROptions.h>
  20. NSTimeInterval kFIRStorageIntegrationTestTimeout = 30;
  21. /**
  22. * Firebase Storage Integration tests
  23. *
  24. * To run these tests, you need to define the following access rights for your Firebase App:
  25. * - unauthentication read/write access to /ios/public
  26. * - authentication read/write access to /ios/private
  27. *
  28. * A sample configuration may look like:
  29. *
  30. * service firebase.storage {
  31. * match /b/{bucket}/o {
  32. * ...
  33. * match /ios {
  34. * match /public/{allPaths=**} {
  35. * allow read, write;
  36. * }
  37. * match /private/{allPaths=**} {
  38. * allow none;
  39. * }
  40. * }
  41. * }
  42. * }
  43. *
  44. * You can define these access rights in the Firebase Console of your project.
  45. */
  46. @interface FIRStorageIntegrationTests : XCTestCase
  47. @property(strong, nonatomic) FIRApp *app;
  48. @property(strong, nonatomic) FIRStorage *storage;
  49. @end
  50. @implementation FIRStorageIntegrationTests
  51. + (void)setUp {
  52. [FIRApp configure];
  53. }
  54. - (void)setUp {
  55. [super setUp];
  56. self.app = [FIRApp defaultApp];
  57. self.storage = [FIRStorage storageForApp:self.app];
  58. static dispatch_once_t once;
  59. dispatch_once(&once, ^{
  60. XCTestExpectation *expectation = [self expectationWithDescription:@"setup"];
  61. FIRStorageReference *ref = [[FIRStorage storage].reference child:@"ios/public/1mb"];
  62. NSData *data = [NSData
  63. dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1mb" ofType:@"dat"]];
  64. XCTAssertNotNil(data, "Could not load bundled file");
  65. [ref putData:data
  66. metadata:nil
  67. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  68. XCTAssertNil(error);
  69. [expectation fulfill];
  70. }];
  71. [self waitForExpectations];
  72. });
  73. }
  74. - (void)tearDown {
  75. self.app = nil;
  76. self.storage = nil;
  77. [super tearDown];
  78. }
  79. - (void)testName {
  80. NSString *aGSURI = [NSString
  81. stringWithFormat:@"gs://%@.appspot.com/path/to", [[FIRApp defaultApp] options].projectID];
  82. FIRStorageReference *ref = [self.storage referenceForURL:aGSURI];
  83. XCTAssertEqualObjects(ref.description, aGSURI);
  84. }
  85. - (void)testUnauthenticatedGetMetadata {
  86. XCTestExpectation *expectation =
  87. [self expectationWithDescription:@"testUnauthenticatedGetMetadata"];
  88. FIRStorageReference *ref = [self.storage.reference child:@"ios/public/1mb"];
  89. [ref metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  90. XCTAssertNotNil(metadata, "Metadata should not be nil");
  91. XCTAssertNil(error, "Error should be nil");
  92. [expectation fulfill];
  93. }];
  94. [self waitForExpectations];
  95. }
  96. - (void)testUnauthenticatedUpdateMetadata {
  97. XCTestExpectation *expectation =
  98. [self expectationWithDescription:@"testUnauthenticatedUpdateMetadata"];
  99. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  100. FIRStorageMetadata *meta = [[FIRStorageMetadata alloc] init];
  101. [meta setContentType:@"lol/custom"];
  102. [meta setCustomMetadata:@{
  103. @"lol" : @"custom metadata is neat",
  104. @"ちかてつ" : @"🚇",
  105. @"shinkansen" : @"新幹線"
  106. }];
  107. [ref updateMetadata:meta
  108. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  109. XCTAssertEqualObjects(meta.contentType, metadata.contentType);
  110. XCTAssertEqualObjects(meta.customMetadata[@"lol"], metadata.customMetadata[@"lol"]);
  111. XCTAssertEqualObjects(meta.customMetadata[@"ちかてつ"],
  112. metadata.customMetadata[@"ちかてつ"]);
  113. XCTAssertEqualObjects(meta.customMetadata[@"shinkansen"],
  114. metadata.customMetadata[@"shinkansen"]);
  115. XCTAssertNil(error, "Error should be nil");
  116. [expectation fulfill];
  117. }];
  118. [self waitForExpectations];
  119. }
  120. - (void)testUnauthenticatedDelete {
  121. XCTestExpectation *expectation = [self expectationWithDescription:@"testUnauthenticatedDelete"];
  122. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
  123. NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
  124. [ref putData:data
  125. metadata:nil
  126. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  127. XCTAssertNotNil(metadata, "Metadata should not be nil");
  128. XCTAssertNil(error, "Error should be nil");
  129. [ref deleteWithCompletion:^(NSError *error) {
  130. XCTAssertNil(error, "Error should be nil");
  131. [expectation fulfill];
  132. }];
  133. }];
  134. [self waitForExpectations];
  135. }
  136. - (void)testDeleteWithNilCompletion {
  137. XCTestExpectation *expectation = [self expectationWithDescription:@"testDeleteWithNilCompletion"];
  138. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
  139. NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
  140. [ref putData:data
  141. metadata:nil
  142. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  143. XCTAssertNotNil(metadata, "Metadata should not be nil");
  144. XCTAssertNil(error, "Error should be nil");
  145. [ref deleteWithCompletion:nil];
  146. [expectation fulfill];
  147. }];
  148. [self waitForExpectations];
  149. }
  150. - (void)testUnauthenticatedSimplePutData {
  151. XCTestExpectation *expectation =
  152. [self expectationWithDescription:@"testUnauthenticatedSimplePutData"];
  153. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"];
  154. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  155. [ref putData:data
  156. metadata:nil
  157. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  158. XCTAssertNotNil(metadata, "Metadata should not be nil");
  159. XCTAssertNil(error, "Error should be nil");
  160. [expectation fulfill];
  161. }];
  162. [self waitForExpectations];
  163. }
  164. - (void)testUnauthenticatedSimplePutEmptyData {
  165. XCTestExpectation *expectation =
  166. [self expectationWithDescription:@"testUnauthenticatedSimplePutEmptyData"];
  167. FIRStorageReference *ref =
  168. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutEmptyData"];
  169. NSData *data = [[NSData alloc] init];
  170. [ref putData:data
  171. metadata:nil
  172. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  173. XCTAssertNotNil(metadata, "Metadata should not be nil");
  174. XCTAssertNil(error, "Error should be nil");
  175. [expectation fulfill];
  176. }];
  177. [self waitForExpectations];
  178. }
  179. - (void)testUnauthenticatedSimplePutDataUnauthorized {
  180. XCTestExpectation *expectation =
  181. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataUnauthorized"];
  182. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/private/secretfile.txt"];
  183. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  184. [ref putData:data
  185. metadata:nil
  186. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  187. XCTAssertNil(metadata, "Metadata should be nil");
  188. XCTAssertNotNil(error, "Error should not be nil");
  189. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  190. [expectation fulfill];
  191. }];
  192. [self waitForExpectations];
  193. }
  194. - (void)testUnauthenticatedSimplePutFile {
  195. XCTestExpectation *expectation =
  196. [self expectationWithDescription:@"testUnauthenticatedSimplePutFile"];
  197. FIRStorageReference *ref =
  198. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFile"];
  199. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  200. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  201. NSURL *fileURL =
  202. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  203. [data writeToURL:fileURL atomically:YES];
  204. FIRStorageUploadTask *task = [ref putFile:fileURL
  205. metadata:nil
  206. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  207. XCTAssertNotNil(metadata, "Metadata should not be nil");
  208. XCTAssertNil(error, "Error should be nil");
  209. }];
  210. __block long uploadedBytes = -1;
  211. [task observeStatus:FIRStorageTaskStatusSuccess
  212. handler:^(FIRStorageTaskSnapshot *snapshot) {
  213. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  214. [expectation fulfill];
  215. }];
  216. [task observeStatus:FIRStorageTaskStatusProgress
  217. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  218. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  219. [[snapshot description] containsString:@"State: Resume"]);
  220. NSProgress *progress = snapshot.progress;
  221. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes);
  222. uploadedBytes = progress.completedUnitCount;
  223. }];
  224. [self waitForExpectations];
  225. }
  226. - (void)testPutFileWithSpecialCharacters {
  227. XCTestExpectation *expectation =
  228. [self expectationWithDescription:@"testPutFileWithSpecialCharacters"];
  229. NSString *fileName = @"hello&+@_ .txt";
  230. FIRStorageReference *ref =
  231. [self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
  232. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  233. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  234. NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:fileName];
  235. [data writeToURL:fileURL atomically:YES];
  236. [ref putFile:fileURL
  237. metadata:nil
  238. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  239. XCTAssertNotNil(metadata, "Metadata should not be nil");
  240. XCTAssertNil(error, "Error should be nil");
  241. XCTAssertEqualObjects(fileName, metadata.name);
  242. FIRStorageReference *download =
  243. [self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
  244. [download metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  245. XCTAssertNotNil(metadata, "Metadata should not be nil");
  246. XCTAssertNil(error, "Error should be nil");
  247. XCTAssertEqualObjects(fileName, metadata.name);
  248. [expectation fulfill];
  249. }];
  250. }];
  251. [self waitForExpectations];
  252. }
  253. - (void)testUnauthenticatedSimplePutDataNoMetadata {
  254. XCTestExpectation *expectation =
  255. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataNoMetadata"];
  256. FIRStorageReference *ref =
  257. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutDataNoMetadata"];
  258. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  259. [ref putData:data
  260. metadata:nil
  261. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  262. XCTAssertNotNil(metadata, "Metadata should not be nil");
  263. XCTAssertNil(error, "Error should be nil");
  264. [expectation fulfill];
  265. }];
  266. [self waitForExpectations];
  267. }
  268. - (void)testUnauthenticatedSimplePutFileNoMetadata {
  269. XCTestExpectation *expectation =
  270. [self expectationWithDescription:@"testUnauthenticatedSimplePutFileNoMetadata"];
  271. FIRStorageReference *ref =
  272. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFileNoMetadata"];
  273. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  274. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  275. NSURL *fileURL =
  276. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  277. [data writeToURL:fileURL atomically:YES];
  278. [ref putFile:fileURL
  279. metadata:nil
  280. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  281. XCTAssertNotNil(metadata, "Metadata should not be nil");
  282. XCTAssertNil(error, "Error should be nil");
  283. [expectation fulfill];
  284. }];
  285. [self waitForExpectations];
  286. }
  287. - (void)testUnauthenticatedSimpleGetData {
  288. XCTestExpectation *expectation =
  289. [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
  290. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  291. [ref dataWithMaxSize:1 * 1024 * 1024
  292. completion:^(NSData *data, NSError *error) {
  293. XCTAssertNotNil(data, "Data should not be nil");
  294. XCTAssertNil(error, "Error should be nil");
  295. [expectation fulfill];
  296. }];
  297. [self waitForExpectations];
  298. }
  299. - (void)testUnauthenticatedSimpleGetDataTooSmall {
  300. XCTestExpectation *expectation =
  301. [self expectationWithDescription:@"testUnauthenticatedSimpleGetDataTooSmall"];
  302. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  303. /// Only allow 1kB size, which is smaller than our file
  304. [ref dataWithMaxSize:1 * 1024
  305. completion:^(NSData *data, NSError *error) {
  306. XCTAssertEqual(data, nil);
  307. XCTAssertEqual(error.code, FIRStorageErrorCodeDownloadSizeExceeded);
  308. [expectation fulfill];
  309. }];
  310. [self waitForExpectations];
  311. }
  312. - (void)testUnauthenticatedSimpleGetFile {
  313. XCTestExpectation *expectation =
  314. [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
  315. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/helloworld"];
  316. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  317. NSURL *fileURL =
  318. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  319. [ref putData:[@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]
  320. metadata:nil
  321. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  322. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  323. [task observeStatus:FIRStorageTaskStatusSuccess
  324. handler:^(FIRStorageTaskSnapshot *snapshot) {
  325. NSString *data = [NSString stringWithContentsOfURL:fileURL
  326. encoding:NSUTF8StringEncoding
  327. error:NULL];
  328. NSString *expectedData = @"Hello World";
  329. XCTAssertEqualObjects(data, expectedData);
  330. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  331. [expectation fulfill];
  332. }];
  333. [task observeStatus:FIRStorageTaskStatusProgress
  334. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  335. NSProgress *progress = snapshot.progress;
  336. NSLog(@"%lld of %lld", progress.completedUnitCount, progress.totalUnitCount);
  337. }];
  338. [task observeStatus:FIRStorageTaskStatusFailure
  339. handler:^(FIRStorageTaskSnapshot *snapshot) {
  340. XCTAssertNil(snapshot.error);
  341. [expectation fulfill];
  342. }];
  343. }];
  344. [self waitForExpectations];
  345. }
  346. - (void)testCancelDownload {
  347. XCTestExpectation *expectation = [self expectationWithDescription:@"testCancelDownload"];
  348. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  349. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  350. NSURL *fileURL =
  351. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"dat"];
  352. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  353. [task observeStatus:FIRStorageTaskStatusFailure
  354. handler:^(FIRStorageTaskSnapshot *snapshot) {
  355. XCTAssertTrue([[snapshot description] containsString:@"State: Failed"]);
  356. [expectation fulfill];
  357. }];
  358. [task observeStatus:FIRStorageTaskStatusProgress
  359. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  360. [task cancel];
  361. }];
  362. [self waitForExpectations];
  363. }
  364. - (void)assertMetadata:(FIRStorageMetadata *)actualMetadata
  365. contentType:(NSString *)expectedContentType
  366. customMetadata:(NSDictionary *)expectedCustomMetadata {
  367. XCTAssertEqualObjects(actualMetadata.cacheControl, @"cache-control");
  368. XCTAssertEqualObjects(actualMetadata.contentDisposition, @"content-disposition");
  369. XCTAssertEqualObjects(actualMetadata.contentEncoding, @"gzip");
  370. XCTAssertEqualObjects(actualMetadata.contentLanguage, @"de");
  371. XCTAssertEqualObjects(actualMetadata.contentType, expectedContentType);
  372. XCTAssertTrue([actualMetadata.md5Hash length] == 24);
  373. for (NSString *key in expectedCustomMetadata) {
  374. XCTAssertEqualObjects([actualMetadata.customMetadata objectForKey:key],
  375. [expectedCustomMetadata objectForKey:key]);
  376. }
  377. }
  378. - (void)assertMetadataNil:(FIRStorageMetadata *)actualMetadata {
  379. XCTAssertNil(actualMetadata.cacheControl);
  380. XCTAssertNil(actualMetadata.contentDisposition);
  381. XCTAssertEqualObjects(actualMetadata.contentEncoding, @"identity");
  382. XCTAssertNil(actualMetadata.contentLanguage);
  383. XCTAssertNil(actualMetadata.contentType);
  384. XCTAssertTrue([actualMetadata.md5Hash length] == 24);
  385. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"a"]);
  386. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"c"]);
  387. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"f"]);
  388. }
  389. - (void)testUpdateMetadata {
  390. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateMetadata"];
  391. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  392. // Update all available metadata
  393. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
  394. metadata.cacheControl = @"cache-control";
  395. metadata.contentDisposition = @"content-disposition";
  396. metadata.contentEncoding = @"gzip";
  397. metadata.contentLanguage = @"de";
  398. metadata.contentType = @"content-type-a";
  399. metadata.customMetadata = @{@"a" : @"b"};
  400. [ref updateMetadata:metadata
  401. completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  402. XCTAssertNil(error);
  403. [self assertMetadata:updatedMetadata
  404. contentType:@"content-type-a"
  405. customMetadata:@{@"a" : @"b"}];
  406. // Update a subset of the metadata using the existing object.
  407. FIRStorageMetadata *metadata = updatedMetadata;
  408. metadata.contentType = @"content-type-b";
  409. metadata.customMetadata = @{@"a" : @"b", @"c" : @"d"};
  410. [ref updateMetadata:metadata
  411. completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  412. XCTAssertNil(error);
  413. [self assertMetadata:updatedMetadata
  414. contentType:@"content-type-b"
  415. customMetadata:@{@"a" : @"b", @"c" : @"d"}];
  416. // Clear all metadata.
  417. FIRStorageMetadata *metadata = updatedMetadata;
  418. metadata.cacheControl = nil;
  419. metadata.contentDisposition = nil;
  420. metadata.contentEncoding = nil;
  421. metadata.contentLanguage = nil;
  422. metadata.contentType = nil;
  423. metadata.customMetadata = [NSDictionary dictionary];
  424. [ref updateMetadata:metadata
  425. completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  426. XCTAssertNil(error);
  427. [self assertMetadataNil:updatedMetadata];
  428. [expectation fulfill];
  429. }];
  430. }];
  431. }];
  432. [self waitForExpectations];
  433. }
  434. - (void)testUnauthenticatedResumeGetFile {
  435. XCTestExpectation *expectation =
  436. [self expectationWithDescription:@"testUnauthenticatedResumeGetFile"];
  437. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  438. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  439. NSURL *fileURL =
  440. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  441. __block long resumeAtBytes = 256 * 1024;
  442. __block long downloadedBytes = 0;
  443. __block double computationResult = 0;
  444. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  445. [task observeStatus:FIRStorageTaskStatusSuccess
  446. handler:^(FIRStorageTaskSnapshot *snapshot) {
  447. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  448. [expectation fulfill];
  449. }];
  450. [task observeStatus:FIRStorageTaskStatusProgress
  451. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  452. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  453. [[snapshot description] containsString:@"State: Resume"]);
  454. NSProgress *progress = snapshot.progress;
  455. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, downloadedBytes);
  456. downloadedBytes = progress.completedUnitCount;
  457. if (progress.completedUnitCount > resumeAtBytes) {
  458. // Making sure the main run loop is busy.
  459. for (int i = 0; i < 500; ++i) {
  460. dispatch_async(dispatch_get_main_queue(), ^{
  461. computationResult = sqrt(INT_MAX - i);
  462. });
  463. }
  464. NSLog(@"Pausing");
  465. [task pause];
  466. resumeAtBytes = INT_MAX;
  467. }
  468. }];
  469. [task observeStatus:FIRStorageTaskStatusPause
  470. handler:^(FIRStorageTaskSnapshot *snapshot) {
  471. XCTAssertEqualObjects([snapshot description], @"<State: Paused>");
  472. NSLog(@"Resuming");
  473. [task resume];
  474. }];
  475. [self waitForExpectations];
  476. XCTAssertEqual(INT_MAX, resumeAtBytes);
  477. XCTAssertEqualWithAccuracy(sqrt(INT_MAX - 499), computationResult, 0.1);
  478. }
  479. - (void)waitForExpectations {
  480. [self waitForExpectationsWithTimeout:kFIRStorageIntegrationTestTimeout
  481. handler:^(NSError *_Nullable error) {
  482. if (error) {
  483. NSLog(@"%@", error);
  484. }
  485. }];
  486. }
  487. @end