Cotton code
87 lines · 4.7 KB · SwiftRaw
// The launch never draws the presence prompt.//// The launch's only Keychain act is `VaultAccess.publish` (RootView, once the// boot is ready): it tells the file this device's PUBLIC vault key. On the// phone the private half is guarded by user presence, so every READ of it is// the Face ID / passcode sheet. The owner's phone asked for the passcode on// every cold start (12 Oct 2026); this guard counts the reads over every state// a signed-in phone can launch in.//// Every key is invented: a scalar counting up, and its public half.import CottonKitimport XCTest@testable import CottonUIfinal class VaultLaunchPromptTests: XCTestCase {    private static let scalar = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8"    private static let publicHalf = "j0DFrbaPJWJK5bIU6nZ6bslNgp09e14a0bpvPiE4KF8"    private func launch(keys: InMemoryVaultKeyStore, cache: InMemorySecretStore) async -> (published: Bool, core: ScriptedCore) {        let core = ScriptedCore([            CottonMethod.vaultPublishKey: .ok(#"{"id":"vk-1","deviceId":"d","changed":false}"#)        ])        let vault = VaultAccess(            keyring: VaultKeyring(keys: keys, cache: cache, slot: VaultKeyring.publicSlot),            ask: { XCTFail("the launch asked the device prompt"); return .allowed })        return (await vault.publish(through: core), core)    }    /// RULE: A cold launch on a signed-in phone makes zero presence-guarded Keychain reads, in every state the vault key can be in; the prompt belongs only before a password is shown or saved (architect, 12 Oct 2026).    func testALaunchNeverReadsThePresenceGuardedKey() async throws {        let states: [(String, InMemoryVaultKeyStore, [String: String])] = [            ("signed in, key and its public half cached",             InMemoryVaultKeyStore(seeded: Self.scalar), [VaultKeyring.publicSlot: Self.publicHalf]),            ("a key whose cached public half was lost",             InMemoryVaultKeyStore(seeded: Self.scalar), [:]),            ("a key the Keychain's index would not report — the mint finds the slot taken",             InMemoryVaultKeyStore(seeded: Self.scalar, indexed: false), [VaultKeyring.publicSlot: Self.publicHalf]),            ("the same, with no cached half either",             InMemoryVaultKeyStore(seeded: Self.scalar, indexed: false), [:]),            ("the first launch, no key yet",             InMemoryVaultKeyStore(), [:]),        ]        for (state, keys, cached) in states {            _ = await launch(keys: keys, cache: InMemorySecretStore(cached))            XCTAssertEqual(keys.readCount, 0, "\(state): the launch read the private key, which is the prompt")        }    }    /// A key the index would not report is never minted over: the wrap the    /// server holds for it stays good, and the launch publishes the cached half.    func testASlotTakenAtMintIsAnsweredFromTheCacheWithoutReadingOrMinting() async throws {        let keys = InMemoryVaultKeyStore(seeded: Self.scalar, indexed: false)        let (published, core) = await launch(            keys: keys, cache: InMemorySecretStore([VaultKeyring.publicSlot: Self.publicHalf]))        XCTAssertTrue(published)        XCTAssertEqual(keys.mintCount, 0)        let sent = await core.calls.filter { $0.method == CottonMethod.vaultPublishKey }        XCTAssertEqual(sent.count, 1)        XCTAssertTrue(sent.first?.requestJSON.contains(Self.publicHalf) == true)    }    /// RULE (12 Nov 2026): the publish names this device, so Keys › Devices titles its card with the phone's own name.    func testThePublishNamesThisDevice() async throws {        let (_, core) = await launch(            keys: InMemoryVaultKeyStore(seeded: Self.scalar), cache: InMemorySecretStore([VaultKeyring.publicSlot: Self.publicHalf]))        let sent = await core.calls.filter { $0.method == CottonMethod.vaultPublishKey }        XCTAssertEqual(sent.count, 1)        let request = try JSONSerialization.jsonObject(with: Data(sent[0].requestJSON.utf8)) as? [String: Any]        XCTAssertEqual(request?["name"] as? String, ThisPhone.deviceName())        XCTAssertFalse(ThisPhone.deviceName().isEmpty)    }    /// The reveal is where the prompt belongs, and it still reads.    func testARevealStillReadsThePrivateKey() async throws {        let keys = InMemoryVaultKeyStore(seeded: Self.scalar)        let core = ScriptedCore([            CottonMethod.vaultUnlock: .ok(#"{"until":"2026-09-08T10:05:00.000Z","vmkVersion":1}"#)        ])        let vault = VaultAccess(            keyring: VaultKeyring(keys: keys, cache: InMemorySecretStore(), slot: VaultKeyring.publicSlot),            ask: { .allowed })        _ = await vault.unlock(through: core)        XCTAssertEqual(keys.readCount, 1)    }}