Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1BuildersTest.kt
W5Slice1BuildersTest.kt359 lines · 16.8 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 4 | import com.vayunmathur.communicate.data.whatsapp.buildAck |
| 5 | import com.vayunmathur.communicate.data.whatsapp.buildChatPresence |
| 6 | import com.vayunmathur.communicate.data.whatsapp.buildContactProto |
| 7 | import com.vayunmathur.communicate.data.whatsapp.buildConversationMessage |
| 8 | import com.vayunmathur.communicate.data.whatsapp.buildConversationPlaintext |
| 9 | import com.vayunmathur.communicate.data.whatsapp.buildCreateGroup |
| 10 | import com.vayunmathur.communicate.data.whatsapp.buildDisappearingTimerProto |
| 11 | import com.vayunmathur.communicate.data.whatsapp.buildFanOutMessageNode |
| 12 | import com.vayunmathur.communicate.data.whatsapp.buildGroupInfoChange |
| 13 | import com.vayunmathur.communicate.data.whatsapp.buildGroupParticipantChange |
| 14 | import com.vayunmathur.communicate.data.whatsapp.buildGroupParticipantsQuery |
| 15 | import com.vayunmathur.communicate.data.whatsapp.buildHistoryOnDemandRequest |
| 16 | import com.vayunmathur.communicate.data.whatsapp.buildKeepalive |
| 17 | import com.vayunmathur.communicate.data.whatsapp.buildLeaveGroup |
| 18 | import com.vayunmathur.communicate.data.whatsapp.buildLocationProto |
| 19 | import com.vayunmathur.communicate.data.whatsapp.buildMediaConnQuery |
| 20 | import com.vayunmathur.communicate.data.whatsapp.buildMediaProto |
| 21 | import com.vayunmathur.communicate.data.whatsapp.buildMexQuery |
| 22 | import com.vayunmathur.communicate.data.whatsapp.buildPollCreationProto |
| 23 | import com.vayunmathur.communicate.data.whatsapp.buildReactionProto |
| 24 | import com.vayunmathur.communicate.data.whatsapp.buildReadReceipt |
| 25 | import com.vayunmathur.communicate.data.whatsapp.buildReceipt |
| 26 | import com.vayunmathur.communicate.data.whatsapp.buildRetryReceipt |
| 27 | import com.vayunmathur.communicate.data.whatsapp.buildSetGroupTopic |
| 28 | import com.vayunmathur.communicate.data.whatsapp.buildTextProto |
| 29 | import com.vayunmathur.communicate.data.whatsapp.buildUsyncDevicesQuery |
| 30 | import com.vayunmathur.communicate.data.whatsapp.deviceSentPlaintext |
| 31 | import com.vayunmathur.communicate.data.whatsapp.isRevokeFromMe |
| 32 | import com.vayunmathur.communicate.data.whatsapp.proto.WhatsAppE2EProto |
| 33 | import com.vayunmathur.communicate.data.whatsapp.senderKeyDistributionPlaintext |
| 34 | import kotlin.test.Test |
| 35 | import kotlin.test.assertEquals |
| 36 | import kotlin.test.assertFailsWith |
| 37 | import kotlin.test.assertFalse |
| 38 | import kotlin.test.assertTrue |
| 39 | |
| 40 | /** |
| 41 | * Slice 1 (w5): node + proto builders (WhatsAppProtocolBuilders.kt / Builders2.kt). |
| 42 | * Pure JVM: asserts wire shapes (tags, attrs, children, proto fields). |
| 43 | */ |
| 44 | class W5Slice1BuildersTest { |
| 45 | |
| 46 | @Test |
| 47 | fun conversationPlaintext_roundTripsThroughParse() { |
| 48 | val bytes = WhatsAppProtocol.buildConversationPlaintext("hello builders") |
| 49 | assertEquals("hello builders", WhatsAppE2EProto.Message.parseFrom(bytes).conversation) |
| 50 | assertEquals("hello builders", WhatsAppProtocol.buildConversationMessage("hello builders").conversation) |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | fun historyOnDemandRequest_setsFields() { |
| 55 | val msg = WhatsAppProtocol.buildHistoryOnDemandRequest("[email protected]", "OLD1", true, 1700000000L, 50) |
| 56 | assertTrue(msg.hasProtocolMessage()) |
| 57 | val pm = msg.protocolMessage |
| 58 | assertEquals(WhatsAppE2EProto.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_MESSAGE, pm.type) |
| 59 | val req = pm.peerDataOperationRequestMessage.historySyncOnDemandRequest |
| 60 | assertEquals("[email protected]", req.chatJid) |
| 61 | assertEquals("OLD1", req.oldestMsgId) |
| 62 | assertTrue(req.oldestMsgFromMe) |
| 63 | assertEquals(1700000000L, req.oldestMsgTimestampMs) |
| 64 | assertEquals(50, req.onDemandMsgCount) |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | fun deviceSentPlaintext_wrapsDestination() { |
| 69 | val inner = WhatsAppProtocol.buildConversationMessage("inner") |
| 70 | val bytes = WhatsAppProtocol.deviceSentPlaintext("[email protected]", inner) |
| 71 | val back = WhatsAppE2EProto.Message.parseFrom(bytes) |
| 72 | assertTrue(back.hasDeviceSentMessage()) |
| 73 | assertEquals("[email protected]", back.deviceSentMessage.destinationJid) |
| 74 | assertEquals("inner", back.deviceSentMessage.message.conversation) |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun skdmPlaintext_carriesGroupAndBytes() { |
| 79 | val skdm = byteArrayOf(1, 2, 3, 4) |
| 80 | val back = WhatsAppE2EProto.Message.parseFrom( |
| 81 | WhatsAppProtocol.senderKeyDistributionPlaintext("[email protected]", skdm), |
| 82 | ) |
| 83 | assertEquals("[email protected]", back.senderKeyDistributionMessage.groupId) |
| 84 | assertTrue(back.senderKeyDistributionMessage.axolotlSenderKeyDistributionMessage.toByteArray().contentEquals(skdm)) |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | fun fanOutNode_shapesParticipantsAndExtras() { |
| 89 | val encs = listOf( |
| 90 | WhatsAppProtocol.ParticipantEnc("1:[email protected]", "msg", byteArrayOf(1)), |
| 91 | WhatsAppProtocol.ParticipantEnc("2:[email protected]", "pkmsg", byteArrayOf(2)), |
| 92 | ) |
| 93 | val node = WhatsAppProtocol.buildFanOutMessageNode( |
| 94 | to = "[email protected]", id = "F1", type = "text", |
| 95 | participantEncs = encs, includeDeviceIdentity = true, deviceIdentity = byteArrayOf(9), |
| 96 | ) |
| 97 | assertEquals("message", node.tag) |
| 98 | assertEquals("F1", node.attrs["id"]) |
| 99 | val parts = node.getChildByTag("participants")!! |
| 100 | assertEquals(2, parts.content.size) |
| 101 | assertEquals("1:[email protected]", (parts.content[0] as WhatsAppProtocol.Node).attrs["jid"]) |
| 102 | assertTrue(node.getChildByTag("device-identity") != null) |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | fun fanOutNode_withoutExtras_omitsIdentity() { |
| 107 | val node = WhatsAppProtocol.buildFanOutMessageNode( |
| 108 | "[email protected]", "F2", "text", emptyList(), false, null, |
| 109 | messageAttrs = mapOf("edit" to "7"), |
| 110 | ) |
| 111 | assertEquals("7", node.attrs["edit"]) |
| 112 | assertEquals(null, node.getChildByTag("device-identity")) |
| 113 | assertEquals(0, node.getChildByTag("participants")!!.content.size) |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | fun retryReceipt_copiesRecipientAndParticipant() { |
| 118 | val orig = WhatsAppProtocol.Node( |
| 119 | tag = "message", |
| 120 | attrs = mapOf("id" to "M1", "from" to "[email protected]", "t" to "5", |
| 121 | "recipient" to "[email protected]", "participant" to "[email protected]"), |
| 122 | ) |
| 123 | val receipt = WhatsAppProtocol.buildRetryReceipt(orig, registrationId = 0x01020304, retryCount = 2, keysNode = null) |
| 124 | assertEquals("receipt", receipt.tag) |
| 125 | assertEquals("retry", receipt.attrs["type"]) |
| 126 | assertEquals("[email protected]", receipt.attrs["to"]) |
| 127 | assertEquals("[email protected]", receipt.attrs["recipient"]) |
| 128 | val retry = receipt.getChildByTag("retry")!! |
| 129 | assertEquals("2", retry.attrs["count"]) |
| 130 | val reg = receipt.getChildByTag("registration")!! |
| 131 | assertTrue(reg.data!!.contentEquals(byteArrayOf(1, 2, 3, 4))) |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | fun groupParticipantsQuery_shape() { |
| 136 | val q = WhatsAppProtocol.buildGroupParticipantsQuery("[email protected]", "Q1") |
| 137 | assertEquals("iq", q.tag) |
| 138 | assertEquals("w:g2", q.attrs["xmlns"]) |
| 139 | assertEquals("interactive", q.getChildByTag("query")!!.attrs["request"]) |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | fun usyncQuery_listsUsers() { |
| 144 | val q = WhatsAppProtocol.buildUsyncDevicesQuery(listOf("[email protected]", "[email protected]"), "U1", "S1") |
| 145 | assertEquals("usync", q.attrs["xmlns"]) |
| 146 | val list = q.getChildByTag("usync")!!.getChildByTag("list")!! |
| 147 | assertEquals(2, list.content.size) |
| 148 | assertEquals("message", q.getChildByTag("usync")!!.attrs["context"]) |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | fun mediaConnQuery_shape() { |
| 153 | val q = WhatsAppProtocol.buildMediaConnQuery("MC1") |
| 154 | assertEquals("w:m", q.attrs["xmlns"]) |
| 155 | assertTrue(q.getChildByTag("media_conn") != null) |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | fun mexQuery_embedsEnvelopeAndArgoMode() { |
| 160 | val n = WhatsAppProtocol.buildMexQuery("123", """{"queryId":"123"}""", "MX1", argoMode = 3L) |
| 161 | assertEquals("w:mex", n.attrs["xmlns"]) |
| 162 | val query = n.getChildByTag("query")!! |
| 163 | assertEquals("123", query.attrs["query_id"]) |
| 164 | assertEquals("3", query.attrs["argo_mode"]) |
| 165 | assertEquals("""{"queryId":"123"}""", String(query.data!!, Charsets.UTF_8)) |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | fun reactionProto_groupOtherSender_setsParticipant() { |
| 170 | val msg = WhatsAppProtocol.buildReactionProto("[email protected]", "T1", "❤️", false, "[email protected]") |
| 171 | val r = msg.reactionMessage |
| 172 | assertEquals("❤️", r.text) |
| 173 | assertEquals("[email protected]", r.key.participant) |
| 174 | assertTrue(r.senderTimestampMs > 0) |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | fun receipt_singleAndMulti() { |
| 179 | val single = WhatsAppProtocol.buildReceipt("[email protected]", listOf("M1"), "read") |
| 180 | assertEquals("M1", single.attrs["id"]) |
| 181 | assertEquals("read", single.attrs["type"]) |
| 182 | assertEquals(null, single.getChildByTag("list")) |
| 183 | |
| 184 | val multi = WhatsAppProtocol.buildReceipt("[email protected]", listOf("M1", "M2", "M3"), "", senderJid = "[email protected]") |
| 185 | assertEquals(null, multi.attrs["type"]) |
| 186 | assertEquals("[email protected]", multi.attrs["participant"]) |
| 187 | assertEquals(2, multi.getChildByTag("list")!!.content.size) |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | fun receipt_emptyIds_throws() { |
| 192 | assertFailsWith<IllegalArgumentException> { |
| 193 | WhatsAppProtocol.buildReceipt("[email protected]", emptyList(), "read") |
| 194 | } |
| 195 | assertFailsWith<IllegalArgumentException> { |
| 196 | WhatsAppProtocol.buildReadReceipt("[email protected]", emptyList()) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | fun readReceipt_groupSetsParticipant() { |
| 202 | val r = WhatsAppProtocol.buildReadReceipt("[email protected]", listOf("M1", "M2"), senderJid = "[email protected]") |
| 203 | assertEquals("read", r.attrs["type"]) |
| 204 | assertEquals("[email protected]", r.attrs["participant"]) |
| 205 | assertTrue(r.attrs["t"]!!.toLong() > 0) |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | fun mediaProto_eachTypeSetsField() { |
| 210 | val img = WhatsAppProtocol.buildMediaProto("u", "p", ByteArray(32), ByteArray(32), ByteArray(32), 10, "image/jpeg", "cap", "image") |
| 211 | assertTrue(img.hasImageMessage()) |
| 212 | assertEquals("cap", img.imageMessage.caption) |
| 213 | val vid = WhatsAppProtocol.buildMediaProto("u", "p", ByteArray(32), ByteArray(32), ByteArray(32), 10, "video/mp4", null, "video") |
| 214 | assertTrue(vid.hasVideoMessage()) |
| 215 | assertEquals("", vid.videoMessage.caption) |
| 216 | val aud = WhatsAppProtocol.buildMediaProto("u", "p", ByteArray(32), ByteArray(32), ByteArray(32), 10, "audio/ogg", null, "audio") |
| 217 | assertTrue(aud.hasAudioMessage()) |
| 218 | val doc = WhatsAppProtocol.buildMediaProto("u", "p", ByteArray(32), ByteArray(32), ByteArray(32), 10, "application/pdf", null, "document") |
| 219 | assertTrue(doc.hasDocumentMessage()) |
| 220 | val sticker = WhatsAppProtocol.buildMediaProto("u", "p", ByteArray(32), ByteArray(32), ByteArray(32), 10, "image/webp", null, "sticker") |
| 221 | assertTrue(sticker.hasStickerMessage()) |
| 222 | val unknown = WhatsAppProtocol.buildMediaProto("u", "p", ByteArray(32), ByteArray(32), ByteArray(32), 10, "x", null, "bogus") |
| 223 | assertEquals(WhatsAppE2EProto.Message.getDefaultInstance(), unknown) |
| 224 | } |
| 225 | |
| 226 | @Test |
| 227 | fun presence_composingPausedAndAudio() { |
| 228 | val composing = WhatsAppProtocol.buildChatPresence("[email protected]", true) |
| 229 | assertEquals("chatstate", composing.tag) |
| 230 | assertTrue(composing.getChildByTag("composing") != null) |
| 231 | val paused = WhatsAppProtocol.buildChatPresence("[email protected]", false, ownJid = "[email protected]") |
| 232 | assertTrue(paused.getChildByTag("paused") != null) |
| 233 | assertEquals("[email protected]", paused.attrs["from"]) |
| 234 | val audio = WhatsAppProtocol.buildChatPresence("[email protected]", true, isAudio = true) |
| 235 | assertEquals("audio", audio.getChildByTag("composing")!!.attrs["media"]) |
| 236 | } |
| 237 | |
| 238 | @Test |
| 239 | fun keepalive_shape() { |
| 240 | val k = WhatsAppProtocol.buildKeepalive("K1") |
| 241 | assertEquals("w:p", k.attrs["xmlns"]) |
| 242 | assertEquals("K1", k.attrs["id"]) |
| 243 | } |
| 244 | |
| 245 | @Test |
| 246 | fun ack_typeOnlyForNonMessageClass() { |
| 247 | val msgAck = WhatsAppProtocol.buildAck("message", "A1", "[email protected]", type = "read") |
| 248 | assertEquals(null, msgAck.attrs["type"]) |
| 249 | val rcptAck = WhatsAppProtocol.buildAck( |
| 250 | "receipt", "A2", "[email protected]", |
| 251 | participant = "[email protected]", recipient = "[email protected]", type = "read", |
| 252 | ) |
| 253 | assertEquals("read", rcptAck.attrs["type"]) |
| 254 | assertEquals("[email protected]", rcptAck.attrs["participant"]) |
| 255 | assertEquals("[email protected]", rcptAck.attrs["recipient"]) |
| 256 | } |
| 257 | |
| 258 | @Test |
| 259 | fun pollCreation_selectableCountClamps() { |
| 260 | val secret = ByteArray(32) |
| 261 | val normal = WhatsAppProtocol.buildPollCreationProto("Q?", listOf("a", "b"), 1, secret) |
| 262 | assertTrue(normal.hasPollCreationMessageV3()) |
| 263 | assertEquals(1, normal.pollCreationMessageV3.selectableOptionsCount) |
| 264 | assertEquals(listOf("a", "b"), normal.pollCreationMessageV3.optionsList.map { it.optionName }) |
| 265 | assertTrue(normal.messageContextInfo.messageSecret.toByteArray().contentEquals(secret)) |
| 266 | val over = WhatsAppProtocol.buildPollCreationProto("Q?", listOf("a"), 5, secret) |
| 267 | assertEquals(0, over.pollCreationMessageV3.selectableOptionsCount) |
| 268 | val neg = WhatsAppProtocol.buildPollCreationProto("Q?", listOf("a"), -1, secret) |
| 269 | assertEquals(0, neg.pollCreationMessageV3.selectableOptionsCount) |
| 270 | } |
| 271 | |
| 272 | @Test |
| 273 | fun locationProto_optionalFields() { |
| 274 | val full = WhatsAppProtocol.buildLocationProto(1.5, -2.5, "Name", "Addr") |
| 275 | assertEquals(1.5, full.locationMessage.degreesLatitude, 1e-9) |
| 276 | assertEquals("Name", full.locationMessage.name) |
| 277 | val bare = WhatsAppProtocol.buildLocationProto(0.0, 0.0) |
| 278 | assertFalse(bare.locationMessage.hasName()) |
| 279 | assertFalse(bare.locationMessage.hasAddress()) |
| 280 | } |
| 281 | |
| 282 | @Test |
| 283 | fun contactProto_setsFields() { |
| 284 | val m = WhatsAppProtocol.buildContactProto("Al", "VCARD") |
| 285 | assertEquals("Al", m.contactMessage.displayName) |
| 286 | assertEquals("VCARD", m.contactMessage.vcard) |
| 287 | } |
| 288 | |
| 289 | @Test |
| 290 | fun disappearingTimerProto_setsValue() { |
| 291 | val m = WhatsAppProtocol.buildDisappearingTimerProto(604800L) |
| 292 | assertEquals(604800, m.protocolMessage.ephemeralExpiration) |
| 293 | } |
| 294 | |
| 295 | @Test |
| 296 | fun groupInfoChange_embedsValue() { |
| 297 | val n = WhatsAppProtocol.buildGroupInfoChange("[email protected]", "subject", "New Name", "G1") |
| 298 | assertEquals("w:g2", n.attrs["xmlns"]) |
| 299 | val child = n.getChildByTag("subject")!! |
| 300 | assertEquals("New Name", String(child.data!!, Charsets.UTF_8)) |
| 301 | } |
| 302 | |
| 303 | @Test |
| 304 | fun setGroupTopic_deleteVsBody() { |
| 305 | val withBody = WhatsAppProtocol.buildSetGroupTopic("[email protected]", "topic here", "N1", previousId = "P0") |
| 306 | val desc = withBody.getChildByTag("description")!! |
| 307 | assertEquals("N1", desc.attrs["id"]) |
| 308 | assertEquals("P0", desc.attrs["prev"]) |
| 309 | assertEquals("topic here", String(desc.getChildByTag("body")!!.data!!, Charsets.UTF_8)) |
| 310 | |
| 311 | val deleted = WhatsAppProtocol.buildSetGroupTopic("[email protected]", "", "N2") |
| 312 | val descDel = deleted.getChildByTag("description")!! |
| 313 | assertEquals("true", descDel.attrs["delete"]) |
| 314 | assertEquals(null, descDel.getChildByTag("body")) |
| 315 | } |
| 316 | |
| 317 | @Test |
| 318 | fun leaveGroup_targetsGus() { |
| 319 | val n = WhatsAppProtocol.buildLeaveGroup("[email protected]", "L1") |
| 320 | assertEquals("g.us", n.attrs["to"]) |
| 321 | assertEquals("[email protected]", n.getChildByTag("leave")!!.getChildByTag("group")!!.attrs["id"]) |
| 322 | } |
| 323 | |
| 324 | @Test |
| 325 | fun createGroup_listsParticipants() { |
| 326 | val n = WhatsAppProtocol.buildCreateGroup("Subject", listOf("[email protected]", "[email protected]"), "C1", "key-1") |
| 327 | val create = n.getChildByTag("create")!! |
| 328 | assertEquals("Subject", create.attrs["subject"]) |
| 329 | assertEquals("key-1", create.attrs["key"]) |
| 330 | assertEquals(2, create.content.size) |
| 331 | } |
| 332 | |
| 333 | @Test |
| 334 | fun groupParticipantChange_actionNode() { |
| 335 | val n = WhatsAppProtocol.buildGroupParticipantChange("[email protected]", listOf("[email protected]"), "add", "P1") |
| 336 | assertEquals("[email protected]", n.attrs["to"]) |
| 337 | assertEquals(1, n.getChildByTag("add")!!.content.size) |
| 338 | } |
| 339 | |
| 340 | @Test |
| 341 | fun isRevokeFromMe_cases() { |
| 342 | assertTrue(WhatsAppProtocol.isRevokeFromMe("", "[email protected]")) |
| 343 | assertTrue(WhatsAppProtocol.isRevokeFromMe("[email protected]", "[email protected]")) |
| 344 | // NOTE: device-suffixed own JID compares by the pre-"@" part only, so "me:1" |
| 345 | // does NOT match "me" (possible gap vs the module's bare-user JID convention; |
| 346 | // flagged in the task report, production left unchanged). |
| 347 | assertFalse(WhatsAppProtocol.isRevokeFromMe("me:[email protected]", "[email protected]")) |
| 348 | assertFalse(WhatsAppProtocol.isRevokeFromMe("[email protected]", "[email protected]")) |
| 349 | } |
| 350 | |
| 351 | @Test |
| 352 | fun textProto_plainVsRich() { |
| 353 | val plain = WhatsAppProtocol.buildTextProto("hi") |
| 354 | assertTrue(plain.hasConversation()) |
| 355 | val rich = WhatsAppProtocol.buildTextProto("hi @a", mentionedJids = listOf("[email protected]")) |
| 356 | assertTrue(rich.hasExtendedTextMessage()) |
| 357 | assertEquals(listOf("[email protected]"), rich.extendedTextMessage.contextInfo.mentionedJidList) |
| 358 | } |
| 359 | } |