StorageIntegration.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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):
  189. XCTAssertEqual(bucket, "ios-opensource-samples.appspot.com")
  190. XCTAssertEqual(object, file)
  191. expectation.fulfill()
  192. default:
  193. XCTFail("Failed with unexpected error: \(error)")
  194. }
  195. case let .failure(error):
  196. XCTFail("Failed with unexpected error: \(error)")
  197. }
  198. }
  199. waitForExpectations()
  200. }
  201. func testSimplePutDataUnauthorizedThrow() throws {
  202. let expectation = self.expectation(description: #function)
  203. let ref = storage.reference(withPath: "ios/private/secretfile.txt")
  204. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  205. ref.putData(data) { result in
  206. do {
  207. try _ = result.get() // .failure will throw
  208. } catch {
  209. expectation.fulfill()
  210. return
  211. }
  212. XCTFail("Unexpected success from unauthorized putData")
  213. expectation.fulfill()
  214. }
  215. waitForExpectations()
  216. }
  217. func testSimplePutFile() throws {
  218. let expectation = self.expectation(description: #function)
  219. let putFileExpectation = self.expectation(description: "putFile")
  220. let ref = storage.reference(withPath: "ios/public/testSimplePutFile")
  221. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  222. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  223. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  224. try data.write(to: fileURL, options: .atomicWrite)
  225. let task = ref.putFile(from: fileURL) { result in
  226. self.assertResultSuccess(result)
  227. putFileExpectation.fulfill()
  228. }
  229. task.observe(StorageTaskStatus.success) { snapshot in
  230. XCTAssertEqual(snapshot.description, "<State: Success>")
  231. expectation.fulfill()
  232. }
  233. var uploadedBytes: Int64 = -1
  234. task.observe(StorageTaskStatus.progress) { snapshot in
  235. XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
  236. snapshot.description.starts(with: "<State: Resume"))
  237. guard let progress = snapshot.progress else {
  238. XCTFail("Failed to get snapshot.progress")
  239. return
  240. }
  241. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
  242. uploadedBytes = progress.completedUnitCount
  243. }
  244. waitForExpectations()
  245. }
  246. func testPutFileLimitedChunk() throws {
  247. defer {
  248. // Reset since tests share storage instance.
  249. storage.uploadChunkSizeBytes = Int64.max
  250. }
  251. let expectation = self.expectation(description: #function)
  252. let putFileExpectation = self.expectation(description: "putFile")
  253. let ref = storage.reference(withPath: "ios/public/testPutFilePauseResume")
  254. let bundle = Bundle(for: StorageIntegrationCommon.self)
  255. let filePath = try XCTUnwrap(bundle.path(forResource: "1mb", ofType: "dat"),
  256. "Failed to get filePath")
  257. let data = try XCTUnwrap(try Data(contentsOf: URL(fileURLWithPath: filePath)),
  258. "Failed to load file")
  259. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  260. let fileURL = tmpDirURL.appendingPathComponent("LargePutFile.txt")
  261. var progressCount = 0
  262. try data.write(to: fileURL, options: .atomicWrite)
  263. // Limit the upload chunk size
  264. storage.uploadChunkSizeBytes = 256_000
  265. let task = ref.putFile(from: fileURL) { result in
  266. XCTAssertGreaterThanOrEqual(progressCount, 4)
  267. self.assertResultSuccess(result)
  268. putFileExpectation.fulfill()
  269. }
  270. task.observe(StorageTaskStatus.success) { snapshot in
  271. XCTAssertEqual(snapshot.description, "<State: Success>")
  272. expectation.fulfill()
  273. }
  274. var uploadedBytes: Int64 = -1
  275. task.observe(StorageTaskStatus.progress) { snapshot in
  276. XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
  277. snapshot.description.starts(with: "<State: Resume"))
  278. guard let progress = snapshot.progress else {
  279. XCTFail("Failed to get snapshot.progress")
  280. return
  281. }
  282. progressCount = progressCount + 1
  283. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
  284. uploadedBytes = progress.completedUnitCount
  285. }
  286. waitForExpectations()
  287. }
  288. func testPutFileTinyChunk() throws {
  289. defer {
  290. // Reset since tests share storage instance.
  291. storage.uploadChunkSizeBytes = Int64.max
  292. }
  293. let expectation = self.expectation(description: #function)
  294. let putFileExpectation = self.expectation(description: "putFile")
  295. let ref = storage.reference(withPath: "ios/public/testPutFilePauseResume")
  296. let bundle = Bundle(for: StorageIntegrationCommon.self)
  297. let filePath = try XCTUnwrap(bundle.path(forResource: "1mb", ofType: "dat"),
  298. "Failed to get filePath")
  299. let data = try XCTUnwrap(try Data(contentsOf: URL(fileURLWithPath: filePath)),
  300. "Failed to load file")
  301. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  302. let fileURL = tmpDirURL.appendingPathComponent("LargePutFile.txt")
  303. var progressCount = 0
  304. try data.write(to: fileURL, options: .atomicWrite)
  305. // Limit the upload chunk size. This should behave exactly like the previous
  306. // test since small chunk sizes are rounded up to 256K.
  307. storage.uploadChunkSizeBytes = 1
  308. let task = ref.putFile(from: fileURL) { result in
  309. XCTAssertGreaterThanOrEqual(progressCount, 4)
  310. XCTAssertLessThanOrEqual(progressCount, 6)
  311. self.assertResultSuccess(result)
  312. putFileExpectation.fulfill()
  313. }
  314. task.observe(StorageTaskStatus.success) { snapshot in
  315. XCTAssertEqual(snapshot.description, "<State: Success>")
  316. expectation.fulfill()
  317. }
  318. var uploadedBytes: Int64 = -1
  319. task.observe(StorageTaskStatus.progress) { snapshot in
  320. XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
  321. snapshot.description.starts(with: "<State: Resume"))
  322. guard let progress = snapshot.progress else {
  323. XCTFail("Failed to get snapshot.progress")
  324. return
  325. }
  326. progressCount = progressCount + 1
  327. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
  328. uploadedBytes = progress.completedUnitCount
  329. }
  330. waitForExpectations()
  331. }
  332. func testAttemptToUploadDirectoryShouldFail() throws {
  333. // This `.numbers` file is actually a directory.
  334. let fileName = "HomeImprovement.numbers"
  335. let expectation = self.expectation(description: #function)
  336. let bundle = Bundle(for: StorageIntegrationCommon.self)
  337. let fileURL = try XCTUnwrap(bundle.url(forResource: fileName, withExtension: ""),
  338. "Failed to get filePath")
  339. let ref = storage.reference(withPath: "ios/public/" + fileName)
  340. ref.putFile(from: fileURL) { result in
  341. self.assertResultFailure(result)
  342. expectation.fulfill()
  343. }
  344. waitForExpectations()
  345. }
  346. func testPutFileWithSpecialCharacters() throws {
  347. let expectation = self.expectation(description: #function)
  348. let fileName = "hello&+@_ .txt"
  349. let ref = storage.reference(withPath: "ios/public/" + fileName)
  350. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  351. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  352. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  353. try data.write(to: fileURL, options: .atomicWrite)
  354. ref.putFile(from: fileURL) { result in
  355. switch result {
  356. case let .success(metadata):
  357. XCTAssertEqual(fileName, metadata.name)
  358. ref.getMetadata { result in
  359. self.assertResultSuccess(result)
  360. }
  361. case let .failure(error):
  362. XCTFail("Unexpected error \(error) from putFile")
  363. }
  364. expectation.fulfill()
  365. }
  366. waitForExpectations()
  367. }
  368. func testSimplePutDataNoMetadata() throws {
  369. let expectation = self.expectation(description: #function)
  370. let ref = storage.reference(withPath: "ios/public/testSimplePutDataNoMetadata")
  371. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  372. ref.putData(data) { result in
  373. self.assertResultSuccess(result)
  374. expectation.fulfill()
  375. }
  376. waitForExpectations()
  377. }
  378. func testSimplePutFileNoMetadata() throws {
  379. let expectation = self.expectation(description: #function)
  380. let fileName = "hello&+@_ .txt"
  381. let ref = storage.reference(withPath: "ios/public/" + fileName)
  382. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  383. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  384. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  385. try data.write(to: fileURL, options: .atomicWrite)
  386. ref.putFile(from: fileURL) { result in
  387. self.assertResultSuccess(result)
  388. expectation.fulfill()
  389. }
  390. waitForExpectations()
  391. }
  392. func testSimpleGetData() {
  393. let expectation = self.expectation(description: #function)
  394. let ref = storage.reference(withPath: "ios/public/1mb")
  395. ref.getData(maxSize: 1024 * 1024) { result in
  396. self.assertResultSuccess(result)
  397. expectation.fulfill()
  398. }
  399. waitForExpectations()
  400. }
  401. func testSimpleGetDataInBackgroundQueue() {
  402. let expectation = self.expectation(description: #function)
  403. let ref = storage.reference(withPath: "ios/public/1mb")
  404. DispatchQueue.global(qos: .background).async {
  405. ref.getData(maxSize: 1024 * 1024) { result in
  406. self.assertResultSuccess(result)
  407. expectation.fulfill()
  408. }
  409. }
  410. waitForExpectations()
  411. }
  412. func testSimpleGetDataWithCustomCallbackQueue() {
  413. let expectation = self.expectation(description: #function)
  414. let callbackQueueLabel = "customCallbackQueue"
  415. let callbackQueueKey = DispatchSpecificKey<String>()
  416. let callbackQueue = DispatchQueue(label: callbackQueueLabel)
  417. callbackQueue.setSpecific(key: callbackQueueKey, value: callbackQueueLabel)
  418. storage.callbackQueue = callbackQueue
  419. let ref = storage.reference(withPath: "ios/public/1mb")
  420. ref.getData(maxSize: 1024 * 1024) { result in
  421. self.assertResultSuccess(result)
  422. XCTAssertFalse(Thread.isMainThread)
  423. let currentQueueLabel = DispatchQueue.getSpecific(key: callbackQueueKey)
  424. XCTAssertEqual(currentQueueLabel, callbackQueueLabel)
  425. expectation.fulfill()
  426. // Reset the callbackQueue to default (main queue).
  427. self.storage.callbackQueue = DispatchQueue.main
  428. callbackQueue.setSpecific(key: callbackQueueKey, value: nil)
  429. }
  430. waitForExpectations()
  431. }
  432. func testSimpleGetDataTooSmall() {
  433. let expectation = self.expectation(description: #function)
  434. let ref = storage.reference(withPath: "ios/public/1mb")
  435. let maxSize: Int64 = 1024
  436. ref.getData(maxSize: maxSize) { result in
  437. switch result {
  438. case .success:
  439. XCTFail("Unexpected success from getData too small")
  440. case let .failure(error as StorageError):
  441. switch error {
  442. case let .downloadSizeExceeded(total, max):
  443. XCTAssertEqual(total, 1_048_576)
  444. XCTAssertEqual(max, maxSize)
  445. default:
  446. XCTFail("Failed with unexpected error: \(error)")
  447. }
  448. case let .failure(error):
  449. XCTFail("Failed with unexpected error: \(error)")
  450. }
  451. expectation.fulfill()
  452. }
  453. waitForExpectations()
  454. }
  455. func testSimpleGetDownloadURL() {
  456. let expectation = self.expectation(description: #function)
  457. let ref = storage.reference(withPath: "ios/public/1mb")
  458. // Download URL format is
  459. // "https://firebasestorage.googleapis.com:443/v0/b/{bucket}/o/{path}?alt=media&token={token}"
  460. let downloadURLPrefix =
  461. "https://firebasestorage.googleapis.com:443/v0/b/ios-opensource-samples" +
  462. ".appspot.com/o/ios%2Fpublic%2F1mb?alt=media&token"
  463. ref.downloadURL { result in
  464. switch result {
  465. case let .success(downloadURL):
  466. let urlString = downloadURL.absoluteString
  467. XCTAssertTrue(urlString.hasPrefix(downloadURLPrefix))
  468. case let .failure(error):
  469. XCTFail("Unexpected error \(error) from downloadURL")
  470. }
  471. expectation.fulfill()
  472. }
  473. waitForExpectations()
  474. }
  475. func testSimpleGetFile() throws {
  476. let expectation = self.expectation(description: #function)
  477. let ref = storage.reference(withPath: "ios/public/helloworld")
  478. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  479. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  480. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  481. ref.putData(data) { result in
  482. switch result {
  483. case .success:
  484. let task = ref.write(toFile: fileURL)
  485. task.observe(StorageTaskStatus.success) { snapshot in
  486. do {
  487. let stringData = try String(contentsOf: fileURL, encoding: .utf8)
  488. XCTAssertEqual(stringData, "Hello Swift World")
  489. XCTAssertEqual(snapshot.description, "<State: Success>")
  490. } catch {
  491. XCTFail("Error processing success snapshot")
  492. }
  493. expectation.fulfill()
  494. }
  495. task.observe(StorageTaskStatus.progress) { snapshot in
  496. XCTAssertNil(snapshot.error, "Error should be nil")
  497. guard let progress = snapshot.progress else {
  498. XCTFail("Missing progress")
  499. return
  500. }
  501. print("\(progress.completedUnitCount) of \(progress.totalUnitCount)")
  502. }
  503. task.observe(StorageTaskStatus.failure) { snapshot in
  504. XCTAssertNil(snapshot.error, "Error should be nil")
  505. }
  506. case let .failure(error):
  507. XCTFail("Unexpected error \(error) from putData")
  508. expectation.fulfill()
  509. }
  510. }
  511. waitForExpectations()
  512. }
  513. func testCancelErrorCode() throws {
  514. let expectation = self.expectation(description: #function)
  515. let ref = storage.reference(withPath: "ios/public/helloworld")
  516. let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
  517. let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
  518. let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
  519. ref.putData(data) { result in
  520. switch result {
  521. case .success:
  522. let task = ref.write(toFile: fileURL)
  523. task.cancel()
  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. case let .failure(error):
  539. XCTFail("Unexpected error \(error) from putData")
  540. expectation.fulfill()
  541. }
  542. }
  543. waitForExpectations()
  544. }
  545. private func assertMetadata(actualMetadata: StorageMetadata,
  546. expectedContentType: String,
  547. expectedCustomMetadata: [String: String]) {
  548. XCTAssertEqual(actualMetadata.cacheControl, "cache-control")
  549. XCTAssertEqual(actualMetadata.contentDisposition, "content-disposition")
  550. XCTAssertEqual(actualMetadata.contentEncoding, "gzip")
  551. XCTAssertEqual(actualMetadata.contentLanguage, "de")
  552. XCTAssertEqual(actualMetadata.contentType, expectedContentType)
  553. XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
  554. for (key, value) in expectedCustomMetadata {
  555. XCTAssertEqual(actualMetadata.customMetadata![key], value)
  556. }
  557. }
  558. private func assertMetadataNil(actualMetadata: StorageMetadata) {
  559. XCTAssertNil(actualMetadata.cacheControl)
  560. XCTAssertNil(actualMetadata.contentDisposition)
  561. XCTAssertEqual(actualMetadata.contentEncoding, "identity")
  562. XCTAssertNil(actualMetadata.contentLanguage)
  563. XCTAssertNil(actualMetadata.contentType)
  564. XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
  565. XCTAssertNil(actualMetadata.customMetadata)
  566. }
  567. func testUpdateMetadata2() {
  568. let expectation = self.expectation(description: #function)
  569. let ref = storage.reference(withPath: "ios/public/1mb")
  570. let metadata = StorageMetadata()
  571. metadata.cacheControl = "cache-control"
  572. metadata.contentDisposition = "content-disposition"
  573. metadata.contentEncoding = "gzip"
  574. metadata.contentLanguage = "de"
  575. metadata.contentType = "content-type-a"
  576. metadata.customMetadata = ["a": "b"]
  577. ref.updateMetadata(metadata) { updatedMetadata, error in
  578. XCTAssertNil(error, "Error should be nil")
  579. guard let updatedMetadata = updatedMetadata else {
  580. XCTFail("Metadata is nil")
  581. expectation.fulfill()
  582. return
  583. }
  584. self.assertMetadata(actualMetadata: updatedMetadata,
  585. expectedContentType: "content-type-a",
  586. expectedCustomMetadata: ["a": "b"])
  587. let metadata = updatedMetadata
  588. metadata.contentType = "content-type-b"
  589. metadata.customMetadata = ["a": "b", "c": "d"]
  590. ref.updateMetadata(metadata) { result in
  591. switch result {
  592. case let .success(updatedMetadata):
  593. self.assertMetadata(actualMetadata: updatedMetadata,
  594. expectedContentType: "content-type-b",
  595. expectedCustomMetadata: ["a": "b", "c": "d"])
  596. metadata.cacheControl = nil
  597. metadata.contentDisposition = nil
  598. metadata.contentEncoding = nil
  599. metadata.contentLanguage = nil
  600. metadata.contentType = nil
  601. metadata.customMetadata = nil
  602. ref.updateMetadata(metadata) { result in
  603. self.assertResultSuccess(result)
  604. expectation.fulfill()
  605. }
  606. case let .failure(error):
  607. XCTFail("Unexpected error \(error) from updateMetadata")
  608. expectation.fulfill()
  609. }
  610. }
  611. }
  612. waitForExpectations()
  613. }
  614. func testResumeGetFile() {
  615. let expectation = self.expectation(description: #function)
  616. let expectationPause = self.expectation(description: "pause")
  617. let expectationResume = self.expectation(description: "resume")
  618. let ref = storage.reference().child("ios/public/1mb")
  619. let fileURL = URL(fileURLWithPath: "\(NSTemporaryDirectory())/hello.txt")
  620. let task = ref.write(toFile: fileURL)
  621. var downloadedBytes: Int64 = 0
  622. var resumeAtBytes = 256 * 1024
  623. task.observe(StorageTaskStatus.success) { snapshot in
  624. XCTAssertEqual(snapshot.description, "<State: Success>")
  625. expectation.fulfill()
  626. }
  627. task.observe(StorageTaskStatus.progress) { snapshot in
  628. let description = snapshot.description
  629. XCTAssertTrue(description.contains("State: Progress") ||
  630. description.contains("State: Resume"))
  631. let progress = snapshot.progress
  632. if let completed = progress?.completedUnitCount {
  633. XCTAssertGreaterThanOrEqual(completed, downloadedBytes)
  634. downloadedBytes = completed
  635. if completed > resumeAtBytes {
  636. task.pause()
  637. expectationPause.fulfill()
  638. resumeAtBytes = Int.max
  639. }
  640. }
  641. }
  642. task.observe(StorageTaskStatus.pause) { snapshot in
  643. XCTAssertEqual(snapshot.description, "<State: Paused>")
  644. task.resume()
  645. expectationResume.fulfill()
  646. }
  647. waitForExpectations()
  648. XCTAssertEqual(resumeAtBytes, Int.max)
  649. }
  650. func testResumeGetFileInBackgroundQueue() {
  651. let expectation = self.expectation(description: #function)
  652. let expectationPause = self.expectation(description: "pause")
  653. let expectationResume = self.expectation(description: "resume")
  654. let ref = storage.reference().child("ios/public/1mb")
  655. let fileURL = URL(fileURLWithPath: "\(NSTemporaryDirectory())/hello.txt")
  656. let task = ref.write(toFile: fileURL)
  657. var downloadedBytes: Int64 = 0
  658. var resumeAtBytes = 256 * 1024
  659. task.observe(StorageTaskStatus.success) { snapshot in
  660. XCTAssertEqual(snapshot.description, "<State: Success>")
  661. expectation.fulfill()
  662. }
  663. task.observe(StorageTaskStatus.progress) { snapshot in
  664. let description = snapshot.description
  665. XCTAssertTrue(description.contains("State: Progress") ||
  666. description.contains("State: Resume"))
  667. let progress = snapshot.progress
  668. if let completed = progress?.completedUnitCount {
  669. XCTAssertGreaterThanOrEqual(completed, downloadedBytes)
  670. downloadedBytes = completed
  671. if completed > resumeAtBytes {
  672. DispatchQueue.global(qos: .background).async {
  673. task.pause()
  674. }
  675. expectationPause.fulfill()
  676. resumeAtBytes = Int.max
  677. }
  678. }
  679. }
  680. task.observe(StorageTaskStatus.pause) { snapshot in
  681. XCTAssertEqual(snapshot.description, "<State: Paused>")
  682. DispatchQueue.global(qos: .background).async {
  683. task.resume()
  684. }
  685. expectationResume.fulfill()
  686. }
  687. waitForExpectations()
  688. XCTAssertEqual(resumeAtBytes, Int.max)
  689. }
  690. func testPagedListFiles() {
  691. let expectation = self.expectation(description: #function)
  692. let ref = storage.reference(withPath: "ios/public/list")
  693. ref.list(maxResults: 2) { result in
  694. switch result {
  695. case let .success(listResult):
  696. XCTAssertEqual(listResult.items, [ref.child("a"), ref.child("b")])
  697. XCTAssertEqual(listResult.prefixes, [])
  698. guard let pageToken = listResult.pageToken else {
  699. XCTFail("pageToken should not be nil")
  700. expectation.fulfill()
  701. return
  702. }
  703. ref.list(maxResults: 2, pageToken: pageToken) { result in
  704. switch result {
  705. case let .success(listResult):
  706. XCTAssertEqual(listResult.items, [])
  707. XCTAssertEqual(listResult.prefixes, [ref.child("prefix")])
  708. XCTAssertNil(listResult.pageToken, "pageToken should be nil")
  709. case let .failure(error):
  710. XCTFail("Unexpected error \(error) from list")
  711. }
  712. expectation.fulfill()
  713. }
  714. case let .failure(error):
  715. XCTFail("Unexpected error \(error) from list")
  716. expectation.fulfill()
  717. }
  718. }
  719. waitForExpectations()
  720. }
  721. func testPagedListFilesTooManyError() {
  722. let expectation = self.expectation(description: #function)
  723. let ref = storage.reference(withPath: "ios/public/list")
  724. ref.list(maxResults: 22222) { result in
  725. switch result {
  726. case .success:
  727. XCTFail("Unexpected success from list")
  728. case let .failure(error as StorageError):
  729. switch error {
  730. case let .invalidArgument(message):
  731. XCTAssertEqual(message, "Argument 'maxResults' must be between 1 and 1000 inclusive.")
  732. default:
  733. XCTFail("Failed with unexpected error: \(error)")
  734. }
  735. case let .failure(error):
  736. XCTFail("Failed with unexpected error: \(error)")
  737. }
  738. expectation.fulfill()
  739. }
  740. waitForExpectations()
  741. }
  742. func testListAllFiles() {
  743. let expectation = self.expectation(description: #function)
  744. let ref = storage.reference(withPath: "ios/public/list")
  745. ref.listAll { result in
  746. switch result {
  747. case let .success(listResult):
  748. XCTAssertEqual(listResult.items, [ref.child("a"), ref.child("b")])
  749. XCTAssertEqual(listResult.prefixes, [ref.child("prefix")])
  750. XCTAssertNil(listResult.pageToken, "pageToken should be nil")
  751. case let .failure(error):
  752. XCTFail("Unexpected error \(error) from list")
  753. }
  754. expectation.fulfill()
  755. }
  756. waitForExpectations()
  757. }
  758. private func waitForExpectations() {
  759. let kFIRStorageIntegrationTestTimeout = 100.0
  760. waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
  761. handler: { error in
  762. if let error = error {
  763. print(error)
  764. }
  765. })
  766. }
  767. private func assertResultSuccess<T>(_ result: Result<T, Error>,
  768. file: StaticString = #file, line: UInt = #line) {
  769. switch result {
  770. case let .success(value):
  771. XCTAssertNotNil(value, file: file, line: line)
  772. case let .failure(error):
  773. XCTFail("Unexpected error \(error)")
  774. }
  775. }
  776. private func assertResultFailure<T>(_ result: Result<T, Error>,
  777. file: StaticString = #file, line: UInt = #line) {
  778. switch result {
  779. case let .success(value):
  780. XCTFail("Unexpected success with value: \(value)")
  781. case let .failure(error):
  782. XCTAssertNotNil(error, file: file, line: line)
  783. }
  784. }
  785. }