Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / whatsapp / W5Slice1AttestationTest.kt

W5Slice1AttestationTest.kt59 lines · 2.3 KiBRaw
1package com.vayunmathur.communicate.whatsapp
2
3import androidx.test.core.app.ApplicationProvider
4import androidx.test.ext.junit.runners.AndroidJUnit4
5import com.vayunmathur.communicate.data.whatsapp.registration.RegistrationAttestation
6import java.security.MessageDigest
7import javax.crypto.Mac
8import javax.crypto.spec.SecretKeySpec
9import kotlin.test.Test
10import kotlin.test.assertEquals
11import kotlin.test.assertTrue
12import org.junit.runner.RunWith
13
14/**
15 * Slice 1 (w5): registration attestation vectors (RegistrationAttestation.kt).
16 * Robolectric (assets + Android Base64/Log).
17 *
18 * - computeToken is deterministic per number, differs per number, and is
19 * standard-base64 (NO_WRAP) of 20 HMAC-SHA1 bytes (28 chars incl. padding).
20 * - signWithAttestation(key, body) matches a reference HMAC-SHA256.
21 * - md5 matches the platform digest.
22 */
23@RunWith(AndroidJUnit4::class)
24class W5Slice1AttestationTest {
25
26 private val context: android.content.Context
27 get() = ApplicationProvider.getApplicationContext()
28
29 @Test
30 fun computeToken_deterministicAndNumberBound() {
31 val a = RegistrationAttestation.computeToken(context, "2024561111")
32 val b = RegistrationAttestation.computeToken(context, "2024561111")
33 assertEquals(a, b)
34 // 20 bytes -> 28 base64 chars with single padding
35 assertEquals(28, a.length)
36 assertTrue(a.endsWith("="), a)
37 val other = RegistrationAttestation.computeToken(context, "2024561112")
38 assertTrue(a != other)
39 }
40
41 @Test
42 fun signWithAttestation_matchesReferenceHmac() {
43 val key = ByteArray(32) { it.toByte() }
44 val mac = Mac.getInstance("HmacSHA256")
45 mac.init(SecretKeySpec(key, "HmacSHA256"))
46 val expected = android.util.Base64.encodeToString(
47 mac.doFinal("hello body".toByteArray(Charsets.UTF_8)),
48 android.util.Base64.NO_WRAP,
49 )
50 assertEquals(expected, RegistrationAttestation.signWithAttestation(key, "hello body"))
51 }
52
53 @Test
54 fun md5_matchesPlatform() {
55 val expected = MessageDigest.getInstance("MD5").digest("abc".toByteArray())
56 assertTrue(RegistrationAttestation.md5("abc".toByteArray()).contentEquals(expected))
57 assertEquals(16, RegistrationAttestation.md5(ByteArray(0)).size)
58 }
59}