FIRStorageIntegrationTests.m 24 KB

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