Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalSendEnvelopeTest.kt
SignalSendEnvelopeTest.kt105 lines · 4.4 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.signal.SignalDeviceMismatch |
| 4 | import com.vayunmathur.communicate.data.signal.transport.SignalPayload |
| 5 | import kotlinx.serialization.json.Json |
| 6 | import kotlinx.serialization.json.jsonArray |
| 7 | import kotlinx.serialization.json.jsonObject |
| 8 | import kotlinx.serialization.json.jsonPrimitive |
| 9 | import kotlin.test.Test |
| 10 | import kotlin.test.assertEquals |
| 11 | import kotlin.test.assertFailsWith |
| 12 | import kotlin.test.assertNull |
| 13 | import kotlin.test.assertTrue |
| 14 | |
| 15 | /** |
| 16 | * Wire vectors for the send envelope. The details that break silently: `CiphertextMessage` and |
| 17 | * `Envelope` type numbers are different spaces, message `content` is base64 **with** padding (pre-keys |
| 18 | * are unpadded), and 409/410 name devices under different keys. |
| 19 | */ |
| 20 | class SignalSendEnvelopeTest { |
| 21 | private val json = Json |
| 22 | |
| 23 | // CiphertextMessage constants: WHISPER_TYPE = 2, PREKEY_TYPE = 3. |
| 24 | // Envelope constants: DOUBLE_RATCHET = 1, PREKEY_MESSAGE = 3. |
| 25 | @Test |
| 26 | fun mapsCiphertextTypesOntoEnvelopeTypes() { |
| 27 | assertEquals(1, SignalPayload.envelopeTypeFor(2)) |
| 28 | assertEquals(3, SignalPayload.envelopeTypeFor(3)) |
| 29 | } |
| 30 | |
| 31 | @Test |
| 32 | fun rejectsCiphertextTypesThatCannotBeSent() { |
| 33 | // Sender-key (7) and plaintext (8) do not belong on this path. |
| 34 | assertFailsWith<IllegalArgumentException> { SignalPayload.envelopeTypeFor(7) } |
| 35 | assertFailsWith<IllegalArgumentException> { SignalPayload.envelopeTypeFor(8) } |
| 36 | } |
| 37 | |
| 38 | @Test |
| 39 | fun buildsTheOutgoingPushMessageListShape() { |
| 40 | val body = SignalPayload.buildPutMessagesBody( |
| 41 | destinationAci = "aci-1", |
| 42 | messages = listOf( |
| 43 | SignalPayload.OutgoingPushMessage(3, 1, 4444, byteArrayOf(1, 2, 3)), |
| 44 | SignalPayload.OutgoingPushMessage(1, 2, 5555, byteArrayOf(4, 5)), |
| 45 | ), |
| 46 | timestamp = 1700000000000L, |
| 47 | ) |
| 48 | val root = json.parseToJsonElement(body.toString(Charsets.UTF_8)).jsonObject |
| 49 | assertEquals("aci-1", root["destination"]?.jsonPrimitive?.content) |
| 50 | assertEquals(1700000000000L, root["timestamp"]?.jsonPrimitive?.content?.toLong()) |
| 51 | assertEquals("false", root["online"]?.jsonPrimitive?.content) |
| 52 | assertEquals("true", root["urgent"]?.jsonPrimitive?.content) |
| 53 | |
| 54 | val messages = root["messages"]!!.jsonArray |
| 55 | assertEquals(2, messages.size) |
| 56 | val first = messages[0].jsonObject |
| 57 | assertEquals("3", first["type"]?.jsonPrimitive?.content) |
| 58 | assertEquals("1", first["destinationDeviceId"]?.jsonPrimitive?.content) |
| 59 | assertEquals("4444", first["destinationRegistrationId"]?.jsonPrimitive?.content) |
| 60 | // 3 bytes -> 4 base64 chars, and padding must be present. |
| 61 | assertEquals("AQID", first["content"]?.jsonPrimitive?.content) |
| 62 | assertEquals("BAU=", messages[1].jsonObject["content"]?.jsonPrimitive?.content) |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | fun putPathCarriesTheStoryFlag() { |
| 67 | assertEquals("/v1/messages/aci-1?story=false", SignalPayload.putMessagesPath("aci-1")) |
| 68 | assertEquals("/v1/messages/aci-1?story=true", SignalPayload.putMessagesPath("aci-1", story = true)) |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | fun parses409MismatchedDevices() { |
| 73 | val m = SignalDeviceMismatch.parse(409, """{"missingDevices":[2,3],"extraDevices":[4]}""")!! |
| 74 | assertEquals(setOf(2, 3), m.fetch) |
| 75 | assertEquals(setOf(4), m.archive) |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | fun parses410StaleDevicesAsArchiveAndRebuild() { |
| 80 | // Archiving alone would leave the device unsendable until a later 409 named it, so a stale |
| 81 | // device is both archived (keeping the old chain readable) and refetched. |
| 82 | val m = SignalDeviceMismatch.parse(410, """{"staleDevices":[2]}""")!! |
| 83 | assertEquals(setOf(2), m.archive) |
| 84 | assertEquals(setOf(2), m.fetch) |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | fun returnsNullWhenNoDevicesAreNamed() { |
| 89 | assertNull(SignalDeviceMismatch.parse(409, """{"missingDevices":[],"extraDevices":[]}""")) |
| 90 | assertNull(SignalDeviceMismatch.parse(410, """{"staleDevices":[]}""")) |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | fun returnsNullForOtherStatusesOrJunk() { |
| 95 | assertNull(SignalDeviceMismatch.parse(400, """{"missingDevices":[1]}""")) |
| 96 | assertNull(SignalDeviceMismatch.parse(409, "not json")) |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | fun ignoresTheOtherStatusFieldNames() { |
| 101 | // A 409 body must not pick up staleDevices, and vice versa. |
| 102 | assertNull(SignalDeviceMismatch.parse(409, """{"staleDevices":[2]}""")) |
| 103 | assertNull(SignalDeviceMismatch.parse(410, """{"missingDevices":[2]}""")) |
| 104 | } |
| 105 | } |