FIRStorageIntegrationTests.m 28 KB

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