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 / SignalPaddingTest.kt

SignalPaddingTest.kt83 lines · 3.7 KiBRaw
1package com.vayunmathur.communicate.signal
2
3import com.google.protobuf.ByteString
4import com.vayunmathur.communicate.data.signal.SignalProtocol
5import org.whispersystems.signalservice.internal.push.SignalServiceProtos
6import kotlin.test.Test
7import kotlin.test.assertEquals
8import kotlin.test.assertFalse
9import kotlin.test.assertTrue
10
11/**
12 * Vectors for Signal's transport padding (trailing `0x80` terminator, 80-byte blocks) and the
13 * `PLAINTEXT_CONTENT` restriction. Pure, no Android — so the malformed-padding branch, which logs,
14 * is deliberately not exercised here.
15 */
16class SignalPaddingTest {
17 @Test
18 fun roundTrip_preservesBodyAcrossBlockBoundaries() {
19 for (size in intArrayOf(0, 1, 2, 77, 78, 79, 80, 81, 158, 159, 160, 161, 1000)) {
20 val body = ByteArray(size) { (it % 251).toByte() }
21 val stripped = SignalProtocol.stripMessagePadding(SignalProtocol.padMessageBody(body))
22 assertTrue(body.contentEquals(stripped), "round-trip failed for size=$size")
23 }
24 }
25
26 @Test
27 fun paddedLength_isOneShyOfABlockMultiple() {
28 // The +1/-1 in the official scheme leaves the cipher room for its own padding byte, so the
29 // padded length is always blocks*80 - 1 rather than a clean multiple.
30 assertEquals(79, SignalProtocol.padMessageBody(ByteArray(0)).size)
31 assertEquals(79, SignalProtocol.padMessageBody(ByteArray(78)).size)
32 assertEquals(159, SignalProtocol.padMessageBody(ByteArray(79)).size)
33 assertEquals(159, SignalProtocol.padMessageBody(ByteArray(158)).size)
34 assertEquals(239, SignalProtocol.padMessageBody(ByteArray(159)).size)
35 }
36
37 @Test
38 fun terminatorFollowsBodyAndRestIsZero() {
39 val body = ByteArray(10) { 0x41 }
40 val padded = SignalProtocol.padMessageBody(body)
41 assertEquals(0x80.toByte(), padded[10])
42 assertTrue(padded.drop(11).all { it == 0x00.toByte() })
43 }
44
45 /**
46 * Regression: a `Content` whose first field is a `DataMessage` serializes with leading tag byte
47 * `0x0A`. The previous implementation stripped any leading byte in `0x00..0x0F`, which silently
48 * ate that tag and broke every text message, reaction and delete.
49 */
50 @Test
51 fun leadingDataMessageTagByteSurvives() {
52 val body = byteArrayOf(0x0A, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F)
53 val stripped = SignalProtocol.stripMessagePadding(SignalProtocol.padMessageBody(body))
54 assertEquals(0x0A.toByte(), stripped.first())
55 assertTrue(body.contentEquals(stripped))
56 }
57
58 @Test
59 fun plaintextContent_acceptsOnlyADecryptionErrorMessage() {
60 val decryptionErrorOnly = SignalServiceProtos.Content.newBuilder()
61 .setDecryptionErrorMessage(ByteString.copyFrom(byteArrayOf(1, 2, 3)))
62 .build()
63 assertTrue(SignalProtocol.isValidPlaintextContent(decryptionErrorOnly))
64 }
65
66 @Test
67 fun plaintextContent_rejectsADataMessage() {
68 // The spoofing case: an unencrypted DataMessage must never be treated as authentic.
69 val dataMessage = SignalServiceProtos.Content.newBuilder()
70 .setDataMessage(SignalServiceProtos.DataMessage.newBuilder().setBody("spoofed"))
71 .build()
72 assertFalse(SignalProtocol.isValidPlaintextContent(dataMessage))
73 }
74
75 @Test
76 fun plaintextContent_rejectsADecryptionErrorSmuggledAlongsideOtherFields() {
77 val smuggled = SignalServiceProtos.Content.newBuilder()
78 .setDecryptionErrorMessage(ByteString.copyFrom(byteArrayOf(1, 2, 3)))
79 .setDataMessage(SignalServiceProtos.DataMessage.newBuilder().setBody("spoofed"))
80 .build()
81 assertFalse(SignalProtocol.isValidPlaintextContent(smuggled))
82 }
83}