communicate: build JNI bridges for host tests, fix MLS join (#tests)
- Commit
6a20b59789ae6c762e363bb08573fc465c03e143- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:16 UTC (21 days ago)
- Parents
36f1234
Two product bugs kept every Pass-3 MLS/Signal host test red: 1. Both Rust crates gated mod jni_bridge to target_os=android, so the host cdylib that JVM tests load (build/rustHostLibs) loaded fine (isAvailable=true) but exported no Java_* symbols: every native call threw UnsatisfiedLinkError, and the E2EE helpers failed closed to null. Build the bridge on host too, like every other module's jni_bindings. 2. RcsE2E.joinEncryptedGroup read MLS storage only from the new conversation's row, but the Welcome's key-package private key lives in the pending snapshot freshKeyPackage stashes under our own 1:1 thread, so a first join always had empty storage and failed. Fall back to the pending snapshot (new DAO query getPending) when the conversation has no row yet. WelcomeJoin's service-callback test also pumps the paused Robolectric main looper while polling for the async cache write. With natives live, RcsSlice3Pass2E2ETest's round trip also surfaced: modelling creator+joiner in one Room DB makes the join replace the creator's row (one row per group id). The test now uses two databases, like the Pass-3 tests. Verified: targeted MlsLifecycle/RustSignal/WelcomeJoin testDevUnitTest runs green (12/12); full :communicate:testDevUnitTest green.
Diff
communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.ktindex 155deb19a6..81b40b84dd 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt@@ -291,6 +291,10 @@ interface RcsMlsGroupDao { @Query("SELECT * FROM rcs_mls_group WHERE conversationId = :conversationId LIMIT 1") suspend fun getByConversation(conversationId: String): RcsMlsGroup? + /** The pending key-package snapshot (see RcsE2E.freshKeyPackage). */+ @Query("SELECT * FROM rcs_mls_group WHERE groupIdHex LIKE 'pending:%' ORDER BY updatedAt DESC LIMIT 1")+ suspend fun getPending(): RcsMlsGroup?+ @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun upsert(group: RcsMlsGroup)
communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.ktindex 4da0facf36..d115623f07 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt@@ -364,7 +364,13 @@ object RcsE2E { ): ByteArray? = withContext(Dispatchers.IO) { if (!RcsFeature.enabled || !RustMlsCrypto.isAvailable) return@withContext null val db = RcsDatabase.getDatabase(context)- val storage = db.mlsGroupDao().getByConversation(conversationId)?.storage ?: ByteArray(0)+ // The Welcome's key-package private key lives in the pending+ // snapshot freshKeyPackage stashed under our own 1:1 thread, not+ // under the (new) conversation's row: without it the native join+ // has no key material and always fails for a first group.+ val storage = db.mlsGroupDao().getByConversation(conversationId)?.storage+ ?: db.mlsGroupDao().getPending()?.storage+ ?: ByteArray(0) runCatching { val out = RustMlsCrypto.joinGroup(storage, welcome) ?: return@runCatching null val storageOut = out.getOrNull(0) ?: storage
communicate/src/main/rust-mls/src/lib.rs
diff --git a/communicate/src/main/rust-mls/src/lib.rs b/communicate/src/main/rust-mls/src/lib.rsindex 7415a91b28..9ec5493b72 100644--- a/communicate/src/main/rust-mls/src/lib.rs+++ b/communicate/src/main/rust-mls/src/lib.rs@@ -15,7 +15,11 @@ pub mod keys; pub mod message; pub mod store; -#[cfg(target_os = "android")]+// JNI bridge must also build for the host: JVM unit tests load the host+// cdylib (build/rustHostLibs) and call the same Java_* entry points. Gating+// this to Android left the host .so loadable but symbol-less, so+// System.loadLibrary succeeded while every native call threw+// UnsatisfiedLinkError under Robolectric. #[allow(unsafe_code)] mod jni_bridge;
communicate/src/main/rust/src/lib.rs
diff --git a/communicate/src/main/rust/src/lib.rs b/communicate/src/main/rust/src/lib.rsindex 92de3d590f..b1e5ac005c 100644--- a/communicate/src/main/rust/src/lib.rs+++ b/communicate/src/main/rust/src/lib.rs@@ -22,7 +22,11 @@ pub mod session; pub mod wire; pub mod signal; -#[cfg(target_os = "android")]+// JNI bridge must also build for the host: JVM unit tests load the host+// cdylib (build/rustHostLibs) and call the same Java_* entry points. Gating+// this to Android left the host .so loadable but symbol-less, so+// System.loadLibrary succeeded while every native call threw+// UnsatisfiedLinkError under Robolectric. mod jni_bridge; #[cfg(test)]
communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt b/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.ktindex c6063f5f2a..46443db399 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt@@ -40,13 +40,27 @@ class RcsSlice3Pass2E2ETest { private val context: Context get() = ApplicationProvider.getApplicationContext() private lateinit var testDb: RcsDatabase+ private lateinit var deviceB: RcsDatabase - @BeforeTest- fun setUp() {- testDb = androidx.room3.Room.inMemoryDatabaseBuilder(context, RcsDatabase::class.java)+ private fun newDb(): RcsDatabase =+ androidx.room3.Room.inMemoryDatabaseBuilder(context, RcsDatabase::class.java) .setDriver(androidx.sqlite.driver.bundled.BundledSQLiteDriver()) .allowMainThreadQueries() .build()++ private suspend fun <T> onDb(db: RcsDatabase, block: suspend () -> T): T {+ RcsDatabase.overrideForTest = db+ try {+ return block()+ } finally {+ RcsDatabase.overrideForTest = testDb+ }+ }++ @BeforeTest+ fun setUp() {+ testDb = newDb()+ deviceB = newDb() RcsDatabase.overrideForTest = testDb RcsPeerKeys.clear() }@@ -55,6 +69,7 @@ class RcsSlice3Pass2E2ETest { fun tearDown() { RcsDatabase.overrideForTest = null testDb.close()+ deviceB.close() RcsPeerKeys.clear() } @@ -201,12 +216,18 @@ class RcsSlice3Pass2E2ETest { assertNotNull(row) assertEquals(identity.toList(), row.identity.toList()) - // Key packages for both sides.+ // Key packages for both sides. The peer lives on a second+ // database: one Room row per group id cannot hold both the+ // creator's and the joiner's snapshot of the same group, so+ // modelling both devices in one DB (as this test used to)+ // makes the join replace the creator's row. val myKp = RcsE2E.freshKeyPackage(context, me) assertNotNull(myKp)- val peerIdentity = RcsE2E.identityFor(context, peer)- assertNotNull(peerIdentity)- val peerKp = RcsE2E.freshKeyPackage(context, peer)+ val peerKp = onDb(deviceB) {+ val peerIdentity = RcsE2E.identityFor(context, peer)+ assertNotNull(peerIdentity)+ RcsE2E.freshKeyPackage(context, peer)+ } assertNotNull(peerKp) // Create a group adding the peer, then join from the Welcome.@@ -214,15 +235,15 @@ class RcsSlice3Pass2E2ETest { assertNotNull(created) val (commit, welcome) = created assertTrue(commit.isNotEmpty() && welcome.isNotEmpty())- val groupId = RcsE2E.joinEncryptedGroup(context, "conv-mls-2", welcome)+ val groupId = onDb(deviceB) { RcsE2E.joinEncryptedGroup(context, "conv-mls-2", welcome) } assertNotNull(groupId) assertEquals(groupId.toList(), RcsE2E.groupIdFor(context, "conv-mls-1")?.toList())- assertEquals(groupId.toList(), RcsE2E.groupIdFor(context, "conv-mls-2")?.toList())+ assertEquals(groupId.toList(), onDb(deviceB) { RcsE2E.groupIdFor(context, "conv-mls-2") }?.toList()) // Encrypt on one side, decrypt on the joined side. val payload = RcsE2E.encryptTo(context, me, "conv-mls-1", "secret hello".toByteArray(Charsets.UTF_8)) assertNotNull(payload)- val decrypted = RcsE2E.decryptFor(context, "conv-mls-2", payload)+ val decrypted = onDb(deviceB) { RcsE2E.decryptFor(context, "conv-mls-2", payload) } assertNotNull(decrypted) assertEquals("secret hello", decrypted.first.toString(Charsets.UTF_8)) assertTrue(decrypted.second)
communicate/src/test/java/com/vayunmathur/communicate/telephony/WelcomeJoinRcsPass3Test.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/telephony/WelcomeJoinRcsPass3Test.kt b/communicate/src/test/java/com/vayunmathur/communicate/telephony/WelcomeJoinRcsPass3Test.ktindex 8e08b6e122..a18b50d5bb 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/telephony/WelcomeJoinRcsPass3Test.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/telephony/WelcomeJoinRcsPass3Test.kt@@ -312,6 +312,8 @@ class WelcomeJoinRcsPass3Test { val dbDeadline = System.currentTimeMillis() + 15_000 var bodies = emptyList<String>() while (System.currentTimeMillis() < dbDeadline) {+ // The service caches on its main-looper scope; pump it.+ org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() bodies = deviceB.cachedMessageDao().getForConversation(meA).map { it.body } if ("cached secret" in bodies) break Thread.sleep(25)