StorageTests.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2022 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 Foundation
  15. import FirebaseCore
  16. @testable import FirebaseStorage
  17. import FirebaseAppCheckInterop
  18. import FirebaseAuthInterop
  19. import SharedTestUtilities
  20. import XCTest
  21. class StorageTests: XCTestCase {
  22. static var app: FirebaseApp?
  23. override class func setUp() {
  24. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  25. gcmSenderID: "00000000000000000-00000000000-000000000")
  26. options.projectID = "myProjectID"
  27. FirebaseApp.configure(name: "test", options: options)
  28. app = FirebaseApp.app(name: "test")
  29. }
  30. func testBucketNotEnforced() throws {
  31. let app = try getApp(bucket: "")
  32. let storage = Storage.storage(app: app)
  33. _ = storage.reference(forURL: "gs://benwu-test1.storage.firebase.com/child")
  34. _ = storage.reference(forURL: "gs://benwu-test2.storage.firebase.com/child")
  35. }
  36. func testBucketEnforced() throws {
  37. let app = try getApp(bucket: "bucket")
  38. let storage = Storage.storage(app: app, url: "gs://benwu-test1.storage.firebase.com")
  39. _ = storage.reference(forURL: "gs://benwu-test1.storage.firebase.com/child")
  40. XCTAssertThrowsObjCException {
  41. _ = storage.reference(forURL: "gs://benwu-test2.storage.firebase.com/child")
  42. }
  43. }
  44. func testInitWithCustomURL() throws {
  45. let app = try getApp(bucket: "bucket")
  46. let storage = Storage.storage(app: app, url: "gs://foo-bar.appspot.com")
  47. XCTAssertEqual("gs://foo-bar.appspot.com/", storage.reference().description)
  48. let storage2 = Storage.storage(app: app, url: "gs://foo-bar.appspot.com/")
  49. XCTAssertEqual("gs://foo-bar.appspot.com/", storage2.reference().description)
  50. }
  51. func testInitWithWrongScheme() throws {
  52. let app = try getApp(bucket: "bucket")
  53. XCTAssertThrowsObjCException {
  54. _ = Storage.storage(app: app, url: "http://foo-bar.appspot.com")
  55. }
  56. }
  57. func testInitWithNoScheme() throws {
  58. let app = try getApp(bucket: "bucket")
  59. XCTAssertThrowsObjCException {
  60. _ = Storage.storage(app: app, url: "foo-bar.appspot.com")
  61. }
  62. }
  63. func testInitWithoutURL() throws {
  64. let app = try getApp(bucket: "bucket")
  65. XCTAssertNoThrowObjCException {
  66. _ = Storage.storage(app: app)
  67. }
  68. }
  69. func testInitWithPath() throws {
  70. let app = try getApp(bucket: "bucket")
  71. XCTAssertThrowsObjCException {
  72. _ = Storage.storage(app: app, url: "gs://foo-bar.appspot.com/child")
  73. }
  74. }
  75. func testInitWithDefaultAndCustomURL() throws {
  76. let app = try getApp(bucket: "bucket")
  77. let defaultInstance = Storage.storage(app: app)
  78. let customInstance = Storage.storage(app: app, url: "gs://foo-bar.appspot.com")
  79. XCTAssertEqual("gs://foo-bar.appspot.com/", customInstance.reference().description)
  80. XCTAssertEqual("gs://bucket/", defaultInstance.reference().description)
  81. }
  82. func testStorageDefaultApp() throws {
  83. let app = try getApp(bucket: "bucket")
  84. let storage = Storage.storage(app: app)
  85. XCTAssertEqual(storage.app.name, app.name)
  86. }
  87. func testStorageNoBucketInConfig() throws {
  88. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  89. gcmSenderID: "00000000000000000-00000000000-000000000")
  90. options.projectID = "myProjectID"
  91. let name = "StorageTestsNil"
  92. let app = FirebaseApp(instanceWithName: name, options: options)
  93. XCTAssertThrowsObjCException {
  94. _ = Storage.storage(app: app)
  95. }
  96. }
  97. func testStorageEmptyBucketInConfig() throws {
  98. let app = try getApp(bucket: "")
  99. let storage = Storage.storage(app: app)
  100. let ref = storage.reference(forURL: "gs://bucket/path/to/object")
  101. XCTAssertEqual(ref.bucket, "bucket")
  102. }
  103. func testStorageWrongBucketInConfig() throws {
  104. let app = try getApp(bucket: "notMyBucket")
  105. let storage = Storage.storage(app: app)
  106. XCTAssertThrowsObjCException {
  107. _ = storage.reference(forURL: "gs://bucket/path/to/object")
  108. }
  109. }
  110. func testUseEmulator() throws {
  111. let app = try getApp(bucket: "bucket-for-testUseEmulator")
  112. let storage = Storage.storage(app: app, url: "gs://foo-bar.appspot.com")
  113. storage.useEmulator(withHost: "localhost", port: 8080)
  114. XCTAssertNoThrow(storage.reference())
  115. }
  116. func testUseEmulatorValidatesHost() throws {
  117. let app = try getApp(bucket: "bucket")
  118. let storage = Storage.storage(app: app, url: "gs://foo-bar.appspot.com")
  119. XCTAssertThrowsObjCException {
  120. storage.useEmulator(withHost: "", port: 8080)
  121. }
  122. }
  123. func testUseEmulatorValidatesPort() throws {
  124. let app = try getApp(bucket: "bucket")
  125. let storage = Storage.storage(app: app, url: "gs://foo-bar.appspot.com")
  126. XCTAssertThrowsObjCException {
  127. storage.useEmulator(withHost: "localhost", port: -1)
  128. }
  129. }
  130. func testUseEmulatorCannotBeCalledAfterObtainingReference() throws {
  131. let app = try getApp(bucket: "bucket")
  132. let storage = Storage.storage(app: app, url: "gs://benwu-test1.storage.firebase.com")
  133. _ = storage.reference()
  134. XCTAssertThrowsObjCException {
  135. storage.useEmulator(withHost: "localhost", port: 8080)
  136. }
  137. }
  138. func testEqual() throws {
  139. let app = try getApp(bucket: "bucket")
  140. let storage = Storage.storage(app: app)
  141. let copy = try XCTUnwrap(storage.copy() as? Storage)
  142. XCTAssertEqual(storage.app.name, copy.app.name)
  143. XCTAssertEqual(storage.hash, copy.hash)
  144. }
  145. func testNotEqual() throws {
  146. let app = try getApp(bucket: "bucket")
  147. let storage = Storage.storage(app: app)
  148. let secondApp = try getApp(bucket: "bucket2")
  149. let storage2 = Storage.storage(app: secondApp)
  150. XCTAssertNotEqual(storage, storage2)
  151. XCTAssertNotEqual(storage.hash, storage2.hash)
  152. }
  153. func testHash() throws {
  154. let app = try getApp(bucket: "bucket")
  155. let storage = Storage.storage(app: app)
  156. let copy = try XCTUnwrap(storage.copy() as? Storage)
  157. XCTAssertEqual(storage.app.name, copy.app.name)
  158. }
  159. // MARK: Private Helpers
  160. // Cache the app associated with each Storage bucket
  161. private static var appDictionary: [String: FirebaseApp] = [:]
  162. private func getApp(bucket: String) throws -> FirebaseApp {
  163. let savedApp = StorageTests.appDictionary[bucket]
  164. guard savedApp == nil else {
  165. return try XCTUnwrap(savedApp)
  166. }
  167. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  168. gcmSenderID: "00000000000000000-00000000000-000000000")
  169. options.projectID = "myProjectID"
  170. options.storageBucket = bucket
  171. let name = "StorageTests\(bucket)"
  172. let app = FirebaseApp(instanceWithName: name, options: options)
  173. StorageTests.appDictionary[bucket] = app
  174. return app
  175. }
  176. private func XCTAssertThrowsObjCException(_ closure: @escaping () -> Void) {
  177. XCTAssertThrowsError(try ExceptionCatcher.catchException(closure))
  178. }
  179. private func XCTAssertNoThrowObjCException(_ closure: @escaping () -> Void) {
  180. XCTAssertNoThrow(try ExceptionCatcher.catchException(closure))
  181. }
  182. }