Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppDiag.kt
WhatsAppDiag.kt92 lines · 3.9 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import kotlin.time.Clock |
| 4 | import kotlin.time.Instant |
| 5 | import kotlinx.datetime.LocalDateTime |
| 6 | import kotlinx.datetime.LocalTime |
| 7 | import kotlinx.datetime.TimeZone |
| 8 | import kotlinx.datetime.format |
| 9 | import kotlinx.datetime.format.char |
| 10 | import kotlinx.datetime.toLocalDateTime |
| 11 | import com.vayunmathur.library.log.Log |
| 12 | import com.vayunmathur.communicate.data.whatsapp.padMessage |
| 13 | import kotlinx.coroutines.flow.MutableStateFlow |
| 14 | import kotlinx.coroutines.flow.StateFlow |
| 15 | import kotlinx.coroutines.flow.asStateFlow |
| 16 | |
| 17 | /** |
| 18 | * In-app diagnostics sink. The dev build strips Log output from logcat, so during pairing |
| 19 | * we mirror every handshake step, server response and error into an observable list that the |
| 20 | * login screen renders on-screen. |
| 21 | */ |
| 22 | object WhatsAppDiag { |
| 23 | private const val MAX_ENTRIES = 300 |
| 24 | private const val MILLIS_DIGITS = 3 |
| 25 | private val timeFmt = LocalTime.Format { |
| 26 | hour(); char(':'); minute(); char(':'); second(); char('.'); secondFraction(MILLIS_DIGITS) |
| 27 | } |
| 28 | |
| 29 | private val _log = MutableStateFlow<List<String>>(emptyList()) |
| 30 | val log: StateFlow<List<String>> = _log.asStateFlow() |
| 31 | |
| 32 | @Synchronized |
| 33 | fun log(tag: String, msg: String) { |
| 34 | val line = |
| 35 | "${Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).time.format(timeFmt)} $tag $msg" |
| 36 | _log.value = (_log.value + line).takeLast(MAX_ENTRIES) |
| 37 | Log.status(tag, msg) |
| 38 | } |
| 39 | |
| 40 | @Synchronized |
| 41 | fun clear() { |
| 42 | _log.value = emptyList() |
| 43 | } |
| 44 | |
| 45 | /** Short hex preview of a byte array for inspecting raw frames. */ |
| 46 | fun preview(data: ByteArray, max: Int = 24): String { |
| 47 | val n = minOf(data.size, max) |
| 48 | val hex = StringBuilder() |
| 49 | for (i in 0 until n) { |
| 50 | hex.append(String.format("%02x", data[i])) |
| 51 | } |
| 52 | return "${data.size}B[${hex}${if (data.size > max) "…" else ""}]" |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Group sender-key (`skmsg`) self-loopback diagnostic (Phase A 1d). Exercises the exact Rust |
| 57 | * sender-key wire that [WhatsAppE2E.encryptGroup]/[buildEncryptedGroupMessageNode] produce: |
| 58 | * |
| 59 | * 1. sender: create sender key → (state, SKDM) |
| 60 | * 2. receiver: processSenderKey(SKDM) → receiver state |
| 61 | * 3. sender: encryptGroup(state, padded) → skmsg ciphertext |
| 62 | * 4. receiver: decryptGroup(receiverState, ciphertext) → plaintext |
| 63 | * 5. assert plaintext == padded |
| 64 | * |
| 65 | * This validates the SKDM + skmsg round-trips through [RustWhatsAppCrypto] end-to-end on-device |
| 66 | * (it requires the native `libcommunicate_signal` .so, so it is a dev diagnostic, NOT a JVM |
| 67 | * unit test). Live peer interop still can't be validated because the test number is banned. |
| 68 | * Returns true on a successful round-trip. |
| 69 | */ |
| 70 | fun verifyGroupSenderKeyRoundTrip(): Boolean { |
| 71 | return try { |
| 72 | val plaintext = WhatsAppProtocol.padMessage("skmsg loopback probe".toByteArray(Charsets.UTF_8)) |
| 73 | val created = com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto.createSenderKeySplit() |
| 74 | val receiverState = |
| 75 | com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto.processSenderKey(created.skdm) |
| 76 | ?: throw IllegalStateException("processSenderKey returned null") |
| 77 | val enc = com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto.encryptGroupSplit( |
| 78 | created.state, |
| 79 | plaintext) |
| 80 | val dec = com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto.decryptGroupSplit( |
| 81 | receiverState, |
| 82 | enc.data) |
| 83 | val ok = dec.data.contentEquals(plaintext) |
| 84 | val verdict = if (ok) "PASS" else "FAIL" |
| 85 | log("WA-SKMSG", "sender-key loopback $verdict skdm=${preview(created.skdm)} ct=${preview(enc.data)}") |
| 86 | ok |
| 87 | } catch (expected: Exception) { |
| 88 | log("WA-SKMSG", "sender-key loopback ERROR ${expected.message}") |
| 89 | false |
| 90 | } |
| 91 | } |
| 92 | } |