communicate: compile the Rust JNI bridges for the host too
- Commit
058dd73b2c4c1095a1d3536a38202ccbe8098a3f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 11:28 UTC (29 days ago)
- Parents
33a42ed
`mod jni_bridge` in communicate_signal and communicate_mls was gated on target_os = "android", so the host cdylibs that cargoBuildHost_* builds for JVM tests loaded fine but exported no Java_* symbols: every RustWhatsAppCrypto / RustSignalCrypto / RustMlsCrypto call threw UnsatisfiedLinkError, and RCS/Signal/ WhatsApp E2E success paths were untestable on the JVM. Both crates already depend on jni unconditionally and the bridges use only jni::sys types. Host libs now export 19 + 11 JNI functions (0 before, checked in an isolated target dir). HostNativeCryptoTest runs a real X25519 agreement and an MLS identity generation through them under Robolectric.
Diff
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..450dc34bd0 100644--- a/communicate/src/main/rust-mls/src/lib.rs+++ b/communicate/src/main/rust-mls/src/lib.rs@@ -15,7 +15,7 @@ pub mod keys; pub mod message; pub mod store; -#[cfg(target_os = "android")]+// Compiled for the host too (see ../rust/src/lib.rs): JVM tests load the host cdylib. #[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..b9ae66bdc2 100644--- a/communicate/src/main/rust/src/lib.rs+++ b/communicate/src/main/rust/src/lib.rs@@ -22,7 +22,8 @@ pub mod session; pub mod wire; pub mod signal; -#[cfg(target_os = "android")]+// Compiled for the host too, so JVM unit tests can call the real crypto through+// `cargoBuildHost_communicate_signal` instead of hitting UnsatisfiedLinkError. mod jni_bridge; #[cfg(test)]
communicate/src/test/java/com/vayunmathur/communicate/data/HostNativeCryptoTest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/data/HostNativeCryptoTest.kt b/communicate/src/test/java/com/vayunmathur/communicate/data/HostNativeCryptoTest.ktnew file mode 100644index 0000000000..b30db71cd5--- /dev/null+++ b/communicate/src/test/java/com/vayunmathur/communicate/data/HostNativeCryptoTest.kt@@ -0,0 +1,41 @@+package com.vayunmathur.communicate.data++import com.vayunmathur.communicate.data.rcs.e2e.RustMlsCrypto+import com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto+import androidx.test.ext.junit.runners.AndroidJUnit4+import org.junit.Test+import org.junit.runner.RunWith+import kotlin.test.assertContentEquals+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++/**+ * The host cdylibs export the JNI bridges, so JVM tests exercise the real Rust crypto.+ * Robolectric: the loaders log through android.util.Log.+ */+@RunWith(AndroidJUnit4::class)+class HostNativeCryptoTest {++ @Test+ fun whatsAppX25519AgreementMatchesFromBothSides() {+ assertTrue(RustWhatsAppCrypto.isAvailable)+ val alice = assertNotNull(RustWhatsAppCrypto.generateKeyPair())+ val bob = assertNotNull(RustWhatsAppCrypto.generateKeyPair())+ assertEquals(64, alice.size)+ val (alicePriv, alicePub) = alice.copyOfRange(0, 32) to alice.copyOfRange(32, 64)+ val (bobPriv, bobPub) = bob.copyOfRange(0, 32) to bob.copyOfRange(32, 64)++ assertContentEquals(alicePub, RustWhatsAppCrypto.publicFromPrivate(alicePriv))+ val shared = assertNotNull(RustWhatsAppCrypto.x25519Agreement(alicePriv, bobPub))+ assertContentEquals(shared, RustWhatsAppCrypto.x25519Agreement(bobPriv, alicePub))+ }++ @Test+ fun mlsGeneratesAnIdentity() {+ assertTrue(RustMlsCrypto.isAvailable)+ val out = assertNotNull(RustMlsCrypto.generateIdentity("+15550100".encodeToByteArray()))+ assertEquals(2, out.size)+ assertTrue(out[1].isNotEmpty())+ }+}