From b743297af1d176fd84633a492f40fe9e236c6e0d Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Tue, 10 Dec 2024 10:21:48 -0600 Subject: [PATCH] tests --- crates/tests/bindgen/Cargo.toml | 2 + crates/tests/bindgen/src/bool.rs | 13 ++++ crates/tests/bindgen/src/bool_event.rs | 60 +++++++++++++++++++ crates/tests/bindgen/src/lib.rs | 2 + crates/tests/bindgen/src/window_long_get_a.rs | 6 -- crates/tests/bindgen/src/window_long_get_w.rs | 6 -- crates/tests/bindgen/src/window_long_set_a.rs | 6 -- crates/tests/bindgen/src/window_long_set_w.rs | 6 -- crates/tests/bindgen/tests/bool.rs | 21 +++++++ crates/tests/misc/extensions/tests/bool32.rs | 1 - crates/tests/misc/extensions/tests/boolean.rs | 2 +- crates/tools/bindgen/src/main.rs | 7 ++- 12 files changed, 104 insertions(+), 28 deletions(-) create mode 100644 crates/tests/bindgen/src/bool.rs create mode 100644 crates/tests/bindgen/src/bool_event.rs create mode 100644 crates/tests/bindgen/tests/bool.rs diff --git a/crates/tests/bindgen/Cargo.toml b/crates/tests/bindgen/Cargo.toml index cc428c14a4..09d372f6a2 100644 --- a/crates/tests/bindgen/Cargo.toml +++ b/crates/tests/bindgen/Cargo.toml @@ -18,6 +18,8 @@ workspace = true workspace = true features = [ "Foundation", + "Win32_Foundation", + "Win32_Security", ] [dependencies.windows-sys] diff --git a/crates/tests/bindgen/src/bool.rs b/crates/tests/bindgen/src/bool.rs new file mode 100644 index 0000000000..eb45e80158 --- /dev/null +++ b/crates/tests/bindgen/src/bool.rs @@ -0,0 +1,13 @@ +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] + +#[inline] +pub unsafe fn EnableMouseInPointer(fenable: bool) -> windows_core::Result<()> { + windows_targets::link!("user32.dll" "system" fn EnableMouseInPointer(fenable : windows::Win32::Foundation:: BOOL) -> windows::Win32::Foundation:: BOOL); + EnableMouseInPointer(fenable.into()).ok() +} diff --git a/crates/tests/bindgen/src/bool_event.rs b/crates/tests/bindgen/src/bool_event.rs new file mode 100644 index 0000000000..1d1b335912 --- /dev/null +++ b/crates/tests/bindgen/src/bool_event.rs @@ -0,0 +1,60 @@ +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] + +#[inline] +pub unsafe fn CreateEventW( + lpeventattributes: Option<*const windows::Win32::Security::SECURITY_ATTRIBUTES>, + bmanualreset: bool, + binitialstate: bool, + lpname: P3, +) -> windows_core::Result +where + P3: windows_core::Param, +{ + windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const windows::Win32::Security:: SECURITY_ATTRIBUTES, bmanualreset : windows::Win32::Foundation:: BOOL, binitialstate : windows::Win32::Foundation:: BOOL, lpname : windows_core::PCWSTR) -> windows::Win32::Foundation:: HANDLE); + let result__ = CreateEventW( + core::mem::transmute(lpeventattributes.unwrap_or(core::mem::zeroed())), + bmanualreset.into(), + binitialstate.into(), + lpname.param().abi(), + ); + (!result__.is_invalid()) + .then_some(result__) + .ok_or_else(windows_core::Error::from_win32) +} +#[inline] +pub unsafe fn NtWaitForSingleObject( + handle: windows::Win32::Foundation::HANDLE, + alertable: bool, + timeout: *mut i64, +) -> windows::Win32::Foundation::NTSTATUS { + windows_targets::link!("ntdll.dll" "system" fn NtWaitForSingleObject(handle : windows::Win32::Foundation:: HANDLE, alertable : windows::Win32::Foundation:: BOOLEAN, timeout : *mut i64) -> windows::Win32::Foundation:: NTSTATUS); + NtWaitForSingleObject( + core::mem::transmute(handle), + alertable.into(), + core::mem::transmute(timeout), + ) +} +#[inline] +pub unsafe fn SetEvent(hevent: windows::Win32::Foundation::HANDLE) -> windows_core::Result<()> { + windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : windows::Win32::Foundation:: HANDLE) -> windows::Win32::Foundation:: BOOL); + SetEvent(core::mem::transmute(hevent)).ok() +} +#[inline] +pub unsafe fn WaitForSingleObjectEx( + hhandle: windows::Win32::Foundation::HANDLE, + dwmilliseconds: u32, + balertable: bool, +) -> windows::Win32::Foundation::WAIT_EVENT { + windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObjectEx(hhandle : windows::Win32::Foundation:: HANDLE, dwmilliseconds : u32, balertable : windows::Win32::Foundation:: BOOL) -> windows::Win32::Foundation:: WAIT_EVENT); + WaitForSingleObjectEx( + core::mem::transmute(hhandle), + core::mem::transmute(dwmilliseconds), + balertable.into(), + ) +} diff --git a/crates/tests/bindgen/src/lib.rs b/crates/tests/bindgen/src/lib.rs index e64b6e6398..f91fef82e1 100644 --- a/crates/tests/bindgen/src/lib.rs +++ b/crates/tests/bindgen/src/lib.rs @@ -1,5 +1,7 @@ // This package was generated by `tool_bindgen`. +pub mod bool; +pub mod bool_event; pub mod class; pub mod class_dep; pub mod class_static; diff --git a/crates/tests/bindgen/src/window_long_get_a.rs b/crates/tests/bindgen/src/window_long_get_a.rs index 1c249c3c4d..407a532f6a 100644 --- a/crates/tests/bindgen/src/window_long_get_a.rs +++ b/crates/tests/bindgen/src/window_long_get_a.rs @@ -26,9 +26,6 @@ pub use GetWindowLongA as GetWindowLongPtrA; #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HANDLE(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HANDLE { - type TypeKind = windows_core::CopyType; -} impl HANDLE { pub fn is_invalid(&self) -> bool { self.0 == -1 as _ || self.0 == 0 as _ @@ -51,9 +48,6 @@ impl Default for HANDLE { #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HWND(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HWND { - type TypeKind = windows_core::CopyType; -} impl HWND { pub fn is_invalid(&self) -> bool { self.0.is_null() diff --git a/crates/tests/bindgen/src/window_long_get_w.rs b/crates/tests/bindgen/src/window_long_get_w.rs index 73580375e4..eccbae8d5a 100644 --- a/crates/tests/bindgen/src/window_long_get_w.rs +++ b/crates/tests/bindgen/src/window_long_get_w.rs @@ -26,9 +26,6 @@ pub unsafe fn GetWindowLongW(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX) -> i32 { #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HANDLE(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HANDLE { - type TypeKind = windows_core::CopyType; -} impl HANDLE { pub fn is_invalid(&self) -> bool { self.0 == -1 as _ || self.0 == 0 as _ @@ -51,9 +48,6 @@ impl Default for HANDLE { #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HWND(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HWND { - type TypeKind = windows_core::CopyType; -} impl HWND { pub fn is_invalid(&self) -> bool { self.0.is_null() diff --git a/crates/tests/bindgen/src/window_long_set_a.rs b/crates/tests/bindgen/src/window_long_set_a.rs index e7f9857a92..048f98bb3a 100644 --- a/crates/tests/bindgen/src/window_long_set_a.rs +++ b/crates/tests/bindgen/src/window_long_set_a.rs @@ -38,9 +38,6 @@ pub use SetWindowLongA as SetWindowLongPtrA; #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HANDLE(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HANDLE { - type TypeKind = windows_core::CopyType; -} impl HANDLE { pub fn is_invalid(&self) -> bool { self.0 == -1 as _ || self.0 == 0 as _ @@ -63,9 +60,6 @@ impl Default for HANDLE { #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HWND(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HWND { - type TypeKind = windows_core::CopyType; -} impl HWND { pub fn is_invalid(&self) -> bool { self.0.is_null() diff --git a/crates/tests/bindgen/src/window_long_set_w.rs b/crates/tests/bindgen/src/window_long_set_w.rs index 9f344a5796..f44729070a 100644 --- a/crates/tests/bindgen/src/window_long_set_w.rs +++ b/crates/tests/bindgen/src/window_long_set_w.rs @@ -38,9 +38,6 @@ pub unsafe fn SetWindowLongW(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX, dwnewlon #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HANDLE(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HANDLE { - type TypeKind = windows_core::CopyType; -} impl HANDLE { pub fn is_invalid(&self) -> bool { self.0 == -1 as _ || self.0 == 0 as _ @@ -63,9 +60,6 @@ impl Default for HANDLE { #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct HWND(pub *mut core::ffi::c_void); -impl windows_core::TypeKind for HWND { - type TypeKind = windows_core::CopyType; -} impl HWND { pub fn is_invalid(&self) -> bool { self.0.is_null() diff --git a/crates/tests/bindgen/tests/bool.rs b/crates/tests/bindgen/tests/bool.rs new file mode 100644 index 0000000000..3f21aec7f0 --- /dev/null +++ b/crates/tests/bindgen/tests/bool.rs @@ -0,0 +1,21 @@ +use test_bindgen::bool_event::*; +use windows::Win32::Foundation::{STATUS_SUCCESS, STATUS_TIMEOUT, WAIT_OBJECT_0, WAIT_TIMEOUT}; + +#[test] +fn test() { + unsafe { + // These bool parameters are actually BOOL. + let event = CreateEventW(None, true, false, None).unwrap(); + + // This bool parameter is actually BOOL. + assert_eq!(WAIT_TIMEOUT, WaitForSingleObjectEx(event, 0, false)); + + // This bool parameter is actually BOOLEAN. + assert_eq!(STATUS_TIMEOUT, NtWaitForSingleObject(event, false, &mut 0)); + + let event = CreateEventW(None, true, true, None).unwrap(); + + assert_eq!(WAIT_OBJECT_0, WaitForSingleObjectEx(event, 0, false)); + assert_eq!(STATUS_SUCCESS, NtWaitForSingleObject(event, false, &mut 0)); + } +} diff --git a/crates/tests/misc/extensions/tests/bool32.rs b/crates/tests/misc/extensions/tests/bool32.rs index 848e7c46ce..53a5acdb8f 100644 --- a/crates/tests/misc/extensions/tests/bool32.rs +++ b/crates/tests/misc/extensions/tests/bool32.rs @@ -27,6 +27,5 @@ fn test() { fn no_run() { unsafe { _ = CreateEventA(None, false, true, None); - _ = CreateEventA(None, BOOL(0), BOOL(1), None); } } diff --git a/crates/tests/misc/extensions/tests/boolean.rs b/crates/tests/misc/extensions/tests/boolean.rs index 2b9727a307..c56c557526 100644 --- a/crates/tests/misc/extensions/tests/boolean.rs +++ b/crates/tests/misc/extensions/tests/boolean.rs @@ -37,7 +37,7 @@ fn no_run() { Default::default(), None, None, - BOOLEAN(1), + true, std::ptr::null_mut(), ); } diff --git a/crates/tools/bindgen/src/main.rs b/crates/tools/bindgen/src/main.rs index 2f79efdf2f..4e77797429 100644 --- a/crates/tools/bindgen/src/main.rs +++ b/crates/tools/bindgen/src/main.rs @@ -93,8 +93,6 @@ fn main() { test("--out fn_no_return_win.rs --filter FatalExit"); test("--out fn_no_return_sys.rs --filter FatalExit --sys"); test("--out fn_result_void_sys.rs --filter SetComputerNameA --sys"); - // TODO: this requires BOOL extensions which are currently only in the `windows` crate - // test("--out fn_result_void_win.rs --filter SetComputerNameA"); // Tests for delegates test("--out delegate.rs --filter DeferralCompletedHandler"); @@ -137,6 +135,11 @@ fn main() { test("--out reference_struct_sys_reference_type.rs --sys --filter GAMING_DEVICE_MODEL_INFORMATION --reference windows_sys,skip-root,GAMING_DEVICE_VENDOR_ID"); test("--out reference_struct_sys_reference_namespace.rs --sys --filter GAMING_DEVICE_MODEL_INFORMATION --reference windows_sys,skip-root,Windows.Win32.Gaming"); + // Tests for BOOL and BOOLEAN parameters + // TODO: this requires BOOL extensions which are currently only in the `windows` crate hence the --reference below + test("--out bool.rs --filter EnableMouseInPointer --reference windows,skip-root,Windows"); + test("--out bool_event.rs --filter CreateEventW SetEvent NtWaitForSingleObject WaitForSingleObjectEx --reference windows,skip-root,Windows"); + // Tests simulating reference dependency and dependent test_raw( "--no-comment --out reference_dependency_flat.rs --filter IMemoryBufferReference --flat",