Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1RegPureTest.kt
W5Slice1RegPureTest.kt117 lines · 4.6 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.mex.MexPersistedQueryProvider |
| 4 | import com.vayunmathur.communicate.data.whatsapp.registration.RegEncoding |
| 5 | import com.vayunmathur.communicate.data.whatsapp.registration.RegistrationIntegrity |
| 6 | import com.vayunmathur.communicate.data.whatsapp.registration.WhatsAppRegistrationConstants |
| 7 | import java.nio.ByteBuffer |
| 8 | import java.util.Base64 |
| 9 | import java.util.UUID |
| 10 | import kotlin.test.Test |
| 11 | import kotlin.test.assertEquals |
| 12 | import kotlin.test.assertTrue |
| 13 | |
| 14 | /** |
| 15 | * Slice 1 (w5): pure registration helpers — RegEncoding (minus android.util.Base64), |
| 16 | * RegistrationIntegrity pure encoders, constants, and the MEX persist-id parser. |
| 17 | * All JVM-pure (java.util.Base64 / kotlinx.serialization). |
| 18 | */ |
| 19 | class W5Slice1RegPureTest { |
| 20 | |
| 21 | // ---- RegEncoding.percentEncode ---- |
| 22 | |
| 23 | @Test |
| 24 | fun percentEncode_unreservedPassThrough() { |
| 25 | val unreserved = "ABCabc019-._~".toByteArray(Charsets.US_ASCII) |
| 26 | assertEquals("ABCabc019-._~", RegEncoding.percentEncode(unreserved)) |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | fun percentEncode_reservedAreUpperHex() { |
| 31 | assertEquals("%00%0A%0D%20%2F%3D%3F%25", RegEncoding.percentEncode(byteArrayOf(0x00, 0x0A, 0x0D, 0x20, 0x2F, 0x3D, 0x3F, 0x25))) |
| 32 | assertEquals("%2B", RegEncoding.percentEncode(byteArrayOf(0x2B))) |
| 33 | } |
| 34 | |
| 35 | @Test |
| 36 | fun percentEncode_utf8BytesEncodedPerByte() { |
| 37 | // 'é' = 0xC3 0xA9 |
| 38 | assertEquals("%C3%A9", RegEncoding.percentEncode("é".toByteArray(Charsets.UTF_8))) |
| 39 | } |
| 40 | |
| 41 | // ---- RegEncoding.uuidToBytes ---- |
| 42 | |
| 43 | @Test |
| 44 | fun uuidToBytes_matchesBigEndianLongs() { |
| 45 | val bytes = RegEncoding.uuidToBytes("550e8400-e29b-41d4-a716-446655440000") |
| 46 | val u = UUID.fromString("550e8400-e29b-41d4-a716-446655440000") |
| 47 | val expected = ByteBuffer.allocate(16).putLong(u.mostSignificantBits).putLong(u.leastSignificantBits).array() |
| 48 | assertTrue(bytes.contentEquals(expected)) |
| 49 | // spot-check first bytes: 0x55 0x0e 0x84 0x00 |
| 50 | assertEquals(0x55.toByte(), bytes[0]) |
| 51 | assertEquals(0x00.toByte(), bytes[3]) |
| 52 | } |
| 53 | |
| 54 | // ---- RegistrationIntegrity pure encoders ---- |
| 55 | |
| 56 | @Test |
| 57 | fun aidOf_isBase64Sha256() { |
| 58 | val expected = Base64.getEncoder().encodeToString( |
| 59 | java.security.MessageDigest.getInstance("SHA-256").digest("android-id-1".toByteArray()), |
| 60 | ) |
| 61 | assertEquals(expected, RegistrationIntegrity.aidOf("android-id-1")) |
| 62 | assertEquals(44, RegistrationIntegrity.aidOf("x").length) |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | fun permissionsHashOf_sortsBeforeHashing() { |
| 67 | val a = RegistrationIntegrity.permissionsHashOf(listOf("b.perm", "a.perm")) |
| 68 | val b = RegistrationIntegrity.permissionsHashOf(listOf("a.perm", "b.perm")) |
| 69 | assertEquals(a, b) |
| 70 | assertEquals(44, a.length) |
| 71 | val different = RegistrationIntegrity.permissionsHashOf(listOf("a.perm")) |
| 72 | assertTrue(a != different) |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | fun tField_isBase64BigEndianLong() { |
| 77 | val expected = Base64.getEncoder().encodeToString(ByteBuffer.allocate(8).putLong(1700000000L).array()) |
| 78 | assertEquals(expected, RegistrationIntegrity.tField(1700000000L)) |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | fun jsonShapes_haveStableKeyOrder() { |
| 83 | assertEquals("""{"sv":true,"sb":false}""", RegistrationIntegrity.emulationJson(true, false)) |
| 84 | assertEquals( |
| 85 | """{"mp":false,"mu":true,"ae":1,"ap":2,"ai":3}""", |
| 86 | RegistrationIntegrity.automationJson(false, true, 1, 2, 3), |
| 87 | ) |
| 88 | assertEquals("""{"em":"abc"}""", RegistrationIntegrity.nativeSignalsJson("abc")) |
| 89 | } |
| 90 | |
| 91 | // ---- constants ---- |
| 92 | |
| 93 | @Test |
| 94 | fun endpoint_buildsV2Url() { |
| 95 | assertEquals("https://v.whatsapp.net/v2/exist", WhatsAppRegistrationConstants.endpoint("exist")) |
| 96 | assertEquals("com.whatsapp", WhatsAppRegistrationConstants.PACKAGE_NAME) |
| 97 | assertEquals(0x05.toByte(), WhatsAppRegistrationConstants.KEY_TYPE_CURVE25519) |
| 98 | assertEquals(0x08.toByte(), WhatsAppRegistrationConstants.KEY_TYPE_KYBER) |
| 99 | } |
| 100 | |
| 101 | // ---- MexPersistedQueryProvider.parsePersistIds ---- |
| 102 | |
| 103 | @Test |
| 104 | fun parsePersistIds_validShape() { |
| 105 | val map = MexPersistedQueryProvider.parsePersistIds( |
| 106 | """{"version":1,"data":{"OpA":"12345678901234567","OpB":"999"}}""", |
| 107 | ) |
| 108 | assertEquals(mapOf("OpA" to "12345678901234567", "OpB" to "999"), map) |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | fun parsePersistIds_malformed_returnsEmpty() { |
| 113 | assertEquals(emptyMap(), MexPersistedQueryProvider.parsePersistIds("not json")) |
| 114 | assertEquals(emptyMap(), MexPersistedQueryProvider.parsePersistIds("""{"version":1}""")) |
| 115 | assertEquals(emptyMap(), MexPersistedQueryProvider.parsePersistIds("")) |
| 116 | } |
| 117 | } |