AuthUserDefaultsTests.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. import FirebaseCore
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class AuthUserDefaultsTests: XCTestCase {
  20. let kKey = "ACCOUNT"
  21. let kData = "DATA"
  22. var storage: AuthUserDefaults!
  23. override func setUp() {
  24. storage = AuthUserDefaults(service: "SERVICE")
  25. storage.clear()
  26. }
  27. /** @fn testReadNonexisting
  28. @brief Tests reading non-existing storage item.
  29. */
  30. func testReadNonexisting() throws {
  31. XCTAssertNil(try storage.data(forKey: kKey))
  32. }
  33. /** @fn testWriteRead
  34. @brief Tests writing and reading a storage item.
  35. */
  36. func testWriteRead() throws {
  37. try storage.setData(dataFromString(kData), forKey: kKey)
  38. XCTAssertEqual(try storage.data(forKey: kKey), try dataFromString(kData))
  39. }
  40. /** @fn testOverwrite
  41. @brief Tests overwriting a storage item.
  42. */
  43. func testOverwrite() throws {
  44. let kOtherData = "OTHER_DATA"
  45. try storage.setData(dataFromString(kData), forKey: kKey)
  46. try storage.setData(dataFromString(kOtherData), forKey: kKey)
  47. XCTAssertEqual(try storage.data(forKey: kKey), try dataFromString(kOtherData))
  48. }
  49. /** @fn testRemove
  50. @brief Tests removing a storage item.
  51. */
  52. func testRemove() throws {
  53. try storage.setData(dataFromString(kData), forKey: kKey)
  54. storage = AuthUserDefaults(service: "O")
  55. }
  56. /** @fn testServices
  57. @brief Tests storage items belonging to different services doesn't affect each other.
  58. */
  59. func testServices() throws {
  60. try storage.setData(dataFromString(kData), forKey: kKey)
  61. storage = AuthUserDefaults(service: "Other service")
  62. XCTAssertNil(try storage.data(forKey: kKey))
  63. }
  64. /** @fn testStandardUserDefaults
  65. @brief Tests standard user defaults are not affected by FIRAuthUserDefaults operations,
  66. */
  67. func testStandardUserDefaults() throws {
  68. let userDefaults = UserDefaults.standard
  69. let bundleID = try XCTUnwrap(Bundle.main.bundleIdentifier)
  70. let count = userDefaults.persistentDomain(forName: bundleID)?.count
  71. try storage.setData(dataFromString(kData), forKey: kKey)
  72. XCTAssertEqual(count, userDefaults.persistentDomain(forName: bundleID)?.count)
  73. }
  74. private func dataFromString(_ str: String) throws -> Data {
  75. return try XCTUnwrap(str.data(using: .utf8))
  76. }
  77. }