diff options
author | Homu <homu@barosl.com> | 2016-12-17 01:09:44 +0900 |
---|---|---|
committer | Homu <homu@barosl.com> | 2016-12-17 01:09:44 +0900 |
commit | 9b81000bdaa1636017f88609337c972a04effad8 (patch) | |
tree | 01384c688ff68f87aa816929f33e436068015447 /test/sys | |
parent | 5e932dcc35a3bb790155b1f438c5dd418ef01f77 (diff) | |
parent | ef257e055688cd088c1a27034742412caa7e208a (diff) | |
download | nix-9b81000bdaa1636017f88609337c972a04effad8.zip |
Auto merge of #478 - conradev:sys-control, r=fiveop
Add support for system control sockets for XNU
I added support for macOS and iOS system sockets, which can be used to control the kernel as described [here](https://developer.apple.com/library/content/documentation/Darwin/Conceptual/NKEConceptual/control/control.html).
To do this, I had to add in support for `ioctl` on those platforms, so I added in `ioctl` support for all BSD-based platforms. The API seems to be the same between [xnu](https://opensource.apple.com/source/xnu/xnu-3248.60.10/bsd/sys/ioccom.h.auto.html), [FreeBSD](https://github.com/freebsd/freebsd/blob/master/sys/sys/ioccom.h), [NetBSD](https://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/sys/ioccom.h), [OpenBSD](http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/sys/ioccom.h?rev=1.5&content-type=text/x-cvsweb-markup) and [Dragonfly BSD](http://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/ioccom.h).
I added a test that runs on macOS and iOS for the functionality. Let me know if I need to make any changes!
Diffstat (limited to 'test/sys')
-rw-r--r-- | test/sys/test_ioctl.rs | 123 | ||||
-rw-r--r-- | test/sys/test_socket.rs | 17 |
2 files changed, 104 insertions, 36 deletions
diff --git a/test/sys/test_ioctl.rs b/test/sys/test_ioctl.rs index 94eca447..1d9fbcc8 100644 --- a/test/sys/test_ioctl.rs +++ b/test/sys/test_ioctl.rs @@ -1,9 +1,7 @@ #![allow(dead_code)] -#![cfg(target_os = "linux")] // no ioctl support for osx yet - // Simple tests to ensure macro generated fns compile -ioctl!(bad do_bad with 0x1234); +ioctl!(do_bad with 0x1234); ioctl!(none do_none with 0, 0); ioctl!(read read_test with 0, 0; u32); ioctl!(write write_test with 0, 0; u64); @@ -15,44 +13,97 @@ ioctl!(readwrite buf readwritebuf_test with 0, 0; u32); // See C code for source of values for op calculations: // https://gist.github.com/posborne/83ea6880770a1aef332e -#[test] -fn test_op_none() { - assert_eq!(io!(b'q', 10), 0x0000710A); - assert_eq!(io!(b'a', 255), 0x000061FF); -} +#[cfg(any(target_os = "linux", target_os = "android"))] +mod linux { + #[test] + fn test_op_none() { + assert_eq!(io!(b'q', 10), 0x0000710A); + assert_eq!(io!(b'a', 255), 0x000061FF); + } -#[test] -fn test_op_write() { - assert_eq!(iow!(b'z', 10, 1), 0x40017A0A); - assert_eq!(iow!(b'z', 10, 512), 0x42007A0A); -} + #[test] + fn test_op_write() { + 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() { - assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x40007A0A); -} + #[cfg(target_pointer_width = "64")] + #[test] + fn test_op_write_64() { + assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x40007A0A); + } -#[test] -fn test_op_read() { - assert_eq!(ior!(b'z', 10, 1), 0x80017A0A); - assert_eq!(ior!(b'z', 10, 512), 0x82007A0A); -} + #[test] + fn test_op_read() { + 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() { - assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x80007A0A); -} + #[cfg(target_pointer_width = "64")] + #[test] + fn test_op_read_64() { + 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); + } -#[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(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_none() { + assert_eq!(io!(b'q', 10), 0x2000710A); + assert_eq!(io!(b'a', 255), 0x200061FF); + } + + #[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); + } } diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 9f4b4278..b5465aa0 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -180,3 +180,20 @@ pub fn test_unixdomain() { assert_eq!(&buf[..], b"hello"); } + +// Test creating and using named system control sockets +#[cfg(any(target_os = "macos", target_os = "ios"))] +#[test] +pub fn test_syscontrol() { + use nix::{Errno, Error}; + use nix::sys::socket::{AddressFamily, SockType, SockFlag}; + use nix::sys::socket::{socket, SockAddr}; + use nix::sys::socket::SYSPROTO_CONTROL; + + let fd = socket(AddressFamily::System, SockType::Datagram, SockFlag::empty(), SYSPROTO_CONTROL).expect("socket failed"); + let _sockaddr = SockAddr::new_sys_control(fd, "com.apple.net.utun_control", 0).expect("resolving sys_control name failed"); + assert_eq!(SockAddr::new_sys_control(fd, "foo.bar.lol", 0).err(), Some(Error::Sys(Errno::ENOENT))); + + // requires root privileges + // connect(fd, &sockaddr).expect("connect failed"); +} |