Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Pass2DiagIntegrityTest.kt
W5Pass2DiagIntegrityTest.kt96 lines · 4.1 KiBRaw
| 1 | package com.vayunmathur.communicate.whatsapp |
| 2 | |
| 3 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppDiag |
| 5 | import com.vayunmathur.communicate.data.whatsapp.registration.RegistrationIntegrity |
| 6 | import kotlin.test.BeforeTest |
| 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 | * Pass-2 slice-1 (whatsapp): the diagnostics sink and the pure registration-integrity |
| 15 | * field builders, which need no socket, auth, or Android keystore. |
| 16 | */ |
| 17 | @RunWith(AndroidJUnit4::class) |
| 18 | class W5Pass2DiagIntegrityTest { |
| 19 | |
| 20 | @BeforeTest |
| 21 | fun setUp() { |
| 22 | WhatsAppDiag.clear() |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | fun diagLogAppendsTimestampedLinesAndClears() { |
| 27 | WhatsAppDiag.log("TAG1", "first message") |
| 28 | WhatsAppDiag.log("TAG2", "second message") |
| 29 | val entries = WhatsAppDiag.log.value |
| 30 | assertEquals(2, entries.size) |
| 31 | assertTrue(entries[0].endsWith("TAG1 first message"), entries[0]) |
| 32 | assertTrue(entries[1].endsWith("TAG2 second message"), entries[1]) |
| 33 | // Timestamp prefix is HH:mm:ss.SSS (12 chars) followed by a space. |
| 34 | assertTrue(entries[0].length > 13 && entries[0][12] == ' ', entries[0]) |
| 35 | WhatsAppDiag.clear() |
| 36 | assertTrue(WhatsAppDiag.log.value.isEmpty()) |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | fun diagLogCapsAtThreeHundredEntries() { |
| 41 | repeat(305) { WhatsAppDiag.log("CAP", "entry $it") } |
| 42 | val entries = WhatsAppDiag.log.value |
| 43 | assertEquals(300, entries.size) |
| 44 | assertTrue(entries.first().endsWith("entry 5"), entries.first()) |
| 45 | assertTrue(entries.last().endsWith("entry 304"), entries.last()) |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | fun diagPreviewFormatsHexWithTruncation() { |
| 50 | assertEquals("0B[]", WhatsAppDiag.preview(byteArrayOf())) |
| 51 | assertEquals("3B[00ff7f]", WhatsAppDiag.preview(byteArrayOf(0, -1, 127))) |
| 52 | val long = ByteArray(30) { it.toByte() } |
| 53 | val preview = WhatsAppDiag.preview(long) |
| 54 | assertTrue(preview.startsWith("30B[000102"), preview) |
| 55 | assertTrue(preview.endsWith("…]"), preview) |
| 56 | // A custom cap truncates earlier without an ellipsis when exactly at the size. |
| 57 | assertEquals("4B[01020304]", WhatsAppDiag.preview(byteArrayOf(1, 2, 3, 4), max = 4)) |
| 58 | assertTrue(WhatsAppDiag.preview(byteArrayOf(1, 2, 3, 4), max = 2).endsWith("…]")) |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | fun integrityAidIsStableBase64Sha256() { |
| 63 | val aid = RegistrationIntegrity.aidOf("android-id-123") |
| 64 | assertEquals(aid, RegistrationIntegrity.aidOf("android-id-123")) |
| 65 | assertTrue(aid != RegistrationIntegrity.aidOf("android-id-124")) |
| 66 | // SHA-256 is 32 bytes -> 44-char base64 with padding. |
| 67 | assertEquals(44, aid.length) |
| 68 | assertTrue(aid.endsWith("=")) |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | fun integrityDerivedFields() { |
| 73 | val hash = RegistrationIntegrity.permissionsHashOf(listOf("perm.a", "perm.b")) |
| 74 | assertEquals(hash, RegistrationIntegrity.permissionsHashOf(listOf("perm.a", "perm.b"))) |
| 75 | assertTrue(hash != RegistrationIntegrity.permissionsHashOf(listOf("perm.a"))) |
| 76 | assertTrue(RegistrationIntegrity.tField(0L).isNotBlank()) |
| 77 | assertFalse(RegistrationIntegrity.tField(0L) == RegistrationIntegrity.tField(86_400L)) |
| 78 | assertEquals( |
| 79 | """{"em":"QUJD"}""", |
| 80 | RegistrationIntegrity.nativeSignalsJson("QUJD"), |
| 81 | ) |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | fun integrityEmulationAndAutomationJson() { |
| 86 | val clean = RegistrationIntegrity.emulationJson(virtio = false, vboxsf = false) |
| 87 | val dirty = RegistrationIntegrity.emulationJson(virtio = true, vboxsf = true) |
| 88 | assertEquals("""{"sv":false,"sb":false}""", clean) |
| 89 | assertEquals("""{"sv":true,"sb":true}""", dirty) |
| 90 | assertEquals("""{"sv":true,"sb":false}""", RegistrationIntegrity.emulationJson(virtio = true, vboxsf = false)) |
| 91 | val automation = RegistrationIntegrity.automationJson( |
| 92 | mockPackages = true, multiUser = false, ae = 1L, ap = 2L, ai = 3L, |
| 93 | ) |
| 94 | assertEquals("""{"mp":true,"mu":false,"ae":1,"ap":2,"ai":3}""", automation) |
| 95 | } |
| 96 | } |