StorageIntegration.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // Copyright 2020 Google LLC
  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 FirebaseAuth
  15. import FirebaseCore
  16. @testable import FirebaseStorage
  17. import XCTest
  18. /**
  19. * Firebase Storage Integration tests
  20. *
  21. * To run these tests, you need to define the following access rights:
  22. *
  23. rules_version = '2';
  24. service firebase.storage {
  25. match /b/{bucket}/o {
  26. match /{directChild=*} {
  27. allow read: if request.auth != null;
  28. }
  29. match /ios {
  30. match /public/{allPaths=**} {
  31. allow write: if request.auth != null;
  32. allow read: if true;
  33. }
  34. match /private/{allPaths=**} {
  35. allow read, write: if false;
  36. }
  37. }
  38. }
  39. }
  40. */
  41. class StorageResultTests: StorageIntegrationCommon {
  42. func testGetMetadata() {
  43. let expectation = self.expectation(description: "testGetMetadata")
  44. let ref = storage.reference().child("ios/public/1mb")
  45. ref.getMetadata { result in
  46. self.assertResultSuccess(result)
  47. expectation.fulfill()
  48. }
  49. waitForExpectations()
  50. }
  51. func testUpdateMetadata() {
  52. let expectation = self.expectation(description: #function)
  53. let meta = StorageMetadata()
  54. meta.contentType = "lol/custom"
  55. meta.customMetadata = ["lol": "custom metadata is neat",
  56. "ちかてつ": "🚇",
  57. "shinkansen": "新幹線"]
  58. let ref = storage.reference(withPath: "ios/public/1mb")
  59. ref.updateMetadata(meta) { result in
  60. switch result {
  61. case let .success(metadata):
  62. XCTAssertEqual(meta.contentType, metadata.contentType)
  63. XCTAssertEqual(meta.customMetadata!["lol"], metadata.customMetadata!["lol"])
  64. XCTAssertEqual(meta.customMetadata!["ちかてつ"], metadata.customMetadata!["ちかてつ"])
  65. XCTAssertEqual(meta.customMetadata!["shinkansen"],
  66. metadata.customMetadata!["shinkansen"])
  67. case let .failure(error):
  68. XCTFail("Unexpected error \(error) from updateMetadata")
  69. }
  70. expectation.fulfill()
  71. }
  72. waitForExpectations()
  73. }
  74. func testDelete() throws {
  75. let expectation = self.expectation(description: #function)
  76. let ref = storage.reference(withPath: "ios/public/fileToDelete")
  77. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  78. ref.putData(data) { result in
  79. self.assertResultSuccess(result)
  80. ref.delete { error in
  81. XCTAssertNil(error, "Error should be nil")
  82. // Next delete should fail and verify the first delete succeeded.
  83. ref.delete { error in
  84. do {
  85. let nsError = try XCTUnwrap(error as? NSError)
  86. XCTAssertEqual(nsError.code, StorageErrorCode.objectNotFound.rawValue)
  87. XCTAssertEqual(
  88. nsError.localizedDescription,
  89. "Object ios/public/fileToDelete does not exist."
  90. )
  91. let userInfo = try XCTUnwrap(nsError.userInfo)
  92. let object = try XCTUnwrap(userInfo["object"] as? String)
  93. XCTAssertEqual(object, "ios/public/fileToDelete")
  94. let responseErrorCode = try XCTUnwrap(userInfo["ResponseErrorCode"] as? Int)
  95. XCTAssertEqual(responseErrorCode, 404)
  96. let responseErrorDomain = try XCTUnwrap(userInfo["ResponseErrorDomain"] as? String)
  97. XCTAssertEqual(responseErrorDomain, "com.google.HTTPStatus")
  98. let bucket = try XCTUnwrap(userInfo["bucket"] as? String)
  99. XCTAssertEqual(bucket, "ios-opensource-samples.appspot.com")
  100. expectation.fulfill()
  101. } catch {
  102. XCTFail("Unexpected unwrap failure")
  103. }
  104. }
  105. }
  106. }
  107. waitForExpectations()
  108. }
  109. func testDeleteWithNilCompletion() throws {
  110. let expectation = self.expectation(description: #function)
  111. let ref = storage.reference(withPath: "ios/public/fileToDelete")
  112. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  113. ref.putData(data) { result in
  114. self.assertResultSuccess(result)
  115. ref.delete(completion: nil)
  116. expectation.fulfill()
  117. }
  118. waitForExpectations()
  119. }
  120. func testSimplePutData() throws {
  121. let expectation = self.expectation(description: #function)
  122. let ref = storage.reference(withPath: "ios/public/testBytesUpload")
  123. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  124. ref.putData(data) { result in
  125. self.assertResultSuccess(result)
  126. expectation.fulfill()
  127. }
  128. waitForExpectations()
  129. }
  130. func testNoDeadlocks() throws {
  131. let storage2 = Storage.storage(url: "")
  132. let expectation1 = expectation(description: #function)
  133. let expectation2 = expectation(description: #function)
  134. let ref = storage.reference(withPath: "ios/public/testBytesUpload")
  135. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  136. ref.putData(data) { result in
  137. expectation1.fulfill()
  138. let ref2 = storage2.reference(withPath: "ios/public/testBytesUpload")
  139. ref2.putData(data) { result in
  140. expectation2.fulfill()
  141. }
  142. }
  143. waitForExpectations(timeout: 30)
  144. }
  145. func testSimplePutSpecialCharacter() throws {
  146. let expectation = self.expectation(description: #function)
  147. let ref = storage.reference(withPath: "ios/public/-._~!$'()*,=:@&+;")
  148. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  149. ref.putData(data) { result in
  150. self.assertResultSuccess(result)
  151. expectation.fulfill()
  152. }
  153. waitForExpectations()
  154. }
  155. func testSimplePutDataInBackgroundQueue() throws {
  156. let expectation = self.expectation(description: #function)
  157. let ref = storage.reference(withPath: "ios/public/testBytesUpload")
  158. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  159. DispatchQueue.global(qos: .background).async {
  160. ref.putData(data) { result in
  161. self.assertResultSuccess(result)
  162. expectation.fulfill()
  163. }
  164. }
  165. waitForExpectations()
  166. }
  167. func testSimplePutEmptyData() {
  168. let expectation = self.expectation(description: #function)
  169. let ref = storage.reference(withPath: "ios/public/testSimplePutEmptyData")
  170. let data = Data()
  171. ref.putData(data) { result in
  172. self.assertResultSuccess(result)
  173. expectation.fulfill()
  174. }
  175. waitForExpectations()
  176. }
  177. func testSimplePutDataUnauthorized() throws {
  178. let expectation = self.expectation(description: #function)
  179. let file = "ios/private/secretfile.txt"
  180. let ref = storage.reference(withPath: file)
  181. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  182. ref.putData(data) { result in
  183. switch result {
  184. case .success:
  185. XCTFail("Unexpected success from unauthorized putData")
  186. case let .failure(error as StorageError):
  187. switch error {
  188. case let .unauthorized(bucket, object, serverError):
  189. XCTAssertEqual(bucket, "ios-opensource-samples.appspot.com")
  190. XCTAssertEqual(object, file)
  191. XCTAssertNil(serverError)
  192. expectation.fulfill()
  193. default:
  194. XCTFail("Failed with unexpected error: \(error)")
  195. }
  196. case let .failure(error):
  197. XCTFail("Failed with unexpected error: \(error)")
  198. }
  199. }
  200. waitForExpectations()
  201. }
  202. func testSimplePutDataUnauthorizedThrow() throws {
  203. let expectation = self.expectation(description: #function)
  204. let ref = storage.reference(withPath: "ios/private/secretfile.txt")
  205. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  206. ref.putData(data) { result in
  207. do {
  208. try _ = result.get() // .failure will throw
  209. } catch {
  210. expectation.fulfill()
  211. return
  212. }
  213. XCTFail("Unexpected success from unauthorized putData")
  214. expectation.fulfill()
  215. }
  216. waitForExpectations()
  217. }
  218. func testSimplePutFile() throws {
  219. let expectation = self.expectation(description: #function)
  220. let putFileExpectation = self.expectation(description: "putFile")
  221. let ref = storage.reference(withPath: "ios/public/testSimplePutFile")
  222. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  223. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  224. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  225. try data.write(to: fileURL, options: .atomicWrite)
  226. let task = ref.putFile(from: fileURL) { result in
  227. self.assertResultSuccess(result)
  228. putFileExpectation.fulfill()
  229. }
  230. task.observe(StorageTaskStatus.success) { snapshot in
  231. XCTAssertEqual(snapshot.description, "<State: Success>")
  232. expectation.fulfill()
  233. }
  234. var uploadedBytes: Int64 = -1
  235. task.observe(StorageTaskStatus.progress) { snapshot in
  236. XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
  237. snapshot.description.starts(with: "<State: Resume"))
  238. guard let progress = snapshot.progress else {
  239. XCTFail("Failed to get snapshot.progress")
  240. return
  241. }
  242. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
  243. uploadedBytes = progress.completedUnitCount
  244. }
  245. waitForExpectations()
  246. }
  247. func testPutFileLimitedChunk() throws {
  248. defer {
  249. // Reset since tests share storage instance.
  250. storage.uploadChunkSizeBytes = Int64.max
  251. }
  252. let expectation = self.expectation(description: #function)
  253. let putFileExpectation = self.expectation(description: "putFile")
  254. let ref = storage.reference(withPath: "ios/public/testPutFilePauseResume")
  255. let bundle = Bundle(for: StorageIntegrationCommon.self)
  256. let filePath = try XCTUnwrap(bundle.path(forResource: "1mb", ofType: "dat"),
  257. "Failed to get filePath")
  258. let data = try XCTUnwrap(Data(contentsOf: URL(fileURLWithPath: filePath)),
  259. "Failed to load file")
  260. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  261. let fileURL = tmpDirURL.appendingPathComponent("LargePutFile.txt")
  262. var progressCount = 0
  263. try data.write(to: fileURL, options: .atomicWrite)
  264. // Limit the upload chunk size
  265. storage.uploadChunkSizeBytes = 256_000
  266. let task = ref.putFile(from: fileURL) { result in
  267. XCTAssertGreaterThanOrEqual(progressCount, 4)
  268. self.assertResultSuccess(result)
  269. putFileExpectation.fulfill()
  270. }
  271. task.observe(StorageTaskStatus.success) { snapshot in
  272. XCTAssertEqual(snapshot.description, "<State: Success>")
  273. expectation.fulfill()
  274. }
  275. var uploadedBytes: Int64 = -1
  276. task.observe(StorageTaskStatus.progress) { snapshot in
  277. XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
  278. snapshot.description.starts(with: "<State: Resume"))
  279. guard let progress = snapshot.progress else {
  280. XCTFail("Failed to get snapshot.progress")
  281. return
  282. }
  283. progressCount = progressCount + 1
  284. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
  285. uploadedBytes = progress.completedUnitCount
  286. }
  287. waitForExpectations()
  288. }
  289. func testPutFileTinyChunk() throws {
  290. defer {
  291. // Reset since tests share storage instance.
  292. storage.uploadChunkSizeBytes = Int64.max
  293. }
  294. let expectation = self.expectation(description: #function)
  295. let putFileExpectation = self.expectation(description: "putFile")
  296. let ref = storage.reference(withPath: "ios/public/testPutFilePauseResume")
  297. let bundle = Bundle(for: StorageIntegrationCommon.self)
  298. let filePath = try XCTUnwrap(bundle.path(forResource: "1mb", ofType: "dat"),
  299. "Failed to get filePath")
  300. let data = try XCTUnwrap(Data(contentsOf: URL(fileURLWithPath: filePath)),
  301. "Failed to load file")
  302. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  303. let fileURL = tmpDirURL.appendingPathComponent("LargePutFile.txt")
  304. var progressCount = 0
  305. try data.write(to: fileURL, options: .atomicWrite)
  306. // Limit the upload chunk size. This should behave exactly like the previous
  307. // test since small chunk sizes are rounded up to 256K.
  308. storage.uploadChunkSizeBytes = 1
  309. let task = ref.putFile(from: fileURL) { result in
  310. XCTAssertGreaterThanOrEqual(progressCount, 4)
  311. XCTAssertLessThanOrEqual(progressCount, 6)
  312. self.assertResultSuccess(result)
  313. putFileExpectation.fulfill()
  314. }
  315. task.observe(StorageTaskStatus.success) { snapshot in
  316. XCTAssertEqual(snapshot.description, "<State: Success>")
  317. expectation.fulfill()
  318. }
  319. var uploadedBytes: Int64 = -1
  320. task.observe(StorageTaskStatus.progress) { snapshot in
  321. XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
  322. snapshot.description.starts(with: "<State: Resume"))
  323. guard let progress = snapshot.progress else {
  324. XCTFail("Failed to get snapshot.progress")
  325. return
  326. }
  327. progressCount = progressCount + 1
  328. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
  329. uploadedBytes = progress.completedUnitCount
  330. }
  331. waitForExpectations()
  332. }
  333. func testAttemptToUploadDirectoryShouldFail() throws {
  334. // This `.numbers` file is actually a directory.
  335. let fileName = "HomeImprovement.numbers"
  336. let expectation = self.expectation(description: #function)
  337. let bundle = Bundle(for: StorageIntegrationCommon.self)
  338. let fileURL = try XCTUnwrap(bundle.url(forResource: fileName, withExtension: ""),
  339. "Failed to get filePath")
  340. let ref = storage.reference(withPath: "ios/public/" + fileName)
  341. ref.putFile(from: fileURL) { result in
  342. self.assertResultFailure(result)
  343. expectation.fulfill()
  344. }
  345. waitForExpectations()
  346. }
  347. func testPutFileWithSpecialCharacters() throws {
  348. let expectation = self.expectation(description: #function)
  349. let fileName = "hello&+@_ .txt"
  350. let ref = storage.reference(withPath: "ios/public/" + fileName)
  351. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  352. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  353. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  354. try data.write(to: fileURL, options: .atomicWrite)
  355. ref.putFile(from: fileURL) { result in
  356. switch result {
  357. case let .success(metadata):
  358. XCTAssertEqual(fileName, metadata.name)
  359. ref.getMetadata { result in
  360. self.assertResultSuccess(result)
  361. }
  362. case let .failure(error):
  363. XCTFail("Unexpected error \(error) from putFile")
  364. }
  365. expectation.fulfill()
  366. }
  367. waitForExpectations()
  368. }
  369. func testSimplePutDataNoMetadata() throws {
  370. let expectation = self.expectation(description: #function)
  371. let ref = storage.reference(withPath: "ios/public/testSimplePutDataNoMetadata")
  372. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  373. ref.putData(data) { result in
  374. self.assertResultSuccess(result)
  375. expectation.fulfill()
  376. }
  377. waitForExpectations()
  378. }
  379. func testSimplePutFileNoMetadata() throws {
  380. let expectation = self.expectation(description: #function)
  381. let fileName = "hello&+@_ .txt"
  382. let ref = storage.reference(withPath: "ios/public/" + fileName)
  383. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  384. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  385. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  386. try data.write(to: fileURL, options: .atomicWrite)
  387. ref.putFile(from: fileURL) { result in
  388. self.assertResultSuccess(result)
  389. expectation.fulfill()
  390. }
  391. waitForExpectations()
  392. }
  393. func testSimpleGetData() {
  394. let expectation = self.expectation(description: #function)
  395. let ref = storage.reference(withPath: "ios/public/1mb")
  396. ref.getData(maxSize: 1024 * 1024) { result in
  397. self.assertResultSuccess(result)
  398. expectation.fulfill()
  399. }
  400. waitForExpectations()
  401. }
  402. func testSimpleGetDataInBackgroundQueue() {
  403. let expectation = self.expectation(description: #function)
  404. let ref = storage.reference(withPath: "ios/public/1mb")
  405. DispatchQueue.global(qos: .background).async {
  406. ref.getData(maxSize: 1024 * 1024) { result in
  407. self.assertResultSuccess(result)
  408. expectation.fulfill()
  409. }
  410. }
  411. waitForExpectations()
  412. }
  413. func testSimpleGetDataWithCustomCallbackQueue() {
  414. let expectation = self.expectation(description: #function)
  415. let callbackQueueLabel = "customCallbackQueue"
  416. let callbackQueueKey = DispatchSpecificKey<String>()
  417. let callbackQueue = DispatchQueue(label: callbackQueueLabel)
  418. callbackQueue.setSpecific(key: callbackQueueKey, value: callbackQueueLabel)
  419. storage.callbackQueue = callbackQueue
  420. let ref = storage.reference(withPath: "ios/public/1mb")
  421. ref.getData(maxSize: 1024 * 1024) { result in
  422. self.assertResultSuccess(result)
  423. XCTAssertFalse(Thread.isMainThread)
  424. let currentQueueLabel = DispatchQueue.getSpecific(key: callbackQueueKey)
  425. XCTAssertEqual(currentQueueLabel, callbackQueueLabel)
  426. expectation.fulfill()
  427. // Reset the callbackQueue to default (main queue).
  428. self.storage.callbackQueue = DispatchQueue.main
  429. callbackQueue.setSpecific(key: callbackQueueKey, value: nil)
  430. }
  431. waitForExpectations()
  432. }
  433. func testSimpleGetDataTooSmall() {
  434. let expectation = self.expectation(description: #function)
  435. let ref = storage.reference(withPath: "ios/public/1mb")
  436. let maxSize: Int64 = 1024
  437. ref.getData(maxSize: maxSize) { result in
  438. switch result {
  439. case .success:
  440. XCTFail("Unexpected success from getData too small")
  441. case let .failure(error as StorageError):
  442. switch error {
  443. case let .downloadSizeExceeded(total, max):
  444. XCTAssertEqual(total, 1_048_576)
  445. XCTAssertEqual(max, maxSize)
  446. default:
  447. XCTFail("Failed with unexpected error: \(error)")
  448. }
  449. case let .failure(error):
  450. XCTFail("Failed with unexpected error: \(error)")
  451. }
  452. expectation.fulfill()
  453. }
  454. waitForExpectations()
  455. }
  456. func testSimpleGetDownloadURL() {
  457. let expectation = self.expectation(description: #function)
  458. let ref = storage.reference(withPath: "ios/public/1mb")
  459. // Download URL format is
  460. // "https://firebasestorage.googleapis.com:443/v0/b/{bucket}/o/{path}?alt=media&token={token}"
  461. let downloadURLPrefix =
  462. "https://firebasestorage.googleapis.com:443/v0/b/ios-opensource-samples" +
  463. ".appspot.com/o/ios%2Fpublic%2F1mb?alt=media&token"
  464. ref.downloadURL { result in
  465. switch result {
  466. case let .success(downloadURL):
  467. let urlString = downloadURL.absoluteString
  468. XCTAssertTrue(urlString.hasPrefix(downloadURLPrefix))
  469. case let .failure(error):
  470. XCTFail("Unexpected error \(error) from downloadURL")
  471. }
  472. expectation.fulfill()
  473. }
  474. waitForExpectations()
  475. }
  476. func testSimpleGetFile() throws {
  477. let expectation = self.expectation(description: #function)
  478. let ref = storage.reference(withPath: "ios/public/helloworld" + #function)
  479. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  480. let fileURL = tmpDirURL.appendingPathComponent(#function + "hello.txt")
  481. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  482. ref.putData(data) { result in
  483. switch result {
  484. case .success:
  485. let task = ref.write(toFile: fileURL)
  486. task.observe(StorageTaskStatus.success) { snapshot in
  487. do {
  488. let stringData = try String(contentsOf: fileURL, encoding: .utf8)
  489. XCTAssertEqual(stringData, "Hello Swift World")
  490. XCTAssertEqual(snapshot.description, "<State: Success>")
  491. } catch {
  492. XCTFail("Error processing success snapshot")
  493. }
  494. expectation.fulfill()
  495. }
  496. task.observe(StorageTaskStatus.progress) { snapshot in
  497. XCTAssertNil(snapshot.error, "Error should be nil")
  498. guard let progress = snapshot.progress else {
  499. XCTFail("Missing progress")
  500. return
  501. }
  502. print("\(progress.completedUnitCount) of \(progress.totalUnitCount)")
  503. }
  504. task.observe(StorageTaskStatus.failure) { snapshot in
  505. XCTAssertNil(snapshot.error, "Error should be nil")
  506. }
  507. case let .failure(error):
  508. XCTFail("Unexpected error \(error) from putData")
  509. expectation.fulfill()
  510. }
  511. }
  512. waitForExpectations()
  513. }
  514. func testCancelErrorCode() throws {
  515. let expectation = self.expectation(description: #function)
  516. let ref = storage.reference(withPath: "ios/public/helloworld" + #function)
  517. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  518. let fileURL = tmpDirURL.appendingPathComponent(#function + "hello.txt")
  519. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  520. ref.putData(data) { result in
  521. switch result {
  522. case .success:
  523. let task = ref.write(toFile: fileURL)
  524. task.observe(StorageTaskStatus.success) { snapshot in
  525. XCTFail("Error processing success snapshot")
  526. expectation.fulfill()
  527. }
  528. task.observe(StorageTaskStatus.failure) { snapshot in
  529. let expected = "User cancelled the upload/download."
  530. if let error = snapshot.error {
  531. let errorDescription = error.localizedDescription
  532. XCTAssertEqual(errorDescription, expected)
  533. let code = (error as NSError).code
  534. XCTAssertEqual(code, StorageErrorCode.cancelled.rawValue)
  535. }
  536. expectation.fulfill()
  537. }
  538. task.cancel()
  539. case let .failure(error):
  540. XCTFail("Unexpected error \(error) from putData")
  541. expectation.fulfill()
  542. }
  543. }
  544. waitForExpectations()
  545. }
  546. private func assertMetadata(actualMetadata: StorageMetadata,
  547. expectedContentType: String,
  548. expectedCustomMetadata: [String: String]) {
  549. XCTAssertEqual(actualMetadata.cacheControl, "cache-control")
  550. XCTAssertEqual(actualMetadata.contentDisposition, "content-disposition")
  551. XCTAssertEqual(actualMetadata.contentEncoding, "gzip")
  552. XCTAssertEqual(actualMetadata.contentLanguage, "de")
  553. XCTAssertEqual(actualMetadata.contentType, expectedContentType)
  554. XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
  555. for (key, value) in expectedCustomMetadata {
  556. XCTAssertEqual(actualMetadata.customMetadata![key], value)
  557. }
  558. }
  559. private func assertMetadataNil(actualMetadata: StorageMetadata) {
  560. XCTAssertNil(actualMetadata.cacheControl)
  561. XCTAssertNil(actualMetadata.contentDisposition)
  562. XCTAssertEqual(actualMetadata.contentEncoding, "identity")
  563. XCTAssertNil(actualMetadata.contentLanguage)
  564. XCTAssertNil(actualMetadata.contentType)
  565. XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
  566. XCTAssertNil(actualMetadata.customMetadata)
  567. }
  568. func testUpdateMetadata2() {
  569. let expectation = self.expectation(description: #function)
  570. let ref = storage.reference(withPath: "ios/public/1mb")
  571. let metadata = StorageMetadata()
  572. metadata.cacheControl = "cache-control"
  573. metadata.contentDisposition = "content-disposition"
  574. metadata.contentEncoding = "gzip"
  575. metadata.contentLanguage = "de"
  576. metadata.contentType = "content-type-a"
  577. metadata.customMetadata = ["a": "b"]
  578. ref.updateMetadata(metadata) { updatedMetadata, error in
  579. XCTAssertNil(error, "Error should be nil")
  580. guard let updatedMetadata = updatedMetadata else {
  581. XCTFail("Metadata is nil")
  582. expectation.fulfill()
  583. return
  584. }
  585. self.assertMetadata(actualMetadata: updatedMetadata,
  586. expectedContentType: "content-type-a",
  587. expectedCustomMetadata: ["a": "b"])
  588. let metadata = updatedMetadata
  589. metadata.contentType = "content-type-b"
  590. metadata.customMetadata = ["a": "b", "c": "d"]
  591. ref.updateMetadata(metadata) { result in
  592. switch result {
  593. case let .success(updatedMetadata):
  594. self.assertMetadata(actualMetadata: updatedMetadata,
  595. expectedContentType: "content-type-b",
  596. expectedCustomMetadata: ["a": "b", "c": "d"])
  597. metadata.cacheControl = nil
  598. metadata.contentDisposition = nil
  599. metadata.contentEncoding = nil
  600. metadata.contentLanguage = nil
  601. metadata.contentType = nil
  602. metadata.customMetadata = nil
  603. ref.updateMetadata(metadata) { result in
  604. self.assertResultSuccess(result)
  605. expectation.fulfill()
  606. }
  607. case let .failure(error):
  608. XCTFail("Unexpected error \(error) from updateMetadata")
  609. expectation.fulfill()
  610. }
  611. }
  612. }
  613. waitForExpectations()
  614. }
  615. func testResumeGetFile() {
  616. let expectation = self.expectation(description: #function)
  617. let expectationPause = self.expectation(description: "pause")
  618. let expectationResume = self.expectation(description: "resume")
  619. let ref = storage.reference().child("ios/public/1mb")
  620. let fileURL = URL(fileURLWithPath: "\(NSTemporaryDirectory())/hello.txt")
  621. let task = ref.write(toFile: fileURL)
  622. var downloadedBytes: Int64 = 0
  623. var resumeAtBytes = 256 * 1024
  624. task.observe(StorageTaskStatus.success) { snapshot in
  625. XCTAssertEqual(snapshot.description, "<State: Success>")
  626. expectation.fulfill()
  627. }
  628. task.observe(StorageTaskStatus.progress) { snapshot in
  629. let description = snapshot.description
  630. XCTAssertTrue(description.contains("State: Progress") ||
  631. description.contains("State: Resume"))
  632. let progress = snapshot.progress
  633. if let completed = progress?.completedUnitCount {
  634. XCTAssertGreaterThanOrEqual(completed, downloadedBytes)
  635. downloadedBytes = completed
  636. if completed > resumeAtBytes {
  637. task.pause()
  638. expectationPause.fulfill()
  639. resumeAtBytes = Int.max
  640. }
  641. }
  642. }
  643. task.observe(StorageTaskStatus.pause) { snapshot in
  644. XCTAssertEqual(snapshot.description, "<State: Paused>")
  645. task.resume()
  646. expectationResume.fulfill()
  647. }
  648. waitForExpectations()
  649. XCTAssertEqual(resumeAtBytes, Int.max)
  650. }
  651. func testResumeGetFileInBackgroundQueue() {
  652. let expectation = self.expectation(description: #function)
  653. let expectationPause = self.expectation(description: "pause")
  654. let expectationResume = self.expectation(description: "resume")
  655. let ref = storage.reference().child("ios/public/1mb")
  656. let fileURL = URL(fileURLWithPath: "\(NSTemporaryDirectory())/hello.txt")
  657. let task = ref.write(toFile: fileURL)
  658. var downloadedBytes: Int64 = 0
  659. var resumeAtBytes = 256 * 1024
  660. task.observe(StorageTaskStatus.success) { snapshot in
  661. XCTAssertEqual(snapshot.description, "<State: Success>")
  662. expectation.fulfill()
  663. }
  664. task.observe(StorageTaskStatus.progress) { snapshot in
  665. let description = snapshot.description
  666. XCTAssertTrue(description.contains("State: Progress") ||
  667. description.contains("State: Resume"))
  668. let progress = snapshot.progress
  669. if let completed = progress?.completedUnitCount {
  670. XCTAssertGreaterThanOrEqual(completed, downloadedBytes)
  671. downloadedBytes = completed
  672. if completed > resumeAtBytes {
  673. DispatchQueue.global(qos: .background).async {
  674. task.pause()
  675. }
  676. expectationPause.fulfill()
  677. resumeAtBytes = Int.max
  678. }
  679. }
  680. }
  681. task.observe(StorageTaskStatus.pause) { snapshot in
  682. XCTAssertEqual(snapshot.description, "<State: Paused>")
  683. DispatchQueue.global(qos: .background).async {
  684. task.resume()
  685. }
  686. expectationResume.fulfill()
  687. }
  688. waitForExpectations()
  689. XCTAssertEqual(resumeAtBytes, Int.max)
  690. }
  691. func testPagedListFiles() {
  692. let expectation = self.expectation(description: #function)
  693. let ref = storage.reference(withPath: "ios/public/list")
  694. ref.list(maxResults: 2) { result in
  695. switch result {
  696. case let .success(listResult):
  697. XCTAssertEqual(listResult.items, [ref.child("a"), ref.child("b")])
  698. XCTAssertEqual(listResult.prefixes, [])
  699. guard let pageToken = listResult.pageToken else {
  700. XCTFail("pageToken should not be nil")
  701. expectation.fulfill()
  702. return
  703. }
  704. ref.list(maxResults: 2, pageToken: pageToken) { result in
  705. switch result {
  706. case let .success(listResult):
  707. XCTAssertEqual(listResult.items, [])
  708. XCTAssertEqual(listResult.prefixes, [ref.child("prefix")])
  709. XCTAssertNil(listResult.pageToken, "pageToken should be nil")
  710. case let .failure(error):
  711. XCTFail("Unexpected error \(error) from list")
  712. }
  713. expectation.fulfill()
  714. }
  715. case let .failure(error):
  716. XCTFail("Unexpected error \(error) from list")
  717. expectation.fulfill()
  718. }
  719. }
  720. waitForExpectations()
  721. }
  722. func testPagedListFilesTooManyError() {
  723. let expectation = self.expectation(description: #function)
  724. let ref = storage.reference(withPath: "ios/public/list")
  725. ref.list(maxResults: 22222) { result in
  726. switch result {
  727. case .success:
  728. XCTFail("Unexpected success from list")
  729. case let .failure(error as StorageError):
  730. switch error {
  731. case let .invalidArgument(message):
  732. XCTAssertEqual(message, "Argument 'maxResults' must be between 1 and 1000 inclusive.")
  733. default:
  734. XCTFail("Failed with unexpected error: \(error)")
  735. }
  736. case let .failure(error):
  737. XCTFail("Failed with unexpected error: \(error)")
  738. }
  739. expectation.fulfill()
  740. }
  741. waitForExpectations()
  742. }
  743. func testListAllFiles() {
  744. let expectation = self.expectation(description: #function)
  745. let ref = storage.reference(withPath: "ios/public/list")
  746. ref.listAll { result in
  747. switch result {
  748. case let .success(listResult):
  749. XCTAssertEqual(listResult.items, [ref.child("a"), ref.child("b")])
  750. XCTAssertEqual(listResult.prefixes, [ref.child("prefix")])
  751. XCTAssertNil(listResult.pageToken, "pageToken should be nil")
  752. case let .failure(error):
  753. XCTFail("Unexpected error \(error) from list")
  754. }
  755. expectation.fulfill()
  756. }
  757. waitForExpectations()
  758. }
  759. private func waitForExpectations() {
  760. let kFIRStorageIntegrationTestTimeout = 100.0
  761. waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
  762. handler: { error in
  763. if let error {
  764. print(error)
  765. }
  766. })
  767. }
  768. private func assertResultSuccess<T>(_ result: Result<T, Error>,
  769. file: StaticString = #file, line: UInt = #line) {
  770. switch result {
  771. case let .success(value):
  772. XCTAssertNotNil(value, file: file, line: line)
  773. case let .failure(error):
  774. XCTFail("Unexpected error \(error)")
  775. }
  776. }
  777. private func assertResultFailure<T>(_ result: Result<T, Error>,
  778. file: StaticString = #file, line: UInt = #line) {
  779. switch result {
  780. case let .success(value):
  781. XCTFail("Unexpected success with value: \(value)")
  782. case let .failure(error):
  783. XCTAssertNotNil(error, file: file, line: line)
  784. }
  785. }
  786. }