Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1BinaryCodecTest.kt
W5Slice1BinaryCodecTest.kt180 lines · 6.8 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 4 | import com.vayunmathur.communicate.data.whatsapp.buildFramedMessage |
| 5 | import com.vayunmathur.communicate.data.whatsapp.decodeNode |
| 6 | import com.vayunmathur.communicate.data.whatsapp.encodeNode |
| 7 | import com.vayunmathur.communicate.data.whatsapp.extractFrame |
| 8 | import kotlin.test.Test |
| 9 | import kotlin.test.assertEquals |
| 10 | import kotlin.test.assertFailsWith |
| 11 | import kotlin.test.assertFalse |
| 12 | import kotlin.test.assertTrue |
| 13 | |
| 14 | /** |
| 15 | * Slice 1 (w5): binary XML codec + frame helpers (WhatsAppProtocolBinary.kt). |
| 16 | * Pure JVM. Round-trip encode→decode must preserve tag/attrs/children/data. |
| 17 | */ |
| 18 | class W5Slice1BinaryCodecTest { |
| 19 | |
| 20 | // NOTE: BinaryEncoder's buffer starts with a 0x00 element (mutableListOf(0) is one |
| 21 | // element, not capacity) which decodeNode's unpack() consumes as the compression-flag |
| 22 | // byte, so encodeNode output decodes directly. That seed is load-bearing wire format; |
| 23 | // production code is intentionally untouched. |
| 24 | private fun roundTrip(node: WhatsAppProtocol.Node): WhatsAppProtocol.Node = |
| 25 | WhatsAppProtocol.decodeNode(WhatsAppProtocol.encodeNode(node)) |
| 26 | |
| 27 | @Test |
| 28 | fun roundTrip_simpleNodeWithAttrs() { |
| 29 | val node = WhatsAppProtocol.Node( |
| 30 | tag = "message", |
| 31 | attrs = mapOf("to" to "[email protected]", "id" to "ABC123", "type" to "text"), |
| 32 | ) |
| 33 | assertEquals(node, roundTrip(node)) |
| 34 | } |
| 35 | |
| 36 | @Test |
| 37 | fun roundTrip_nestedChildren() { |
| 38 | val node = WhatsAppProtocol.Node( |
| 39 | tag = "iq", |
| 40 | attrs = mapOf("id" to "1", "type" to "get", "xmlns" to "usync"), |
| 41 | content = listOf( |
| 42 | WhatsAppProtocol.Node( |
| 43 | tag = "usync", |
| 44 | attrs = mapOf("mode" to "query"), |
| 45 | content = listOf(WhatsAppProtocol.Node(tag = "query")), |
| 46 | ), |
| 47 | ), |
| 48 | ) |
| 49 | assertEquals(node, roundTrip(node)) |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | fun roundTrip_binaryData() { |
| 54 | val data = ByteArray(64) { it.toByte() } |
| 55 | val node = WhatsAppProtocol.Node(tag = "enc", attrs = mapOf("v" to "2", "type" to "pkmsg"), data = data) |
| 56 | val back = roundTrip(node) |
| 57 | assertEquals("enc", back.tag) |
| 58 | assertEquals(mapOf("v" to "2", "type" to "pkmsg"), back.attrs) |
| 59 | assertTrue(back.data!!.contentEquals(data)) |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | fun roundTrip_jidAttributes_useJidPairEncoding() { |
| 64 | val node = WhatsAppProtocol.Node( |
| 65 | tag = "user", |
| 66 | attrs = mapOf("jid" to "[email protected]"), |
| 67 | ) |
| 68 | assertEquals(node, roundTrip(node)) |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | fun roundTrip_deviceJid_usesAdJidEncoding() { |
| 73 | val node = WhatsAppProtocol.Node( |
| 74 | tag = "user", |
| 75 | attrs = mapOf("jid" to "15551234567.0:[email protected]"), |
| 76 | ) |
| 77 | assertEquals(node, roundTrip(node)) |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun roundTrip_numericStrings_usePackedEncoding() { |
| 82 | val node = WhatsAppProtocol.Node( |
| 83 | tag = "item", |
| 84 | attrs = mapOf("id" to "123456789012345", "code" to "ABCDEF1234"), |
| 85 | ) |
| 86 | assertEquals(node, roundTrip(node)) |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun roundTrip_emptyAttrsAreDropped() { |
| 91 | val node = WhatsAppProtocol.Node(tag = "x", attrs = mapOf("a" to "", "b" to "v")) |
| 92 | val back = roundTrip(node) |
| 93 | assertEquals(mapOf("b" to "v"), back.attrs) |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | fun roundTrip_unicodeRawString() { |
| 98 | val node = WhatsAppProtocol.Node(tag = "body", data = "héllo wörld 👋".toByteArray(Charsets.UTF_8)) |
| 99 | val back = roundTrip(node) |
| 100 | assertEquals("héllo wörld 👋", String(back.data!!, Charsets.UTF_8)) |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | fun decode_emptyFrame_throws() { |
| 105 | assertFailsWith<IllegalStateException> { WhatsAppProtocol.decodeNode(ByteArray(0)) } |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | fun node_helpers_getChildrenAndChildByTag() { |
| 110 | val child = WhatsAppProtocol.Node(tag = "body") |
| 111 | val node = WhatsAppProtocol.Node(tag = "message", content = listOf(child)) |
| 112 | assertEquals(listOf(child), node.getChildren()) |
| 113 | assertEquals(child, node.getChildByTag("body")) |
| 114 | assertEquals(null, node.getChildByTag("missing")) |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | fun node_equalsAndHashCode_withData() { |
| 119 | val a = WhatsAppProtocol.Node(tag = "enc", data = byteArrayOf(1, 2, 3)) |
| 120 | val b = WhatsAppProtocol.Node(tag = "enc", data = byteArrayOf(1, 2, 3)) |
| 121 | val c = WhatsAppProtocol.Node(tag = "enc", data = byteArrayOf(1, 2, 4)) |
| 122 | val d = WhatsAppProtocol.Node(tag = "enc") |
| 123 | assertEquals(a, b) |
| 124 | assertEquals(a.hashCode(), b.hashCode()) |
| 125 | assertTrue(a != c) |
| 126 | assertTrue(a != d) |
| 127 | assertTrue(d != a) |
| 128 | assertFalse(a.equals("not a node")) |
| 129 | } |
| 130 | |
| 131 | // ---- frame helpers ---- |
| 132 | |
| 133 | @Test |
| 134 | fun framedMessage_withHeader_roundTripsThroughExtract() { |
| 135 | val payload = "frame payload".toByteArray() |
| 136 | val header = byteArrayOf('W'.code.toByte(), 'A'.code.toByte(), 6, 3) |
| 137 | val frame = WhatsAppProtocol.buildFramedMessage(payload, header) |
| 138 | assertEquals(4 + 3 + payload.size, frame.size) |
| 139 | assertTrue(frame.take(4).toByteArray().contentEquals(header)) |
| 140 | assertTrue(WhatsAppProtocol.extractFrame(frame.drop(4).toByteArray()).contentEquals(payload)) |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | fun framedMessage_noHeader_lengthPrefixOnly() { |
| 145 | val payload = ByteArray(300) { it.toByte() } |
| 146 | val frame = WhatsAppProtocol.buildFramedMessage(payload, null) |
| 147 | assertEquals(3 + 300, frame.size) |
| 148 | assertTrue(WhatsAppProtocol.extractFrame(frame).contentEquals(payload)) |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | fun framedMessage_tooLarge_throws() { |
| 153 | assertFailsWith<IllegalArgumentException> { |
| 154 | WhatsAppProtocol.buildFramedMessage(ByteArray(WhatsAppProtocol.FRAME_MAX_SIZE), null) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | fun extractFrame_shortOrTruncated_returnsInput() { |
| 160 | val short = byteArrayOf(1, 2) |
| 161 | assertTrue(WhatsAppProtocol.extractFrame(short).contentEquals(short)) |
| 162 | // claims length 16 but only 3 bytes follow |
| 163 | val truncated = byteArrayOf(0, 0, 16, 1, 2, 3) |
| 164 | assertTrue(WhatsAppProtocol.extractFrame(truncated).contentEquals(truncated)) |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | fun binaryToken_lookups() { |
| 169 | // well-known single-byte token |
| 170 | val idx = WhatsAppProtocol.BinaryToken.indexOfSingleToken("message") |
| 171 | assertTrue(idx >= 0, "message should be a single-byte token") |
| 172 | assertEquals(-1, WhatsAppProtocol.BinaryToken.indexOfSingleToken("definitely-not-a-token-xyz")) |
| 173 | val (dict, tok, found) = WhatsAppProtocol.BinaryToken.indexOfDoubleByteToken("not-a-token-xyz") |
| 174 | assertTrue(!found) |
| 175 | assertEquals(0, dict) |
| 176 | assertEquals(0, tok) |
| 177 | assertEquals("", WhatsAppProtocol.BinaryToken.getDoubleToken(99, 0)) |
| 178 | assertEquals("", WhatsAppProtocol.BinaryToken.getDoubleToken(0, 99999)) |
| 179 | } |
| 180 | } |