Skip to content

Commit 44ed4c0

Browse files
committed
Add tests that call generated ioctl fns
1 parent aeec797 commit 44ed4c0

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

test/sys/test_ioctl.rs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,145 @@ ioctl!(write_buf writebuf_test_u32 with 0, 0; u32);
2222
ioctl!(write_buf writebuf_test_u64 with 0, 0; u64);
2323
ioctl!(readwrite_buf readwritebuf_test with 0, 0; u32);
2424

25+
#[cfg(any(target_os = "linux", target_os = "android"))]
26+
mod linux_ioctls {
27+
use std::mem;
28+
use std::os::unix::io::AsRawFd;
29+
30+
use tempfile::tempfile;
31+
use libc::{TCGETS, TCSBRK, TCSETS, TIOCNXCL, termios};
32+
33+
use nix::Error::Sys;
34+
use nix::errno::ENOTTY;
35+
36+
// Compile-test the generated bad none TIOCNXCL function
37+
ioctl!(bad none tiocnxcl with TIOCNXCL);
38+
#[test]
39+
fn test_ioctl_bad_none() {
40+
let file = tempfile().unwrap();
41+
let res = unsafe { tiocnxcl(file.as_raw_fd()) };
42+
assert_eq!(res, Err(Sys(ENOTTY)));
43+
}
44+
45+
// Compile-test the generated bad read TCGETS function
46+
ioctl!(bad read tcgets with TCGETS; termios);
47+
#[test]
48+
fn test_ioctl_bad_read() {
49+
let file = tempfile().unwrap();
50+
let mut termios = unsafe { mem::uninitialized() };
51+
let res = unsafe { tcgets(file.as_raw_fd(), &mut termios) };
52+
assert_eq!(res, Err(Sys(ENOTTY)));
53+
}
54+
55+
// Compile-test the generated bad write_int TCSBRK function
56+
ioctl!(bad write_int tcsbrk with TCSBRK);
57+
#[test]
58+
fn test_ioctl_bad_write_int() {
59+
let file = tempfile().unwrap();
60+
let res = unsafe { tcsbrk(file.as_raw_fd(), 0) };
61+
assert_eq!(res, Err(Sys(ENOTTY)));
62+
}
63+
64+
// Compile-test the generated bad write_ptr TCSETS function
65+
ioctl!(bad write_ptr tcsets with TCSETS; termios);
66+
#[test]
67+
fn test_ioctl_bad_write_ptr() {
68+
let file = tempfile().unwrap();
69+
let termios: termios = unsafe { mem::uninitialized() };
70+
let res = unsafe { tcsets(file.as_raw_fd(), &termios) };
71+
assert_eq!(res, Err(Sys(ENOTTY)));
72+
}
73+
74+
// FIXME: Find a suitable example for "bad readwrite".
75+
76+
// From linux/videodev2.h
77+
ioctl!(none log_status with b'V', 70);
78+
#[test]
79+
fn test_ioctl_none() {
80+
let file = tempfile().unwrap();
81+
let res = unsafe { log_status(file.as_raw_fd()) };
82+
assert_eq!(res, Err(Sys(ENOTTY)));
83+
}
84+
85+
#[repr(C)]
86+
pub struct v4l2_audio {
87+
index: u32,
88+
name: [u8; 32],
89+
capability: u32,
90+
mode: u32,
91+
reserved: [u32; 2],
92+
}
93+
94+
// From linux/videodev2.h
95+
ioctl!(write_ptr s_audio with b'V', 34; v4l2_audio);
96+
#[test]
97+
fn test_ioctl_read() {
98+
let file = tempfile().unwrap();
99+
let data: v4l2_audio = unsafe { mem::uninitialized() };
100+
let res = unsafe { g_audio(file.as_raw_fd(), &data) };
101+
assert_eq!(res, Err(Sys(ENOTTY)));
102+
}
103+
104+
// From linux/net/bluetooth/hci_sock.h
105+
const HCI_IOC_MAGIC: u8 = b'H';
106+
const HCI_IOC_HCIDEVUP: u8 = 201;
107+
ioctl!(write_int hcidevup with HCI_IOC_MAGIC, HCI_IOC_HCIDEVUP);
108+
#[test]
109+
fn test_ioctl_write_int() {
110+
let file = tempfile().unwrap();
111+
let res = unsafe { hcidevup(file.as_raw_fd(), 0) };
112+
assert_eq!(res, Err(Sys(ENOTTY)));
113+
}
114+
115+
// From linux/videodev2.h
116+
ioctl!(write_ptr g_audio with b'V', 33; v4l2_audio);
117+
#[test]
118+
fn test_ioctl_write_ptr() {
119+
let file = tempfile().unwrap();
120+
let mut data: v4l2_audio = unsafe { mem::uninitialized() };
121+
let res = unsafe { g_audio(file.as_raw_fd(), &mut data) };
122+
assert_eq!(res, Err(Sys(ENOTTY)));
123+
}
124+
125+
// From linux/videodev2.h
126+
ioctl!(readwrite enum_audio with b'V', 65; v4l2_audio);
127+
#[test]
128+
fn test_ioctl_readwrite() {
129+
let file = tempfile().unwrap();
130+
let mut data: v4l2_audio = unsafe { mem::uninitialized() };
131+
let res = unsafe { enum_audio(file.as_raw_fd(), &mut data) };
132+
assert_eq!(res, Err(Sys(ENOTTY)));
133+
}
134+
135+
// FIXME: Find a suitable example for read_buf.
136+
137+
#[repr(C)]
138+
pub struct spi_ioc_transfer {
139+
tx_buf: u64,
140+
rx_buf: u64,
141+
len: u32,
142+
speed_hz: u32,
143+
delay_usecs: u16,
144+
bits_per_word: u8,
145+
cs_change: u8,
146+
tx_nbits: u8,
147+
rx_nbits: u8,
148+
pad: u16,
149+
}
150+
151+
// From linux/spi/spidev.h
152+
ioctl!(write_buf spi_ioc_message with super::SPI_IOC_MAGIC, super::SPI_IOC_MESSAGE; spi_ioc_transfer);
153+
#[test]
154+
fn test_ioctl_write_buf() {
155+
let file = tempfile().unwrap();
156+
let mut data: [spi_ioc_transfer; 4] = unsafe { mem::uninitialized() };
157+
let res = unsafe { spi_ioc_message(file.as_raw_fd(), &mut data[..]) };
158+
assert_eq!(res, Err(Sys(ENOTTY)));
159+
}
160+
161+
// FIXME: Find a suitable example for readwrite_buf.
162+
}
163+
25164
// See C code for source of values for op calculations (does NOT work for mips/powerpc):
26165
// https://gist.github.com/posborne/83ea6880770a1aef332e
27166
//

0 commit comments

Comments
 (0)