Skip to content

Commit

Permalink
Execute UniFFI bindings via a simple Python test (#87)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mgeisler authored Feb 26, 2024
1 parent fd94672 commit d34b971
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mls-rs-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
20 changes: 20 additions & 0 deletions mls-rs-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
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
"#,
)
}
}
21 changes: 21 additions & 0 deletions mls-rs-uniffi/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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(())
}

0 comments on commit d34b971

Please sign in to comment.