4 extern crate libloading
;
5 use libloading
::{Symbol, Library}
;
7 const LIBPATH
: &'
static str = concat
!(env
!("OUT_DIR"), "/libtest_helpers.module");
10 static ONCE
: ::std
::sync
::Once
= ::std
::sync
::Once
::new();
12 let rustc
= option_env
!("RUSTC").unwrap_or_else(|| { "rustc".into() }
);
13 let mut cmd
= ::std
::process
::Command
::new(rustc
);
15 .arg("src/test_helpers.rs")
19 .arg(env
!("LIBLOADING_TEST_TARGET"))
24 .expect("could not compile the test helpers!");
31 let lib
= Library
::new(LIBPATH
).unwrap();
33 let f
: Symbol
<unsafe extern fn(u32) -> u32> = lib
.get(b
"test_identity_u32\0").unwrap();
34 assert_eq
!(42, f(42));
39 #[derive(Clone,Copy,PartialEq,Debug)]
50 let lib
= Library
::new(LIBPATH
).unwrap();
52 let f
: Symbol
<unsafe extern fn(S
) -> S
> = lib
.get(b
"test_identity_struct\0").unwrap();
53 assert_eq
!(S { a: 1, b: 2, c: 3, d: 4 }
, f(S { a: 1, b: 2, c: 3, d: 4 }
));
60 let lib
= Library
::new(LIBPATH
).unwrap();
62 let f
: Symbol
<unsafe extern fn(S
) -> S
> = lib
.get(b
"test_identity_struct\0").unwrap();
63 let f2
: Symbol
<unsafe extern fn(S
) -> S
> = lib
.get(b
"test_identity_struct").unwrap();
69 fn wrong_name_fails() {
70 Library
::new(concat
!(env
!("OUT_DIR"), "/libtest_help")).err().unwrap();
74 fn missing_symbol_fails() {
76 let lib
= Library
::new(LIBPATH
).unwrap();
78 lib
.get
::<*mut ()>(b
"test_does_not_exist").err().unwrap();
79 lib
.get
::<*mut ()>(b
"test_does_not_exist\0").err().unwrap();
84 fn interior_null_fails() {
86 let lib
= Library
::new(LIBPATH
).unwrap();
88 lib
.get
::<*mut ()>(b
"test_does\0_not_exist").err().unwrap();
89 lib
.get
::<*mut ()>(b
"test\0_does_not_exist\0").err().unwrap();
94 fn test_incompatible_type() {
96 let lib
= Library
::new(LIBPATH
).unwrap();
98 assert
!(match lib
.get
::<()>(b
"test_identity_u32\0") {
99 Err(libloading
::Error
::IncompatibleSize
) => true,
106 fn test_incompatible_type_named_fn() {
108 unsafe fn get
<'a
, T
>(l
: &'a Library
, _
: T
) -> Result
<Symbol
<'a
, T
>, libloading
::Error
> {
109 l
.get
::<T
>(b
"test_identity_u32\0")
111 let lib
= Library
::new(LIBPATH
).unwrap();
113 assert
!(match get(&lib
, test_incompatible_type_named_fn
) {
114 Err(libloading
::Error
::IncompatibleSize
) => true,
121 fn test_static_u32() {
123 let lib
= Library
::new(LIBPATH
).unwrap();
125 let var
: Symbol
<*mut u32> = lib
.get(b
"TEST_STATIC_U32\0").unwrap();
127 let help
: Symbol
<unsafe extern fn() -> u32> = lib
.get(b
"test_get_static_u32\0").unwrap();
128 assert_eq
!(42, help());
133 fn test_static_ptr() {
135 let lib
= Library
::new(LIBPATH
).unwrap();
137 let var
: Symbol
<*mut *mut ()> = lib
.get(b
"TEST_STATIC_PTR\0").unwrap();
138 **var
= *var
as *mut _
;
139 let works
: Symbol
<unsafe extern fn() -> bool
> =
140 lib
.get(b
"test_check_static_ptr\0").unwrap();
147 fn library_this_get() {
148 use libloading
::os
::unix
::Library
;
150 let _lib
= Library
::new(LIBPATH
).unwrap();
151 let this
= Library
::this();
152 // SAFE: functions are never called
154 // Library we loaded in `_lib` (should be RTLD_LOCAL).
155 // FIXME: inconsistent behaviour between macos and other posix systems
156 // assert!(this.get::<unsafe extern "C" fn()>(b"test_identity_u32").is_err());
157 // Something obscure from libc...
158 assert
!(this
.get
::<unsafe extern "C" fn()>(b
"freopen").is_ok());
165 use libloading
::os
::windows
::Library
;
167 let _lib
= Library
::new(LIBPATH
).unwrap();
168 let this
= Library
::this().expect("this library");
169 // SAFE: functions are never called
171 // Library we loaded in `_lib`.
172 assert
!(this
.get
::<unsafe extern "C" fn()>(b
"test_identity_u32").is_err());
173 // Something "obscure" from kernel32...
174 assert
!(this
.get
::<unsafe extern "C" fn()>(b
"GetLastError").is_err());
180 fn works_getlasterror() {
181 use winapi
::um
::errhandlingapi
;
182 use winapi
::shared
::minwindef
::DWORD
;
183 use libloading
::os
::windows
::{Library, Symbol}
;
185 let lib
= Library
::new("kernel32.dll").unwrap();
186 let gle
: Symbol
<unsafe extern "system" fn() -> DWORD
> = unsafe {
187 lib
.get(b
"GetLastError").unwrap()
190 errhandlingapi
::SetLastError(42);
191 assert_eq
!(errhandlingapi
::GetLastError(), gle())
197 fn works_getlasterror0() {
198 use winapi
::um
::errhandlingapi
;
199 use winapi
::shared
::minwindef
::DWORD
;
200 use libloading
::os
::windows
::{Library, Symbol}
;
202 let lib
= Library
::new("kernel32.dll").unwrap();
203 let gle
: Symbol
<unsafe extern "system" fn() -> DWORD
> = unsafe {
204 lib
.get(b
"GetLastError\0").unwrap()
207 errhandlingapi
::SetLastError(42);
208 assert_eq
!(errhandlingapi
::GetLastError(), gle())