Skip to content

Commit d131e87

Browse files
committed
Expand ioctl testing
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.
1 parent aeec797 commit d131e87

File tree

5 files changed

+272
-83
lines changed

5 files changed

+272
-83
lines changed

nix-test/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn main() {
1616
gcc::Config::new()
1717
.file("src/const.c")
1818
.file("src/sizes.c")
19+
.file("src/ioctls.c")
1920
.define(os, None)
2021
.compile("libnixtest.a");
2122
}

nix-test/src/ioctls.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <sys/ioctl.h>
2+
#include <stdint.h>
3+
4+
uint64_t io_a_255() {
5+
return _IO('a', 255);
6+
}
7+
8+
uint64_t io_q_10() {
9+
return _IO('q', 10);
10+
}

nix-test/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ impl GetConst for u32 {
5959
ffi::get_int_const(name) as u32
6060
}
6161
}
62+
63+
pub mod ioctl {
64+
#[link(name = "nixtest", kind = "static")]
65+
extern {
66+
pub fn io_a_255() -> u64;
67+
pub fn io_q_10() -> u64;
68+
}
69+
}

src/sys/ioctl/mod.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,119 @@ macro_rules! ioctl {
318318
}
319319
);
320320
}
321+
322+
#[cfg(test)]
323+
mod test {
324+
pub use super::ioctl_num_type as ioctl_num_type;
325+
326+
use ::nixtest::ioctl::*;
327+
328+
#[test]
329+
fn test_io() {
330+
assert_eq!(io!(b'q', 10), unsafe { io_q_10() } as ioctl_num_type);
331+
assert_eq!(io!(b'a', 255), unsafe { io_a_255() } as ioctl_num_type);
332+
}
333+
}
334+
335+
#[cfg(test)]
336+
#[cfg(any(target_os = "linux", target_os = "android"))]
337+
mod linux {
338+
#[test]
339+
fn test_op_write() {
340+
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
341+
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
342+
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
343+
} else {
344+
assert_eq!(iow!(b'z', 10, 1), 0x40017A0A);
345+
assert_eq!(iow!(b'z', 10, 512), 0x42007A0A);
346+
}
347+
}
348+
349+
#[cfg(target_pointer_width = "64")]
350+
#[test]
351+
fn test_op_write_64() {
352+
if cfg!(any(target_arch="powerpc64")){
353+
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
354+
} else {
355+
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
356+
}
357+
358+
}
359+
360+
#[test]
361+
fn test_op_read() {
362+
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
363+
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
364+
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
365+
} else {
366+
assert_eq!(ior!(b'z', 10, 1), 0x80017A0A);
367+
assert_eq!(ior!(b'z', 10, 512), 0x82007A0A);
368+
}
369+
}
370+
371+
#[cfg(target_pointer_width = "64")]
372+
#[test]
373+
fn test_op_read_64() {
374+
if cfg!(any(target_arch="powerpc64")){
375+
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
376+
} else {
377+
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
378+
}
379+
}
380+
381+
#[test]
382+
fn test_op_read_write() {
383+
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
384+
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
385+
}
386+
387+
#[cfg(target_pointer_width = "64")]
388+
#[test]
389+
fn test_op_read_write_64() {
390+
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
391+
}
392+
}
393+
394+
#[cfg(any(target_os = "macos",
395+
target_os = "ios",
396+
target_os = "netbsd",
397+
target_os = "openbsd",
398+
target_os = "freebsd",
399+
target_os = "dragonfly"))]
400+
mod bsd {
401+
#[test]
402+
fn test_op_write() {
403+
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
404+
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
405+
}
406+
407+
#[cfg(target_pointer_width = "64")]
408+
#[test]
409+
fn test_op_write_64() {
410+
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
411+
}
412+
413+
#[test]
414+
fn test_op_read() {
415+
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
416+
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
417+
}
418+
419+
#[cfg(target_pointer_width = "64")]
420+
#[test]
421+
fn test_op_read_64() {
422+
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
423+
}
424+
425+
#[test]
426+
fn test_op_read_write() {
427+
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
428+
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
429+
}
430+
431+
#[cfg(target_pointer_width = "64")]
432+
#[test]
433+
fn test_op_read_write_64() {
434+
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
435+
}
436+
}

0 commit comments

Comments
 (0)