Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Pass2E2ESendTest.kt
W5Pass2E2ESendTest.kt113 lines · 5.4 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import android.app.Application |
| 4 | import android.util.Base64 |
| 5 | import androidx.room3.Room |
| 6 | import androidx.test.core.app.ApplicationProvider |
| 7 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 8 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppAuthData |
| 9 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppClient |
| 10 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppDatabase |
| 11 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppDiag |
| 12 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 13 | import com.vayunmathur.communicate.data.whatsapp.buildEncryptedGroupTextNode |
| 14 | import com.vayunmathur.communicate.data.whatsapp.buildEncryptedTextNode |
| 15 | import com.vayunmathur.communicate.data.whatsapp.e2e.WhatsAppE2E |
| 16 | import com.vayunmathur.communicate.data.whatsapp.getUserDevices |
| 17 | import com.vayunmathur.communicate.data.whatsapp.padMessage |
| 18 | import com.vayunmathur.communicate.data.whatsapp.sendMessage |
| 19 | import com.vayunmathur.communicate.data.whatsapp.sendMessageAdvanced |
| 20 | import com.vayunmathur.communicate.data.whatsapp.transport.WhatsAppSocket |
| 21 | import kotlin.test.BeforeTest |
| 22 | import kotlin.test.Test |
| 23 | import kotlin.test.assertEquals |
| 24 | import kotlin.test.assertNotNull |
| 25 | import kotlin.test.assertNull |
| 26 | import kotlin.test.assertTrue |
| 27 | import kotlinx.coroutines.test.runTest |
| 28 | import 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) |
| 40 | class 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 | } |