DeviceKeyStoreConformanceSuite
Reusable contract test suite for DeviceKeyStore implementations.
A device is recognised when it comes back only if it comes back with the same key, so a store has one job that matters: hand back the same key, made once, for as long as it promises to keep it — and when what it kept is damaged, hand back a new key rather than a crash. Every Test here is a required invariant of that contract.
Subclass and implement newFixture. Lives in commonMain of :kuilt-conformance, beside DurableStoreConformanceSuite, so every store — the in-memory one, each platform store in :kuilt-identity, and a store a consumer writes against the public interface — subclasses it from its own test source set on whichever targets it exists for.
class SecureEnclaveDeviceKeyStoreConformanceTest : DeviceKeyStoreConformanceSuite() {
override suspend fun newFixture(): DeviceKeyStoreFixture {
val place = freshKeychainService()
return DeviceKeyStoreFixture.Durable(
reopen = { beforeKeyIsMade -> SecureEnclaveDeviceKeyStore(place, beforeKeyIsMade) },
corruptKey = { place.overwriteKeyItem(byteArrayOf(1, 2, 3)) },
// …one hook per failure the store must survive
)
}
}What the suite rests on, and checks
newFixture hands back an unshared medium. distinctFixturesAreDistinctDevices checks it.
A reopen crosses a handle boundary. The DeviceKeyStoreFixture.Durable arm asserts the reopened store and the key it returns are different objects, and that the reopened store made no key (KeyGate.made is zero), so a reopen that returned a cached store, or quietly made a fresh key, is a red rather than a pass.
Every damage hook really damages. Each damage property first runs a control reopen that keeps the key, so a "new key after damage" verdict cannot come from a store that never kept one.
Every rig says whether it fired. A hook for a state the medium may not be able to reach returns a two-armed answer — DeviceKeyStoreFixture.UnreadableKey and DeviceKeyStoreFixture.KeyWriteInterruption — and the arm that claims the state was reached carries a probe the property checks, so a hook that does nothing reds instead of passing. DeviceKeyStoreFixture.UnreadableGrants has one arm, since every durable medium can fail a read, and its probe must throw for the property to pass.
Replace damage, refuse a medium that cannot answer
The suite pushes a store two ways at once, and they must not be confused. A key that was read and is not a key — truncated, edited, two halves of different keys — is damage, and the store replaces it: the device carries on as a new device. A key the medium cannot read right now — a permission it lacks, a file system loop, a Keychain locked before the first unlock — is not damage, and the store throws: replacing it would turn a passing condition into a new identity on every launch it lasts for. anUnreadableKeyIsRefusedNotReplaced is the second half.
Grants get the same split. A record that was read and does not decode is dropped (corruptGrantsAreDroppedNotFatal); a record the medium cannot read right now is refused, and is never rewritten while it is out of reach (unreadableGrantsAreRefusedNotRewritten). Reading that failure as "no grants" would let the next DeviceKeyStore.keepGrant write a record holding only the new grant over every grant the device had.
What it deliberately does not require
That a store drops grants when it replaces a key. A grant names the old key, so it is useless with the new one and harmless to keep; whether to tidy it away is the store's choice.
That a replacement is reported. A store should log it, and a platform store's own tests can assert its log line, but a log line is a platform backend's business and not reachable here.
What it cannot detect
That a durable store's bytes reached stable storage rather than the operating system's cache. A reopen in one process reads through the same page cache, so dropping an fsync reds nothing here — the residual DurableStoreConformanceSuite states for the same reason. And how well the key is kept — file permissions, a Keychain accessibility class, a non-extractable browser key — is each platform store's own test, since the contract cannot see where a key lives.
Functions
A cleared key — a deleted file, a removed Keychain item — starts the device afresh, without a throw.
A key the store cannot read back — a truncated file, garbage in a Keychain item — is replaced with a new key, and the app keeps working as a new device: no throw, and no prompt.
DeviceKeyStore.keepFreshGrant gives up a held grant dated more than GrantTrust.CLOCK_SKEW_ALLOWANCE_MILLIS after the one it keeps, although that one is older; DeviceKeyStore.keepGrant never does (identity Task 2.5, Decided 21 and 24).
The first call makes a key and every call after returns it, and the key signs under its own name.
A key whose private half does not sign for the name it is stored under is refused like a corrupt one. This is the self-test's property: without it a store reading such a pair would hand out a name it cannot prove, and the device would fail every admission while looking healthy.
A crash part-way through writing a key leaves the store readable: the key held before the write is still the key, and a later write over the crash's leftovers succeeds.
DeviceKeyStore.keepGrant never lets an older copy displace the grant held, however much older (Decided 21): a renewal replayed or delivered late, more than GrantTrust.CLOCK_SKEW_ALLOWANCE_MILLIS behind the pass held, is ignored like any other. A renewal names no Hello, so nothing tells a replay from a fresh pass, and only DeviceKeyStore.keepFreshGrant may give a held pass up for an older one. On the durable arm the rule is read again after a restart.
A key the medium cannot read right now is refused, not replaced: DeviceKeyStore.deviceKey throws, no key is made, and once the medium can read again the original key is back.
What a restart does to the key: the DeviceKeyStoreFixture.Durable arm keeps it, the DeviceKeyStoreFixture.KeepsNothing arm forgets it. The wrong arm is a red in both directions.
Concurrent first calls agree on one key, and a store makes it once.
Grants the store cannot read back are dropped, not fatal: the key is untouched, the device holds no grants, and the next grant it is given is kept. A grant is not a secret and a lost one costs only an ordinary admission next time, so dropping them is the right failure.
The fixture precondition the rest of this file rests on: two fixtures are two devices.
What a restart does to the grants: kept by the durable arm, forgotten by the other.
The one rule every store applies: the newest grant per issuer and scope, where a tie keeps the held one. The one exception, a pass on a room's Welcome kept with DeviceKeyStore.keepFreshGrant, is aFreshPassReplacesOneDatedBeyondTheAllowance's.
Two stores open over one medium at once — the app called the platform default twice, or two processes share a directory — are one device: one key between them, and neither store's grants lost to the other's write.
A grants record the medium cannot read right now is refused, not read as "no grants": DeviceKeyStore.grants and DeviceKeyStore.keepGrant throw an Exception, nothing writes the record while it is out of reach, and once the medium can read again the held grant is intact.