Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Pass2InboundNotificationsTest.kt
W5Pass2InboundNotificationsTest.kt203 lines · 9.2 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | // FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch. |
| 4 | |
| 5 | import android.app.Application |
| 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.WhatsAppDiag |
| 11 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppEvent |
| 12 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 13 | import com.vayunmathur.communicate.data.whatsapp.encodeNode |
| 14 | import com.vayunmathur.communicate.data.whatsapp.handleIncomingMessage |
| 15 | import com.vayunmathur.communicate.data.whatsapp.transport.WhatsAppSocket |
| 16 | import kotlin.test.BeforeTest |
| 17 | import kotlin.test.Test |
| 18 | import kotlin.test.assertEquals |
| 19 | import kotlin.test.assertTrue |
| 20 | import kotlinx.coroutines.CoroutineScope |
| 21 | import kotlinx.coroutines.Dispatchers |
| 22 | import kotlinx.coroutines.Job |
| 23 | import kotlinx.coroutines.launch |
| 24 | import org.junit.runner.RunWith |
| 25 | |
| 26 | /** |
| 27 | * Pass-2 slice-1 (whatsapp inbound): notification stanza routing (group changes, |
| 28 | * identity/picture/account-sync, server sync, calls) and undecryptable-message |
| 29 | * handling, through the public [handleIncomingMessage] entry point. |
| 30 | */ |
| 31 | @RunWith(AndroidJUnit4::class) |
| 32 | class W5Pass2InboundNotificationsTest { |
| 33 | |
| 34 | private val auth = WhatsAppAuthData( |
| 35 | phoneNumber = "+15550001", pushName = "Tester", wid = "[email protected]", |
| 36 | noisePrivateKey = "", noisePublicKey = "", identityPrivateKey = "", identityPublicKey = "", |
| 37 | registrationId = 14, signedPreKeyId = 1, signedPreKeyPublic = "", signedPreKeyPrivate = "", |
| 38 | signedPreKeySignature = "", lid = "12345@lid", |
| 39 | ) |
| 40 | |
| 41 | private val receivedEvents = mutableListOf<WhatsAppEvent>() |
| 42 | |
| 43 | @BeforeTest |
| 44 | fun setUp() { |
| 45 | val app = ApplicationProvider.getApplicationContext<Application>() |
| 46 | WhatsAppClient.appContext = app |
| 47 | WhatsAppClient.authData = auth |
| 48 | WhatsAppClient.db = null |
| 49 | WhatsAppClient.webSocket = WhatsAppSocket(auth) |
| 50 | WhatsAppClient.stateMutable.value = WhatsAppClient.State.Connected |
| 51 | WhatsAppClient.nameCache.clear() |
| 52 | WhatsAppClient.knownGroups.clear() |
| 53 | WhatsAppClient.undecryptableTracker.clear() |
| 54 | WhatsAppDiag.clear() |
| 55 | receivedEvents.clear() |
| 56 | CoroutineScope(Dispatchers.Default + Job()).launch { |
| 57 | WhatsAppClient.events.collect { receivedEvents.add(it) } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private fun await(label: String, condition: () -> Boolean) { |
| 62 | val deadline = System.currentTimeMillis() + 15_000 |
| 63 | while (System.currentTimeMillis() < deadline) { |
| 64 | if (condition()) return |
| 65 | Thread.sleep(25) |
| 66 | } |
| 67 | error("timed out waiting for $label; diag=${WhatsAppDiag.log.value.takeLast(4)} events=$receivedEvents") |
| 68 | } |
| 69 | |
| 70 | private fun feed(node: WhatsAppProtocol.Node) { |
| 71 | WhatsAppClient.handleIncomingMessage(WhatsAppProtocol.encodeNode(node)) |
| 72 | } |
| 73 | |
| 74 | private fun notification(type: String, from: String, vararg children: WhatsAppProtocol.Node, attrs: Map<String, String> = emptyMap()) = |
| 75 | WhatsAppProtocol.Node( |
| 76 | tag = "notification", |
| 77 | attrs = mapOf("type" to type, "from" to from, "id" to "n-1", "t" to "1700000000") + attrs, |
| 78 | content = children.toList(), |
| 79 | ) |
| 80 | |
| 81 | @Test |
| 82 | fun `group notifications emit system messages`() { |
| 83 | val node = notification( |
| 84 | "w:gp2", "[email protected]", |
| 85 | WhatsAppProtocol.Node(tag = "subject", attrs = mapOf("subject" to "New Club")), |
| 86 | WhatsAppProtocol.Node(tag = "description", data = "All the things".toByteArray()), |
| 87 | WhatsAppProtocol.Node( |
| 88 | tag = "add", |
| 89 | content = listOf(WhatsAppProtocol.Node(tag = "participant", attrs = mapOf("jid" to "[email protected]"))), |
| 90 | ), |
| 91 | WhatsAppProtocol.Node(tag = "announce", attrs = mapOf("announce" to "true")), |
| 92 | WhatsAppProtocol.Node(tag = "locked"), |
| 93 | WhatsAppProtocol.Node(tag = "link", attrs = mapOf("link_type" to "community")), |
| 94 | WhatsAppProtocol.Node(tag = "unlink", attrs = mapOf("unlink_type" to "community")), |
| 95 | WhatsAppProtocol.Node(tag = "mystery"), |
| 96 | ) |
| 97 | feed(node) |
| 98 | await("group events") { receivedEvents.count { it is WhatsAppEvent.IncomingMessage } >= 5 } |
| 99 | val bodies = receivedEvents.filterIsInstance<WhatsAppEvent.IncomingMessage>().map { it.body } |
| 100 | assertTrue(bodies.any { it.contains("New Club") }, bodies.toString()) |
| 101 | assertTrue(bodies.any { it.contains("[email protected]") }, bodies.toString()) |
| 102 | assertTrue(bodies.all { it.startsWith("[") }, bodies.toString()) |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | fun `identity picture and account sync notifications`() { |
| 107 | feed( |
| 108 | notification( |
| 109 | "encrypt", "[email protected]", |
| 110 | WhatsAppProtocol.Node(tag = "identity"), |
| 111 | attrs = mapOf("participant" to "[email protected]"), |
| 112 | ), |
| 113 | ) |
| 114 | await("identity event") { |
| 115 | receivedEvents.filterIsInstance<WhatsAppEvent.IncomingMessage>() |
| 116 | .any { it.body.contains("Security code changed") } |
| 117 | } |
| 118 | feed( |
| 119 | notification( |
| 120 | "account_sync", "s.whatsapp.net", |
| 121 | WhatsAppProtocol.Node(tag = "push", attrs = mapOf("name" to "New Push", "jid" to "[email protected]")), |
| 122 | WhatsAppProtocol.Node(tag = "contact", attrs = mapOf("name" to "Contact Name", "jid" to "[email protected]")), |
| 123 | WhatsAppProtocol.Node(tag = "other"), |
| 124 | ), |
| 125 | ) |
| 126 | await("push names cached") { |
| 127 | WhatsAppClient.nameCache["[email protected]"] == "New Push" && |
| 128 | WhatsAppClient.nameCache["[email protected]"] == "Contact Name" |
| 129 | } |
| 130 | // Picture set/delete and the silently ignored types complete without events. |
| 131 | val before = receivedEvents.size |
| 132 | feed(notification("picture", "[email protected]", WhatsAppProtocol.Node(tag = "set", attrs = mapOf("id" to "pic1")))) |
| 133 | feed(notification("picture", "[email protected]", WhatsAppProtocol.Node(tag = "delete"))) |
| 134 | feed(notification("devices", "[email protected]")) |
| 135 | feed(notification("mediaretry", "[email protected]")) |
| 136 | feed(notification("totally-unknown", "[email protected]")) |
| 137 | Thread.sleep(400) |
| 138 | assertEquals(before, receivedEvents.size) |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | fun `server sync notification triggers known collections only`() { |
| 143 | val node = notification( |
| 144 | "server_sync", "s.whatsapp.net", |
| 145 | WhatsAppProtocol.Node(tag = "collection", attrs = mapOf("name" to "regular")), |
| 146 | WhatsAppProtocol.Node(tag = "collection", attrs = mapOf("name" to "not-a-collection")), |
| 147 | WhatsAppProtocol.Node(tag = "collection"), |
| 148 | ) |
| 149 | feed(node) |
| 150 | await("app state fetch logged") { WhatsAppDiag.log.value.any { it.contains("app-state: regular synced") } } |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | fun `call notifications emit for fresh offers and ignore stale ones`() { |
| 155 | val fresh = notification( |
| 156 | "call", "[email protected]", |
| 157 | WhatsAppProtocol.Node(tag = "offer", attrs = mapOf("call-type" to "video")), |
| 158 | attrs = mapOf("t" to (System.currentTimeMillis() / 1000).toString(), "id" to "call-1"), |
| 159 | ) |
| 160 | feed(fresh) |
| 161 | await("call event") { |
| 162 | receivedEvents.filterIsInstance<WhatsAppEvent.IncomingMessage>() |
| 163 | .any { it.body.contains("video call") && it.messageId == "call-call-1" } |
| 164 | } |
| 165 | val before = receivedEvents.size |
| 166 | val stale = notification( |
| 167 | "call", "[email protected]", |
| 168 | WhatsAppProtocol.Node(tag = "offer", attrs = mapOf()), |
| 169 | attrs = mapOf("t" to "1000000000", "id" to "call-old"), |
| 170 | ) |
| 171 | feed(stale) |
| 172 | Thread.sleep(400) |
| 173 | assertEquals(before, receivedEvents.size) |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | fun `encrypted message without a session logs decrypt failure`() { |
| 178 | val message = WhatsAppProtocol.Node( |
| 179 | tag = "message", |
| 180 | attrs = mapOf("type" to "text", "from" to "[email protected]", "id" to "ENC1"), |
| 181 | content = listOf( |
| 182 | WhatsAppProtocol.Node(tag = "enc", attrs = mapOf("type" to "msg", "v" to "2"), data = byteArrayOf(1, 2, 3, 4)), |
| 183 | ), |
| 184 | ) |
| 185 | feed(message) |
| 186 | await("decrypt failed diag") { WhatsAppDiag.log.value.any { it.contains("DECRYPT FAILED") && it.contains("ENC1") } } |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | fun `plaintext message is tracked as undecryptable with retry cap`() { |
| 191 | repeat(7) { i -> |
| 192 | feed( |
| 193 | WhatsAppProtocol.Node( |
| 194 | tag = "message", |
| 195 | attrs = mapOf("type" to "text", "from" to "[email protected]", "id" to "PLAIN1"), |
| 196 | ), |
| 197 | ) |
| 198 | Thread.sleep(60) |
| 199 | assertEquals(i + 1, WhatsAppClient.undecryptableTracker.values.sum().coerceAtMost(i + 1)) |
| 200 | } |
| 201 | assertTrue(WhatsAppClient.undecryptableTracker.isNotEmpty()) |
| 202 | } |
| 203 | } |