|
| 1 | +uniffi_callbacks::uniffi_reexport_scaffolding!(); |
| 2 | +uniffi_coverall::uniffi_reexport_scaffolding!(); |
| 3 | + |
| 4 | +#[cfg(test)] |
| 5 | +mod tests { |
| 6 | + use cargo_metadata::Message; |
| 7 | + use libloading::{Library, Symbol}; |
| 8 | + use std::ffi::CString; |
| 9 | + use std::os::raw::c_void; |
| 10 | + use std::process::{Command, Stdio}; |
| 11 | + use std::str::FromStr; |
| 12 | + use uniffi::{FfiConverter, ForeignCallback, RustBuffer, RustCallStatus}; |
| 13 | + use uniffi_bindgen::ComponentInterface; |
| 14 | + |
| 15 | + // Load the dynamic library that was built for this crate. The external functions from |
| 16 | + // `uniffi_callbacks' and `uniffi_coverall` should be present. |
| 17 | + pub fn load_library() -> Library { |
| 18 | + let mut cmd = Command::new("cargo"); |
| 19 | + cmd.arg("build").arg("--message-format=json").arg("--lib"); |
| 20 | + cmd.stdout(Stdio::piped()); |
| 21 | + let mut child = cmd.spawn().unwrap(); |
| 22 | + let output = std::io::BufReader::new(child.stdout.take().unwrap()); |
| 23 | + let artifacts = Message::parse_stream(output) |
| 24 | + .filter_map(|message| match message { |
| 25 | + Err(e) => panic!("{}", e), |
| 26 | + Ok(Message::CompilerArtifact(artifact)) => { |
| 27 | + if artifact.target.name == "library" |
| 28 | + && artifact.target.kind.iter().any(|item| item == "cdylib") |
| 29 | + { |
| 30 | + Some(artifact) |
| 31 | + } else { |
| 32 | + None |
| 33 | + } |
| 34 | + } |
| 35 | + _ => None, |
| 36 | + }) |
| 37 | + .collect::<Vec<_>>(); |
| 38 | + if !child.wait().unwrap().success() { |
| 39 | + panic!("Failed to execute `cargo build`"); |
| 40 | + } |
| 41 | + let artifact = match artifacts.len() { |
| 42 | + 1 => &artifacts[0], |
| 43 | + n => panic!("Found {} artfiacts from cargo build", n), |
| 44 | + }; |
| 45 | + let cdylib_files: Vec<_> = artifact |
| 46 | + .filenames |
| 47 | + .iter() |
| 48 | + .filter(|nm| matches!(nm.extension(), Some(std::env::consts::DLL_EXTENSION))) |
| 49 | + .collect(); |
| 50 | + let library_path = match cdylib_files.len() { |
| 51 | + 1 => cdylib_files[0].to_string(), |
| 52 | + _ => panic!("Failed to build exactly one cdylib file"), |
| 53 | + }; |
| 54 | + unsafe { Library::new(library_path).unwrap() } |
| 55 | + } |
| 56 | + |
| 57 | + pub fn has_symbol<T>(library: &Library, name: &str) -> bool { |
| 58 | + unsafe { |
| 59 | + library |
| 60 | + .get::<T>(CString::new(name).unwrap().as_bytes_with_nul()) |
| 61 | + .is_ok() |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub fn get_symbol<'lib, T>(library: &'lib Library, name: &str) -> Symbol<'lib, T> { |
| 66 | + unsafe { |
| 67 | + library |
| 68 | + .get::<T>(CString::new(name).unwrap().as_bytes_with_nul()) |
| 69 | + .unwrap() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_symbols_present() { |
| 75 | + let library = load_library(); |
| 76 | + let coveralls_ci = |
| 77 | + ComponentInterface::from_str(include_str!("../../coverall/src/coverall.udl")).unwrap(); |
| 78 | + let callbacks_ci = |
| 79 | + ComponentInterface::from_str(include_str!("../../callbacks/src/callbacks.udl")) |
| 80 | + .unwrap(); |
| 81 | + |
| 82 | + // UniFFI internal function |
| 83 | + assert!(has_symbol::< |
| 84 | + unsafe extern "C" fn(i32, &mut RustCallStatus) -> RustBuffer, |
| 85 | + >( |
| 86 | + &library, coveralls_ci.ffi_rustbuffer_alloc().name() |
| 87 | + )); |
| 88 | + |
| 89 | + // Top-level function |
| 90 | + assert!( |
| 91 | + has_symbol::<unsafe extern "C" fn(&mut RustCallStatus) -> u64>( |
| 92 | + &library, |
| 93 | + coveralls_ci |
| 94 | + .get_function_definition("get_num_alive") |
| 95 | + .unwrap() |
| 96 | + .ffi_func() |
| 97 | + .name() |
| 98 | + ) |
| 99 | + ); |
| 100 | + |
| 101 | + // Object method |
| 102 | + assert!( |
| 103 | + has_symbol::<unsafe extern "C" fn(&mut RustCallStatus) -> u64>( |
| 104 | + &library, |
| 105 | + coveralls_ci |
| 106 | + .get_object_definition("Coveralls") |
| 107 | + .unwrap() |
| 108 | + .get_method("get_name") |
| 109 | + .ffi_func() |
| 110 | + .name() |
| 111 | + ) |
| 112 | + ); |
| 113 | + |
| 114 | + // Callback init func |
| 115 | + assert!(has_symbol::< |
| 116 | + unsafe extern "C" fn(ForeignCallback, &mut RustCallStatus) -> (), |
| 117 | + >( |
| 118 | + &library, |
| 119 | + callbacks_ci |
| 120 | + .get_callback_interface_definition("ForeignGetters") |
| 121 | + .unwrap() |
| 122 | + .ffi_init_callback() |
| 123 | + .name() |
| 124 | + )); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_calls() { |
| 129 | + let mut call_status = RustCallStatus::default(); |
| 130 | + let library = load_library(); |
| 131 | + let coveralls_ci = |
| 132 | + ComponentInterface::from_str(include_str!("../../coverall/src/coverall.udl")).unwrap(); |
| 133 | + let object_def = coveralls_ci.get_object_definition("Coveralls").unwrap(); |
| 134 | + |
| 135 | + let get_num_alive: Symbol<unsafe extern "C" fn(&mut RustCallStatus) -> u64> = get_symbol( |
| 136 | + &library, |
| 137 | + coveralls_ci |
| 138 | + .get_function_definition("get_num_alive") |
| 139 | + .unwrap() |
| 140 | + .ffi_func() |
| 141 | + .name(), |
| 142 | + ); |
| 143 | + let coveralls_new: Symbol< |
| 144 | + unsafe extern "C" fn(RustBuffer, &mut RustCallStatus) -> *const c_void, |
| 145 | + > = get_symbol( |
| 146 | + &library, |
| 147 | + object_def.primary_constructor().unwrap().ffi_func().name(), |
| 148 | + ); |
| 149 | + let coveralls_get_name: Symbol< |
| 150 | + unsafe extern "C" fn(*const c_void, &mut RustCallStatus) -> RustBuffer, |
| 151 | + > = get_symbol( |
| 152 | + &library, |
| 153 | + object_def.get_method("get_name").ffi_func().name(), |
| 154 | + ); |
| 155 | + let coveralls_free: Symbol<unsafe extern "C" fn(*const c_void, &mut RustCallStatus) -> ()> = |
| 156 | + get_symbol(&library, object_def.ffi_object_free().name()); |
| 157 | + |
| 158 | + let num_alive = unsafe { get_num_alive(&mut call_status) }; |
| 159 | + assert_eq!(call_status.code, 0); |
| 160 | + assert_eq!(num_alive, 0); |
| 161 | + |
| 162 | + let obj_id = unsafe { coveralls_new(String::lower("TestName".into()), &mut call_status) }; |
| 163 | + assert_eq!(call_status.code, 0); |
| 164 | + |
| 165 | + let name_buf = unsafe { coveralls_get_name(obj_id, &mut call_status) }; |
| 166 | + assert_eq!(call_status.code, 0); |
| 167 | + assert_eq!(String::try_lift(name_buf).unwrap(), "TestName"); |
| 168 | + |
| 169 | + let num_alive = unsafe { get_num_alive(&mut call_status) }; |
| 170 | + assert_eq!(call_status.code, 0); |
| 171 | + assert_eq!(num_alive, 1); |
| 172 | + |
| 173 | + unsafe { coveralls_free(obj_id, &mut call_status) }; |
| 174 | + assert_eq!(call_status.code, 0); |
| 175 | + |
| 176 | + let num_alive = unsafe { get_num_alive(&mut call_status) }; |
| 177 | + assert_eq!(call_status.code, 0); |
| 178 | + assert_eq!(num_alive, 0); |
| 179 | + } |
| 180 | +} |
0 commit comments