// This phone's identity, over an in-memory store. // // The double is not a convenience: the simulator's Keychain is shared between test // runs, so a test that minted into it would pass exactly once, and the ONE case // that matters most — a slot that exists and cannot be read — cannot be set up // in a real Keychain at all without corrupting it. import Security import XCTest @testable import CottonKit final class DeviceKeyTests: XCTestCase { func testItIsMintedOnceAndNeverAgain() { let store = InMemoryDeviceKeyStore() let first = try? DeviceKey.seed(store: store) let second = try? DeviceKey.seed(store: store) XCTAssertNotNil(first) XCTAssertEqual(first, second, "a seed IS an identity; a second one is a different phone") XCTAssertEqual(store.mintCount, 1) } func testASeedIsThirtyTwoBytesAsFortyThreeCharacters() { let store = InMemoryDeviceKeyStore() let seed = try? DeviceKey.seed(store: store) XCTAssertEqual(seed?.count, 43) XCTAssertEqual(DeviceKey.decode(seed ?? "")?.count, 32) // base64url, unpadded — what Rust decodes and what `x-cotton-device` // carries. A `+`, a `/` or a `=` here is a header the server cannot // read. XCTAssertFalse(seed?.contains("+") ?? true) XCTAssertFalse(seed?.contains("/") ?? true) XCTAssertFalse(seed?.contains("=") ?? true) } func testAnUnreadableSlotRefusesAndNeverMintsASecondIdentity() { // Not "no key". Minting over an unreadable slot would strand every // approval the first one holds, silently — which is the whole reason // this is a refusal with its own screen and its own destructive button. let store = InMemoryDeviceKeyStore(seeded: "this is not a seed") XCTAssertThrowsError(try DeviceKey.seed(store: store)) { error in XCTAssertEqual(error as? DeviceKeyError, .slotUnreadable) } XCTAssertEqual(store.mintCount, 0) XCTAssertEqual(try? store.read(), "this is not a seed", "the slot stands") } func testARightLengthSlotThatIsNotBase64IsAlsoRefused() { // 43 characters is necessary and not sufficient: the bytes have to // decode to 32, or the signature Rust makes with them is nobody's. let store = InMemoryDeviceKeyStore(seeded: String(repeating: "!", count: 43)) XCTAssertThrowsError(try DeviceKey.seed(store: store)) XCTAssertEqual(store.mintCount, 0) } func testStartingOverForgetsTheSlotAndTheNextSeedIsNew() { // A person's deliberate act from the refusal screen's destructive pill. // The server will know this phone as a NEW device, to be paired again. let store = InMemoryDeviceKeyStore() let first = try? DeviceKey.seed(store: store) DeviceKey.forget(store: store) let second = try? DeviceKey.seed(store: store) XCTAssertNotNil(second) XCTAssertNotEqual(first, second) XCTAssertEqual(store.mintCount, 2) } func testTheItemIsWrittenReadableAfterTheFirstUnlockAndNeverLeavesThisPhone() { // `ThisDeviceOnly` is the identity half: a restore onto another phone // carries no key, so that phone is a NEW device and pairs again. // `AfterFirstUnlock` is the availability half, and it is the repair: on // `WhenUnlocked` a launch while the phone was locked read // `errSecInteractionNotAllowed`, which this app turns into // `device_key_unreadable` — a screen whose only button mints a second // identity, over a key that was merely unreadable for a few seconds. XCTAssertEqual( KeychainDeviceKeyStore.accessibility as String, kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly as String ) let attributes = KeychainDeviceKeyStore.addAttributes( service: DeviceKey.service, account: DeviceKey.account ) XCTAssertEqual( attributes[kSecAttrAccessible as String] as! CFString as String, kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly as String, "the item is ADDED with this build's accessibility" ) } func testTheMatchQueryCarriesNoAccessibilityAtAll() { // **A search attribute FILTERS.** A query carrying the new // accessibility would not match the item the last build wrote under the // old one: the read would answer `errSecItemNotFound`, the app would // read that as a phone with no key, mint a second identity, and strand // every approval the first one holds. So the query names the class, the // service and the account, and nothing else — and the read re-files the // item it found (`SecItemUpdate`) instead. let query = KeychainDeviceKeyStore.matchQuery( service: DeviceKey.service, account: DeviceKey.account ) XCTAssertNil( query[kSecAttrAccessible as String], "an accessibility in the match query is a phone that cannot find its own key" ) XCTAssertEqual(query[kSecAttrService as String] as? String, DeviceKey.service) XCTAssertEqual(query[kSecAttrAccount as String] as? String, DeviceKey.account) XCTAssertEqual(query[kSecClass as String] as! CFString as String, kSecClassGenericPassword as String) } func testTheSlotIsThisAppSOwnAndNotTheExpoBuildS() { // The previous app kept `{"v":1,"seed":"…"}` under `cotton.device.key`. // This app writes a bare seed under its own account name, so a phone // upgrading re-pairs as a new device — acceptable under // [i-m-the-only-cotton-user-the-app-carries-no-backward-compatibili] // and written down in Cotton note "The phone and Mac app — how a screen is built". // The service is the flavor's; a test process is Cotton Dev, which // never files under the real app's `com.cottondb.cotton`. XCTAssertEqual(DeviceKey.service, AppFlavor.current.phoneKeychainService) XCTAssertEqual(AppFlavor.real.phoneKeychainService, "com.cottondb.cotton") XCTAssertNotEqual(DeviceKey.service, "com.cottondb.cotton") XCTAssertEqual(DeviceKey.account, "device-key") XCTAssertNotEqual(DeviceKey.account, "cotton.device.key") } }