mirror of
https://github.com/openharmony/third_party_rust_libloading.git
synced 2026-07-21 00:26:36 -04:00
Add tests for ordinals
These tests include binary .dll files into the tests directory, because rustc does not allow us to export symbols through ordinal numbers on Windows, sadly. Both DLLs contain exactly 1 function (other than DllMain) each, with signature `fn() -> *const c_char` returning a pointer to "bunny\0". Thanks to WindowsBunny (aka. retep998) for building the DLLs.
This commit is contained in:
@@ -212,8 +212,3 @@ fn works_getlasterror0() {
|
||||
assert_eq!(kernel32::GetLastError(), gle())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fails_new_kernel23() {
|
||||
Library::new("kernel23").err().unwrap();
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,56 @@
|
||||
#![cfg(windows)]
|
||||
extern crate libloading;
|
||||
use libloading::os::windows::*;
|
||||
use std::ffi::CStr;
|
||||
|
||||
// The ordinal DLL contains exactly one function (other than DllMain, that is) with ordinal number
|
||||
// 1. This function has the sugnature `fn() -> *const c_char` and returns a string "bunny\0" (in
|
||||
// reference to WindowsBunny).
|
||||
//
|
||||
// Both x86_64 and x86 versions of the .dll are functionally the same. Ideally we would compile the
|
||||
// dlls with well known ordinals from our own testing helpers library, but rustc does not allow
|
||||
// specifying a custom .def file (https://github.com/rust-lang/rust/issues/35089)
|
||||
//
|
||||
// The DLLs were kindly compiled by WindowsBunny (aka. @retep998).
|
||||
|
||||
#[cfg(target_arch="x86")]
|
||||
fn load_ordinal_lib() -> Library {
|
||||
Library::new("tests/nagisa32.dll").expect("nagisa32.dll")
|
||||
}
|
||||
|
||||
#[cfg(target_arch="x86_64")]
|
||||
fn load_ordinal_lib() -> Library {
|
||||
Library::new("tests/nagisa64.dll").expect("nagisa64.dll")
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
|
||||
#[test]
|
||||
fn test_ordinal() {
|
||||
let lib = load_ordinal_lib();
|
||||
unsafe {
|
||||
let windows: Symbol<unsafe fn() -> *const i8> = lib.get_ordinal(1).expect("function");
|
||||
assert_eq!(CStr::from_ptr(windows()).to_bytes(), b"bunny");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
|
||||
#[test]
|
||||
fn test_ordinal_missing_fails() {
|
||||
let lib = load_ordinal_lib();
|
||||
unsafe {
|
||||
let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(2);
|
||||
r.err().unwrap();
|
||||
let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(!0);
|
||||
r.err().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_kernel23() {
|
||||
Library::new("kernel23").err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_kernel32_no_ext() {
|
||||
Library::new("kernel32").unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user