Skip to content

Commit

Permalink
Expand ioctl testing
Browse files Browse the repository at this point in the history
Add tests of the generated functions using real ioctl values. These values should
be confirmed by the kernel when they're called, so it provides a nice check on
both the definition and ergonomics of the generated functions but also the value of
the generated ioctl as well.

Additionally, change the testing for ioctl number generation to use the same value
as that calculated by the C macros. This gets rid of those hard-coded values and
simplifies supporting additional platforms, especially ones that no devs have command-
line access to.
  • Loading branch information
Susurrus committed Jul 20, 2017
1 parent aeec797 commit d131e87
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 83 deletions.
1 change: 1 addition & 0 deletions nix-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn main() {
gcc::Config::new()
.file("src/const.c")
.file("src/sizes.c")
.file("src/ioctls.c")
.define(os, None)
.compile("libnixtest.a");
}
10 changes: 10 additions & 0 deletions nix-test/src/ioctls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <sys/ioctl.h>
#include <stdint.h>

uint64_t io_a_255() {
return _IO('a', 255);
}

uint64_t io_q_10() {
return _IO('q', 10);
}
8 changes: 8 additions & 0 deletions nix-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ impl GetConst for u32 {
ffi::get_int_const(name) as u32
}
}

pub mod ioctl {
#[link(name = "nixtest", kind = "static")]
extern {
pub fn io_a_255() -> u64;
pub fn io_q_10() -> u64;
}
}
116 changes: 116 additions & 0 deletions src/sys/ioctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,119 @@ macro_rules! ioctl {
}
);
}

#[cfg(test)]
mod test {
pub use super::ioctl_num_type as ioctl_num_type;

use ::nixtest::ioctl::*;

#[test]
fn test_io() {
assert_eq!(io!(b'q', 10), unsafe { io_q_10() } as ioctl_num_type);
assert_eq!(io!(b'a', 255), unsafe { io_a_255() } as ioctl_num_type);
}
}

#[cfg(test)]
#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
#[test]
fn test_op_write() {
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
} else {
assert_eq!(iow!(b'z', 10, 1), 0x40017A0A);
assert_eq!(iow!(b'z', 10, 512), 0x42007A0A);
}
}

#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_write_64() {
if cfg!(any(target_arch="powerpc64")){
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
} else {
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
}

}

#[test]
fn test_op_read() {
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
} else {
assert_eq!(ior!(b'z', 10, 1), 0x80017A0A);
assert_eq!(ior!(b'z', 10, 512), 0x82007A0A);
}
}

#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_64() {
if cfg!(any(target_arch="powerpc64")){
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
} else {
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
}
}

#[test]
fn test_op_read_write() {
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_write_64() {
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
}
}

#[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "netbsd",
target_os = "openbsd",
target_os = "freebsd",
target_os = "dragonfly"))]
mod bsd {
#[test]
fn test_op_write() {
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_write_64() {
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
}

#[test]
fn test_op_read() {
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_64() {
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
}

#[test]
fn test_op_read_write() {
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
}

#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_write_64() {
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
}
}
Loading

0 comments on commit d131e87

Please sign in to comment.