965644f75922

add bidirectional adoption v2 process conformance gate (v2.17.31)

time
2026-09-11 02:17 UTC · 2 months ago
machine
koh2
agent
gpt-5
session
adoption v2 gate
parent
2ecce3b958d0
2 files VERSION+1 −1app/src/test/java/com/peachpasswords/sync/CrossClientSyncProcessTest.kt+112 −0
VERSION+1 −1
@@ -1 +1 @@1-2.17.301+2.17.31

view file

app/src/test/java/com/peachpasswords/sync/CrossClientSyncProcessTest.kt+112 −0
@@ -13,6 +13,7 @@1313 import kotlin.system.exitProcess1414 import kotlinx.coroutines.runBlocking1515 import org.junit.Assert.assertEquals16+import org.junit.Assert.assertNotNull1617 import org.junit.Test1718 1819 /**@@ -27,6 +28,117 @@2728  * the matrix in one place.2829  */2930 class CrossClientSyncProcessTest {31+    @Test32+    fun exchange_adoption_v2_with_browser_process() {33+        val role = configured("PEACH_ADOPTION_HARNESS_ROLE") ?: return34+        val portFile = File(requireConfigured("PEACH_SYNC_HARNESS_PORT_FILE"))35+        val transcriptFile = File(requireConfigured("PEACH_SYNC_HARNESS_TRANSCRIPT_FILE"))36+        val raw = if (role == "source") {37+            ServerSocket(0).also { portFile.writeText(it.localPort.toString()) }38+                .accept().also { portFile.delete() }39+        } else {40+            Socket().also {41+                it.connect(InetSocketAddress("127.0.0.1", requireConfigured("PEACH_SYNC_HARNESS_PORT").toInt()), 5_000)42+            }43+        }44+        raw.use { socket ->45+            val relay = LineRelaySocket(socket)46+            val root = ByteArray(32) { (it + 1).toByte() }47+            val token = "AAECAwQFBgcICQoLDA0ODw"48+            val keys = localSyncKeypair(randomLocalIdentityCredentials("android-source"))49+            val identity = harnessIdentity("android", keys.publicKey)50+            var pending = false51+            var rootAfterPending = false52+            var promoted = false53+            var collisionChecks = 054+            var rotations = 055+            val adopted = runBlocking {56+                if (role == "source") {57+                    runRelayAdoptionSourceSession(58+                        relay, token, "wss://relay.example.test", keys.secretKey, keys.publicKey, identity,59+                        object : AdoptionSourceV2Deps {60+                            override val sourceIdentity = identity61+                            override suspend fun rootKeyHex(): String {62+                                rootAfterPending = pending63+                                return Rendezvous.bytesToHex(root)64+                            }65+                            override suspend fun vaultId() = computeVaultId(root)66+                            override suspend fun license() = "PPK-cross-client"67+                            override suspend fun onConsent(request: AdoptionFrame.Request) = true68+                            override suspend fun onConsent(request: AdoptionFrame.RequestV2) = true69+                            override suspend fun persistPendingIdentity(device: PairedDevice, nowMs: Long): Boolean {70+                                pending = true71+                                return true72+                            }73+                            override suspend fun completePendingIdentity(device: PairedDevice): Boolean {74+                                promoted = true75+                                return true76+                            }77+                            override suspend fun upsertPairedDevice(device: PairedDevice) = Unit78+                            override fun hasDeviceIdentityCollision(device: PairedDevice) = collisionChecks++ == 079+                            override suspend fun rotateSourceIdentity() = identity80+                            override fun snapshotCapable() = false81+                        },82+                        confirm = { _, _ -> true },83+                    )84+                } else {85+                    runRelayAdoptionReceiverSession(86+                        relay,87+                        PairingInvitation("wss://relay.example.test", token, noiseStaticPublicKeyHex = requireConfigured("PEACH_ADOPTION_HARNESS_STATIC")),88+                        harnessReceiverDeps(identity, root) { rotations += 1 },89+                        confirm = { true },90+                    )91+                }92+            }93+            transcriptFile.writeText(94+                "{\"status\":\"${if (adopted != null) "completed" else "rejected"}\"," +95+                    "\"pendingBeforeRoot\":$rootAfterPending,\"promotedAfterApplied\":$promoted,\"rotations\":$rotations}",96+            )97+            assertNotNull(adopted)98+            if (role == "source") {99+                assertEquals(true, rootAfterPending)100+                assertEquals(true, promoted)101+            }102+        }103+    }104+105+    private fun harnessIdentity(name: String, publicKey: ByteArray) = LocalDeviceIdentity(106+        "$name-id", name, DeviceType.ANDROID, Rendezvous.bytesToHex(publicKey), "cd".repeat(32), 49142,107+        "wss://relay.example.test",108+    )109+110+    private fun harnessReceiverDeps(111+        identity: LocalDeviceIdentity,112+        root: ByteArray,113+        onRotate: () -> Unit,114+    ) = object : AdoptionReceiverV2Deps {115+        override val deviceName = "android"116+        override val deviceType = DeviceType.ANDROID117+        override fun generateDeviceId() = identity.deviceId118+        override fun localIdentity() = identity119+        override suspend fun rotateLocalIdentity() = identity.copy(deviceId = "android-rotated").also { onRotate() }120+        override suspend fun confirmSas() = true121+        override suspend fun validateSource(prepare: AdoptionFrame.Prepare) = Unit122+        override suspend fun hasSourceIdentityCollision(prepare: AdoptionFrame.Prepare) = false123+        override suspend fun createVaultFromRootKey(rootKeyHex: String) = computeVaultId(hexToPubkey(rootKeyHex))124+        override suspend fun deriveLocalIdentity() = identity125+        override suspend fun upsertPairedDevice(device: PairedDevice) = Unit126+        override fun onError(error: String) = throw AssertionError(error)127+        override suspend fun license() = "PPK-cross-client"128+    }129+130+    private class LineRelaySocket(socket: Socket) : RelayBinarySocket {131+        private val input = BufferedReader(InputStreamReader(socket.getInputStream(), Charsets.UTF_8))132+        private val output = BufferedWriter(OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8))133+        override fun send(frame: ByteArray) {134+            output.write(Base64.getEncoder().encodeToString(frame))135+            output.newLine()136+            output.flush()137+        }138+        override suspend fun receive(): ByteArray = Base64.getDecoder().decode(input.readLine() ?: error("cross-client peer closed"))139+        override fun close(code: Int, reason: String) = Unit140+    }141+30142     @Test31143     fun exchange_production_frames_with_browser_process() {32144         val mode = configured("PEACH_SYNC_HARNESS_MODE") ?: return

view file