// 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 importCottonKitimportSecurityimport XCTestfinalclassVaultKeyGroupTests: XCTestCase {privatelet service = "com.cottondb.testhost.vault-group-\(UUID().uuidString)"privatelet account = "vault-key"/// A synthetic X25519 scalar, never anyone's key.privatelet 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, `<prefix><id>`.privatefunc prefix() throws -> String {let probe: [String: Any] = [kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: service + ".probe", kSecAttrAccount asString: "probe", kSecValueData asString: 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 asString: kSecClassGenericPassword, kSecAttrService asString: service + ".probe", kSecReturnAttributes asString: true, kSecMatchLimit asString: kSecMatchLimitOne] XCTAssertEqual(SecItemCopyMatching(query as CFDictionary, &found), errSecSuccess)let group = try XCTUnwrap((found as? [String: Any])?[kSecAttrAccessGroup asString] as? String)returntry XCTUnwrap(group.components(separatedBy: "com.cottondb.testhost").first) }privatefunc groups() throws -> (old: String, shared: String) {let prefix = try prefix()return (prefix + "com.cottondb.testhost", prefix + "com.cottondb.testhost.keys") }privatefunc itemIn(_ group: String) -> Bool {let query: [String: Any] = [kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: service, kSecAttrAccount asString: account, kSecAttrAccessGroup asString: group, kSecReturnAttributes asString: true]returnSecItemCopyMatching(query as CFDictionary, nil) == errSecSuccess }privatefunc seedOld(_ group: String) {let item: [String: Any] = [kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: service, kSecAttrAccount asString: account, kSecAttrAccessGroup asString: group, kSecValueData asString: Data(synthetic.utf8)] XCTAssertEqual(SecItemAdd(item as CFDictionary, nil), errSecSuccess) }overridefunc tearDown() {SecItemDelete([kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: 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(tryVaultKey.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(tryVaultKey.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.")) }}