| 1 | #![cfg(windows)] |
| 2 | extern crate libloading; |
| 3 | use libloading::os::windows::*; |
| 4 | use std::ffi::CStr; |
| 5 | use std::os::raw::c_void; |
| 6 | // The ordinal DLL contains exactly one function (other than DllMain, that is) with ordinal number |
| 7 | // 1. This function has the sugnature `fn() -> *const c_char` and returns a string "bunny\0" (in |
| 8 | // reference to WindowsBunny). |
| 9 | // |
| 10 | // Both x86_64 and x86 versions of the .dll are functionally the same. Ideally we would compile the |
| 11 | // dlls with well known ordinals from our own testing helpers library, but rustc does not allow |
| 12 | // specifying a custom .def file (https://github.com/rust-lang/rust/issues/35089) |
| 13 | // |
| 14 | // The DLLs were kindly compiled by WindowsBunny (aka. @retep998). |
| 15 | |
| 16 | #[cfg(target_arch = "x86")] |
| 17 | fn load_ordinal_lib() -> Library { |
| 18 | unsafe { Library::new("tests/nagisa32.dll").expect("nagisa32.dll") } |
| 19 | } |
| 20 | |
| 21 | #[cfg(target_arch = "x86_64")] |
| 22 | fn load_ordinal_lib() -> Library { |
| 23 | unsafe { Library::new("tests/nagisa64.dll").expect("nagisa64.dll") } |
| 24 | } |
| 25 | |
| 26 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 27 | #[test] |
| 28 | fn test_ordinal() { |
| 29 | let lib = load_ordinal_lib(); |
| 30 | unsafe { |
| 31 | let windows: Symbol<unsafe fn() -> *const i8> = lib.get_ordinal(1).expect("function"); |
| 32 | assert_eq!(CStr::from_ptr(windows()).to_bytes(), b"bunny"); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 37 | #[test] |
| 38 | fn test_try_into_ptr() { |
| 39 | let lib = load_ordinal_lib(); |
| 40 | unsafe { |
| 41 | let windows: Symbol<unsafe fn() -> *const i8> = lib.get_ordinal(1).expect("function"); |
| 42 | let ptr: *mut c_void = windows.as_raw_ptr(); |
| 43 | assert!(!ptr.is_null()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 48 | #[test] |
| 49 | fn test_ordinal_missing_fails() { |
| 50 | let lib = load_ordinal_lib(); |
| 51 | unsafe { |
| 52 | let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(2); |
| 53 | r.err().unwrap(); |
| 54 | let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(!0); |
| 55 | r.err().unwrap(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn test_new_kernel23() { |
| 61 | unsafe { |
| 62 | Library::new("kernel23").err().unwrap(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn test_new_kernel32_no_ext() { |
| 68 | unsafe { |
| 69 | Library::new("kernel32").unwrap(); |
| 70 | } |
| 71 | } |