76bf9ff4e828
extract the source session adapter and cover every callback exhaustively
- time
- 2026-09-10 23:07 UTC · 2 months ago
- machine
- koh2
- agent
- deepseek/deepseek-v4-flash
- session
- adoption adapter coverage
- parent
- a39e1ade4a2b
↓ peach-android-76bf9ff4.tar.gzatlas at this save
files changed4 files
app/src/main/java/com/peachpasswords/sync/AdoptionProtocol.kt+71 −0app/src/main/java/com/peachpasswords/sync/RelayAdoptionHost.kt+10 −36app/src/main/java/com/peachpasswords/sync/RelayAdoptionReceiver.kt+8 −47app/src/test/java/com/peachpasswords/sync/AdoptionAdapterCoverageTest.kt+191 −0app/src/main/java/com/peachpasswords/sync/AdoptionProtocol.kt+71 −0
@@ -90,6 +90,77 @@9090 9191 fun AdoptionSourceV2Deps.allowLegacyV1(): Boolean = LEGACY_V1_ALLOWED9292 93+/**94+ * Bind a source session's deps to the live session facts.95+ *96+ * Both relay source adapters — the phone showing a QR, and the phone scanning an97+ * empty invitation — build the same object; the only differences are which98+ * identity is presented and whether snapshot support is advertised, so both are99+ * parameters rather than two near-identical anonymous objects.100+ *101+ * Extracted from the session functions so §10.5 can be proven: the test dirties102+ * every member here and asserts each one reaches its sink. A callback that is103+ * dropped, or one that returns without calling through, fails that test.104+ */105+internal fun bindSourceSessionDeps(106+ sourceDeps: AdoptionSourceV2Deps,107+ sourceIdentity: LocalDeviceIdentity,108+ pairState: AdoptionPairState,109+ sas: String,110+ snapshotCapable: Boolean,111+ confirm: suspend (sas: String, request: AdoptionFrame.Request) -> Boolean,112+): AdoptionSourceV2Deps = object : AdoptionSourceV2Deps {113+ override val sourceIdentity = sourceIdentity114+ override val pairState = pairState115+ override suspend fun rootKeyHex() = sourceDeps.rootKeyHex()116+ override suspend fun vaultId() = sourceDeps.vaultId()117+ override suspend fun license() = sourceDeps.license()118+ override suspend fun applyLicense(license: String) = sourceDeps.applyLicense(license)119+ override suspend fun onConsent(request: AdoptionFrame.Request) = true120+121+ // The transcript sends ADOPT_CONFIRM_REQUIRED before calling this, so the122+ // receiver's own prompt is already live when we ask our human to compare the123+ // same SAS. The request is only a prompt label here — but the call MUST124+ // reach `confirm`: returning true without it was a real defect, because the125+ // source-side prompt then never appeared.126+ override suspend fun onConsent(request: AdoptionFrame.RequestV2): Boolean =127+ confirm(128+ sas,129+ AdoptionFrame.Request(130+ sessionRequestId = request.deviceId,131+ deviceName = request.deviceName,132+ deviceType = request.deviceType,133+ license = request.license,134+ snapshotCapable = request.snapshotCapable,135+ ),136+ )137+138+ override suspend fun persistPendingHolder(request: AdoptionFrame.Request, nowMs: Long) =139+ sourceDeps.persistPendingHolder(request, nowMs)140+141+ override suspend fun completePendingHolder(request: AdoptionFrame.Request, device: PairedDevice) =142+ sourceDeps.completePendingHolder(request, device)143+144+ override suspend fun persistPendingIdentity(device: PairedDevice, nowMs: Long) =145+ sourceDeps.persistPendingIdentity(device, nowMs)146+147+ override suspend fun completePendingIdentity(device: PairedDevice) =148+ sourceDeps.completePendingIdentity(device)149+150+ override suspend fun upsertPairedDevice(device: PairedDevice) =151+ sourceDeps.upsertPairedDevice(device)152+153+ override fun hasDeviceIdentityCollision(device: PairedDevice) =154+ sourceDeps.hasDeviceIdentityCollision(device)155+156+ override suspend fun rotateSourceIdentity() = sourceDeps.rotateSourceIdentity()157+ override fun snapshotCapable() = snapshotCapable158+ override suspend fun vaultSnapshot() = sourceDeps.vaultSnapshot()159+ override suspend fun outgoingDeviceManifest() = sourceDeps.outgoingDeviceManifest()160+ override suspend fun markSynced(deviceId: String) = sourceDeps.markSynced(deviceId)161+ override suspend fun duressSlotJson() = sourceDeps.duressSlotJson()162+}163+93164 private fun AdoptionFrame.RequestV2.valid(): Boolean =94165 adoptionVersion == ADOPTION_VERSION &&95166 deviceId.isNotEmpty() &&app/src/main/java/com/peachpasswords/sync/RelayAdoptionHost.kt+10 −36
@@ -217,42 +217,16 @@217217 ),218218 relayUrl = Rendezvous.normalizeRelayUrl(relayUrl),219219 )220- val boundDeps = object : AdoptionSourceV2Deps {221- override val sourceIdentity = localIdentity222- override val pairState: AdoptionPairState = pairState223- override suspend fun rootKeyHex() = sourceDeps.rootKeyHex()224- override suspend fun vaultId() = sourceDeps.vaultId()225- override suspend fun license() = sourceDeps.license()226- override suspend fun applyLicense(license: String) = sourceDeps.applyLicense(license)227- override suspend fun onConsent(request: AdoptionFrame.Request) = true228-229- // The transcript sends ADOPT_CONFIRM_REQUIRED before calling this, so230- // the receiver's own prompt is already live when we ask our human to231- // compare the same SAS. The request is only a prompt label here.232- override suspend fun onConsent(request: AdoptionFrame.RequestV2): Boolean =233- confirm(234- SASDerivation.derive(noise.handshakeHash),235- AdoptionFrame.Request(236- sessionRequestId = request.deviceId,237- deviceName = request.deviceName,238- deviceType = request.deviceType,239- license = request.license,240- snapshotCapable = request.snapshotCapable,241- ),242- )243- override suspend fun persistPendingHolder(request: AdoptionFrame.Request, nowMs: Long) = sourceDeps.persistPendingHolder(request, nowMs)244- override suspend fun completePendingHolder(request: AdoptionFrame.Request, device: PairedDevice) = sourceDeps.completePendingHolder(request, device)245- override suspend fun persistPendingIdentity(device: PairedDevice, nowMs: Long) = sourceDeps.persistPendingIdentity(device, nowMs)246- override suspend fun completePendingIdentity(device: PairedDevice) = sourceDeps.completePendingIdentity(device)247- override suspend fun upsertPairedDevice(device: PairedDevice) = sourceDeps.upsertPairedDevice(device)248- override fun hasDeviceIdentityCollision(device: PairedDevice) = sourceDeps.hasDeviceIdentityCollision(device)249- override suspend fun rotateSourceIdentity() = sourceDeps.rotateSourceIdentity()250- override fun snapshotCapable() = sourceDeps.snapshotCapable()251- override suspend fun vaultSnapshot() = sourceDeps.vaultSnapshot()252- override suspend fun outgoingDeviceManifest() = sourceDeps.outgoingDeviceManifest()253- override suspend fun markSynced(deviceId: String) = sourceDeps.markSynced(deviceId)254- override suspend fun duressSlotJson() = sourceDeps.duressSlotJson()255- }220+ // §10.5: the whole forwarding surface lives in one binder so a dropped221+ // callback is caught by AdoptionAdapterCoverageTest rather than by a peer.222+ val boundDeps = bindSourceSessionDeps(223+ sourceDeps = sourceDeps,224+ sourceIdentity = localIdentity,225+ pairState = pairState,226+ sas = SASDerivation.derive(noise.handshakeHash),227+ snapshotCapable = sourceDeps.snapshotCapable(),228+ confirm = confirm,229+ )256230 val adopted = when (request) {257231 is AdoptionFrame.RequestV2 -> grantAdoptionV2(channel, request, boundDeps)258232 is AdoptionFrame.Request -> grantAdoption(channel, request, boundDeps)app/src/main/java/com/peachpasswords/sync/RelayAdoptionReceiver.kt+8 −47
@@ -402,53 +402,14 @@402402 ),403403 relayUrl = invitation.relayUrl,404404 )405- val boundDeps = object : AdoptionSourceV2Deps {406- override val sourceIdentity = sourceDeps.sourceIdentity407- override val pairState = pairState408- override suspend fun rootKeyHex(): String = sourceDeps.rootKeyHex()409- override suspend fun vaultId(): String = sourceDeps.vaultId()410- override suspend fun license(): String? = sourceDeps.license()411- override suspend fun applyLicense(license: String) {412- sourceDeps.applyLicense(license)413- }414- override suspend fun onConsent(request: AdoptionFrame.Request): Boolean = true415-416- // Called by the v2 transcript after ADOPT_CONFIRM_REQUIRED goes out,417- // so both prompts are live at once. The prompt itself is the same418- // human SAS comparison as v1; the label is never persisted.419- override suspend fun onConsent(request: AdoptionFrame.RequestV2): Boolean =420- confirm(421- SASDerivation.derive(noise.handshakeHash),422- AdoptionFrame.Request(423- sessionRequestId = request.deviceId,424- deviceName = request.deviceName,425- deviceType = request.deviceType,426- license = request.license,427- snapshotCapable = request.snapshotCapable,428- ),429- )430-431- override suspend fun persistPendingHolder(request: AdoptionFrame.Request, nowMs: Long) =432- sourceDeps.persistPendingHolder(request, nowMs)433- override suspend fun completePendingHolder(request: AdoptionFrame.Request, device: PairedDevice) =434- sourceDeps.completePendingHolder(request, device)435- override suspend fun persistPendingIdentity(device: PairedDevice, nowMs: Long) =436- sourceDeps.persistPendingIdentity(device, nowMs)437- override suspend fun completePendingIdentity(device: PairedDevice) =438- sourceDeps.completePendingIdentity(device)439- override fun snapshotCapable(): Boolean = true440- override suspend fun upsertPairedDevice(device: PairedDevice) {441- sourceDeps.upsertPairedDevice(device)442- }443- override fun hasDeviceIdentityCollision(device: PairedDevice): Boolean =444- sourceDeps.hasDeviceIdentityCollision(device)445- override suspend fun rotateSourceIdentity(): LocalDeviceIdentity =446- sourceDeps.rotateSourceIdentity()447- override suspend fun vaultSnapshot() = sourceDeps.vaultSnapshot()448- override suspend fun outgoingDeviceManifest() = sourceDeps.outgoingDeviceManifest()449- override suspend fun markSynced(deviceId: String) = sourceDeps.markSynced(deviceId)450- override suspend fun duressSlotJson() = sourceDeps.duressSlotJson()451- }405+ val boundDeps = bindSourceSessionDeps(406+ sourceDeps = sourceDeps,407+ sourceIdentity = sourceDeps.sourceIdentity,408+ pairState = pairState,409+ sas = SASDerivation.derive(noise.handshakeHash),410+ snapshotCapable = true,411+ confirm = confirm,412+ )452413 // A v2 receiver introduces itself first; a v1 receiver still opens with453414 // the label-only ADOPT_REQUEST and is served by the legacy path.454415 val adopted = when (request) {app/src/test/java/com/peachpasswords/sync/AdoptionAdapterCoverageTest.kt+191 −0
@@ -0,0 +1,191 @@1+package com.peachpasswords.sync2+3+import kotlinx.coroutines.runBlocking4+import org.junit.Assert.assertEquals5+import org.junit.Assert.assertFalse6+import org.junit.Assert.assertTrue7+import org.junit.Test8+9+/**10+ * §10.5: every relay adapter must forward every v2 callback, and a callback must11+ * never be one that only fails once a peer asks for it.12+ *13+ * The v2 interfaces extend the v1 surface, so a *missing* member is a compile14+ * error. What the compiler cannot see is a member that is present and returns15+ * without calling through — which is exactly the defect this exists to catch:16+ * the host's `onConsent(RequestV2)` once returned `true` without reaching the17+ * SAS prompt, so the source-side confirmation never appeared.18+ *19+ * [bindSourceSessionDeps] is the whole forwarding surface for both relay source20+ * adapters, so dirtying every member here covers both.21+ */22+class AdoptionAdapterCoverageTest {23+24+ private val request = AdoptionFrame.Request(25+ sessionRequestId = "session-request",26+ deviceName = "Phone",27+ deviceType = "android",28+ )29+30+ private val requestV2 = AdoptionFrame.RequestV2(31+ adoptionVersion = ADOPTION_VERSION,32+ deviceId = "receiver-id",33+ deviceName = "Phone",34+ deviceType = "android",35+ longTermPublicKeyHex = "aa".repeat(32),36+ signingPublicKeyHex = "bb".repeat(32),37+ )38+39+ private val device = PairedDevice(40+ deviceId = "receiver-id",41+ deviceName = "Phone",42+ deviceType = "android",43+ longTermPublicKeyHex = "aa".repeat(32),44+ pairedAt = 1L,45+ )46+47+ private val presented = LocalDeviceIdentity(48+ deviceId = "presented-id",49+ deviceName = "This browser",50+ deviceType = DeviceType.ANDROID,51+ longTermPublicKeyHex = "cc".repeat(32),52+ signingPublicKeyHex = "dd".repeat(32),53+ )54+55+ /// Records which members the binder actually reached.56+ private class RecordingSourceDeps : AdoptionSourceV2Deps {57+ val calls = mutableSetOf<String>()58+59+ override val sourceIdentity = LocalDeviceIdentity(60+ deviceId = "probe-id",61+ deviceName = "Probe",62+ deviceType = DeviceType.ANDROID,63+ longTermPublicKeyHex = "ee".repeat(32),64+ signingPublicKeyHex = "ff".repeat(32),65+ )66+67+ override val pairState = AdoptionPairState("01".repeat(32), "wss://probe.test")68+69+ override suspend fun rootKeyHex() = "11".repeat(32).also { calls += "rootKeyHex" }70+ override suspend fun vaultId() = "22".repeat(32).also { calls += "vaultId" }71+ override suspend fun license() = "PPK-probe".also { calls += "license" }72+ override suspend fun applyLicense(license: String) {73+ calls += "applyLicense"74+ }75+76+ override suspend fun onConsent(request: AdoptionFrame.Request) =77+ true.also { calls += "onConsentV1" }78+79+ override suspend fun onConsent(request: AdoptionFrame.RequestV2) =80+ true.also { calls += "onConsentV2" }81+82+ override suspend fun persistPendingHolder(request: AdoptionFrame.Request, nowMs: Long) =83+ true.also { calls += "persistPendingHolder" }84+85+ override suspend fun completePendingHolder(request: AdoptionFrame.Request, device: PairedDevice) =86+ true.also { calls += "completePendingHolder" }87+88+ override suspend fun persistPendingIdentity(device: PairedDevice, nowMs: Long) =89+ true.also { calls += "persistPendingIdentity" }90+91+ override suspend fun completePendingIdentity(device: PairedDevice) =92+ true.also { calls += "completePendingIdentity" }93+94+ override suspend fun upsertPairedDevice(device: PairedDevice) {95+ calls += "upsertPairedDevice"96+ }97+98+ override fun hasDeviceIdentityCollision(device: PairedDevice) =99+ false.also { calls += "hasDeviceIdentityCollision" }100+101+ override suspend fun rotateSourceIdentity() =102+ sourceIdentity.also { calls += "rotateSourceIdentity" }103+104+ override fun snapshotCapable() = false.also { calls += "snapshotCapable" }105+106+ override suspend fun vaultSnapshot() = null.also { calls += "vaultSnapshot" }107+ override suspend fun outgoingDeviceManifest() = null.also { calls += "outgoingDeviceManifest" }108+109+ override suspend fun markSynced(deviceId: String) {110+ calls += "markSynced"111+ }112+113+ override suspend fun duressSlotJson() = null.also { calls += "duressSlotJson" }114+ }115+116+ @Test117+ fun `the session binder forwards every member, and covers the whole interface`() = runBlocking {118+ val probe = RecordingSourceDeps()119+ val sessionPairState = AdoptionPairState("33".repeat(32), "wss://session.test")120+ var confirmedSas: String? = null121+ var confirmedRequest: AdoptionFrame.Request? = null122+123+ val bound = bindSourceSessionDeps(124+ sourceDeps = probe,125+ sourceIdentity = presented,126+ pairState = sessionPairState,127+ sas = "12345678",128+ snapshotCapable = true,129+ confirm = { sas, request ->130+ confirmedSas = sas131+ confirmedRequest = request132+ true133+ },134+ )135+136+ // Session facts are bound, not forwarded.137+ assertEquals("the presented identity is the session's", presented, bound.sourceIdentity)138+ assertEquals(sessionPairState, bound.pairState)139+ assertTrue("snapshot capability is a session fact", bound.snapshotCapable())140+141+ bound.rootKeyHex()142+ bound.vaultId()143+ bound.license()144+ bound.applyLicense("PPK-x")145+ bound.onConsent(request)146+ bound.persistPendingHolder(request, 1L)147+ bound.completePendingHolder(request, device)148+ bound.persistPendingIdentity(device, 1L)149+ bound.completePendingIdentity(device)150+ bound.upsertPairedDevice(device)151+ bound.hasDeviceIdentityCollision(device)152+ bound.rotateSourceIdentity()153+ bound.vaultSnapshot()154+ bound.outgoingDeviceManifest()155+ bound.markSynced(device.deviceId)156+ bound.duressSlotJson()157+ assertEquals(true, bound.onConsent(requestV2))158+159+ // The v2 consent must reach the prompt. This is the defect that motivated160+ // §10.5: a `true` here without calling through loses the user's SAS.161+ assertEquals("the v2 consent must carry the session SAS", "12345678", confirmedSas)162+ assertEquals(163+ "the v2 consent must hand the human the requesting device's label",164+ "receiver-id",165+ confirmedRequest?.sessionRequestId,166+ )167+ // v1 consent is answered by the caller, which has already run its own SAS168+ // prompt before the grant; the binder must not prompt a second time.169+ assertFalse("the v1 consent is answered locally", probe.calls.contains("onConsentV1"))170+171+ // Exhaustive: a member added to the interface and not exercised here172+ // fails this, rather than surfacing once a peer triggers it.173+ val declared = AdoptionSourceV2Deps::class.java.methods174+ .map { it.name }175+ .filterNot { it in OBJECT_METHODS }176+ .toSet()177+ val covered = probe.calls178+ .filterNot { it == "onConsentV1" || it == "onConsentV2" }179+ .toSet() +180+ setOf("onConsent", "getSourceIdentity", "getPairState", "snapshotCapable")181+ assertEquals(182+ "every AdoptionSourceV2Deps member must be covered by this test",183+ declared,184+ covered,185+ )186+ }187+188+ private companion object {189+ val OBJECT_METHODS = setOf("equals", "hashCode", "toString")190+ }191+}