|
| 1 | +//! The ABI canary crossing test. |
| 2 | +//! |
| 3 | +//! Builds this crate's `cdylib`, loads it over `libloading` (exactly as |
| 4 | +//! `binoc-python`'s native-plugin loader does), and asserts that plugin |
| 5 | +//! description and a render round-trip across the `extern "C"` + JSON boundary. |
| 6 | +//! If the renderer ABI or its wire contract drifts, this fails to load or |
| 7 | +//! round-trip — the property the fat in-process build would otherwise give up. |
| 8 | +
|
| 9 | +use std::ffi::{CStr, CString}; |
| 10 | +use std::os::raw::c_char; |
| 11 | +use std::path::PathBuf; |
| 12 | +use std::process::Command; |
| 13 | + |
| 14 | +use binoc_sdk::plugin_abi::{PluginDescription, RenderRequest, RenderResponse}; |
| 15 | +use binoc_sdk::{Changeset, DiffNode, SDK_VERSION}; |
| 16 | + |
| 17 | +type DescribeFn = unsafe extern "C" fn() -> *mut c_char; |
| 18 | +type RenderFn = unsafe extern "C" fn(u32, *const c_char) -> *mut c_char; |
| 19 | +type FreeFn = unsafe extern "C" fn(*mut c_char); |
| 20 | + |
| 21 | +/// Build the canary `cdylib` and return the path to the built artifact. |
| 22 | +/// |
| 23 | +/// The nested `cargo build` guarantees the `cdylib` exists (a plain |
| 24 | +/// `cargo test` links the crate's rlib but may not emit the `cdylib`); it is a |
| 25 | +/// no-op when the artifact is already current. The artifact lives in the |
| 26 | +/// profile directory two levels up from the test binary |
| 27 | +/// (`<target>/<profile>/deps/<test>` → `<target>/<profile>/`). |
| 28 | +fn build_and_locate_cdylib() -> PathBuf { |
| 29 | + let status = Command::new(env!("CARGO")) |
| 30 | + .args(["build", "-p", "binoc-abi-canary"]) |
| 31 | + .status() |
| 32 | + .expect("run cargo build for the canary cdylib"); |
| 33 | + assert!( |
| 34 | + status.success(), |
| 35 | + "failed to build the binoc-abi-canary cdylib" |
| 36 | + ); |
| 37 | + |
| 38 | + let mut dir = std::env::current_exe().expect("locate the test executable"); |
| 39 | + dir.pop(); // drop the test binary file name |
| 40 | + if dir.ends_with("deps") { |
| 41 | + dir.pop(); // drop `deps`, landing in the profile directory |
| 42 | + } |
| 43 | + |
| 44 | + let file_name = format!( |
| 45 | + "{}binoc_abi_canary{}", |
| 46 | + std::env::consts::DLL_PREFIX, |
| 47 | + std::env::consts::DLL_SUFFIX |
| 48 | + ); |
| 49 | + let path = dir.join(&file_name); |
| 50 | + assert!( |
| 51 | + path.exists(), |
| 52 | + "canary cdylib not found at {} (looked for {file_name})", |
| 53 | + path.display() |
| 54 | + ); |
| 55 | + path |
| 56 | +} |
| 57 | + |
| 58 | +/// Take ownership of a C string produced by the plugin and free it via the |
| 59 | +/// plugin's own `_binoc_free_string`, mirroring the host loader's contract. |
| 60 | +unsafe fn take_owned(ptr: *mut c_char, free_fn: FreeFn) -> String { |
| 61 | + assert!(!ptr.is_null(), "plugin returned a null string"); |
| 62 | + let value = CStr::from_ptr(ptr) |
| 63 | + .to_str() |
| 64 | + .expect("plugin string is valid UTF-8") |
| 65 | + .to_string(); |
| 66 | + free_fn(ptr); |
| 67 | + value |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +fn native_renderer_crosses_the_c_abi() { |
| 72 | + let path = build_and_locate_cdylib(); |
| 73 | + |
| 74 | + unsafe { |
| 75 | + let lib = libloading::Library::new(&path).expect("dlopen the canary cdylib"); |
| 76 | + |
| 77 | + let describe_fn: DescribeFn = *lib |
| 78 | + .get::<DescribeFn>(b"_binoc_plugin_describe") |
| 79 | + .expect("_binoc_plugin_describe symbol"); |
| 80 | + let render_fn: RenderFn = *lib |
| 81 | + .get::<RenderFn>(b"_binoc_renderer_render") |
| 82 | + .expect("_binoc_renderer_render symbol"); |
| 83 | + let free_fn: FreeFn = *lib |
| 84 | + .get::<FreeFn>(b"_binoc_free_string") |
| 85 | + .expect("_binoc_free_string symbol"); |
| 86 | + |
| 87 | + // ── Description crosses, and the plugin agrees with the host SDK. ── |
| 88 | + let description_json = take_owned(describe_fn(), free_fn); |
| 89 | + let description: PluginDescription = |
| 90 | + serde_json::from_str(&description_json).expect("parse PluginDescription"); |
| 91 | + assert_eq!( |
| 92 | + description.sdk_version, SDK_VERSION, |
| 93 | + "plugin was built against a different binoc-sdk than the host" |
| 94 | + ); |
| 95 | + assert_eq!(description.renderers.len(), 1); |
| 96 | + assert_eq!(description.renderers[0].name, "canary.echo"); |
| 97 | + |
| 98 | + // ── A render round-trips over the wire. ── |
| 99 | + let node = DiffNode::new("modify", "file", "data.bin"); |
| 100 | + let request = RenderRequest { |
| 101 | + changesets: vec![Changeset::new("left", "right", Some(node))], |
| 102 | + config: serde_json::json!({ "mode": "canary" }), |
| 103 | + }; |
| 104 | + let request_json = serde_json::to_string(&request).expect("serialize RenderRequest"); |
| 105 | + let request_c = CString::new(request_json).expect("request has no interior nul"); |
| 106 | + |
| 107 | + let response_json = take_owned(render_fn(0, request_c.as_ptr()), free_fn); |
| 108 | + let response: RenderResponse = |
| 109 | + serde_json::from_str(&response_json).expect("parse RenderResponse"); |
| 110 | + |
| 111 | + match response { |
| 112 | + RenderResponse::Ok { output } => { |
| 113 | + let echoed: serde_json::Value = |
| 114 | + serde_json::from_str(&output).expect("renderer output is JSON"); |
| 115 | + assert_eq!(echoed["root_action"], "modify"); |
| 116 | + assert_eq!(echoed["changesets"], 1); |
| 117 | + assert_eq!(echoed["mode"], "canary"); |
| 118 | + } |
| 119 | + RenderResponse::Error { message } => panic!("renderer returned an error: {message}"), |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments