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

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

SignalGroupIdTest.kt80 lines · 2.9 KiBRaw
1package com.vayunmathur.communicate.signal
2
3import com.vayunmathur.communicate.data.signal.SignalGroups
4import com.vayunmathur.communicate.data.signal.SignalProtocol
5import com.vayunmathur.communicate.data.signal.groupIdentifierOf
6import kotlin.test.Test
7import kotlin.test.assertEquals
8import kotlin.test.assertFalse
9import kotlin.test.assertNull
10import kotlin.test.assertTrue
11
12/**
13 * The group identifier must be *derived* from the master key, never a slice of it. The master key
14 * encrypts the group's attributes and membership, so leaking it through a conversation id — which ends
15 * up in database keys and log lines — would be handing out the group's contents.
16 */
17class SignalGroupIdTest {
18 private val masterKey = ByteArray(32) { (it + 1).toByte() }
19
20 @Test
21 fun identifierIsThirtyTwoBytes() {
22 assertEquals(32, SignalGroups.groupIdentifierBytes(masterKey).size)
23 }
24
25 @Test
26 fun identifierIsNotDerivedFromTheMasterKeyBytes() {
27 val id = SignalGroups.groupIdentifierBytes(masterKey)
28 assertFalse(id.contentEquals(masterKey))
29 // The old implementation was the hex of the first 8 bytes; make sure that shape is gone.
30 val leakedPrefix = SignalGroups.run { masterKey.toHex() }.take(16)
31 assertFalse(SignalGroups.groupIdFromMasterKey(masterKey).startsWith(leakedPrefix))
32 }
33
34 @Test
35 fun identifierIsDeterministic() {
36 assertEquals(
37 SignalGroups.groupIdFromMasterKey(masterKey),
38 SignalGroups.groupIdFromMasterKey(masterKey.copyOf()),
39 )
40 }
41
42 @Test
43 fun differentMasterKeysGiveDifferentIdentifiers() {
44 val other = ByteArray(32) { (it + 2).toByte() }
45 assertFalse(
46 SignalGroups.groupIdFromMasterKey(masterKey) == SignalGroups.groupIdFromMasterKey(other),
47 )
48 }
49
50 @Test
51 fun conversationIdRoundTripsToTheIdentifierBytes() {
52 val conversationId = SignalProtocol.toConversationId("", masterKey)
53 assertTrue(SignalProtocol.isGroupConversation(conversationId))
54 val recovered = SignalProtocol.groupIdentifierOf(conversationId)
55 assertTrue(SignalGroups.groupIdentifierBytes(masterKey).contentEquals(recovered))
56 }
57
58 @Test
59 fun oneToOneConversationIdsAreTheAci() {
60 val aci = "3f0e5a2c-1111-2222-3333-444455556666"
61 assertEquals(aci, SignalProtocol.toConversationId(aci, null as ByteArray?))
62 assertFalse(SignalProtocol.isGroupConversation(aci))
63 assertNull(SignalProtocol.groupIdentifierOf(aci))
64 }
65
66 @Test
67 fun hexRoundTrip() {
68 val bytes = ByteArray(32) { (it * 7).toByte() }
69 val hex = SignalGroups.run { bytes.toHex() }
70 assertEquals(64, hex.length)
71 assertTrue(bytes.contentEquals(SignalGroups.hexToBytes(hex)))
72 }
73
74 @Test
75 fun malformedHexIsRejected() {
76 assertNull(SignalGroups.hexToBytes(""))
77 assertNull(SignalGroups.hexToBytes("abc"))
78 assertNull(SignalGroups.hexToBytes("zz"))
79 }
80}