Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1RegBundleTest.kt
W5Slice1RegBundleTest.kt101 lines · 4.0 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppAuthData |
| 5 | import com.vayunmathur.communicate.data.whatsapp.registration.RegistrationKeys |
| 6 | import java.util.Base64 |
| 7 | import kotlin.test.Test |
| 8 | import kotlin.test.assertEquals |
| 9 | import kotlin.test.assertFalse |
| 10 | import kotlin.test.assertTrue |
| 11 | import org.junit.runner.RunWith |
| 12 | |
| 13 | /** |
| 14 | * Slice 1 (w5): RegistrationKeys.bundleFields — the /v2 E2E key bundle. |
| 15 | * Robolectric: bundleFields decodes via RegEncoding.b64Url (android.util.Base64). |
| 16 | * Uses a synthetic scaffold (no native keygen) so assertions are deterministic. |
| 17 | */ |
| 18 | @RunWith(AndroidJUnit4::class) |
| 19 | class W5Slice1RegBundleTest { |
| 20 | |
| 21 | private fun b64Std(bytes: ByteArray): String = Base64.getEncoder().encodeToString(bytes) |
| 22 | |
| 23 | private fun scaffold( |
| 24 | registrationId: Int = 1234, |
| 25 | signedPreKeyId: Int = 1, |
| 26 | pqId: Int = 0, |
| 27 | pqPub: String = "", |
| 28 | pqSig: String = "", |
| 29 | ) = WhatsAppAuthData( |
| 30 | phoneNumber = "+15550001", |
| 31 | pushName = "", |
| 32 | wid = "", |
| 33 | noisePrivateKey = b64Std(ByteArray(32) { 1 }), |
| 34 | noisePublicKey = b64Std(ByteArray(32) { 2 }), |
| 35 | identityPrivateKey = b64Std(ByteArray(32) { 3 }), |
| 36 | identityPublicKey = b64Std(ByteArray(32) { 4 }), |
| 37 | registrationId = registrationId, |
| 38 | signedPreKeyId = signedPreKeyId, |
| 39 | signedPreKeyPublic = b64Std(ByteArray(32) { 5 }), |
| 40 | signedPreKeyPrivate = b64Std(ByteArray(32) { 6 }), |
| 41 | signedPreKeySignature = b64Std(ByteArray(64) { 7 }), |
| 42 | pqLastResortKeyId = pqId, |
| 43 | pqLastResortPublic = pqPub, |
| 44 | pqLastResortSecret = "", |
| 45 | pqLastResortSignature = pqSig, |
| 46 | ) |
| 47 | |
| 48 | private fun b64Url(bytes: ByteArray): String = |
| 49 | Base64.getUrlEncoder().withoutPadding().encodeToString(bytes) |
| 50 | |
| 51 | @Test |
| 52 | fun bundleFields_classicSeven_presentAndUrlSafe() { |
| 53 | val fields = RegistrationKeys.bundleFields(scaffold()) |
| 54 | assertEquals( |
| 55 | setOf("e_regid", "e_keytype", "e_ident", "e_skey_id", "e_skey_val", "e_skey_sig", "authkey"), |
| 56 | fields.keys, |
| 57 | ) |
| 58 | // e_regid = 4-byte BE of 1234, url-b64 no padding |
| 59 | assertEquals(b64Url(byteArrayOf(0, 0, 0x04.toByte(), 0xD2.toByte())), fields["e_regid"]) |
| 60 | // e_keytype = b64url(0x05) |
| 61 | assertEquals(b64Url(byteArrayOf(0x05)), fields["e_keytype"]) |
| 62 | // e_skey_id = 3-byte BE of 1 |
| 63 | assertEquals(b64Url(byteArrayOf(0, 0, 1)), fields["e_skey_id"]) |
| 64 | assertEquals(b64Url(ByteArray(32) { 4 }), fields["e_ident"]) |
| 65 | assertEquals(b64Url(ByteArray(32) { 2 }), fields["authkey"]) |
| 66 | fields.values.forEach { v -> |
| 67 | assertFalse(v.contains('+') || v.contains('/') || v.contains('='), v) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | fun bundleFields_pqAllOrNone() { |
| 73 | // no PQ by default |
| 74 | assertFalse(RegistrationKeys.bundleFields(scaffold()).keys.any { it.startsWith("e_pq_") }) |
| 75 | // partial PQ (id set, missing sig) is omitted entirely |
| 76 | val partial = scaffold(pqId = 1, pqPub = b64Std(ByteArray(1568) { 9 })) |
| 77 | assertFalse(RegistrationKeys.bundleFields(partial).keys.any { it.startsWith("e_pq_") }) |
| 78 | // full triple is emitted |
| 79 | val full = scaffold( |
| 80 | pqId = 1, |
| 81 | pqPub = b64Std(ByteArray(1568) { 9 }), |
| 82 | pqSig = b64Std(ByteArray(64) { 8 }), |
| 83 | ) |
| 84 | val fields = RegistrationKeys.bundleFields(full) |
| 85 | assertEquals(b64Url(byteArrayOf(0, 0, 1)), fields["e_pq_last_resort_id"]) |
| 86 | assertEquals(b64Url(ByteArray(1568) { 9 }), fields["e_pq_last_resort_val"]) |
| 87 | assertEquals(b64Url(ByteArray(64) { 8 }), fields["e_pq_last_resort_sig"]) |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | fun bundleFields_regIdBoundary() { |
| 92 | // max 0x3FFF in 4-byte BE |
| 93 | val fields = RegistrationKeys.bundleFields(scaffold(registrationId = 0x3FFF)) |
| 94 | assertEquals(b64Url(byteArrayOf(0, 0, 0x3F, 0xFF.toByte())), fields["e_regid"]) |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | fun emitPqLastResort_defaultsOff() { |
| 99 | assertFalse(RegistrationKeys.EMIT_PQ_LAST_RESORT) |
| 100 | } |
| 101 | } |