From d34b971314199fc73cee5a5fb830bc6a732fb629 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Mon, 26 Feb 2024 17:38:42 +0100 Subject: [PATCH] Execute UniFFI bindings via a simple Python test (#87) This uses the UniFFI test helpers to make the `libmls_rs_uniffi.so` file available to the Python script. I would have liked to use `$CARGO_TARGET_TMPDIR` to place the temporary files in a stable place under `target/`, but this variable is only set for integration tests, not unit tests. So the script now ends up somewhere in your system temp directory. We could decide to only use integration tests for these tests, let me know what you think! The test is very simple here: it simply executes the Python script, which then has to return with a zero exit code. If it fails, the output is printed and you then have to debug the Python code by hand. Related to #81. --- mls-rs-uniffi/Cargo.toml | 4 ++++ mls-rs-uniffi/src/lib.rs | 20 ++++++++++++++++++++ mls-rs-uniffi/src/test_utils.rs | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 mls-rs-uniffi/src/test_utils.rs diff --git a/mls-rs-uniffi/Cargo.toml b/mls-rs-uniffi/Cargo.toml index e7fb4919..45005ad8 100644 --- a/mls-rs-uniffi/Cargo.toml +++ b/mls-rs-uniffi/Cargo.toml @@ -24,3 +24,7 @@ uniffi = "0.26.0" [target.'cfg(mls_build_async)'.dependencies] tokio = { version = "1.36.0", features = ["sync"] } + +[dev-dependencies] +tempfile = "3.10.0" +uniffi_bindgen = "0.26.0" diff --git a/mls-rs-uniffi/src/lib.rs b/mls-rs-uniffi/src/lib.rs index c7221750..48eca2a0 100644 --- a/mls-rs-uniffi/src/lib.rs +++ b/mls-rs-uniffi/src/lib.rs @@ -17,6 +17,9 @@ //! //! [UniFFI]: https://mozilla.github.io/uniffi-rs/ +#[cfg(test)] +pub mod test_utils; + use std::sync::Arc; #[cfg(not(mls_build_async))] @@ -572,3 +575,20 @@ impl Group { } } } + +#[cfg(all(test, not(mls_build_async)))] +mod sync_tests { + use crate::test_utils::run_python; + + #[test] + fn test_generate_signature_keypair() -> Result<(), Box> { + run_python( + r#" +from mls_rs_uniffi import CipherSuite, generate_signature_keypair + +signature_keypair = generate_signature_keypair(CipherSuite.CURVE25519_AES128) +assert signature_keypair.cipher_suite == CipherSuite.CURVE25519_AES128 +"#, + ) + } +} diff --git a/mls-rs-uniffi/src/test_utils.rs b/mls-rs-uniffi/src/test_utils.rs new file mode 100644 index 00000000..904443e7 --- /dev/null +++ b/mls-rs-uniffi/src/test_utils.rs @@ -0,0 +1,21 @@ +use uniffi_bindgen::bindings::python; + +/// Run Python code in `script`. +/// +/// The script can use `import mls_rs_uniffi` to get access to the +/// Python bindings. +pub fn run_python(script: &str) -> Result<(), Box> { + let tmp_dir = tempfile::TempDir::with_prefix("run-python-")?; + let script_path = tmp_dir.path().join("script.py"); + std::fs::write(&script_path, script)?; + + python::run_script( + tmp_dir.path().to_str().unwrap(), + "mls-rs-uniffi", + script_path.to_str().unwrap(), + vec![], + &uniffi_bindgen::bindings::RunScriptOptions::default(), + )?; + + Ok(()) +}