Cotton code
108 lines · 5.3 KB · SwiftRaw
// Taking Cotton off this phone takes its VAULT key too — both halves.//// The crate holds no Keychain entitlement, so the APP is what forgets the two// items (the device key and the vault key). `BootScreensTests` already holds the// device-key half of that: after an erase the device key store answers nothing.// The VAULT half was not held at all — the existing case lets `erase` build a// real `VaultKeyring` over the simulator's own Keychain, where nothing can be// observed and nothing is asserted.//// The failure that leaves: a phone whose vault key survived an erase publishes// a public half whose private half it can no longer pair against, or — worse —// keeps the private half of the key the passwords on a server it was thrown off// are wrapped for. And the halves are two Keychain items, so forgetting one and// not the other is a single missing line: the cached PUBLIC half with no// private one is the one state `VaultKeyring.publicKey()` cannot answer without// prompting, which is a Passwords screen that can never open again.//// Both halves are driven through the doubles `CottonKit` ships// (`InMemoryVaultKeyStore`, `InMemorySecretStore`, `InMemoryDeviceKeyStore`), so// nothing here touches a real Keychain — a simulator's Keychain is shared// between runs, which is the reason `VaultKeyTests` and `DeviceKeyTests` drive// doubles too. Every value is invented.import CottonKitimport XCTest@testable import CottonUI@MainActorfinal class VaultKeyEraseTests: XCTestCase {    /// A scalar counting up — 32 bytes, base64url, 43 characters. Not a key    /// anything holds.    private static let scalar = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8"    /// A stand-in for the cached public half. Its value is never checked here —    /// only whether the slot still holds something.    private static let cachedPublicHalf = "j0DFrbaPJWJK5bIU6nZ6bslNgp09e14a0bpvPiE4KF8"    /// A keyring holding BOTH halves, over stores a test can read back.    private func seededKeyring() -> (VaultKeyring, InMemoryVaultKeyStore, InMemorySecretStore) {        let keys = InMemoryVaultKeyStore(seeded: VaultKeyEraseTests.scalar)        let cache = InMemorySecretStore([            VaultKeyring.publicSlot: VaultKeyEraseTests.cachedPublicHalf        ])        return (VaultKeyring(keys: keys, cache: cache), keys, cache)    }    /// RULE: Erasing Cotton from this phone forgets both halves of its password key, not only the key that identifies the phone.    ///    /// The failure it prevents: a key left behind after an erase can still open    /// passwords this phone was thrown off, and a phone that kept only half of    /// it can never open its own passwords again.    func testAnEraseForgetsBothHalvesOfTheVaultKeyAndTheDeviceKey() async {        let core = ScriptedCore([:])        let boot = BootModel(core: core)        let device = InMemoryDeviceKeyStore(seeded: String(repeating: "a", count: 43))        let (vault, keys, cache) = seededKeyring()        await boot.erase(store: device, vault: vault)        XCTAssertNil(try? device.read(), "the phone's own identity is gone")        XCTAssertFalse(keys.exists(), "the PRIVATE half of the vault key survived the erase")        XCTAssertNil(            cache.read(VaultKeyring.publicSlot),            """            the cached PUBLIC half survived the erase. A public half with no \            private one is the one state VaultKeyring.publicKey() cannot answer \            without a Face ID prompt, so the Passwords screens never open again.            """        )    }    /// RULE: Starting this phone over forgets both halves of its password key along with the key that identifies the phone.    ///    /// The failure it prevents: starting over makes this a new device to the    /// server, so a password key kept from the old identity is one no server    /// will ever wrap for again — the Passwords screens wait for a Mac that has    /// nothing left to send.    func testStartingOverForgetsBothHalvesOfTheVaultKeyAndTheDeviceKey() async {        let core = ScriptedCore([:])        let boot = BootModel(core: core)        let device = InMemoryDeviceKeyStore(seeded: String(repeating: "b", count: 43))        let (vault, keys, cache) = seededKeyring()        await boot.resetKey(store: device, vault: vault)        XCTAssertNil(try? device.read(), "a new start mints a new identity, so the old one goes")        XCTAssertFalse(keys.exists(), "the PRIVATE half of the vault key survived the reset")        XCTAssertNil(            cache.read(VaultKeyring.publicSlot),            "the cached PUBLIC half survived the reset — the next join would publish a key with no private half"        )    }    /// RULE: Forgetting this phone's password key clears both the guarded half and the cached copy of its public half.    ///    /// The unit under the two cases above, held separately so a failure names    /// which layer broke: the keyring itself, or the boot model's wiring to it.    func testTheKeyringItselfTakesBothHalves() {        let (vault, keys, cache) = seededKeyring()        XCTAssertTrue(keys.exists())        XCTAssertNotNil(cache.read(VaultKeyring.publicSlot))        vault.forget()        XCTAssertFalse(keys.exists())        XCTAssertNil(cache.read(VaultKeyring.publicSlot))    }}