// The vault key moves into the keychain group the app shares with its Keys // provider, once, in the simulator's own keychain: read from the app's own // group, written into the shared one, and only then deleted from the old; a // write that cannot land leaves the old item where it was. Run in the test // host, whose entitlement holds two groups (App/TestHost/TestHost.entitlements); // the item is unguarded because the simulator has no presence to guard with. // The host must be signed for the simulator to give it a keychain; the // everyday runner builds unsigned, and there the class skips with that reason. @testable import CottonKit import Security import XCTest final class VaultKeyGroupTests: XCTestCase { private let service = "com.cottondb.testhost.vault-group-\(UUID().uuidString)" private let account = "vault-key" /// A synthetic X25519 scalar, never anyone's key. private let synthetic = String(repeating: "B", count: 42) + "A" /// The team prefix the simulator signs the host with: the group a probe /// item is filed in by default is the entitlement's first, ``. private func prefix() throws -> String { let probe: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service + ".probe", kSecAttrAccount as String: "probe", kSecValueData as String: Data("probe".utf8)] SecItemDelete(probe as CFDictionary) let added = SecItemAdd(probe as CFDictionary, nil) // An unsigned host (scripts/ios-test.sh builds with // CODE_SIGNING_ALLOWED=NO) holds no keychain at all: said, not passed. if added == errSecMissingEntitlement { throw XCTSkip("the test host is unsigned and holds no keychain group; run signed (CODE_SIGNING_ALLOWED=YES CODE_SIGN_IDENTITY=-)") } XCTAssertEqual(added, errSecSuccess) defer { SecItemDelete(probe as CFDictionary) } var found: CFTypeRef? let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service + ".probe", kSecReturnAttributes as String: true, kSecMatchLimit as String: kSecMatchLimitOne] XCTAssertEqual(SecItemCopyMatching(query as CFDictionary, &found), errSecSuccess) let group = try XCTUnwrap((found as? [String: Any])?[kSecAttrAccessGroup as String] as? String) return try XCTUnwrap(group.components(separatedBy: "com.cottondb.testhost").first) } private func groups() throws -> (old: String, shared: String) { let prefix = try prefix() return (prefix + "com.cottondb.testhost", prefix + "com.cottondb.testhost.keys") } private func itemIn(_ group: String) -> Bool { let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account, kSecAttrAccessGroup as String: group, kSecReturnAttributes as String: true] return SecItemCopyMatching(query as CFDictionary, nil) == errSecSuccess } private func seedOld(_ group: String) { let item: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account, kSecAttrAccessGroup as String: group, kSecValueData as String: Data(synthetic.utf8)] XCTAssertEqual(SecItemAdd(item as CFDictionary, nil), errSecSuccess) } override func tearDown() { SecItemDelete([kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service] as CFDictionary) } // RULE: A vault key filed in the app's own group is read, written into the shared group and deleted from the old, in that order, once: the read answers the same key, the next read finds it in the shared group, and nothing is left behind. func testAKeyInTheAppsOwnGroupMovesIntoTheSharedGroupOnce() throws { let (old, shared) = try groups() seedOld(old) let store = KeychainVaultKeyStore(service: service, account: account, accessGroup: shared, legacyGroup: old, guarded: false) XCTAssertTrue(store.exists(), "an old item is a key this install has") XCTAssertFalse(try store.add(String(repeating: "C", count: 43)), "never a second key beside the old one") XCTAssertEqual(try store.read(), synthetic) XCTAssertTrue(itemIn(shared)) XCTAssertFalse(itemIn(old)) XCTAssertEqual(try store.read(), synthetic) XCTAssertEqual(try VaultKey.key(store: store), synthetic) } // RULE: A write into the shared group that cannot land leaves the old item where it was, and the key is still answered. func testAFailedMoveLeavesTheOldItem() throws { let (old, _) = try groups() seedOld(old) let unheld = try prefix() + "com.cottondb.not-this-hosts" let store = KeychainVaultKeyStore(service: service, account: account, accessGroup: unheld, legacyGroup: old, guarded: false) XCTAssertEqual(try store.read(), synthetic) XCTAssertTrue(itemIn(old), "the old item stays when the new write fails") } // RULE: A key minted where there was none is written into the shared group alone, and forgetting removes it. func testAMintedKeyIsFiledInTheSharedGroup() throws { let (old, shared) = try groups() let store = KeychainVaultKeyStore(service: service, account: account, accessGroup: shared, legacyGroup: old, guarded: false) let minted = try XCTUnwrap(try VaultKey.mintIfAbsent(store: store)) XCTAssertTrue(itemIn(shared)) XCTAssertFalse(itemIn(old)) XCTAssertEqual(try store.read(), minted) store.forget() XCTAssertFalse(store.exists()) } // RULE: Only a team-prefixed group is named: an unsigned build's stamp (no prefix) or an unresolved one names none, and the key stays where the system files it. func testOnlyATeamPrefixedGroupIsNamed() { XCTAssertEqual(VaultKey.teamPrefixed("M7V3QVXZQH.com.cottondb.keys.dev.keys"), "M7V3QVXZQH.com.cottondb.keys.dev.keys") XCTAssertNil(VaultKey.teamPrefixed("com.cottondb.keys.dev.keys")) XCTAssertNil(VaultKey.teamPrefixed("$(AppIdentifierPrefix)com.cottondb.keys")) XCTAssertNil(VaultKey.teamPrefixed("M7V3QVXZQH.")) } }