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