From 2487d27ca855a26257e5e05c99ab651f58660997 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Thu, 28 Jul 2016 15:47:34 +0300 Subject: [PATCH] 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. --- src/os/windows/mod.rs | 5 ---- tests/nagisa32.dll | Bin 0 -> 3072 bytes tests/nagisa64.dll | Bin 0 -> 2560 bytes tests/windows.rs | 56 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/nagisa32.dll create mode 100644 tests/nagisa64.dll create mode 100644 tests/windows.rs diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 7fe73b2..4b3ab01 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -212,8 +212,3 @@ fn works_getlasterror0() { assert_eq!(kernel32::GetLastError(), gle()) } } - -#[test] -fn fails_new_kernel23() { - Library::new("kernel23").err().unwrap(); -} diff --git a/tests/nagisa32.dll b/tests/nagisa32.dll new file mode 100644 index 0000000000000000000000000000000000000000..0a6218ade9c051e3ebc34c5ad4a687c44263fb33 GIT binary patch literal 3072 zcmeHHK}!Nr5S}%SvJ?*;gP!1dkvw@@qM(D3SrG+`uI{1>`ZiX@@-GC{wf|utoun>Z z`~?#91G;qUW;1X1gooe83O zpTP0eTOZPp(4TOX8EE>WxV7`%cF^ApCm=O6hBZrQHHIby5@WevKwiOE+AhX9v^*MV zQIR}wqiQ#e%%*DA>(H^e>2#XJBQS?_OK4cC!3D>JUV+prhB4`@YI(v$y_6^xdY0;T ryJoqHG2U6BSyI1<3NZZy%5Basr#}{)_$w2@f07hGjQ>-B|2Kh8N)2p9 literal 0 HcmV?d00001 diff --git a/tests/nagisa64.dll b/tests/nagisa64.dll new file mode 100644 index 0000000000000000000000000000000000000000..bacaa4b969411958776c0aad5a848114b1b7bf51 GIT binary patch literal 2560 zcmeHH&r1SP5FRzd3dLiGz-R1v35>v-2LlgAVnP&%ySkza`Zm^;%2TJnLqD$l9|qDX z@X{gw0_lIKTb*p??LJxvLXU>S&dkpDzW10n?0yHQ_YeYr74+#8V2GJ4BjVqIxUs}j z3vQCreGU!_I80Ve4&H(>b>sR;@F;%bCep_V9?cEu*eRqw9)y^hY@L_GpxE zrBBi)cLS^^>9UtAA{WhClQ^=Y!ajg;9fH*7!?DEPVVi9PV*o)!=t5}mI0f-~6%YUh zTwfK?PZk&U_5?vgs=G<1-?fV{29Wd4nkgZFYbN4sB-N>3I7k)qq?UbmmnSd)O|=c( z(E(lvL_qN55C}ud;G?=UcRo7^(s>bdpJf1^edY3XfhUl=34bL3Mz5ab`&nhPUsB?^ zeqMt~bn%zPGCy ztrnb{+_26&grgycbV_KJ(&hqVt{h2yVpx-&s&!Y0s27cDp0JO0T8z5#EZZ?*sc literal 0 HcmV?d00001 diff --git a/tests/windows.rs b/tests/windows.rs new file mode 100644 index 0000000..343aaa1 --- /dev/null +++ b/tests/windows.rs @@ -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 *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 *const i8>, _> = lib.get_ordinal(2); + r.err().unwrap(); + let r: Result *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(); +}