FakeAuthKeychainStorage.swift 2.8 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. @testable import FirebaseAuth
  15. import Foundation
  16. import XCTest
  17. /** @class AuthKeychainStorage
  18. @brief The utility class to update the real keychain
  19. */
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class FakeAuthKeychainStorage: AuthKeychainStorage {
  22. // Fake Keychain. It's a dictionary, keyed by service name, for each key-value store dictionary
  23. private var fakeKeychain: [String: [String: Any]] = [:]
  24. private var fakeLegacyKeychain: [String: Any] = [:]
  25. func get(query: [String: Any], result: inout AnyObject?) -> OSStatus {
  26. if let service = queryService(query) {
  27. guard let value = fakeKeychain[service]?[queryKey(query)] else {
  28. return errSecItemNotFound
  29. }
  30. let returnArrayofDictionary = [[kSecValueData as String: value]]
  31. result = returnArrayofDictionary as AnyObject
  32. return noErr
  33. } else {
  34. guard let value = fakeLegacyKeychain[queryKey(query)] else {
  35. return errSecItemNotFound
  36. }
  37. let returnArrayofDictionary = [[kSecValueData as String: value]]
  38. result = returnArrayofDictionary as AnyObject
  39. return noErr
  40. }
  41. }
  42. func add(query: [String: Any]) -> OSStatus {
  43. if let service = queryService(query) {
  44. fakeKeychain[service]?[queryKey(query)] = query[kSecValueData as String]
  45. } else {
  46. fakeLegacyKeychain[queryKey(query)] = query[kSecValueData as String]
  47. }
  48. return noErr
  49. }
  50. func update(query: [String: Any], attributes: [String: Any]) -> OSStatus {
  51. return add(query: query)
  52. }
  53. @discardableResult func delete(query: [String: Any]) -> OSStatus {
  54. if let service = queryService(query) {
  55. fakeKeychain[service]?[queryKey(query)] = nil
  56. } else {
  57. fakeLegacyKeychain[queryKey(query)] = nil
  58. }
  59. return noErr
  60. }
  61. private func queryKey(_ query: [String: Any]) -> String {
  62. do {
  63. return try XCTUnwrap(query[kSecAttrAccount as String] as? String)
  64. } catch {
  65. XCTFail("\(error)")
  66. return ""
  67. }
  68. }
  69. private func queryService(_ query: [String: Any]) -> String? {
  70. guard let service = query[kSecAttrService as String] as? String else {
  71. return nil
  72. }
  73. if fakeKeychain[service] == nil {
  74. fakeKeychain[service] = [:]
  75. }
  76. return service
  77. }
  78. }