Repository Public
1// SP Drive iOS2// File: KitTests/Harness.swift3// Description: Test rig: simulated devices (own database and file storage) signed in to the disposable backend, and a network gate4// that cuts a device off to play offline scenarios. No test double stands in for the server.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Synchronization10import Testing11@testable import SPDriveKit1213enum TestEnv {14 static let values: [String: String] = {15 let path = NSString(string: "~/.config/spdrive-ios-test/credentials.env").expandingTildeInPath16 var out: [String: String] = [:]17 for line in (try? String(contentsOfFile: path, encoding: .utf8))?.split(separator: "\n") ?? [] {18 let parts = line.split(separator: "=", maxSplits: 1)19 if parts.count == 2 { out[String(parts[0])] = String(parts[1]) }20 }21 return out22 }()23 static var tokenA: String { values["SPDRIVE_TEST_TOKEN"] ?? "" }24 static var tokenB: String { values["SPDRIVE_TEST_TOKEN_B"] ?? "" }25 static var tokenC: String { values["SPDRIVE_TEST_TOKEN_C"] ?? "" }26 static var emailC: String { values["SPDRIVE_TEST_EMAIL_C"] ?? "" }27 static var available: Bool { !tokenA.isEmpty }28 /// The 2.0 tests also need the third account (grants).29 static var next: Bool { available && !tokenC.isEmpty }30}3132/// Fails every request to a host while that host is « unplugged ». Each simulated device reaches the backend through its own host name.33final class NetworkGate: URLProtocol, @unchecked Sendable {34 static let down = Mutex<Set<String>>([])35 /// Requests whose path contains the key fail this many more times.36 static let failures = Mutex<[String: Int]>([:])37 static func set(_ host: String, offline: Bool) { down.withLock { if offline { $0.insert(host) } else { $0.remove(host) } } }38 static func failNext(_ n: Int, pathContaining key: String, code: URLError.Code = .notConnectedToInternet) { failures.withLock { $0[key] = n }; codes.withLock { $0[key] = code } }39 static let codes = Mutex<[String: URLError.Code]>([:])40 /// Requests whose URL contains one of these keys are accepted and never answered (a connection that hangs) until the key is removed.41 static let stalled = Mutex<Set<String>>([])42 static func stall(_ key: String, _ on: Bool) { stalled.withLock { if on { $0.insert(key) } else { $0.remove(key) } } }43 /// How many requests were seen (stalled or failed) per key.44 static let seen = Mutex<[String: Int]>([:])4546 override class func canInit(with request: URLRequest) -> Bool {47 guard let url = request.url, let host = url.host else { return false }48 if down.withLock({ $0.contains(host) }) { return true }49 if let key = stalled.withLock({ $0.first { url.absoluteString.contains($0) } }) { seen.withLock { $0[key, default: 0] += 1 }; return true }50 return failures.withLock { f in51 for (key, n) in f where n > 0 && url.absoluteString.contains(key) { f[key] = n - 1; return true }52 return false53 }54 }55 override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }56 private let stopped = Mutex(false)57 override func startLoading() {58 let url = request.url?.absoluteString ?? ""59 if Self.isStalled(url) {60 // a connection that hangs; when the key is released it breaks with a timeout, as a real stalled connection would61 waitForRelease(url)62 return63 }64 let code = Self.codes.withLock { c in c.first { url.contains($0.key) }?.value } ?? .notConnectedToInternet65 client?.urlProtocol(self, didFailWithError: URLError(Self.down.withLock { $0.contains(request.url?.host ?? "") } ? .notConnectedToInternet : code))66 }67 override func stopLoading() { stopped.withLock { $0 = true } }68 static func isStalled(_ url: String) -> Bool { stalled.withLock { $0.contains { url.contains($0) } } }69 private func waitForRelease(_ url: String) {70 DispatchQueue.global().asyncAfter(deadline: .now() + 0.04) { [self] in71 if stopped.withLock({ $0 }) { return }72 if Self.isStalled(url) { waitForRelease(url) } else { client?.urlProtocol(self, didFailWithError: URLError(.timedOut)) }73 }74 }75}7677/// A value shared between a test and the engine's closures.78final class Shared<T: Sendable>: @unchecked Sendable {79 private let lock = NSLock()80 private var value: T81 init(_ v: T) { value = v }82 @discardableResult func withLock<R>(_ body: (inout T) -> R) -> R { lock.lock(); defer { lock.unlock() }; return body(&value) }83}8485/// One simulated device.86final class Device: Sendable {87 let host: String88 let root: URL89 let store: DriveStore90 let files: LocalFiles91 let api: DriveAPI92 let engine: SyncEngine9394 /// `host` distinguishes devices for the network gate: "localhost" and "127.0.0.1" both reach the tunnel.95 let conditions = Shared(TransferConditions.ideal)96 let policy = Shared(TransferPolicy.default)9798 init(token: String = TestEnv.tokenA, host: String = "localhost", reuse root: URL? = nil, concurrency: Int = 3, driver: (@Sendable (URL, URLSessionConfiguration) -> TransferDriver)? = nil) async throws {99 self.host = host100 self.root = root ?? FileManager.default.temporaryDirectory.appendingPathComponent("spdrive-test-\(UUID().uuidString)", isDirectory: true)101 try FileManager.default.createDirectory(at: self.root, withIntermediateDirectories: true)102 store = try DriveStore(path: self.root.appendingPathComponent("drive.sqlite").path)103 files = LocalFiles(persistent: self.root.appendingPathComponent("persistent"), cache: self.root.appendingPathComponent("cache"))104 let address = "10.\(Int.random(in: 1...250)).\(Int.random(in: 1...250)).\(Int.random(in: 1...250))"105 func config() -> URLSessionConfiguration {106 let c = URLSessionConfiguration.ephemeral107 c.protocolClasses = [NetworkGate.self] + (c.protocolClasses ?? [])108 c.timeoutIntervalForRequest = 30109 // each simulated device has an address of its own: the 2.0 server limits an address to 900 requests a minute, and the110 // test bed (which trusts its local proxy header) would otherwise see every device as 127.0.0.1111 c.httpAdditionalHeaders = ["X-Forwarded-For": address]112 return c113 }114 api = DriveAPI(baseURL: URL(string: "http://\(host):8663")!, tokens: MemoryTokenStore(token), configuration: config())115 let conditions = conditions, policy = policy116 engine = SyncEngine(store: store, files: files, remote: api, driver: driver?(files.staging, config()) ?? InlineTransferDriver(staging: files.staging, configuration: config()), concurrency: { concurrency },117 conditions: { conditions.withLock { $0 } }, policy: { policy.withLock { $0 } })118 await engine.start()119 }120121 var offline: Bool { get { false } }122 func setOffline(_ off: Bool) { NetworkGate.set(host, offline: off) }123124 /// Same storage, new process: what a relaunch of the app (or a reboot of the phone) looks like to the engine.125 func relaunched(driver: (@Sendable (URL, URLSessionConfiguration) -> TransferDriver)? = nil) async throws -> Device { try await Device(token: api.tokens.token() ?? "", host: host, reuse: root, driver: driver) }126127 /// Changes what the device « sees » (path, power) and tells the engine, as the app does.128 func set(_ change: (inout TransferConditions) -> Void) async { conditions.withLock { change(&$0) }; await engine.conditionsChanged() }129 func setPolicy(_ change: (inout TransferPolicy) -> Void) async { policy.withLock { change(&$0) }; await engine.conditionsChanged() }130131 /// Syncs until nothing is left to send or fetch.132 func settle(timeout: TimeInterval = 60, sourceLocation: SourceLocation = #_sourceLocation) async {133 let deadline = Date.now.addingTimeInterval(timeout)134 while Date.now < deadline {135 await engine.sync()136 let busy = store.transfers(states: [.queued, .running]).isEmpty == false || store.ops().contains { $0.state != .failed }137 if !busy { return }138 // not faster: every simulated device reaches the server from the same address, which the 2.0 server limits to 900 requests a minute139 try? await Task.sleep(for: .milliseconds(300))140 }141 Issue.record("device did not settle: ops=\(store.ops()) transfers=\(store.transfers(states: [.queued, .running, .failed]))", sourceLocation: sourceLocation)142 }143144 var rootID: String { store.rootID ?? "" }145 func child(_ name: String, of parent: String) -> DriveItem? { store.children(of: parent).first { $0.name == name } }146147 /// A fresh working folder so tests do not see each other's files.148 func workspace(_ label: String = "t") async throws -> DriveItem {149 await engine.sync()150 let f = try await engine.createFolder(name: "\(label)-\(UUID().uuidString.prefix(8))", in: rootID)151 await settle()152 return try #require(child(f.name, of: rootID))153 }154155 func tempFile(_ name: String, _ data: Data) throws -> URL {156 let dir = root.appendingPathComponent("src-\(UUID().uuidString.prefix(6))")157 try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)158 let u = dir.appendingPathComponent(name)159 try data.write(to: u)160 return u161 }162}163164func randomData(_ count: Int) -> Data {165 var d = Data(count: count)166 d.withUnsafeMutableBytes { _ = SecRandomCopyBytes(kSecRandomDefault, count, $0.baseAddress!) }167 return d168}169170func eventually(_ timeout: TimeInterval = 20, _ condition: @Sendable () async -> Bool) async -> Bool {171 let deadline = Date.now.addingTimeInterval(timeout)172 while Date.now < deadline {173 if await condition() { return true }174 try? await Task.sleep(for: .milliseconds(50))175 }176 return await condition()177}178179/// A transfer driver for crash scenarios. It moves bytes for real through an inline driver, but can « die »: after `deliver` finished180/// steps, results are captured instead of being reported and new requests are ignored — what a killed process looks like. A later181/// instance replays captured results at start, as the system does for a background session after a relaunch.182final class ScriptedDriver: TransferDriver, @unchecked Sendable {183 private let inner: InlineTransferDriver184 private let lock = NSLock()185 private var handler: (@Sendable (BackgroundEvent) -> Void)?186 private var remaining: Int187 private var _captured: [BackgroundEvent] = []188 private var _uploads = 0, _downloads = 0, _ignored = 0189 private let replay: [BackgroundEvent]190191 init(staging: URL, configuration: URLSessionConfiguration, deliver: Int = .max, replay: [BackgroundEvent] = []) {192 inner = InlineTransferDriver(staging: staging, configuration: configuration)193 remaining = deliver194 self.replay = replay195 }196 var captured: [BackgroundEvent] { lock.lock(); defer { lock.unlock() }; return _captured }197 var uploads: Int { lock.lock(); defer { lock.unlock() }; return _uploads }198 var downloads: Int { lock.lock(); defer { lock.unlock() }; return _downloads }199 var ignored: Int { lock.lock(); defer { lock.unlock() }; return _ignored }200 private var dead: Bool { remaining <= 0 }201202 func start(handler: @escaping @Sendable (BackgroundEvent) -> Void) {203 lock.lock(); self.handler = handler; lock.unlock()204 inner.start { [weak self] e in205 guard let self else { return }206 self.lock.lock()207 var deliver = true208 if case .finished = e {209 if self.remaining <= 0 { deliver = false; self._captured.append(e) } else { self.remaining -= 1 }210 } else if self.remaining <= 0 { deliver = false }211 let h = self.handler212 self.lock.unlock()213 if deliver { h?(e) }214 }215 replay.forEach(handler)216 }217 func download(_ request: URLRequest, tag: String, resumeData: Data?, expectedBytes: Int64) {218 lock.lock(); let d = dead && !_captured.isEmpty; if d { _ignored += 1 } else { _downloads += 1 }; lock.unlock()219 if !d { inner.download(request, tag: tag, resumeData: resumeData, expectedBytes: expectedBytes) }220 }221 func upload(_ request: URLRequest, fromFile file: URL, tag: String) {222 lock.lock(); let d = dead; if d { _ignored += 1 } else { _uploads += 1 }; lock.unlock()223 if !d { inner.upload(request, fromFile: file, tag: tag) }224 }225 func activeTags() async -> Set<String> { [] }226 func cancel(tag: String) async { await inner.cancel(tag: tag) }227 func cancelAll() async { await inner.cancelAll() }228}229