RelayFramesTest.kt

2.0 KB · raw

1package com.peachpasswords.sync23import org.junit.Assert.assertEquals4import org.junit.Assert.assertNull5import org.junit.Assert.assertTrue6import org.junit.Test78/**9 * The relay frames share a channel with ordinary sync frames, so the one thing10 * that must never break is telling them apart: a sync frame misread as a relay11 * frame would be forwarded to another device, and a relay frame misread as sync12 * would be handed to the vault.13 */14class RelayFramesTest {1516    @Test fun `a relay request round-trips`() {17        val key = "ab".repeat(32)18        val decoded = decodeRelayMessage(RelayMessage.Request(key).encodeToBytes())19        assertEquals(RelayMessage.Request(key), decoded)20    }2122    @Test fun `frames use the type discriminator the browser sync frames use`() {23        val json = String(RelayMessage.Request("00".repeat(32)).encodeToBytes(), Charsets.UTF_8)24        assertTrue(json, json.contains("\"type\":\"RELAY_REQUEST\""))25        assertTrue(json, json.contains("\"targetDeviceId\":"))26    }2728    @Test fun `sync traffic is not mistaken for a relay frame`() {29        val hello = SyncMessage.Hello("v", "d", 1, 2).encodeToBytes()30        assertNull(decodeRelayMessage(hello))31    }3233    @Test fun `junk and empty frames are not relay frames`() {34        assertNull(decodeRelayMessage(ByteArray(0)))35        assertNull(decodeRelayMessage(byteArrayOf(0x01, 0x02, 0x03)))36        assertNull(decodeRelayMessage("not json".toByteArray()))37    }3839    @Test fun `a payload survives forwarding unchanged`() {40        val opaque = java.util.Base64.getEncoder().encodeToString(byteArrayOf(0, 1, 2, -1, -2))41        val decoded = decodeRelayMessage(RelayMessage.Payload(opaque).encodeToBytes())42        assertEquals(opaque, (decoded as RelayMessage.Payload).payload)43    }4445    @Test fun `the parameterless responses decode by type alone`() {46        listOf(47            RelayMessage.Accept,48            RelayMessage.Unavailable,49            RelayMessage.Disabled,50            RelayMessage.Close,51        ).forEach { assertEquals(it, decodeRelayMessage(it.encodeToBytes())) }52    }53}