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

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalSlice2SealedCacheTest.kt

SignalSlice2SealedCacheTest.kt146 lines · 6.3 KiBRaw
1package com.vayunmathur.communicate.signal
2
3// FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch.
4
5import androidx.room3.Room
6import androidx.sqlite.driver.bundled.BundledSQLiteDriver
7import androidx.test.core.app.ApplicationProvider
8import com.vayunmathur.communicate.data.signal.SignalDatabase
9import com.vayunmathur.communicate.data.signal.SignalProfileKey
10import com.vayunmathur.communicate.data.signal.SignalSealedSender
11import com.vayunmathur.communicate.data.signal.SignalSenderCertificate
12import com.vayunmathur.communicate.data.signal.transport.SignalTrust
13import kotlin.test.AfterTest
14import kotlin.test.BeforeTest
15import kotlin.test.Test
16import kotlin.test.assertEquals
17import kotlin.test.assertNotNull
18import kotlin.test.assertNull
19import kotlin.test.assertTrue
20import kotlinx.coroutines.test.runTest
21import org.junit.runner.RunWith
22import org.robolectric.RobolectricTestRunner
23import org.robolectric.annotation.Config
24
25/**
26 * Wave 5 (w6 slice 2/6): sealed-sender certificate cache + profile-key/access-key store paths in
27 * [SignalSealedSender], plus the [SignalTrust] union factory. All local behavior — no network:
28 * cached certificates are built with a self-signed chain (as in SignalSlice2E2ETest), and the
29 * trust factory is exercised against the bundled CA assets.
30 */
31@RunWith(RobolectricTestRunner::class)
32@Config(sdk = [34])
33class SignalSlice2SealedCacheTest {
34
35 private lateinit var db: SignalDatabase
36
37 @BeforeTest
38 fun open() {
39 val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()
40 db = Room.inMemoryDatabaseBuilder(ctx, SignalDatabase::class.java)
41 .setDriver(BundledSQLiteDriver()).allowMainThreadQueries().build()
42 SignalTrust.clearCache()
43 }
44
45 @AfterTest
46 fun close() {
47 SignalTrust.clearCache()
48 db.close()
49 }
50
51 private fun selfSignedCertBytes(senderAci: String = "3f0e5a2c-1111-2222-3333-444455556666"): ByteArray {
52 val trustKey = org.signal.libsignal.protocol.ecc.ECKeyPair.generate()
53 val serverKey = org.signal.libsignal.protocol.ecc.ECKeyPair.generate()
54 val serverCert = org.signal.libsignal.metadata.certificate.ServerCertificate(
55 trustKey.privateKey, 1, serverKey.publicKey,
56 )
57 val senderKey = org.signal.libsignal.protocol.ecc.ECPublicKey(
58 org.signal.libsignal.protocol.IdentityKeyPair.generate().publicKey.publicKey.serialize(),
59 )
60 return serverCert.issue(
61 serverKey.privateKey, senderAci, java.util.Optional.empty(), 1, senderKey,
62 System.currentTimeMillis() + 86_400_000L,
63 ).serialized
64 }
65
66 @Test
67 fun cachedCertificate_isReturnedWithoutNetwork() = runTest {
68 val bytes = selfSignedCertBytes()
69 val parsed = org.signal.libsignal.metadata.certificate.SenderCertificate(bytes)
70 // Self-signed test certs expire a day out, inside the production 24h refresh
71 // buffer — so extend the stored expiration the way a real server-issued cert
72 // (a week out) would carry it; the record bytes stay the self-signed ones.
73 val storedExp = System.currentTimeMillis() + 7 * 86_400_000L
74 db.senderCertificateDao().upsert(
75 SignalSenderCertificate(
76 record = bytes, expiration = storedExp, includesE164 = false,
77 ),
78 )
79 // No network is available in tests — a cache hit must return before any fetch.
80 val got = SignalSealedSender.senderCertificate(db, "auth", null, includeE164 = false)
81 assertNotNull(got)
82 assertEquals(parsed.expiration, got.expiration)
83 }
84
85 @Test
86 fun expiredCertificate_isNotUsed() = runTest {
87 val bytes = selfSignedCertBytes()
88 db.senderCertificateDao().upsert(
89 SignalSenderCertificate(
90 record = bytes, expiration = System.currentTimeMillis() - 1000L,
91 includesE164 = false,
92 ),
93 )
94 // Expired: the fetch fails (no network) so null means "send identified instead".
95 assertNull(SignalSealedSender.senderCertificate(db, "auth", null, includeE164 = false))
96 }
97
98 @Test
99 fun e164Mismatch_isNotAHit() = runTest {
100 val bytes = selfSignedCertBytes()
101 val parsed = org.signal.libsignal.metadata.certificate.SenderCertificate(bytes)
102 db.senderCertificateDao().upsert(
103 SignalSenderCertificate(
104 record = bytes, expiration = parsed.expiration, includesE164 = true,
105 ),
106 )
107 // Cached cert carries E164 but we asked without: must refetch (fails offline -> null).
108 assertNull(SignalSealedSender.senderCertificate(db, "auth", null, includeE164 = false))
109 }
110
111 @Test
112 fun corruptCachedRecord_fallsThroughToNull() = runTest {
113 db.senderCertificateDao().upsert(
114 SignalSenderCertificate(
115 record = byteArrayOf(1, 2, 3),
116 expiration = System.currentTimeMillis() + 86_400_000L,
117 includesE164 = false,
118 ),
119 )
120 assertNull(SignalSealedSender.senderCertificate(db, "auth", null, includeE164 = false))
121 }
122
123 @Test
124 fun profileKeyRoundTrip_accessKey() = runTest {
125 val aci = "7c1d9b4e-aaaa-bbbb-cccc-ddddeeeeffff"
126 assertNull(SignalSealedSender.accessKeyFor(db, aci))
127 val key = ByteArray(32) { (it + 1).toByte() }
128 SignalSealedSender.rememberProfileKey(db, aci, key)
129 val access = SignalSealedSender.accessKeyFor(db, aci)
130 assertNotNull(access)
131 assertEquals(16, access!!.size)
132 assertTrue(access.contentEquals(SignalSealedSender.deriveAccessKey(key)))
133 // Wrong-size keys are ignored, never stored.
134 SignalSealedSender.rememberProfileKey(db, "other", ByteArray(5))
135 assertNull(SignalSealedSender.accessKeyFor(db, "other"))
136 }
137
138 @Test
139 fun trustFactory_buildsFromBundledRoots() {
140 val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()
141 val factory = SignalTrust.sslSocketFactory(ctx)
142 assertNotNull(factory)
143 // Cached instance is returned on repeat calls.
144 assertTrue(factory === SignalTrust.sslSocketFactory(ctx))
145 }
146}