FIRStorageIntegrationTests.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 = 60;
  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. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1mb" ofType:@"dat"];
  61. if (filePath == nil) {
  62. // Use bundleForClass to allow 1mb.dat to be in the test target's bundle.
  63. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  64. filePath = [bundle pathForResource:@"1mb" ofType:@"dat"];
  65. }
  66. NSData *data = [NSData dataWithContentsOfFile:filePath];
  67. XCTAssertNotNil(data, "Could not load bundled file");
  68. [ref putData:data
  69. metadata:nil
  70. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  71. XCTAssertNil(error);
  72. [expectation fulfill];
  73. }];
  74. [self waitForExpectations];
  75. });
  76. }
  77. - (void)tearDown {
  78. self.app = nil;
  79. self.storage = nil;
  80. [super tearDown];
  81. }
  82. - (void)testName {
  83. NSString *aGSURI = [NSString
  84. stringWithFormat:@"gs://%@.appspot.com/path/to", [[FIRApp defaultApp] options].projectID];
  85. FIRStorageReference *ref = [self.storage referenceForURL:aGSURI];
  86. XCTAssertEqualObjects(ref.description, aGSURI);
  87. }
  88. - (void)testUnauthenticatedGetMetadata {
  89. XCTestExpectation *expectation =
  90. [self expectationWithDescription:@"testUnauthenticatedGetMetadata"];
  91. FIRStorageReference *ref = [self.storage.reference child:@"ios/public/1mb"];
  92. [ref metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  93. XCTAssertNotNil(metadata, "Metadata should not be nil");
  94. XCTAssertNil(error, "Error should be nil");
  95. [expectation fulfill];
  96. }];
  97. [self waitForExpectations];
  98. }
  99. - (void)testUnauthenticatedUpdateMetadata {
  100. XCTestExpectation *expectation =
  101. [self expectationWithDescription:@"testUnauthenticatedUpdateMetadata"];
  102. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  103. FIRStorageMetadata *meta = [[FIRStorageMetadata alloc] init];
  104. [meta setContentType:@"lol/custom"];
  105. [meta setCustomMetadata:@{
  106. @"lol" : @"custom metadata is neat",
  107. @"ちかてつ" : @"🚇",
  108. @"shinkansen" : @"新幹線"
  109. }];
  110. [ref updateMetadata:meta
  111. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  112. XCTAssertEqualObjects(meta.contentType, metadata.contentType);
  113. XCTAssertEqualObjects(meta.customMetadata[@"lol"], metadata.customMetadata[@"lol"]);
  114. XCTAssertEqualObjects(meta.customMetadata[@"ちかてつ"],
  115. metadata.customMetadata[@"ちかてつ"]);
  116. XCTAssertEqualObjects(meta.customMetadata[@"shinkansen"],
  117. metadata.customMetadata[@"shinkansen"]);
  118. XCTAssertNil(error, "Error should be nil");
  119. [expectation fulfill];
  120. }];
  121. [self waitForExpectations];
  122. }
  123. - (void)testUnauthenticatedDelete {
  124. XCTestExpectation *expectation = [self expectationWithDescription:@"testUnauthenticatedDelete"];
  125. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
  126. NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
  127. [ref putData:data
  128. metadata:nil
  129. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  130. XCTAssertNotNil(metadata, "Metadata should not be nil");
  131. XCTAssertNil(error, "Error should be nil");
  132. [ref deleteWithCompletion:^(NSError *error) {
  133. XCTAssertNil(error, "Error should be nil");
  134. [expectation fulfill];
  135. }];
  136. }];
  137. [self waitForExpectations];
  138. }
  139. - (void)testDeleteWithNilCompletion {
  140. XCTestExpectation *expectation = [self expectationWithDescription:@"testDeleteWithNilCompletion"];
  141. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
  142. NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
  143. [ref putData:data
  144. metadata:nil
  145. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  146. XCTAssertNotNil(metadata, "Metadata should not be nil");
  147. XCTAssertNil(error, "Error should be nil");
  148. [ref deleteWithCompletion:nil];
  149. [expectation fulfill];
  150. }];
  151. [self waitForExpectations];
  152. }
  153. - (void)testUnauthenticatedSimplePutData {
  154. XCTestExpectation *expectation =
  155. [self expectationWithDescription:@"testUnauthenticatedSimplePutData"];
  156. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"];
  157. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  158. [ref putData:data
  159. metadata:nil
  160. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  161. XCTAssertNotNil(metadata, "Metadata should not be nil");
  162. XCTAssertNil(error, "Error should be nil");
  163. [expectation fulfill];
  164. }];
  165. [self waitForExpectations];
  166. }
  167. - (void)testUnauthenticatedSimplePutSpecialCharacter {
  168. XCTestExpectation *expectation =
  169. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataEscapedName"];
  170. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/-._~!$'()*,=:@&+;"];
  171. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  172. [ref putData:data
  173. metadata:nil
  174. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  175. XCTAssertNotNil(metadata, "Metadata should not be nil");
  176. XCTAssertNil(error, "Error should be nil");
  177. [expectation fulfill];
  178. }];
  179. [self waitForExpectations];
  180. }
  181. - (void)testUnauthenticatedSimplePutDataInBackgroundQueue {
  182. XCTestExpectation *expectation =
  183. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataInBackgroundQueue"];
  184. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"];
  185. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  186. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  187. [ref putData:data
  188. metadata:nil
  189. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  190. XCTAssertNotNil(metadata, "Metadata should not be nil");
  191. XCTAssertNil(error, "Error should be nil");
  192. [expectation fulfill];
  193. }];
  194. });
  195. [self waitForExpectations];
  196. }
  197. - (void)testUnauthenticatedSimplePutEmptyData {
  198. XCTestExpectation *expectation =
  199. [self expectationWithDescription:@"testUnauthenticatedSimplePutEmptyData"];
  200. FIRStorageReference *ref =
  201. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutEmptyData"];
  202. NSData *data = [[NSData alloc] init];
  203. [ref putData:data
  204. metadata:nil
  205. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  206. XCTAssertNotNil(metadata, "Metadata should not be nil");
  207. XCTAssertNil(error, "Error should be nil");
  208. [expectation fulfill];
  209. }];
  210. [self waitForExpectations];
  211. }
  212. - (void)testUnauthenticatedSimplePutDataUnauthorized {
  213. XCTestExpectation *expectation =
  214. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataUnauthorized"];
  215. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/private/secretfile.txt"];
  216. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  217. [ref putData:data
  218. metadata:nil
  219. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  220. XCTAssertNil(metadata, "Metadata should be nil");
  221. XCTAssertNotNil(error, "Error should not be nil");
  222. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  223. [expectation fulfill];
  224. }];
  225. [self waitForExpectations];
  226. }
  227. - (void)testUnauthenticatedSimplePutFile {
  228. XCTestExpectation *expectation =
  229. [self expectationWithDescription:@"testUnauthenticatedSimplePutFile"];
  230. FIRStorageReference *ref =
  231. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFile"];
  232. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  233. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  234. NSURL *fileURL =
  235. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  236. [data writeToURL:fileURL atomically:YES];
  237. FIRStorageUploadTask *task = [ref putFile:fileURL
  238. metadata:nil
  239. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  240. XCTAssertNotNil(metadata, "Metadata should not be nil");
  241. XCTAssertNil(error, "Error should be nil");
  242. }];
  243. __block long uploadedBytes = -1;
  244. [task observeStatus:FIRStorageTaskStatusSuccess
  245. handler:^(FIRStorageTaskSnapshot *snapshot) {
  246. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  247. [expectation fulfill];
  248. }];
  249. [task observeStatus:FIRStorageTaskStatusProgress
  250. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  251. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  252. [[snapshot description] containsString:@"State: Resume"]);
  253. NSProgress *progress = snapshot.progress;
  254. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes);
  255. uploadedBytes = (long)progress.completedUnitCount;
  256. }];
  257. [self waitForExpectations];
  258. }
  259. - (void)testPutFileWithSpecialCharacters {
  260. XCTestExpectation *expectation =
  261. [self expectationWithDescription:@"testPutFileWithSpecialCharacters"];
  262. NSString *fileName = @"hello&+@_ .txt";
  263. FIRStorageReference *ref =
  264. [self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
  265. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  266. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  267. NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:fileName];
  268. [data writeToURL:fileURL atomically:YES];
  269. [ref putFile:fileURL
  270. metadata:nil
  271. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  272. XCTAssertNotNil(metadata, "Metadata should not be nil");
  273. XCTAssertNil(error, "Error should be nil");
  274. XCTAssertEqualObjects(fileName, metadata.name);
  275. FIRStorageReference *download =
  276. [self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
  277. [download metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  278. XCTAssertNotNil(metadata, "Metadata should not be nil");
  279. XCTAssertNil(error, "Error should be nil");
  280. XCTAssertEqualObjects(fileName, metadata.name);
  281. [expectation fulfill];
  282. }];
  283. }];
  284. [self waitForExpectations];
  285. }
  286. - (void)testUnauthenticatedSimplePutDataNoMetadata {
  287. XCTestExpectation *expectation =
  288. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataNoMetadata"];
  289. FIRStorageReference *ref =
  290. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutDataNoMetadata"];
  291. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  292. [ref putData:data
  293. metadata:nil
  294. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  295. XCTAssertNotNil(metadata, "Metadata should not be nil");
  296. XCTAssertNil(error, "Error should be nil");
  297. [expectation fulfill];
  298. }];
  299. [self waitForExpectations];
  300. }
  301. - (void)testUnauthenticatedSimplePutFileNoMetadata {
  302. XCTestExpectation *expectation =
  303. [self expectationWithDescription:@"testUnauthenticatedSimplePutFileNoMetadata"];
  304. FIRStorageReference *ref =
  305. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFileNoMetadata"];
  306. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  307. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  308. NSURL *fileURL =
  309. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  310. [data writeToURL:fileURL atomically:YES];
  311. [ref putFile:fileURL
  312. metadata:nil
  313. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  314. XCTAssertNotNil(metadata, "Metadata should not be nil");
  315. XCTAssertNil(error, "Error should be nil");
  316. [expectation fulfill];
  317. }];
  318. [self waitForExpectations];
  319. }
  320. - (void)testUnauthenticatedSimpleGetData {
  321. XCTestExpectation *expectation =
  322. [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
  323. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  324. [ref dataWithMaxSize:1 * 1024 * 1024
  325. completion:^(NSData *data, NSError *error) {
  326. XCTAssertNotNil(data, "Data should not be nil");
  327. XCTAssertNil(error, "Error should be nil");
  328. [expectation fulfill];
  329. }];
  330. [self waitForExpectations];
  331. }
  332. - (void)testUnauthenticatedSimpleGetDataInBackgroundQueue {
  333. XCTestExpectation *expectation =
  334. [self expectationWithDescription:@"testUnauthenticatedSimpleGetDataInBackgroundQueue"];
  335. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  336. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  337. [ref dataWithMaxSize:1 * 1024 * 1024
  338. completion:^(NSData *data, NSError *error) {
  339. XCTAssertNotNil(data, "Data should not be nil");
  340. XCTAssertNil(error, "Error should be nil");
  341. [expectation fulfill];
  342. }];
  343. });
  344. [self waitForExpectations];
  345. }
  346. - (void)testUnauthenticatedSimpleGetDataTooSmall {
  347. XCTestExpectation *expectation =
  348. [self expectationWithDescription:@"testUnauthenticatedSimpleGetDataTooSmall"];
  349. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  350. /// Only allow 1kB size, which is smaller than our file
  351. [ref dataWithMaxSize:1 * 1024
  352. completion:^(NSData *data, NSError *error) {
  353. XCTAssertNil(data);
  354. XCTAssertEqual(error.code, FIRStorageErrorCodeDownloadSizeExceeded);
  355. [expectation fulfill];
  356. }];
  357. [self waitForExpectations];
  358. }
  359. - (void)testUnauthenticatedSimpleGetDownloadURL {
  360. XCTestExpectation *expectation =
  361. [self expectationWithDescription:@"testUnauthenticatedSimpleGetDownloadURL"];
  362. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  363. // Download URL format is
  364. // "https://firebasestorage.googleapis.com/v0/b/{bucket}/o/{path}?alt=media&token={token}"
  365. NSString *downloadURLPattern =
  366. @"^https:\\/\\/firebasestorage.googleapis.com\\/v0\\/b\\/[^\\/]*\\/o\\/"
  367. @"ios%2Fpublic%2F1mb\\?alt=media&token=[a-z0-9-]*$";
  368. [ref downloadURLWithCompletion:^(NSURL *downloadURL, NSError *error) {
  369. XCTAssertNil(error);
  370. NSRegularExpression *testRegex =
  371. [NSRegularExpression regularExpressionWithPattern:downloadURLPattern options:0 error:nil];
  372. NSString *urlString = [downloadURL absoluteString];
  373. XCTAssertEqual([testRegex numberOfMatchesInString:urlString
  374. options:0
  375. range:NSMakeRange(0, [urlString length])],
  376. 1);
  377. [expectation fulfill];
  378. }];
  379. [self waitForExpectations];
  380. }
  381. - (void)testUnauthenticatedSimpleGetFile {
  382. XCTestExpectation *expectation =
  383. [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
  384. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/helloworld"];
  385. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  386. NSURL *fileURL =
  387. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  388. [ref putData:[@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]
  389. metadata:nil
  390. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  391. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  392. [task observeStatus:FIRStorageTaskStatusSuccess
  393. handler:^(FIRStorageTaskSnapshot *snapshot) {
  394. NSString *data = [NSString stringWithContentsOfURL:fileURL
  395. encoding:NSUTF8StringEncoding
  396. error:NULL];
  397. NSString *expectedData = @"Hello World";
  398. XCTAssertEqualObjects(data, expectedData);
  399. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  400. [expectation fulfill];
  401. }];
  402. [task observeStatus:FIRStorageTaskStatusProgress
  403. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  404. NSProgress *progress = snapshot.progress;
  405. NSLog(@"%lld of %lld", progress.completedUnitCount, progress.totalUnitCount);
  406. }];
  407. [task observeStatus:FIRStorageTaskStatusFailure
  408. handler:^(FIRStorageTaskSnapshot *snapshot) {
  409. XCTAssertNil(snapshot.error);
  410. [expectation fulfill];
  411. }];
  412. }];
  413. [self waitForExpectations];
  414. }
  415. - (void)testCancelDownload {
  416. XCTestExpectation *expectation = [self expectationWithDescription:@"testCancelDownload"];
  417. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  418. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  419. NSURL *fileURL =
  420. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"dat"];
  421. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  422. [task observeStatus:FIRStorageTaskStatusFailure
  423. handler:^(FIRStorageTaskSnapshot *snapshot) {
  424. XCTAssertTrue([[snapshot description] containsString:@"State: Failed"]);
  425. [expectation fulfill];
  426. }];
  427. [task observeStatus:FIRStorageTaskStatusProgress
  428. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  429. [task cancel];
  430. }];
  431. [self waitForExpectations];
  432. }
  433. - (void)assertMetadata:(FIRStorageMetadata *)actualMetadata
  434. contentType:(NSString *)expectedContentType
  435. customMetadata:(NSDictionary *)expectedCustomMetadata {
  436. XCTAssertEqualObjects(actualMetadata.cacheControl, @"cache-control");
  437. XCTAssertEqualObjects(actualMetadata.contentDisposition, @"content-disposition");
  438. XCTAssertEqualObjects(actualMetadata.contentEncoding, @"gzip");
  439. XCTAssertEqualObjects(actualMetadata.contentLanguage, @"de");
  440. XCTAssertEqualObjects(actualMetadata.contentType, expectedContentType);
  441. XCTAssertTrue([actualMetadata.md5Hash length] == 24);
  442. for (NSString *key in expectedCustomMetadata) {
  443. XCTAssertEqualObjects([actualMetadata.customMetadata objectForKey:key],
  444. [expectedCustomMetadata objectForKey:key]);
  445. }
  446. }
  447. - (void)assertMetadataNil:(FIRStorageMetadata *)actualMetadata {
  448. XCTAssertNil(actualMetadata.cacheControl);
  449. XCTAssertNil(actualMetadata.contentDisposition);
  450. XCTAssertEqualObjects(actualMetadata.contentEncoding, @"identity");
  451. XCTAssertNil(actualMetadata.contentLanguage);
  452. XCTAssertNil(actualMetadata.contentType);
  453. XCTAssertTrue([actualMetadata.md5Hash length] == 24);
  454. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"a"]);
  455. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"c"]);
  456. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"f"]);
  457. }
  458. - (void)testUpdateMetadata {
  459. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateMetadata"];
  460. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  461. // Update all available metadata
  462. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
  463. metadata.cacheControl = @"cache-control";
  464. metadata.contentDisposition = @"content-disposition";
  465. metadata.contentEncoding = @"gzip";
  466. metadata.contentLanguage = @"de";
  467. metadata.contentType = @"content-type-a";
  468. metadata.customMetadata = @{@"a" : @"b"};
  469. [ref updateMetadata:metadata
  470. completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  471. XCTAssertNil(error);
  472. [self assertMetadata:updatedMetadata
  473. contentType:@"content-type-a"
  474. customMetadata:@{@"a" : @"b"}];
  475. // Update a subset of the metadata using the existing object.
  476. FIRStorageMetadata *metadata = updatedMetadata;
  477. metadata.contentType = @"content-type-b";
  478. metadata.customMetadata = @{@"a" : @"b", @"c" : @"d"};
  479. [ref updateMetadata:metadata
  480. completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  481. XCTAssertNil(error);
  482. [self assertMetadata:updatedMetadata
  483. contentType:@"content-type-b"
  484. customMetadata:@{@"a" : @"b", @"c" : @"d"}];
  485. // Clear all metadata.
  486. FIRStorageMetadata *metadata = updatedMetadata;
  487. metadata.cacheControl = nil;
  488. metadata.contentDisposition = nil;
  489. metadata.contentEncoding = nil;
  490. metadata.contentLanguage = nil;
  491. metadata.contentType = nil;
  492. metadata.customMetadata = [NSDictionary dictionary];
  493. [ref updateMetadata:metadata
  494. completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  495. XCTAssertNil(error);
  496. [self assertMetadataNil:updatedMetadata];
  497. [expectation fulfill];
  498. }];
  499. }];
  500. }];
  501. [self waitForExpectations];
  502. }
  503. - (void)testUnauthenticatedResumeGetFile {
  504. XCTestExpectation *expectation =
  505. [self expectationWithDescription:@"testUnauthenticatedResumeGetFile"];
  506. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  507. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  508. NSURL *fileURL =
  509. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  510. __block long resumeAtBytes = 256 * 1024;
  511. __block long downloadedBytes = 0;
  512. __block double computationResult = 0;
  513. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  514. [task observeStatus:FIRStorageTaskStatusSuccess
  515. handler:^(FIRStorageTaskSnapshot *snapshot) {
  516. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  517. [expectation fulfill];
  518. }];
  519. [task observeStatus:FIRStorageTaskStatusProgress
  520. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  521. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  522. [[snapshot description] containsString:@"State: Resume"]);
  523. NSProgress *progress = snapshot.progress;
  524. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, downloadedBytes);
  525. downloadedBytes = (long)progress.completedUnitCount;
  526. if (progress.completedUnitCount > resumeAtBytes) {
  527. // Making sure the main run loop is busy.
  528. for (int i = 0; i < 500; ++i) {
  529. dispatch_async(dispatch_get_main_queue(), ^{
  530. computationResult = sqrt(INT_MAX - i);
  531. });
  532. }
  533. NSLog(@"Pausing");
  534. [task pause];
  535. resumeAtBytes = INT_MAX;
  536. }
  537. }];
  538. [task observeStatus:FIRStorageTaskStatusPause
  539. handler:^(FIRStorageTaskSnapshot *snapshot) {
  540. XCTAssertEqualObjects([snapshot description], @"<State: Paused>");
  541. NSLog(@"Resuming");
  542. [task resume];
  543. }];
  544. [self waitForExpectations];
  545. XCTAssertEqual(INT_MAX, resumeAtBytes);
  546. XCTAssertEqualWithAccuracy(sqrt(INT_MAX - 499), computationResult, 0.1);
  547. }
  548. - (void)testUnauthenticatedResumeGetFileInBackgroundQueue {
  549. XCTestExpectation *expectation =
  550. [self expectationWithDescription:@"testUnauthenticatedResumeGetFileInBackgroundQueue"];
  551. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  552. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  553. NSURL *fileURL =
  554. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  555. __block long resumeAtBytes = 256 * 1024;
  556. __block long downloadedBytes = 0;
  557. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  558. [task observeStatus:FIRStorageTaskStatusSuccess
  559. handler:^(FIRStorageTaskSnapshot *snapshot) {
  560. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  561. [expectation fulfill];
  562. }];
  563. [task observeStatus:FIRStorageTaskStatusProgress
  564. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  565. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  566. [[snapshot description] containsString:@"State: Resume"]);
  567. NSProgress *progress = snapshot.progress;
  568. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, downloadedBytes);
  569. downloadedBytes = (long)progress.completedUnitCount;
  570. if (progress.completedUnitCount > resumeAtBytes) {
  571. NSLog(@"Pausing");
  572. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  573. [task pause];
  574. });
  575. resumeAtBytes = INT_MAX;
  576. }
  577. }];
  578. [task observeStatus:FIRStorageTaskStatusPause
  579. handler:^(FIRStorageTaskSnapshot *snapshot) {
  580. XCTAssertEqualObjects([snapshot description], @"<State: Paused>");
  581. NSLog(@"Resuming");
  582. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  583. [task resume];
  584. });
  585. }];
  586. [self waitForExpectations];
  587. XCTAssertEqual(INT_MAX, resumeAtBytes);
  588. }
  589. - (void)waitForExpectations {
  590. [self waitForExpectationsWithTimeout:kFIRStorageIntegrationTestTimeout
  591. handler:^(NSError *_Nullable error) {
  592. if (error) {
  593. NSLog(@"%@", error);
  594. }
  595. }];
  596. }
  597. @end