Cotton code
65 lines · 2.8 KB · SwiftRaw
// The code-hosting tokens an old build left in this phone's Keychain, and the// one thing left to do about them.//// The tokens live in `cotton_secrets` now and nothing reads a slot any more.// Without this sweep a live credential for somebody else's server would sit in// an installed phone's Keychain with nothing that can read it, no screen that// mentions it and no path that removes it — which is not a thing to do with// somebody's credential.//// Every fixture value is synthetic and is never asserted on.import CottonKitimport XCTest@testable import CottonUIfinal class CodeHostingLegacyKeychainTests: XCTestCase {    private func slot(_ host: String) -> String {        CodeHostingLegacyKeychain.slot(for: host)    }    /// THE SLOTS CANNOT BE ENUMERATED, so the index is what makes a complete    /// forget possible — and the index goes with them.    func testEverySlotTheIndexNamesGoesAndSoDoesTheIndex() async {        let elsewhere = "cotton.byok.anthropic"        let store = InMemorySecretStore([            CodeHostingLegacyKeychain.indexSlot: #"["github.com","git.example.org"]"#,            slot("github.com"): #"{"kind":"api_token","token":"SYNTHETIC-0000"}"#,            slot("git.example.org"): #"{"kind":"api_token","token":"SYNTHETIC-0000"}"#,            elsewhere: #"{"kind":"anthropic","key":"SYNTHETIC-0000"}"#,        ])        await CodeHostingLegacyKeychain.forget(in: store)        XCTAssertEqual(store.occupied, [elsewhere], "the token slots and the index go, and nothing else does")        // Idempotent: the settings sheet calls this on every visit.        await CodeHostingLegacyKeychain.forget(in: store)        XCTAssertEqual(store.occupied, [elsewhere])    }    /// A CORRUPT INDEX COSTS COMPLETENESS, NOT SAFETY. The slot it could not    /// name survives — nothing can enumerate it — but the index itself is    /// deleted, so the next launch does not read the same broken value again.    func testACorruptIndexStillTakesTheIndexWithIt() async {        let store = InMemorySecretStore([            CodeHostingLegacyKeychain.indexSlot: "{not json",            slot("github.com"): #"{"kind":"api_token","token":"SYNTHETIC-0000"}"#,        ])        await CodeHostingLegacyKeychain.forget(in: store)        XCTAssertEqual(store.occupied, [slot("github.com")])    }    func testTheSlotNameIsTheOneTheOldBuildWrote() {        XCTAssertEqual(slot("GitHub.com"), "cotton.codehost.github.com")        XCTAssertEqual(CodeHostingLegacyKeychain.indexSlot, "cotton.codehost.index")    }    /// Nothing in the Keychain and nothing to do — the ordinary case on every    /// phone that never ran the old build.    func testAPhoneThatNeverHeldOneIsUntouched() async {        let store = InMemorySecretStore()        await CodeHostingLegacyKeychain.forget(in: store)        XCTAssertTrue(store.occupied.isEmpty)    }}