From 38e497f4200a0d2fcb043941d4a5792c76e47bbb Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Wed, 30 Aug 2023 18:11:45 +0800 Subject: [PATCH 3/3] [file_utils] Verify ioctl request code in tests (cherry picked from commit f049fda90bbf74ab26bfd38e26e7c92de8f50e30) --- src/ioctl.rs | 3 ++ src/ioctl/tests.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/ioctl/tests.rs diff --git a/src/ioctl.rs b/src/ioctl.rs index 84933648..221bd4e9 100644 --- a/src/ioctl.rs +++ b/src/ioctl.rs @@ -1,5 +1,8 @@ /* Rust port of kernel include/uapi/asm-generic/ioctl.h */ +#[cfg(test)] +mod tests; + //------------------------------------------ #[cfg(target_env = "musl")] diff --git a/src/ioctl/tests.rs b/src/ioctl/tests.rs new file mode 100644 index 00000000..17a395df --- /dev/null +++ b/src/ioctl/tests.rs @@ -0,0 +1,86 @@ +use crate::ioctl::*; + +//------------------------------------------ + +#[cfg(any( + target_arch = "mips", + target_arch = "mips64", + target_arch = "powerpc", + target_arch = "powerpc64", + target_arch = "powerpc64le", + target_arch = "sparc", + target_arch = "sparc64" +))] +mod expected { + use super::RequestType; + pub const BLKDISCARD: RequestType = 0x20001277; + + #[cfg(target_pointer_width = "32")] + mod sized { + use super::RequestType; + pub const BLKBSZSET: RequestType = 0x80041271; + pub const BLKGETSIZE64: RequestType = 0x40041272; + } + + #[cfg(target_pointer_width = "64")] + mod sized { + use super::RequestType; + pub const BLKBSZSET: RequestType = 0x80081271; + pub const BLKGETSIZE64: RequestType = 0x40081272; + } + + pub use sized::*; +} + +#[cfg(not(any( + target_arch = "mips", + target_arch = "mips64", + target_arch = "powerpc", + target_arch = "powerpc64", + target_arch = "powerpc64le", + target_arch = "sparc", + target_arch = "sparc64" +)))] +mod expected { + use super::RequestType; + pub const BLKDISCARD: RequestType = 0x1277; + + #[cfg(target_pointer_width = "32")] + mod sized { + use super::RequestType; + pub const BLKBSZSET: RequestType = 0x40041271; + pub const BLKGETSIZE64: RequestType = 0x80041272; + } + + #[cfg(target_pointer_width = "64")] + mod sized { + use super::RequestType; + pub const BLKBSZSET: RequestType = 0x40081271; + pub const BLKGETSIZE64: RequestType = 0x80081272; + } + + pub use sized::*; +} + +#[test] +fn test_ioc_none() { + assert_eq!(crate::request_code_none!(0x12, 119), expected::BLKDISCARD); +} + +#[test] +fn test_ioc_read_usize() { + assert_eq!( + crate::request_code_read!(0x12, 114, usize), + expected::BLKGETSIZE64 + ); +} + +#[test] +fn test_ioc_write_usize() { + assert_eq!( + crate::request_code_write!(0x12, 113, usize), + expected::BLKBSZSET + ); +} + +//------------------------------------------ -- 2.41.0