Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / WhatsAppAttestationTest.kt
WhatsAppAttestationTest.kt71 lines · 2.6 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.registration.RegistrationIntegrity |
| 4 | import com.vayunmathur.communicate.data.whatsapp.registration.WhatsAppPqPreKey |
| 5 | import java.nio.ByteBuffer |
| 6 | import java.util.Base64 |
| 7 | import kotlin.test.Test |
| 8 | import kotlin.test.assertContentEquals |
| 9 | import kotlin.test.assertEquals |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | /** |
| 13 | * Wire-shape assertions for the attestation integrity encoders + PQ signing-input framing |
| 14 | * (Phase B 2a/2d). Pure (java.util.Base64 + java.security), no Android runtime required. |
| 15 | */ |
| 16 | class WhatsAppAttestationTest { |
| 17 | |
| 18 | private val dec = Base64.getDecoder() |
| 19 | |
| 20 | @Test |
| 21 | fun aid_isBase64Sha256_44chars_deterministic() { |
| 22 | val a = RegistrationIntegrity.aidOf("abc123deviceid") |
| 23 | val b = RegistrationIntegrity.aidOf("abc123deviceid") |
| 24 | assertEquals(a, b) |
| 25 | assertEquals(44, a.length) // base64 of a 32-byte SHA-256 digest |
| 26 | assertEquals(32, dec.decode(a).size) |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | fun permissionsHash_isOrderIndependent() { |
| 31 | val a = RegistrationIntegrity.permissionsHashOf(listOf("B", "A", "C")) |
| 32 | val b = RegistrationIntegrity.permissionsHashOf(listOf("C", "B", "A")) |
| 33 | assertEquals(a, b) |
| 34 | assertEquals(32, dec.decode(a).size) |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | fun tField_isBigEndianInt64Base64() { |
| 39 | val t = 1_786_666_203L |
| 40 | val decoded = dec.decode(RegistrationIntegrity.tField(t)) |
| 41 | assertEquals(8, decoded.size) |
| 42 | assertEquals(t, ByteBuffer.wrap(decoded).long) |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | fun jsonEncoders_matchSpecShapes() { |
| 47 | assertEquals("""{"sv":false,"sb":true}""", RegistrationIntegrity.emulationJson(false, true)) |
| 48 | assertEquals( |
| 49 | """{"mp":true,"mu":false,"ae":10,"ap":20,"ai":0}""", |
| 50 | RegistrationIntegrity.automationJson(true, false, 10, 20, 0), |
| 51 | ) |
| 52 | assertEquals("""{"em":"AAAA"}""", RegistrationIntegrity.nativeSignalsJson("AAAA")) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun pqSigningInput_isTypeBytePlusPublicKey() { |
| 57 | val pub = ByteArray(1568) { (it and 0xFF).toByte() } |
| 58 | val input = WhatsAppPqPreKey.signingInput(pub) |
| 59 | assertEquals(1 + 1568, input.size) |
| 60 | assertEquals(0x08.toByte(), input[0]) // KEY_TYPE_KYBER |
| 61 | assertContentEquals(pub, input.copyOfRange(1, input.size)) |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | fun pqSigningInput_typeByteDistinguishesFromCurve() { |
| 66 | // Curve25519 signed-prekey uses 0x05; the PQ prekey MUST use 0x08 so the signatures over |
| 67 | // otherwise-identical key bytes differ. |
| 68 | val pub = ByteArray(32) { 7 } |
| 69 | assertTrue(WhatsAppPqPreKey.signingInput(pub)[0] != 0x05.toByte()) |
| 70 | } |
| 71 | } |