// A TEST PROCESS NEVER TOUCHES A SHIPPING APP'S OWN KEYCHAIN ITEMS. // // On a Mac the Keychain a test reaches is the person's real login keychain — // the one the installed CottonDB keeps its device key and its vault key in — // and `swift test` runs on every Mac that takes CI jobs. The old guard asked // only "is there a bundle identifier", and `xctest` has one // (`com.apple.dt.xctest.tool`), so a screen model built with its DEFAULT // keyring reached the real items: `BootModel.erase` / `resetKey` deleted the // Mac's vault key and the join minted a new one. The legacy keychain gates // an item by the code signature that CREATED it, so the installed app then met // a SecurityAgent dialog nobody was there to answer, started its server with // no vault key, and the Mac served as standby-only. Measured 28 Sep 2026 on // the build box: the vault-key item's creation date (11:55:03Z) is the minute an // `xctest` run was live there, and the app's boot said "the Keychain dialog // went unanswered inside the boot's budget". // // So every real store asks here before it builds a query. A test that reaches // a shipping service is a bug in the TEST — it must hand the model an // in-memory store — and it stops the run naming the item, rather than quietly // taking a key off the Mac it runs on. A test that exercises the Keychain on // purpose does it under a throwaway service of its own, which passes. // // The phone is not gated: an iOS test runs in a simulator, whose keychain // belongs to a device that exists for the test. import Foundation public enum KeychainGate { /// The services the shipping apps file their items under: the Mac app's /// own and the phone's (which the Mac never uses, but a Mac test reaching /// it would still be writing into the person's login keychain) — for /// BOTH flavors. A test runs as Cotton Dev, and an installed Cotton Dev's /// keys are as much a running app's as CottonDB's: a test that took them /// would strand that install exactly the way the 28 Sep run stranded the /// real one. So the gate refuses the union, not just this build's own. public static let shippingServices: Set = Set( AppFlavor.allCases.flatMap { [$0.macKeychainService, $0.phoneKeychainService] } ) /// This process is a test runner. `XCTestCase` is loaded into every /// `xctest` process; the environment variables cover a runner that sets /// them before XCTest is linked. public static let isTestProcess: Bool = { if NSClassFromString("XCTestCase") != nil { return true } let environment = ProcessInfo.processInfo.environment return environment["XCTestConfigurationFilePath"] != nil || environment["XCTestBundlePath"] != nil || environment["XCTestSessionIdentifier"] != nil }() /// Whether a Keychain call addressing `service` must not run, decided from /// the two facts and nothing else. Pure, so the rule is held by a test. public static func refuses(service: String, testProcess: Bool, onMac: Bool) -> Bool { onMac && testProcess && shippingServices.contains(service) } /// Stop the run if this call would touch a shipping item from a test. public static func check(service: String, account: String) { #if os(macOS) let onMac = true #else let onMac = false #endif if refuses(service: service, testProcess: isTestProcess, onMac: onMac) { preconditionFailure( "a test reached this Mac's own Keychain item \(service) / \(account) — " + "hand the model an in-memory store; a test that reached it would " + "take the key off the Mac it runs on (KeychainGate.swift)" ) } } }