1package com.peachpasswords.sync23import org.junit.Assert.assertEquals4import org.junit.Assert.assertNull5import org.junit.Assert.assertSame6import org.junit.Assert.assertThrows7import org.junit.Test8import java.security.SecureRandom910class RendezvousTest {11 private val handshakeHash = Rendezvous.secretFromHex(12 "0fbab8cd1782bf4b17d69beb69f5b80b0bf24d93da4924959c76ee701c32e655",13 )1415 @Test fun `matches browser rendezvous KATs`() {16 val secret = Rendezvous.deriveSecret(handshakeHash)17 assertEquals(18 "0803691c8ac7330883c0987e33cea4a171992366ce4b06b88691e84ca689773d",19 Rendezvous.bytesToHex(secret),20 )21 assertEquals(22 "9UY3j5AHnwYSApvymtY965su9n60rQOv8QZ2PZzHuKI",23 Rendezvous.deriveId(secret, 500_000),24 )25 }2627 @Test fun `matches browser symmetric mesh capability KAT`() {28 val alicePrivate = ByteArray(32) { 1 }29 val bobPrivate = ByteArray(32) { 2 }30 val alicePublic = org.bouncycastle.crypto.params.X25519PrivateKeyParameters(alicePrivate, 0).generatePublicKey().encoded31 val bobPublic = org.bouncycastle.crypto.params.X25519PrivateKeyParameters(bobPrivate, 0).generatePublicKey().encoded32 val forward = Rendezvous.deriveMeshSecret(alicePrivate, alicePublic, bobPublic)33 val reverse = Rendezvous.deriveMeshSecret(bobPrivate, bobPublic, alicePublic)3435 assertEquals(Rendezvous.bytesToHex(forward), Rendezvous.bytesToHex(reverse))36 }3738 @Test fun `uses required waiting and dialing window order`() {39 val secret = Rendezvous.deriveSecret(handshakeHash)40 val now = 500_000L * 3600 + 359941 assertEquals(42 listOf(43 Rendezvous.deriveId(secret, 500_000),44 Rendezvous.deriveId(secret, 499_999),45 ),46 Rendezvous.waitingIds(secret, now),47 )48 assertEquals(49 listOf(50 Rendezvous.deriveId(secret, 500_000),51 Rendezvous.deriveId(secret, 499_999),52 Rendezvous.deriveId(secret, 500_001),53 ),54 Rendezvous.dialingIds(secret, now),55 )56 }5758 @Test fun `opaque capabilities resolve current and previous windows only`() {59 val secretHex = "42".repeat(32)60 val peer = PairedDevice(61 deviceId = "browser-1",62 longTermPublicKeyHex = "ab".repeat(32),63 pairedAt = 1L,64 rendezvousSecretHex = secretHex,65 )66 val legacy = PairedDevice(67 deviceId = "legacy",68 longTermPublicKeyHex = "cd".repeat(32),69 pairedAt = 1L,70 )71 val secret = Rendezvous.secretFromHex(secretHex)72 val now = 500_000L * 3600 + 359973 val current = Rendezvous.deriveId(secret, 500_000)74 val previous = Rendezvous.deriveId(secret, 499_999)75 val next = Rendezvous.deriveId(secret, 500_001)7677 assertSame(peer, Rendezvous.resolvePeer(listOf(legacy, peer), current, now))78 assertSame(peer, Rendezvous.resolvePeer(listOf(legacy, peer), previous, now))79 assertNull(Rendezvous.resolvePeer(listOf(legacy, peer), next, now))80 assertNull(Rendezvous.resolvePeer(listOf(legacy, peer), "Z".repeat(43), now))81 assertNull(Rendezvous.resolvePeer(listOf(legacy), current, now))82 }8384 @Test fun `first-pair token is 128-bit base64url`() {85 val deterministic = object : SecureRandom() {86 override fun nextBytes(bytes: ByteArray) {87 bytes.indices.forEach { bytes[it] = it.toByte() }88 }89 }90 assertEquals("AAECAwQFBgcICQoLDA0ODw", Rendezvous.newPairingToken(deterministic))91 }9293 @Test fun `relay URL requires credential-free wss`() {94 assertEquals(95 "wss://relay.example.test/base",96 Rendezvous.normalizeRelayUrl("wss://relay.example.test/base/"),97 )98 assertThrows(IllegalArgumentException::class.java) {99 Rendezvous.normalizeRelayUrl("ws://relay.example.test")100 }101 assertThrows(IllegalArgumentException::class.java) {102 Rendezvous.normalizeRelayUrl("wss://user:[email protected]")103 }104 }105106 @Test fun `completed pairing persists secret and the listener relay`() {107 val device = PairedDevice(108 deviceId = "browser-1",109 deviceName = "Firefox",110 deviceType = "extension",111 longTermPublicKeyHex = "ab".repeat(32),112 pairedAt = 1L,113 )114 val bound = Rendezvous.bindPairState(115 device,116 handshakeHash,117 "wss://relay.example.test/",118 )119 assertEquals(120 "0803691c8ac7330883c0987e33cea4a171992366ce4b06b88691e84ca689773d",121 bound.rendezvousSecretHex,122 )123 assertEquals("wss://relay.example.test", bound.relayUrl)124 }125}