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

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Pass2E2ESendTest.kt

W5Pass2E2ESendTest.kt113 lines · 5.4 KiBRaw
1package com.vayunmathur.communicate.whatsapp
2
3import android.app.Application
4import android.util.Base64
5import androidx.room3.Room
6import androidx.test.core.app.ApplicationProvider
7import androidx.test.ext.junit.runners.AndroidJUnit4
8import com.vayunmathur.communicate.data.whatsapp.WhatsAppAuthData
9import com.vayunmathur.communicate.data.whatsapp.WhatsAppClient
10import com.vayunmathur.communicate.data.whatsapp.WhatsAppDatabase
11import com.vayunmathur.communicate.data.whatsapp.WhatsAppDiag
12import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol
13import com.vayunmathur.communicate.data.whatsapp.buildEncryptedGroupTextNode
14import com.vayunmathur.communicate.data.whatsapp.buildEncryptedTextNode
15import com.vayunmathur.communicate.data.whatsapp.e2e.WhatsAppE2E
16import com.vayunmathur.communicate.data.whatsapp.getUserDevices
17import com.vayunmathur.communicate.data.whatsapp.padMessage
18import com.vayunmathur.communicate.data.whatsapp.sendMessage
19import com.vayunmathur.communicate.data.whatsapp.sendMessageAdvanced
20import com.vayunmathur.communicate.data.whatsapp.transport.WhatsAppSocket
21import kotlin.test.BeforeTest
22import kotlin.test.Test
23import kotlin.test.assertEquals
24import kotlin.test.assertNotNull
25import kotlin.test.assertNull
26import kotlin.test.assertTrue
27import kotlinx.coroutines.test.runTest
28import org.junit.runner.RunWith
29
30/**
31 * Pass-2 slice-1 (whatsapp send/E2E): the encrypted-send pipeline with a real
32 * in-memory [WhatsAppDatabase] bound, so `ensureE2E` constructs and the fan-out
33 * runs until device sessions turn out to be unavailable (the unconnected socket
34 * refuses every IQ, so no device can be encrypted for and builds fail soft).
35 * Sender-key group crypto itself needs the Rust `communicate_signal` native
36 * library, which is not loadable from this module's JVM tests
37 * (UnsatisfiedLinkError), so it is deliberately not exercised here.
38 */
39@RunWith(AndroidJUnit4::class)
40class W5Pass2E2ESendTest {
41
42 private fun b64(bytes: ByteArray): String = Base64.encodeToString(bytes, Base64.NO_WRAP)
43
44 private val auth = WhatsAppAuthData(
45 phoneNumber = "+15550001", pushName = "Tester", wid = "[email protected]",
46 noisePrivateKey = b64(ByteArray(32) { 1 }), noisePublicKey = b64(ByteArray(32) { 2 }),
47 identityPrivateKey = b64(ByteArray(32) { 3 }), identityPublicKey = b64(ByteArray(32) { 4 }),
48 registrationId = 14, signedPreKeyId = 5,
49 signedPreKeyPublic = b64(ByteArray(32) { 5 }), signedPreKeyPrivate = b64(ByteArray(32) { 6 }),
50 signedPreKeySignature = b64(ByteArray(64) { 7 }), lid = "12345@lid",
51 )
52
53 private lateinit var db: WhatsAppDatabase
54
55 @BeforeTest
56 fun setUp() {
57 val app = ApplicationProvider.getApplicationContext<Application>()
58 db = Room.inMemoryDatabaseBuilder(app, WhatsAppDatabase::class.java)
59 .setSingleConnectionPool()
60 .allowMainThreadQueries().build()
61 WhatsAppClient.appContext = app
62 WhatsAppClient.authData = auth
63 WhatsAppClient.db = db
64 WhatsAppClient.webSocket = WhatsAppSocket(auth)
65 WhatsAppClient.stateMutable.value = WhatsAppClient.State.Connected
66 WhatsAppClient.pendingMessageIDs.clear()
67 WhatsAppClient.recentSentDMs.clear()
68 WhatsAppDiag.clear()
69 }
70
71 @Test
72 fun `e2e constructs against the in-memory database`() = runTest {
73 val crypto = WhatsAppClient.ensureE2E(auth)
74 assertNotNull(crypto)
75 assertTrue(crypto === WhatsAppClient.ensureE2E(auth)) // cached instance
76 assertEquals(32, crypto.ownIdentityPublicKey.size)
77 assertEquals("777" to 0, crypto.signalAddress("[email protected]"))
78 assertEquals("777" to 3, crypto.signalAddress("777:[email protected]"))
79 assertEquals("777" to 0, crypto.signalAddress("[email protected]"))
80 assertTrue(!crypto.hasSession("[email protected]"))
81 crypto.deleteSession("[email protected]") // deleting an absent session is fine
82 }
83
84 @Test
85 fun `dm send pipeline runs fan-out and fails soft without sessions`() = runTest {
86 assertNull(WhatsAppClient.sendMessage("[email protected]", "hello there"))
87 // The plaintext was cached for retry resends even though no device encrypted.
88 assertEquals(1, WhatsAppClient.recentSentDMs.size)
89 val cached = WhatsAppClient.recentSentDMs.values.single()
90 assertEquals("[email protected]", cached.to)
91 assertEquals("text", cached.type)
92 assertTrue(cached.msgPlaintextPadded.isNotEmpty())
93 assertNotNull(cached.dsmPlaintextPadded)
94 assertNull(WhatsAppClient.buildEncryptedTextNode("[email protected]", "id-x", "body"))
95 assertNull(
96 WhatsAppClient.sendMessageAdvanced(
97 "[email protected]", "hi @777",
98 mentionedJids = listOf("[email protected]"),
99 ),
100 )
101 assertTrue(WhatsAppDiag.log.value.any { it.contains("No devices could be encrypted") || it.contains("send:") })
102 // Device discovery itself is an honest empty (Iq unanswered).
103 assertEquals(emptyList(), WhatsAppClient.getUserDevices(listOf("[email protected]")))
104 assertEquals(emptyList(), WhatsAppClient.getUserDevices(emptyList()))
105 }
106
107 @Test
108 fun `ensure session reports false without a prekey answer`() = runTest {
109 assertTrue(!WhatsAppClient.ensureSession("[email protected]"))
110 assertTrue(!WhatsAppClient.ensureSession("777:[email protected]"))
111 }
112
113}