Cotton code
66 lines · 2.6 KB · SwiftRaw
// The rule that keeps a test off the Keychain items of the Mac it runs on.//// The failure it exists for is in `KeychainGate.swift`: a Mac UI test built a// screen model with its default keyring, deleted the Mac's real vault key and// minted one only `xctest` could read, and the installed app served standby-only.// The stop itself is a `preconditionFailure`, which a test cannot catch; what is// held here is the decision it takes, and the two facts it reads.import CottonKitimport XCTestfinal class KeychainGateTests: XCTestCase {    func testATestOnAMacNeverReachesAShippingService() {        for service in KeychainGate.shippingServices {            XCTAssertTrue(                KeychainGate.refuses(service: service, testProcess: true, onMac: true),                "\(service) is the person's own item"            )        }    }    func testTheShippingAppItselfIsNeverRefused() {        for service in KeychainGate.shippingServices {            XCTAssertFalse(KeychainGate.refuses(service: service, testProcess: false, onMac: true))        }    }    func testATestUnderAThrowawayServiceOfItsOwnPasses() {        XCTAssertFalse(            KeychainGate.refuses(                service: "com.cottondb.tests.\(UUID().uuidString)",                testProcess: true,                onMac: true            )        )    }    func testAPhoneTestIsNotGatedBecauseItsKeychainIsTheSimulators() {        for service in KeychainGate.shippingServices {            XCTAssertFalse(KeychainGate.refuses(service: service, testProcess: true, onMac: false))        }    }    func testTheServicesAreTheOnesTheStoresFileUnder() {        XCTAssertTrue(KeychainGate.shippingServices.contains(VaultKey.service))        XCTAssertTrue(KeychainGate.shippingServices.contains(DeviceKey.service))    }    /// A test runs as Cotton Dev, and an installed Cotton Dev's keys are a    /// running app's too — so the gate covers both flavors, not this build's.    func testBothFlavorsServicesAreGated() {        for flavor in AppFlavor.allCases {            XCTAssertTrue(KeychainGate.shippingServices.contains(flavor.macKeychainService), "\(flavor)")            XCTAssertTrue(KeychainGate.shippingServices.contains(flavor.phoneKeychainService), "\(flavor)")        }        XCTAssertTrue(KeychainGate.shippingServices.contains("com.decemberfm.cotton.mac"))        XCTAssertTrue(KeychainGate.shippingServices.contains("com.cottondb.cotton"))    }    func testThisProcessKnowsItIsATest() {        XCTAssertTrue(            KeychainGate.isTestProcess,            "a runner the gate cannot see is a runner that reaches the real Keychain"        )    }}