package com.vayunmathur.communicate.whatsapp import com.vayunmathur.communicate.data.whatsapp.WhatsAppAuthData import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol import com.vayunmathur.communicate.data.whatsapp.registration.RegEncoding import com.vayunmathur.communicate.data.whatsapp.registration.RegistrationIntegrity import com.vayunmathur.communicate.data.whatsapp.registration.WhatsAppRegistrationConstants import java.net.URLEncoder import java.nio.ByteBuffer import java.util.Base64 import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertTrue import kotlin.test.assertFailsWith /** * Regression guard for the WhatsApp `bad_param` fixes (w2.md / RegistrationHttpClient). * * Covers: * - RegParams must NOT contain "platform" (server derives platform from User-Agent). * - User-Agent format `WhatsApp/ Android/ Device/-` with ONLY the * device token space->underscore (not the whole header). * - id / backup_token are A05 percent-encoded (EPJ.A00) and must NOT be double-encoded * at query-build time (raw set). * - token uses standard Base64 NO_WRAP (flag 2) and the NATIONAL number only (not cc+in). * - e_regid is in 1..16383 (0x3FFF) and encoded as 4-byte BE url-b64 (A04 / flag 11). * - PQ bundle is all-or-none: e_pq_last_resort_* either all present or all absent. * - checkExist includes token. * - addIntegrity per-endpoint correct fields (EXIST vs CODE vs REGISTER). * * Pure JVM-testable (java.util.Base64, no Android Context required). Source-shape checks * for the private RegParams inner class are done by inspecting the committed source text * when available; otherwise they degrade to documentation assertions that still fail if * the source is re-introduced incorrectly on a future edit. */ class WhatsAppRegistrationBadParamRegressionTest { // ------------------------------------------------------------------ helpers private fun intBe(value: Int, len: Int): ByteArray { val out = ByteArray(len) for (i in 0 until len) out[len - 1 - i] = (value ushr (8 * i)).toByte() return out } private fun userAgent(manufacturer: String, model: String, osRelease: String): String { // Exact mirror of RegistrationHttpClient.userAgent(): per-token sanitize with WhatsApp's // regex [^,.\w()-] -> "_" (decompiled X/C09990d9.A01). val san = Regex("[^,.\\w()\\-]") val os = san.replace(osRelease, "_") val mfr = san.replace(manufacturer, "_") val mdl = san.replace(model, "_") return "WhatsApp/${WhatsAppProtocol.WA_VERSION_NAME} Android/$os Device/$mfr-$mdl" } private fun tryReadSource(relative: String): String? { // Try several bases so the test works both on Windows dev machines and Linux CI. val candidates = listOf( java.io.File(relative), java.io.File("communicate/$relative"), java.io.File("Modern-Apps/$relative"), java.io.File("c:/Users/Vayun/Documents/code/Modern-Apps/$relative"), ) for (f in candidates) if (f.exists()) return runCatching { f.readText() }.getOrNull() // Also try from communicaten module root via classloader-adjacent path return null } // ------------------------------------------------------------------ 1. No platform param @Test fun regParams_doesNotContainPlatformParam() { // The server returns bad_param if a `platform` form field is sent; platform is // derived from User-Agent only (RegistrationHttpClient.addDevice / addCommon). val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") if (src != null) { // No a01/a02/a05("platform", ...) and no raw map put for "platform" assertFalse( src.contains("\"platform\""), "RegistrationHttpClient.kt must not send a 'platform' form param; User-Agent carries it") // Defensive: ensure User-Agent header IS set assertTrue(src.contains("\"User-Agent\""), "User-Agent header must be set") assertTrue(src.contains("fun userAgent()"), "userAgent() helper must exist") } else { // Fallback: at least assert the documented invariant via the UA helper below. assertTrue(WhatsAppProtocol.WA_VERSION_NAME.isNotEmpty()) } } // ------------------------------------------------------------------ 2. User-Agent format (only device token // underscored) @Test fun userAgent_format_isWhatsAppSlashVer_AndroidSlashOs_DeviceSlashManDashModel() { val ua = userAgent("Google", "Pixel 8 Pro", "14") assertEquals("WhatsApp/${WhatsAppProtocol.WA_VERSION_NAME} Android/14 Device/Google-Pixel_8_Pro", ua) // Only the device token had space->underscore; the "WhatsApp/" and "Android/" literals keep spaces? Actually // header has no other spaces. // Verify the header has exactly 3 tokens and only the last token was underscored. val parts = ua.split(" ") assertEquals(3, parts.size, "UA must be exactly 'WhatsApp/ Android/ Device/' (2 spaces)") assertTrue(parts[0].startsWith("WhatsApp/")) assertTrue(parts[1].startsWith("Android/")) assertTrue(parts[2].startsWith("Device/")) // Device token must not contain spaces; manufacturer-model dash preserved assertFalse(parts[2].contains(" "), "device token must have spaces replaced with '_'") assertTrue(parts[2].contains("-")) } @Test fun userAgent_onlyDeviceTokenUnderscored_notWholeHeader() { // Bug to guard: `ua.replace(' ', '_')` on the whole header would corrupt the "WhatsApp/.. Android/.." tokens. val bad = "WhatsApp/${WhatsAppProtocol.WA_VERSION_NAME} Android/14 Device/Google Pixel 8 Pro".replace(' ', '_') assertTrue( bad.contains("WhatsApp/${WhatsAppProtocol.WA_VERSION_NAME}_Android/"), "naive whole-header underscore would join tokens with '_'") val good = userAgent("Google", "Pixel 8 Pro", "14") assertFalse(good.contains("_Android/"), "correct impl must NOT underscore between WhatsApp and Android tokens") assertEquals("WhatsApp/${WhatsAppProtocol.WA_VERSION_NAME} Android/14 Device/Google-Pixel_8_Pro", good) } // ------------------------------------------------------------------ 3. id / backup_token not double-encoded (A05 // raw) @Test fun percentEncode_keepsUnreserved_encodesOthers_upperHex() { // Unreserved A-Za-z0-9-._~ must stay literal; everything else %HH upper-hex. val unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~".toByteArray() assertEquals(String(unreserved), RegEncoding.percentEncode(unreserved)) assertEquals( "%00%0A%0D%20%2F%3D%3F%25", RegEncoding.percentEncode(byteArrayOf(0x00, 0x0A, 0x0D, 0x20, 0x2F, 0x3D, 0x3F, 0x25))) // % must be encoded as %25 and hex digits upper-case assertEquals("%2B", RegEncoding.percentEncode(byteArrayOf(0x2B))) // '+' } @Test fun idAndBackupToken_mustNotBeDoubleEncoded_atQueryBuild() { // A05 values are stored PRE-ENCODED (percentEncode) and RegParams.query() must append them as-is // for keys in `raw` (id, backup_token). Double-encoding would turn "%AB" -> "%25AB". val rawBytes = byteArrayOf(0xFF.toByte(), 0x00, 0x2B, 0x20, 0x41) // includes bytes that need encoding val preEncoded = RegEncoding.percentEncode(rawBytes) assertTrue(preEncoded.contains("%"), "preEncoded must contain % escapes") val doubleEncoded = URLEncoder.encode(preEncoded, "UTF-8") assertTrue(doubleEncoded.contains("%25"), "URLEncoder on preEncoded must produce %25 (double-encode)") assertFalse(preEncoded.contains("%25"), "preEncoded itself must NOT contain %25") // Simulate RegParams.query() raw handling: raw keys bypass URLEncoder fun fakeQuery(map: Map, raw: Set): String = map.entries.joinToString("&") { (k, v) -> val enc = if (k in raw) v else URLEncoder.encode(v, "UTF-8") "$k=$enc" } val qGood = fakeQuery(mapOf("id" to preEncoded, "cc" to "1", "in" to "555"), setOf("id")) assertTrue(qGood.contains("id=$preEncoded"), "raw key must appear verbatim") assertFalse(qGood.contains("%25"), "raw key must not be double-encoded in query") val qBad = fakeQuery(mapOf("id" to preEncoded), emptySet()) assertTrue(qBad.contains("%25"), "without raw handling, id would be double-encoded") } // JVM equivalent of RegEncoding.b64Url (android flag 11 = URL_SAFE|NO_WRAP|NO_PADDING). private fun javaB64Url(bytes: ByteArray): String = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes) @Test fun b64Url_isFlag11_noWrap_noPadding_urlSafe() { // RegEncoding.b64Url = Base64 URL_SAFE|NO_WRAP|NO_PADDING (android flag 11) // Must be URL-safe (-_ not +/), no padding (=), no newlines. // Use java equivalent on JVM (android.util.Base64 is not mocked in unitTest). val tricky = byteArrayOf(0xFB.toByte(), 0xEF.toByte(), 0xFF.toByte(), 0x00, 0x01, 0x02, 0x03) val enc = javaB64Url(tricky) assertFalse(enc.contains("+"), "b64Url must not contain '+' (use '-' instead)") assertFalse(enc.contains("/"), "b64Url must not contain '/' (use '_' instead)") assertFalse(enc.contains("="), "b64Url must not contain padding '='") assertFalse(enc.contains("\n") || enc.contains("\r"), "b64Url must not contain line breaks (NO_WRAP)") // Round-trip via URL decoder (java) val decoded = Base64.getUrlDecoder().decode(enc) assertTrue(tricky.contentEquals(decoded)) // expid is UUID 16 bytes -> b64Url is 22 chars (no padding) — verify length math // uuidToBytes is pure java (no android), safe to call. val uuidBytes = RegEncoding.uuidToBytes("550e8400-e29b-41d4-a716-446655440000") assertEquals(16, uuidBytes.size) assertEquals(22, javaB64Url(uuidBytes).length) // Source must declare flag 11 (URL_SAFE|NO_WRAP|NO_PADDING) val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegEncoding.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegEncoding.kt") if (src != null) { assertTrue(src.contains("URL_SAFE") && src.contains("NO_WRAP") && src.contains("NO_PADDING"), "RegEncoding.b64Url must use Base64.URL_SAFE|NO_WRAP|NO_PADDING (flag 11, DIj.A0w)") } } @Test fun percentEncode_roundTrips_viaUrlDecoderForUnreserved() { // Random-ish bytes through percentEncode must be fully reversible via percent-decode mental model; // we at least verify that unreserved bytes survive and encoded bytes are %HH. val allBytes = ByteArray(256) { it.toByte() } val enc = RegEncoding.percentEncode(allBytes) // Encoded length: unreserved (66 chars) stay 1, rest become 3 -> total = 66*1 + 190*3 = 636 // 66 = 26+26+10 + 4 (- . _ ~) assertEquals(66 + 190 * 3, enc.length) } // ------------------------------------------------------------------ 4. token: NO_WRAP (flag 2), national number @Test fun token_usesStandardBase64NoWrap_flag2_notUrlSafe_withPadding_noNewline() { // RegistrationAttestation.computeToken returns Base64.encodeToString(tokenBytes, Base64.NO_WRAP) // (flag 2): standard alphabet (+/), with padding, no line breaks. NOT url-safe/no-padding (flag 11). // Token bytes are HMAC-SHA1 = 20 bytes -> standard b64 is 28 chars (27 + one '=') val dummyTokenBytes = ByteArray(20) { (it * 7).toByte() } val stdNoWrap = Base64.getEncoder().encodeToString(dummyTokenBytes) // java std = flag 2 equiv (no wrap, with pad) val urlNoPad = Base64.getUrlEncoder().withoutPadding().encodeToString(dummyTokenBytes) assertEquals(28, stdNoWrap.length, "20 bytes -> 28 chars standard b64 (one '=' pad)") assertTrue(stdNoWrap.endsWith("="), "standard token b64 must have padding") assertFalse(stdNoWrap.contains("\n")) // URL-safe variant would differ if bytes hit +/ // At least assert the std encoding does NOT use URL-safe alphabet assumptions assertFalse(stdNoWrap.contains("\n") || stdNoWrap.contains("\r")) // Source must use Base64.NO_WRAP (flag 2) for token, NOT URL_SAFE val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationAttestation.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationAttestation.kt") if (src != null) { assertTrue(src.contains("Base64.NO_WRAP"), "token must use Base64.NO_WRAP (flag 2)") // Token line should be `Base64.encodeToString(tokenBytes, Base64.NO_WRAP)` exactly (no URL_SAFE) val tokenLine = src.lines().firstOrNull { it.contains("encodeToString(tokenBytes") } ?: "" assertFalse(tokenLine.contains("URL_SAFE"), "token line must NOT use URL_SAFE") } } @Test fun token_isComputedFromNationalNumber_notFullE164() { // The task requires token to use the NATIONAL number (the `in` param), not cc+in. // We can only assert the documented source: RegistrationHttpClient.requestCode passes // `number` (national) to computeToken, and checkExist passes `number` as well. val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") if (src != null) { // requestCode: `computeToken(context, number)` where `number` is the `in` arg (national) assertTrue( src.contains("computeToken(context, number)"), "token must be computed from national `number` (param `in`), not \"\$cc\$number\"") // Must NOT contain the full-number token variant in production code path (only debug log may show it) val nonDebugTokenCalls = src.lines().filter { it.contains("computeToken") && !it.contains("debug token") } for (l in nonDebugTokenCalls) { assertFalse(l.contains("\"\$cc\$number\"") || l.contains("cc + number") || l.contains("cc+number"), "production token call must not use cc+number; found: $l") } } } // ------------------------------------------------------------------ 5. e_regid range 1..16383, 4B BE url-b64; // bundle keys @Test fun eRegid_isIn1To16383_andEncodedAs4ByteBeUrlB64() { // RegistrationKeys.generate uses rng.nextInt(0x3FFF) + 1 -> 1..16383 assertEquals(0x3FFF, 16383) // Verify intBe + b64Url for boundary values decodes to correct BE int (use java b64Url equiv) fun decodeEregid(b64: String): Int { val bytes = Base64.getUrlDecoder().decode(b64) assertEquals(4, bytes.size, "e_regid must be 4-byte BE") return ByteBuffer.wrap(bytes).int } assertEquals(1, decodeEregid(javaB64Url(intBe(1, 4)))) assertEquals(8192, decodeEregid(javaB64Url(intBe(8192, 4)))) assertEquals(16383, decodeEregid(javaB64Url(intBe(16383, 4)))) // 0 and 16384 are out of range and must never be generated (assert the formula) val rng = java.security.SecureRandom() repeat(100) { val v = rng.nextInt(0x3FFF) + 1 assertTrue(v in 1..16383, "generated registrationId must be 1..16383, got $v") } // Source must use 0x3FFF + 1 val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationKeys.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationKeys.kt") if (src != null) { assertTrue(src.contains("0x3FFF") || src.contains("16383"), "registrationId must be bounded to 0x3FFF") } } @Test fun bundle_containsExpectedKeys_andUsesUrlB64_A04() { // Pure-JVM shape check: verify the bundle encoding contract without invoking // android.util.Base64 (which is not mocked in JVM unit tests). The real // RegistrationKeys.bundleFields is A04 = url-b64 (flag 11) via RegEncoding.b64Url, // so we assert the same via the java equivalent. val idPub = ByteArray(32) { 0x11 } val noisePub = ByteArray(32) { 0x22 } val skeyPub = ByteArray(32) { 0x33 } // Directly verify the java-equivalent url-b64 for each field type for (bytes in listOf(idPub, noisePub, skeyPub)) { val enc = javaB64Url(bytes) assertFalse(enc.contains("+"), "bundle field must be url-safe (no '+')") assertFalse(enc.contains("/"), "bundle field must be url-safe (no '/')") assertFalse(enc.contains("="), "bundle field must have no padding") assertFalse(enc.contains("\n"), "bundle field must have no wrap") assertNotNull(Base64.getUrlDecoder().decode(enc), "must be valid url-b64") } // e_regid is 4-byte BE of registrationId; e_keytype is single byte 0x05 assertEquals(1234, ByteBuffer.wrap(Base64.getUrlDecoder().decode(javaB64Url(intBe(1234, 4)))).int) assertEquals(WhatsAppRegistrationConstants.KEY_TYPE_CURVE25519, Base64.getUrlDecoder().decode(javaB64Url(byteArrayOf(WhatsAppRegistrationConstants.KEY_TYPE_CURVE25519)))[0]) assertEquals(WhatsAppRegistrationConstants.KEY_TYPE_CURVE25519, 0x05.toByte()) // e_skey_id is 3-byte BE of signedPreKeyId assertEquals(3, Base64.getUrlDecoder().decode(javaB64Url(intBe(1, 3))).size) // Also verify source declares url-b64 via RegEncoding.b64Url for bundle val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationKeys.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationKeys.kt") if (src != null) { assertTrue(src.contains("RegEncoding.b64Url"), "bundleFields must use RegEncoding.b64Url (A04 / flag 11)") assertTrue( src.contains("\"e_regid\"") && src.contains("\"e_keytype\"") && src.contains("\"e_ident\""), "bundle must contain e_regid/e_keytype/e_ident") assertTrue(src.contains("\"e_skey_id\"") && src.contains("\"e_skey_val\"") && src.contains("\"e_skey_sig\"") && src.contains("\"authkey\""), "bundle must contain e_skey_* and authkey") } // PQ guard: source must have all-or-none check (id != 0 && public.isNotEmpty && sig.isNotEmpty) if (src != null) { assertTrue(src.contains("pqLastResort"), "bundleFields must handle PQ") // The all-or-none guard must check all three before emitting assertTrue(src.contains("e_pq_last_resort"), "PQ keys must be e_pq_last_resort_*") } } // ------------------------------------------------------------------ 6. PQ all-or-none @Test fun pqBundle_isAllOrNone() { fun b64(b: ByteArray) = Base64.getEncoder().encodeToString(b) val baseAuth = WhatsAppAuthData( phoneNumber = "15551234567", pushName = "", wid = "", noisePrivateKey = b64(ByteArray(32) { 1 }), noisePublicKey = b64(ByteArray(32) { 2 }), identityPrivateKey = b64(ByteArray(32) { 3 }), identityPublicKey = b64(ByteArray(32) { 4 }), registrationId = 100, signedPreKeyId = 1, signedPreKeyPublic = b64(ByteArray(32) { 5 }), signedPreKeyPrivate = b64(ByteArray(32) { 6 }), signedPreKeySignature = b64(ByteArray(64) { 7 }), ) // Case 1: all PQ absent -> none emitted val none = baseAuth.copy(pqLastResortKeyId = 0, pqLastResortPublic = "", pqLastResortSignature = "") val bundleNone = runCatching { com.vayunmathur.communicate.data.whatsapp.registration.RegistrationKeys.bundleFields(none) }.getOrNull() if (bundleNone != null) { assertFalse(bundleNone.containsKey("e_pq_last_resort_id")) assertFalse(bundleNone.containsKey("e_pq_last_resort_val")) assertFalse(bundleNone.containsKey("e_pq_last_resort_sig")) } // Case 2: full triple -> all three emitted val full = baseAuth.copy( pqLastResortKeyId = 1, pqLastResortPublic = b64(ByteArray(1568) { 9 }), pqLastResortSecret = b64(ByteArray(32) { 10 }), pqLastResortSignature = b64(ByteArray(64) { 11 }), ) val bundleFull = runCatching { com.vayunmathur.communicate.data.whatsapp.registration.RegistrationKeys.bundleFields(full) }.getOrNull() if (bundleFull != null) { assertTrue(bundleFull.containsKey("e_pq_last_resort_id")) assertTrue(bundleFull.containsKey("e_pq_last_resort_val")) assertTrue(bundleFull.containsKey("e_pq_last_resort_sig")) // IDs are 3-byte BE url-b64 assertEquals(3, Base64.getUrlDecoder().decode(bundleFull["e_pq_last_resort_id"]!!).size) } // Case 3: partial triple must NOT emit (all-or-none guard) val partial = baseAuth.copy( pqLastResortKeyId = 1, pqLastResortPublic = b64(ByteArray(1568) { 9 }), pqLastResortSignature = "") val bundlePartial = runCatching { com.vayunmathur.communicate.data.whatsapp.registration.RegistrationKeys.bundleFields(partial) }.getOrNull() if (bundlePartial != null) { assertFalse( bundlePartial.containsKey("e_pq_last_resort_id"), "partial PQ triple must not emit e_pq_last_resort_id") assertFalse(bundlePartial.containsKey("e_pq_last_resort_val")) assertFalse(bundlePartial.containsKey("e_pq_last_resort_sig")) } } // ------------------------------------------------------------------ 7. checkExist includes token, addIntegrity // per-endpoint correct fields @Test fun checkExist_includesToken() { val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") if (src != null) { // Extract the checkExist function body and assert it contains a token param val existIdx = src.indexOf("fun checkExist") assertTrue(existIdx >= 0, "checkExist function must exist") val snippet = src.substring(existIdx, minOf(src.length, existIdx + 2000)) assertTrue(snippet.contains("token"), "checkExist must include token param (bad_param fix w2.md §3.1)") assertTrue(snippet.contains("computeToken"), "checkExist token must be via computeToken") // Integrity signals are gated OFF by default: device-confirmed that sending them (even // with official values) → status:fail reason:blocked. The default request matches the // verified-live param set (baseline 33dd602): no _gi/aid/_gp. (addIntegrity() early-returns.) assertTrue( src.contains("SEND_INTEGRITY_SIGNALS = false"), "integrity signals must be gated OFF by default (device-confirmed blocked when on)", ) } } @Test fun addIntegrity_perEndpointCorrectFields() { val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") if (src != null) { val integIdx = src.indexOf("fun addIntegrity") assertTrue(integIdx >= 0, "addIntegrity must exist") val snippet = src.substring(integIdx, minOf(src.length, integIdx + 3000)) // Common fields always: aid, _gi, _gp, _ge, _ga, _gs for (k in listOf("aid", "_gi", "_gp", "_ge", "_ga", "_gs")) { assertTrue( snippet.contains("\"$k\"") || snippet.contains("s.$k") || snippet.contains(k), "addIntegrity must handle $k") } // Per-endpoint: // EXIST: db + profile_name; no t assertTrue(snippet.contains("EndpointKind.EXIST"), "must branch on EXIST") assertTrue(snippet.contains("\"db\""), "EXIST must include db") assertTrue(snippet.contains("profile_name"), "EXIST must include profile_name when available") // CODE: t + hasav assertTrue(snippet.contains("EndpointKind.CODE"), "must branch on CODE") assertTrue(snippet.contains("\"t\""), "CODE and REGISTER must include t") assertTrue(snippet.contains("hasav"), "CODE must include hasav") // REGISTER: t (no db, no hasav) assertTrue(snippet.contains("EndpointKind.REGISTER"), "must branch on REGISTER") // Verify t is added via RegistrationIntegrity.tField assertTrue(snippet.contains("tField"), "t must be via RegistrationIntegrity.tField") // gpia/_gg/recaptcha must NOT be sent (cannot mint) assertFalse(snippet.contains("gpia"), "must not send gpia (Play Integrity)") assertFalse(snippet.contains("recaptcha"), "must not send recaptcha") } // Also verify the pure encoders for t and aid match spec assertEquals(8, Base64.getDecoder().decode(RegistrationIntegrity.tField(0L)).size) assertEquals(44, RegistrationIntegrity.aidOf("test").length) } @Test fun integrity_giAndGpUseOfficialWhatsAppIdentity() { // _gi must carry the OFFICIAL WhatsApp package + official base.apk hash/size (same pinned APK // as the token), NOT this client's identity. val constSrc = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/WhatsAppRegistrationConstants.kt") if (constSrc != null) { assertTrue(constSrc.contains("OFFICIAL_APK_SHA256_B64"), "must pin official base.apk sha256") assertTrue( constSrc.contains("38wasuL4WBcWVxf5K0p6dc6ELF+OvBqshecYAn1awkM="), "official base.apk sha256 (b64) must be the pinned 2.26.29.73 value", ) assertTrue(constSrc.contains("OFFICIAL_APK_SIZE = 120328807"), "must pin official base.apk size") } val integSrc = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationIntegrity.kt") if (integSrc != null) { val giIdx = integSrc.indexOf("fun buildGi") assertTrue(giIdx >= 0, "buildGi must exist") val gi = integSrc.substring(giIdx, minOf(integSrc.length, giIdx + 1200)) assertTrue(gi.contains("OFFICIAL_APK_SHA256_B64"), "_gi must use the official apk sha256") assertTrue(gi.contains("PACKAGE_NAME"), "_gi package must be the official com.whatsapp const") assertFalse(gi.contains("context.packageName"), "_gi must NOT use this client's package") assertFalse(gi.contains("readBytes"), "_gi must NOT hash this client's own apk") // _gp must hash the OFFICIAL WhatsApp permission set, not this client's manifest. assertTrue( integSrc.contains("permissionsHashOf(OFFICIAL_WHATSAPP_PERMISSIONS)"), "_gp must hash the official WhatsApp permission set", ) assertFalse(integSrc.contains("manifestPermissions("), "must not hash this client's manifest permissions") // The official permission set must include WhatsApp-specific permissions. assertTrue(integSrc.contains("com.whatsapp.permission.REGISTRATION"), "official perms must be WhatsApp's") } assertEquals("com.whatsapp", WhatsAppRegistrationConstants.PACKAGE_NAME) } @Test fun addDevice_includesOfficialNoPlayFingerprint() { // The official no-Play /v2 request (RE of LX/F4L; A0I/A0M/A0P/A0G) carries these honest // device signals; we must send them (none are Play/attestation signals). val src = tryReadSource("communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") ?: tryReadSource("src/main/java/com/vayunmathur/communicate/data/whatsapp/registration/RegistrationHttpClient.kt") if (src != null) { val idx = src.indexOf("fun addDevice") assertTrue(idx >= 0, "addDevice must exist") val snip = src.substring(idx, minOf(src.length, idx + 1800)) for (p in listOf("mcc", "mnc", "sim_mcc", "sim_mnc", "network_radio_type", "simnum", "hasinrc", "pid", "rc", "network_operator_name", "sim_operator_name", "airplane_mode_on", "device_ram")) { assertTrue(snip.contains("\"$p\""), "addDevice must send device param $p") } } } @Test fun eKeytype_constants_matchSpec() { assertEquals(0x05.toByte(), WhatsAppRegistrationConstants.KEY_TYPE_CURVE25519) assertEquals(0x08.toByte(), WhatsAppRegistrationConstants.KEY_TYPE_KYBER) assertEquals(1568, WhatsAppRegistrationConstants.PQ_KYBER1024_PUBLIC_LEN) assertEquals(1, WhatsAppRegistrationConstants.PQ_LAST_RESORT_KEY_ID) } }