1package com.peachpasswords.sync23import org.junit.Assert.assertEquals4import org.junit.Assert.assertNull5import org.junit.Test67class PairedDeviceRevocationTest {8 private val browser = PairedDevice(9 deviceId = "browser-1",10 deviceName = "Chrome",11 deviceType = "extension",12 longTermPublicKeyHex = "ab".repeat(32),13 pairedAt = 1L,14 )1516 @Test17 fun `new revocation records the key and removes the paired row`() {18 val mutation = planDeviceRevocation(19 devices = listOf(browser),20 revocations = emptyList(),21 deviceId = browser.deviceId,22 revokedByDeviceId = "phone-1",23 nowMs = 42L,24 )2526 assertEquals(emptyList<PairedDevice>(), mutation.devices)27 assertEquals(1, mutation.revocations.size)28 assertEquals(browser.longTermPublicKeyHex, mutation.added?.longTermPublicKeyHex)29 assertEquals(42L, mutation.added?.revokedAt)30 }3132 @Test33 fun `full ledger admits another issuer by evicting the largest issuer oldest record`() {34 val existing = List(500) { index ->35 DeviceRevocation("terminal-$index", index.toString(16).padStart(64, '0'), index.toLong(), "issuer-a")36 }37 val merged = mergeRevocationRecords(existing, listOf(38 DeviceRevocation("peer-extra", "ff".repeat(32), 501L, "issuer-b"),39 ))40 assertEquals(500, merged.records.size)41 assertEquals(listOf("peer-extra"), merged.admitted.map { it.deviceId })42 assertEquals(false, merged.records.any { it.deviceId == "terminal-0" })43 assertEquals(true, merged.records.any { it.deviceId == "peer-extra" })44 }4546 @Test47 fun `same issuer local flood uses bounded capacity`() {48 var records = emptyList<DeviceRevocation>()49 repeat(501) { index ->50 records = if (records.size < 500) records + DeviceRevocation("local-$index", index.toString(16).padStart(64, '0'), index.toLong(), "issuer-a")51 else records.drop(1) + DeviceRevocation("local-$index", index.toString(16).padStart(64, '0'), index.toLong(), "issuer-a")52 }53 assertEquals(500, records.size)54 assertEquals("local-1", records.first().deviceId)55 assertEquals("local-500", records.last().deviceId)56 }5758 @Test59 fun `different issuer local capacity preserves genuine issuer record`() {60 val existing = List(500) { index ->61 DeviceRevocation("terminal-$index", index.toString(16).padStart(64, '0'), index.toLong(), if (index == 0) "issuer-a" else "issuer-b")62 }63 val removable = existing.indexOfFirst { it.revokedByDeviceId != "issuer-a" }64 val records = existing.toMutableList().also { it.removeAt(removable); it += DeviceRevocation("local-new", "ff".repeat(32), 501L, "issuer-a") }65 assertEquals(500, records.size)66 assertEquals("terminal-0", records.first().deviceId)67 assertEquals("local-new", records.last().deviceId)68 }6970 @Test71 fun `peer flood evicts only its own oldest records once it is largest issuer`() {72 val existing = listOf(73 DeviceRevocation("genuine", "aa".repeat(32), 1L, "honest-issuer"),74 )75 val flood = List(600) { index ->76 DeviceRevocation(77 "junk-$index",78 index.toString(16).padStart(64, '0'),79 index.toLong() + 2,80 "flooding-issuer",81 )82 }8384 val merged = mergeRevocationRecords(existing, flood)8586 assertEquals(500, merged.records.size)87 assertEquals(600, merged.admitted.size)88 assertEquals(true, merged.records.any { it.deviceId == "genuine" })89 assertEquals(false, merged.records.any { it.deviceId == "junk-0" })90 assertEquals(true, merged.records.any { it.deviceId == "junk-599" })91 }9293 @Test94 fun `existing revocation still removes a stale overlapping paired row`() {95 val existing = DeviceRevocation(96 deviceId = browser.deviceId,97 longTermPublicKeyHex = browser.longTermPublicKeyHex!!,98 revokedAt = 1L,99 revokedByDeviceId = "phone-1",100 )101102 val mutation = planDeviceRevocation(103 devices = listOf(browser),104 revocations = listOf(existing),105 deviceId = browser.deviceId,106 revokedByDeviceId = "phone-1",107 nowMs = 99L,108 )109110 assertEquals(emptyList<PairedDevice>(), mutation.devices)111 assertEquals(listOf(existing), mutation.revocations)112 assertNull(mutation.added)113 }114115 @Test116 fun `fresh re-pair rotates a stable profile id already in the revocation ledger`() {117 val revoked = DeviceRevocation(118 deviceId = browser.deviceId,119 longTermPublicKeyHex = browser.longTermPublicKeyHex!!,120 revokedAt = 1L,121 revokedByDeviceId = "chrome-1",122 )123124 assertEquals(true, hasAdoptionIdentityConflict(browser, emptyList(), listOf(revoked)))125 }126127 @Test128 fun `fresh re-pair rotates a stable profile key revoked under an older id`() {129 val revoked = DeviceRevocation(130 deviceId = "old-firefox-id",131 longTermPublicKeyHex = browser.longTermPublicKeyHex!!,132 revokedAt = 1L,133 revokedByDeviceId = "chrome-1",134 )135136 assertEquals(true, hasAdoptionIdentityConflict(browser, emptyList(), listOf(revoked)))137 }138}