Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalDataMessageTest.kt

SignalDataMessageTest.kt65 lines · 2.7 KiBRaw
1package com.vayunmathur.communicate.signal
2
3import com.vayunmathur.communicate.data.signal.transport.SignalPayload
4import kotlinx.serialization.json.jsonObject
5import kotlinx.serialization.json.jsonPrimitive
6import kotlinx.serialization.json.long
7import kotlin.test.Test
8import kotlin.test.assertEquals
9
10/**
11 * The envelope timestamp and `DataMessage.timestamp` must be the same value: recipients reject a
12 * mismatch outright ("Timestamps don't match!"), and the two were previously generated from separate
13 * clock reads, so anything slow in between produced a message the peer silently discarded.
14 *
15 * `requiredProtocolVersion` must also reflect only the features actually used — claiming `CURRENT`
16 * on a plain message makes any older client reject it as unsupported.
17 */
18class SignalDataMessageTest {
19 @Test
20 fun plainMessageClaimsNoProtocolFloor() {
21 val dm = SignalPayload.buildDataMessage(body = "hello", timestamp = 1_700_000_000_000L)
22 assertEquals(0, dm.requiredProtocolVersion)
23 assertEquals(1_700_000_000_000L, dm.timestamp)
24 }
25
26 @Test
27 fun reactionClaimsTheReactionsVersion() {
28 val reaction = org.whispersystems.signalservice.internal.push.SignalServiceProtos.DataMessage.Reaction
29 .newBuilder()
30 .setEmoji("x")
31 .setTargetSentTimestamp(1L)
32 .build()
33 val dm = SignalPayload.buildDataMessage(body = "", timestamp = 1L, reaction = reaction)
34 // REACTIONS = 4
35 assertEquals(4, dm.requiredProtocolVersion)
36 }
37
38 @Test
39 fun pollClaimsThePollsVersion() {
40 val poll = org.whispersystems.signalservice.internal.push.SignalServiceProtos.DataMessage.PollCreate
41 .newBuilder()
42 .setQuestion("q")
43 .build()
44 val dm = SignalPayload.buildDataMessage(body = "q", timestamp = 1L, pollCreate = poll)
45 // POLLS = 8
46 assertEquals(8, dm.requiredProtocolVersion)
47 }
48
49 @Test
50 fun envelopeTimestampMatchesTheDataMessage() {
51 val sent = 1_700_000_123_456L
52 val dm = SignalPayload.buildDataMessage(body = "hi", timestamp = sent)
53 val content = SignalPayload.buildContentWithDataMessage(dm)
54 // What the send path puts on the OutgoingPushMessageList must equal what the peer validates.
55 val body = SignalPayload.buildPutMessagesBody(
56 destinationAci = "aci-1",
57 messages = listOf(SignalPayload.OutgoingPushMessage(1, 1, 1, byteArrayOf(1))),
58 timestamp = content.dataMessage.timestamp,
59 )
60 val root = kotlinx.serialization.json.Json
61 .parseToJsonElement(body.toString(Charsets.UTF_8))
62 .let { kotlinx.serialization.json.JsonObject(it.jsonObject) }
63 assertEquals(sent, root["timestamp"]?.jsonPrimitive?.long)
64 }
65}