AppCheckE2ETests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2023 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 FirebaseAppCheck
  15. import FirebaseCore
  16. import XCTest
  17. final class AppCheckE2ETests: XCTestCase {
  18. let appName = "test_app_name"
  19. var app: FirebaseApp!
  20. override func setUp() {
  21. AppCheck.setAppCheckProviderFactory(TestAppCheckProviderFactory())
  22. let options = FirebaseOptions(
  23. googleAppID: "1:123456789:ios:abc123",
  24. gcmSenderID: "123456789"
  25. )
  26. options.projectID = "test_project_id"
  27. options.apiKey = "test_api_key"
  28. FirebaseApp.configure(name: appName, options: options)
  29. app = FirebaseApp.app(name: appName)
  30. }
  31. override func tearDown() {
  32. let semaphore = DispatchSemaphore(value: 0)
  33. app.delete { _ in
  34. semaphore.signal()
  35. }
  36. semaphore.wait()
  37. }
  38. func testInitAppCheck() throws {
  39. let appCheck = AppCheck.appCheck(app: app)
  40. XCTAssertNotNil(appCheck)
  41. }
  42. func testInitAppCheckDebugProvider() throws {
  43. let debugProvider = AppCheckDebugProvider(app: app)
  44. XCTAssertNotNil(debugProvider)
  45. }
  46. func testInitAppCheckDebugProviderFactory() throws {
  47. let debugProvider = AppCheckDebugProviderFactory().createProvider(with: app)
  48. XCTAssertNotNil(debugProvider)
  49. }
  50. @available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, watchOS 9.0, *)
  51. func testInitDeviceCheckProvider() throws {
  52. let deviceCheckProvider = DeviceCheckProvider(app: app)
  53. XCTAssertNotNil(deviceCheckProvider)
  54. }
  55. @available(iOS 11.0, macOS 10.15, macCatalyst 13.0, tvOS 11.0, watchOS 9.0, *)
  56. func testDeviceCheckProviderFactoryCreate() throws {
  57. let deviceCheckProvider = DeviceCheckProviderFactory().createProvider(with: app)
  58. XCTAssertNotNil(deviceCheckProvider)
  59. }
  60. @available(iOS 14.0, macOS 11.3, macCatalyst 14.5, tvOS 15.0, watchOS 9.0, *)
  61. func testInitAppAttestProvider() throws {
  62. let appAttestProvider = AppAttestProvider(app: app)
  63. XCTAssertNotNil(appAttestProvider)
  64. }
  65. // The following test is disabled on macOS since `token(forcingRefresh:handler:)` requires a
  66. // provisioning profile to access the keychain to cache tokens.
  67. // See go/firebase-macos-keychain-popups for more details.
  68. #if !os(macOS) && !targetEnvironment(macCatalyst)
  69. func testGetToken() throws {
  70. guard let appCheck = AppCheck.appCheck(app: app) else {
  71. XCTFail("AppCheck instance is nil.")
  72. return
  73. }
  74. let expectation = XCTestExpectation()
  75. appCheck.token(forcingRefresh: true) { token, error in
  76. XCTAssertNil(error)
  77. XCTAssertNotNil(token)
  78. XCTAssertEqual(token?.token, TestAppCheckProvider.tokenValue)
  79. expectation.fulfill()
  80. }
  81. wait(for: [expectation], timeout: 0.5)
  82. }
  83. #endif // !os(macOS) && !targetEnvironment(macCatalyst)
  84. func testGetLimitedUseToken() throws {
  85. guard let appCheck = AppCheck.appCheck(app: app) else {
  86. XCTFail("AppCheck instance is nil.")
  87. return
  88. }
  89. let expectation = XCTestExpectation()
  90. appCheck.limitedUseToken { token, error in
  91. XCTAssertNil(error)
  92. XCTAssertNotNil(token)
  93. XCTAssertEqual(token!.token, TestAppCheckProvider.limitedUseTokenValue)
  94. expectation.fulfill()
  95. }
  96. wait(for: [expectation], timeout: 0.5)
  97. }
  98. }
  99. class TestAppCheckProvider: NSObject, AppCheckProvider {
  100. static let tokenValue = "TestToken"
  101. static let limitedUseTokenValue = "TestLimitedUseToken"
  102. func getToken(completion handler: @escaping (AppCheckToken?, Error?) -> Void) {
  103. let token = AppCheckToken(
  104. token: TestAppCheckProvider.tokenValue,
  105. expirationDate: Date.distantFuture
  106. )
  107. handler(token, nil)
  108. }
  109. func getLimitedUseToken(completion handler: @escaping (AppCheckToken?, Error?) -> Void) {
  110. let token = AppCheckToken(
  111. token: TestAppCheckProvider.limitedUseTokenValue,
  112. expirationDate: Date.distantFuture
  113. )
  114. handler(token, nil)
  115. }
  116. }
  117. class TestAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
  118. func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
  119. return TestAppCheckProvider()
  120. }
  121. }