TeraStateTestFixtures.swift (3264B)
1 import Foundation 2 import RadrootsKit 3 @testable import TeraApp 4 5 struct StateFixture { 6 let root: URL 7 let roots: RadrootsAppleFileRoots 8 let bootstrap: TeraConfigurationBootstrap 9 10 init() throws { 11 root = FileManager.default.temporaryDirectory 12 .appendingPathComponent("radroots-state-tests-\(UUID().uuidString.lowercased())") 13 try FileManager.default.createDirectory(at: root, withIntermediateDirectories: false) 14 roots = try RadrootsAppleFileRoots( 15 appIdentifier: "org.radroots.tests", 16 dataRoot: root.appendingPathComponent("data"), 17 cacheRoot: root.appendingPathComponent("cache"), 18 temporaryRoot: root.appendingPathComponent("tmp") 19 ) 20 bootstrap = TeraConfigurationBootstrap( 21 runtimeMode: "localhost-dev", 22 relayURLs: ["ws://127.0.0.1:8080"], 23 blossomOrigins: ["http://127.0.0.1:3000"], 24 keychainServicePrefix: "org.radroots.tests", 25 bundleIdentifier: "org.radroots.tests", 26 appMetadata: TeraRuntimeAppMetadata( 27 bundleIdentifier: "org.radroots.tests", 28 version: "0.1.0-alpha", 29 buildNumber: "1", 30 buildSHA: nil 31 ) 32 ) 33 } 34 35 func remove() { 36 try? FileManager.default.removeItem(at: root) 37 } 38 } 39 40 final class InMemorySecureStore: RadrootsSecureStore, @unchecked Sendable { 41 private let lock = NSLock() 42 private var values: [RadrootsSecureStoreKey: Data] = [:] 43 44 func put( 45 _ value: Data, 46 for key: RadrootsSecureStoreKey, 47 policy _: RadrootsSecretAccessPolicy 48 ) throws { 49 lock.withLock { values[key] = value } 50 } 51 52 func contains(_ key: RadrootsSecureStoreKey) throws -> Bool { 53 lock.withLock { values[key] != nil } 54 } 55 56 func get(_ key: RadrootsSecureStoreKey) throws -> Data? { 57 lock.withLock { values[key] } 58 } 59 60 func delete(_ key: RadrootsSecureStoreKey) throws { 61 lock.withLock { _ = values.removeValue(forKey: key) } 62 } 63 64 func deleteNamespace(_ namespace: String) throws { 65 lock.withLock { values = values.filter { $0.key.namespace != namespace } } 66 } 67 } 68 69 final class InMemoryIdentityMetadataStore: RadrootsIdentityMetadataStore, @unchecked Sendable { 70 private let lock = NSLock() 71 private var values: [RadrootsIdentityMetadataSlot: Data] = [:] 72 73 func data(for slot: RadrootsIdentityMetadataSlot) throws -> Data? { 74 lock.withLock { values[slot] } 75 } 76 77 func put(_ data: Data, for slot: RadrootsIdentityMetadataSlot) throws { 78 lock.withLock { values[slot] = data } 79 } 80 81 func delete(_ slot: RadrootsIdentityMetadataSlot) throws { 82 lock.withLock { _ = values.removeValue(forKey: slot) } 83 } 84 } 85 86 struct AllowingUserPresence: RadrootsUserPresence { 87 func currentStatus() async throws -> RadrootsUserPresenceStatus { 88 RadrootsUserPresenceStatus( 89 support: .deviceCredential, 90 biometryKind: .none, 91 canEvaluateDeviceCredential: true, 92 canEvaluateBiometrics: false 93 ) 94 } 95 96 func verify(_ request: RadrootsUserPresenceRequest) async throws -> RadrootsUserPresenceResult { 97 RadrootsUserPresenceResult(policy: request.policy, verified: true) 98 } 99 }