Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / MlsLifecycleRcsPass3Test.kt
MlsLifecycleRcsPass3Test.kt250 lines · 11.3 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | import android.content.Context |
| 4 | import androidx.room3.Room |
| 5 | import androidx.sqlite.driver.bundled.BundledSQLiteDriver |
| 6 | import androidx.test.core.app.ApplicationProvider |
| 7 | import com.vayunmathur.communicate.data.rcs.RcsDatabase |
| 8 | import com.vayunmathur.communicate.data.rcs.RcsSipTransport |
| 9 | import com.vayunmathur.communicate.data.rcs.e2e.RcsE2E |
| 10 | import com.vayunmathur.communicate.data.rcs.e2e.RcsKeyDirectory |
| 11 | import com.vayunmathur.communicate.data.rcs.e2e.RcsPeerKeys |
| 12 | import com.vayunmathur.communicate.data.rcs.e2e.RustMlsCrypto |
| 13 | import kotlinx.coroutines.test.runTest |
| 14 | import org.junit.runner.RunWith |
| 15 | import org.robolectric.RobolectricTestRunner |
| 16 | import kotlin.test.AfterTest |
| 17 | import kotlin.test.BeforeTest |
| 18 | import kotlin.test.Test |
| 19 | import kotlin.test.assertEquals |
| 20 | import kotlin.test.assertFalse |
| 21 | import kotlin.test.assertNotNull |
| 22 | import kotlin.test.assertNull |
| 23 | import kotlin.test.assertTrue |
| 24 | |
| 25 | /** |
| 26 | * Pass 3: the native MLS lifecycle directly and as a three-device group — |
| 27 | * identity/key-package/group/join/encrypt/decrypt/remove — plus the |
| 28 | * transport-mediated setup helpers through the fake-sender seam. |
| 29 | */ |
| 30 | @RunWith(RobolectricTestRunner::class) |
| 31 | class MlsLifecycleRcsPass3Test { |
| 32 | |
| 33 | private val context: Context get() = ApplicationProvider.getApplicationContext() |
| 34 | private lateinit var deviceA: RcsDatabase |
| 35 | private lateinit var deviceB: RcsDatabase |
| 36 | private lateinit var deviceC: RcsDatabase |
| 37 | |
| 38 | private fun newDb(): RcsDatabase = |
| 39 | Room.inMemoryDatabaseBuilder(context, RcsDatabase::class.java) |
| 40 | .setDriver(BundledSQLiteDriver()) |
| 41 | .allowMainThreadQueries() |
| 42 | .build() |
| 43 | |
| 44 | private suspend fun <T> onDevice(db: RcsDatabase, block: suspend () -> T): T { |
| 45 | RcsDatabase.overrideForTest = db |
| 46 | try { |
| 47 | return block() |
| 48 | } finally { |
| 49 | RcsDatabase.overrideForTest = deviceA |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private val sent = mutableListOf<Triple<String, String, ByteArray>>() |
| 54 | |
| 55 | @BeforeTest fun setUp() { |
| 56 | deviceA = newDb(); deviceB = newDb(); deviceC = newDb() |
| 57 | RcsDatabase.overrideForTest = deviceA |
| 58 | RcsPeerKeys.clear() |
| 59 | sent.clear() |
| 60 | } |
| 61 | |
| 62 | @AfterTest fun tearDown() { |
| 63 | RcsSipTransport.fakeSenderForTest = null |
| 64 | RcsDatabase.overrideForTest = null |
| 65 | deviceA.close(); deviceB.close(); deviceC.close() |
| 66 | RcsPeerKeys.clear() |
| 67 | } |
| 68 | |
| 69 | @Test fun directNativeLifecycleAndKindProbes() { |
| 70 | if (!RustMlsCrypto.isAvailable) { |
| 71 | // Fail-closed probes without the library. |
| 72 | assertEquals(2, runCatching { RustMlsCrypto.payloadKind(byteArrayOf(1)) }.getOrElse { 2 }) |
| 73 | return |
| 74 | } |
| 75 | val aId = RustMlsCrypto.generateIdentity("+15550002001".toByteArray())?.getOrNull(1) |
| 76 | val bId = RustMlsCrypto.generateIdentity("+15550002002".toByteArray())?.getOrNull(1) |
| 77 | assertNotNull(aId); assertNotNull(bId) |
| 78 | |
| 79 | val bPkg = RustMlsCrypto.buildKeyPackage(ByteArray(0), bId) |
| 80 | assertNotNull(bPkg) |
| 81 | val (bStorage, kpB) = bPkg[0] to bPkg[1] |
| 82 | |
| 83 | val created = RustMlsCrypto.createGroup(ByteArray(0), aId) |
| 84 | assertNotNull(created) |
| 85 | var aStorage = created[0] |
| 86 | val groupId = created[1] |
| 87 | assertTrue(groupId.isNotEmpty()) |
| 88 | |
| 89 | val added = RustMlsCrypto.addMembers(aStorage, aId, groupId, arrayOf(kpB)) |
| 90 | assertNotNull(added) |
| 91 | aStorage = added[0] |
| 92 | val commit = added[1] |
| 93 | val welcome = added[2] |
| 94 | |
| 95 | // Framing probes: welcome kind 0, ciphertext kind 1, junk kind 2. |
| 96 | assertEquals(0, RustMlsCrypto.payloadKind(welcome)) |
| 97 | assertEquals(2, RustMlsCrypto.payloadKind(byteArrayOf(0, 1, 2, 3))) |
| 98 | val welcomeMagic = RustMlsCrypto.welcomeMagic() |
| 99 | assertNotNull(welcomeMagic) |
| 100 | assertTrue(welcome.copyOf(welcomeMagic.size).contentEquals(welcomeMagic)) |
| 101 | |
| 102 | val joined = RustMlsCrypto.joinGroup(bStorage, welcome) |
| 103 | assertNotNull(joined) |
| 104 | var bStorageNow = joined[0] |
| 105 | assertEquals(groupId.toList(), joined[1].toList()) |
| 106 | |
| 107 | val encrypted = RustMlsCrypto.encrypt(aStorage, aId, groupId, "direct hello".toByteArray()) |
| 108 | assertNotNull(encrypted) |
| 109 | aStorage = encrypted[0] |
| 110 | val payload = encrypted[1] |
| 111 | assertEquals(1, RustMlsCrypto.payloadKind(payload)) |
| 112 | val messageMagic = RustMlsCrypto.messageMagic() |
| 113 | assertNotNull(messageMagic) |
| 114 | assertTrue(payload.copyOf(messageMagic.size).contentEquals(messageMagic)) |
| 115 | |
| 116 | val decrypted = RustMlsCrypto.decrypt(bStorageNow, groupId, payload) |
| 117 | assertNotNull(decrypted) |
| 118 | bStorageNow = decrypted[0] |
| 119 | assertEquals(1.toByte(), decrypted[1].firstOrNull()) |
| 120 | assertEquals("direct hello", decrypted[2].toString(Charsets.UTF_8)) |
| 121 | |
| 122 | // Removing leaf 1 (B) yields a commit; afterwards B cannot read new |
| 123 | // ciphertext, whether or not its crate accepts merging the removal |
| 124 | // commit (a removed member is outside the new epoch either way). |
| 125 | val removed = RustMlsCrypto.removeMembers(aStorage, aId, groupId, "1".toByteArray(Charsets.US_ASCII)) |
| 126 | assertNotNull(removed) |
| 127 | aStorage = removed[0] |
| 128 | // The raw commit lacks the message framing, so the crate may throw |
| 129 | // here rather than return null; RcsE2E.decryptFor runCatches this. |
| 130 | val merged = runCatching { RustMlsCrypto.decrypt(bStorageNow, groupId, removed[1]) }.getOrNull() |
| 131 | if (merged != null) { |
| 132 | assertEquals(0.toByte(), merged[1].firstOrNull()) |
| 133 | bStorageNow = merged[0] |
| 134 | } |
| 135 | val afterRemoval = RustMlsCrypto.encrypt(aStorage, aId, groupId, "after removal".toByteArray()) |
| 136 | assertNotNull(afterRemoval) |
| 137 | assertNull(runCatching { RustMlsCrypto.decrypt(bStorageNow, groupId, afterRemoval[1]) }.getOrNull()) |
| 138 | |
| 139 | // Garbage inputs fail closed (null, or a native error the Kotlin |
| 140 | // layer converts to null). |
| 141 | assertNull(runCatching { RustMlsCrypto.joinGroup(ByteArray(0), byteArrayOf(1, 2, 3)) }.getOrNull()) |
| 142 | assertNull(runCatching { RustMlsCrypto.decrypt(bStorageNow, groupId, byteArrayOf(4, 5, 6)) }.getOrNull()) |
| 143 | assertNull(runCatching { RustMlsCrypto.encrypt(ByteArray(0), aId, groupId, byteArrayOf(1)) }.getOrNull()) |
| 144 | } |
| 145 | |
| 146 | @Test fun threeDeviceGroupCreateJoinDecryptAndRemove() = runTest { |
| 147 | if (!RustMlsCrypto.isAvailable) return@runTest |
| 148 | val meA = "+15550002001" |
| 149 | val meB = "+15550002002" |
| 150 | val meC = "+15550002003" |
| 151 | val groupConv = "group-pass3" |
| 152 | |
| 153 | val kpB = onDevice(deviceB) { |
| 154 | RcsE2E.identityFor(context, meB) |
| 155 | RcsE2E.freshKeyPackage(context, meB) |
| 156 | } |
| 157 | val kpC = onDevice(deviceC) { |
| 158 | RcsE2E.identityFor(context, meC) |
| 159 | RcsE2E.freshKeyPackage(context, meC) |
| 160 | } |
| 161 | assertNotNull(kpB); assertNotNull(kpC) |
| 162 | |
| 163 | val created = RcsE2E.createEncryptedGroup(context, meA, groupConv, listOf(kpB, kpC)) |
| 164 | assertNotNull(created) |
| 165 | val (_, welcome) = created |
| 166 | |
| 167 | val gidB = onDevice(deviceB) { RcsE2E.joinEncryptedGroup(context, groupConv, welcome) } |
| 168 | val gidC = onDevice(deviceC) { RcsE2E.joinEncryptedGroup(context, groupConv, welcome) } |
| 169 | assertNotNull(gidB); assertNotNull(gidC) |
| 170 | assertEquals(gidB.toList(), gidC.toList()) |
| 171 | |
| 172 | // A broadcasts; both joiners read it. |
| 173 | val payload = RcsE2E.encryptTo(context, meA, groupConv, "hi all".toByteArray()) |
| 174 | assertNotNull(payload) |
| 175 | val readB = onDevice(deviceB) { RcsE2E.decryptFor(context, groupConv, payload) } |
| 176 | val readC = onDevice(deviceC) { RcsE2E.decryptFor(context, groupConv, payload) } |
| 177 | assertEquals("hi all", readB?.first?.toString(Charsets.UTF_8)) |
| 178 | assertEquals("hi all", readC?.first?.toString(Charsets.UTF_8)) |
| 179 | |
| 180 | // Removal through the E.164 map on a second tracked group: no |
| 181 | // transport, so the commit cannot be delivered and the call reports |
| 182 | // false, but the member map is still pruned locally. Fresh key |
| 183 | // packages: the ones above were consumed by the first group. |
| 184 | val kpB2 = onDevice(deviceB) { RcsE2E.freshKeyPackage(context, meB) } |
| 185 | val kpC2 = onDevice(deviceC) { RcsE2E.freshKeyPackage(context, meC) } |
| 186 | assertNotNull(kpB2); assertNotNull(kpC2) |
| 187 | val trackedConv = "group-pass3-tracked" |
| 188 | RcsE2E.setupEncryptedGroup(context, meA, trackedConv, mapOf(meB to kpB2, meC to kpC2)) |
| 189 | assertEquals(1, RcsE2E.leafIndexFor(context, trackedConv, meB) ?: -1) |
| 190 | assertNotNull(RcsE2E.leafIndexFor(context, trackedConv, meC)) |
| 191 | val removed = RcsE2E.removeMembersByE164(context, meA, trackedConv, listOf(meC, "+15559999999")) |
| 192 | assertFalse(removed) // no transport seam installed in this test |
| 193 | assertNull(RcsE2E.leafIndexFor(context, trackedConv, meC)) |
| 194 | assertNotNull(RcsE2E.leafIndexFor(context, trackedConv, meB)) |
| 195 | // Removing nobody / unknown groups fails closed. |
| 196 | assertFalse(RcsE2E.removeMembersByE164(context, meA, trackedConv, emptyList())) |
| 197 | assertFalse(RcsE2E.removeMembers(context, meA, "no-group", listOf(1))) |
| 198 | assertFalse(RcsE2E.removeMembers(context, meA, trackedConv, emptyList())) |
| 199 | } |
| 200 | |
| 201 | @Test fun transportMediatedSetupPublishesAndWelcomes() = runTest { |
| 202 | if (!RustMlsCrypto.isAvailable) return@runTest |
| 203 | RcsSipTransport.fakeSenderForTest = { startLine, headers, body -> |
| 204 | sent += Triple(startLine, headers, body.copyOf()) |
| 205 | true |
| 206 | } |
| 207 | val meA = "+15550002001" |
| 208 | val meB = "+15550002002" |
| 209 | |
| 210 | // Publishing our key package reaches the fake transport and is cached. |
| 211 | assertTrue(RcsKeyDirectory.publishTo(context, meA, meB)) |
| 212 | assertNotNull(RcsKeyDirectory.lastPublishedFor(meA)) |
| 213 | assertTrue(sent.any { it.second.contains(RcsE2E.CT_KEY_PACKAGE) || String(it.third).contains(RcsE2E.CT_KEY_PACKAGE) }) |
| 214 | |
| 215 | // Key-package request flows also succeed with a sender installed. |
| 216 | assertTrue(RcsE2E.requestKeyPackage(meB)) |
| 217 | assertTrue(RcsE2E.requestEncryptedChat(context, meA, meB)) |
| 218 | |
| 219 | // B's package arrives (cached in memory, as the inbound route does), |
| 220 | // then a full 1:1 setup sends Welcome + commit through the transport. |
| 221 | val kpB = onDevice(deviceB) { |
| 222 | RcsE2E.identityFor(context, meB) |
| 223 | RcsE2E.freshKeyPackage(context, meB) |
| 224 | } |
| 225 | assertNotNull(kpB) |
| 226 | val ok = RcsE2E.setupGroupWithPeer(context, meA, meB, meB, kpB) |
| 227 | assertTrue(ok) |
| 228 | assertNotNull(RcsE2E.groupIdFor(context, meB)) |
| 229 | assertNotNull(RcsE2E.leafIndexFor(context, meB, meB)) |
| 230 | val bodies = sent.map { String(it.third) } |
| 231 | assertTrue(bodies.any { it.contains(RcsE2E.CT_WELCOME) }, "sent kinds: ${sent.size}") |
| 232 | assertTrue(bodies.any { it.contains(RcsE2E.CT_COMMIT) }) |
| 233 | } |
| 234 | |
| 235 | @Test fun transportRefusalPropagatesAsFalse() = runTest { |
| 236 | if (!RustMlsCrypto.isAvailable) return@runTest |
| 237 | RcsSipTransport.fakeSenderForTest = { _, _, _ -> false } |
| 238 | val meA = "+15550002001" |
| 239 | val meB = "+15550002002" |
| 240 | val kpB = onDevice(deviceB) { |
| 241 | RcsE2E.identityFor(context, meB) |
| 242 | RcsE2E.freshKeyPackage(context, meB) |
| 243 | } |
| 244 | assertNotNull(kpB) |
| 245 | // The group is created locally, but every send is refused. |
| 246 | assertFalse(RcsE2E.setupGroupWithPeer(context, meA, meB, meB, kpB)) |
| 247 | assertNotNull(RcsE2E.groupIdFor(context, meB)) |
| 248 | assertFalse(RcsKeyDirectory.publishTo(context, meA, meB)) |
| 249 | } |
| 250 | } |