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

Modern-Apps.git / library / e2ee-p2p / src / test / java / com / vayunmathur / e2ee / InviteFingerprintTest.kt

InviteFingerprintTest.kt73 lines · 2.7 KiBRaw
1package com.vayunmathur.e2ee
2
3import kotlin.io.encoding.Base64
4import kotlin.io.encoding.ExperimentalEncodingApi
5import kotlin.test.Test
6import kotlin.test.assertEquals
7import kotlin.test.assertFalse
8import kotlin.test.assertTrue
9
10class InviteFingerprintTest {
11 private val bundle = ByteArray(3184) { it.toByte() }
12
13 @Test
14 fun deterministic_and_truncated_to_length() {
15 val fp = InviteFingerprint.compute(12345L, bundle)
16 assertEquals(InviteFingerprint.LENGTH, fp.size)
17 assertTrue(fp.contentEquals(InviteFingerprint.compute(12345L, bundle)), "same inputs, same digest")
18 }
19
20 @Test
21 fun bound_to_the_user_id() {
22 val fp = InviteFingerprint.compute(12345L, bundle)
23 assertFalse(
24 fp.contentEquals(InviteFingerprint.compute(12346L, bundle)),
25 "a fingerprint must not carry over to another user's id",
26 )
27 }
28
29 @Test
30 fun changes_with_the_bundle() {
31 val substituted = bundle.copyOf().also { it[1000] = (it[1000] + 1).toByte() }
32 assertFalse(
33 InviteFingerprint.compute(12345L, bundle).contentEquals(
34 InviteFingerprint.compute(12345L, substituted)
35 ),
36 "a substituted key must not match the link's fingerprint",
37 )
38 }
39
40 @Test
41 fun length_delimits_the_id_from_the_bundle() {
42 // Without the fixed-width id, id||bundle could be re-split: a shifted boundary
43 // between the two would hash the same and let one invite's digest name another pair.
44 assertFalse(
45 InviteFingerprint.compute(0x0000000000000001L, byteArrayOf(2, 3)).contentEquals(
46 InviteFingerprint.compute(0x0000000000000102L, byteArrayOf(3))
47 )
48 )
49 }
50
51 /**
52 * Known-answer vectors shared with the Swift implementation (see `InviteFingerprint` in
53 * Find-Family-iOS/FindFamilyiOS/Crypto/Crypto.swift). A link minted on one platform has to
54 * verify on the other, and nothing else in either test suite would catch a divergence in
55 * the domain string, the id width, or the truncation length.
56 */
57 @Test
58 fun matches_the_cross_platform_test_vector() {
59 assertEquals(
60 "iXVzYL2J4imtjXPgh-dmtw",
61 b64Url(InviteFingerprint.compute(12345L, bundle)),
62 )
63 // Negative ids exist in the wild (the iOS client generates them), so the id must be
64 // hashed as its unsigned 64-bit bit pattern rather than sign-extended or formatted.
65 assertEquals(
66 "rjnCvl7blekCiZv2q9QJOA",
67 b64Url(InviteFingerprint.compute(-2L, bundle)),
68 )
69 }
70
71 @OptIn(ExperimentalEncodingApi::class)
72 private fun b64Url(bytes: ByteArray): String = Base64.UrlSafe.encode(bytes).trimEnd('=')
73}