diff options
author | Bryant Mairs <bryantmairs@google.com> | 2018-06-01 13:32:50 -0700 |
---|---|---|
committer | Bryant Mairs <bryantmairs@google.com> | 2018-06-01 14:47:42 -0700 |
commit | 7e870c054778036ff23ac1ee5fcb0ee4b04a19de (patch) | |
tree | 936673169e7c77c96e84362fb9ccf7dbff9e79ab /test | |
parent | badb45113ba98d46a28b54c5f9a9ff122774976c (diff) | |
download | nix-7e870c054778036ff23ac1ee5fcb0ee4b04a19de.zip |
Remove emulation of FD_CLOEXEC/O_NONBLOCK
Rather than using the native implementation of these constants
on supported platforms, the native implementation was instead
emulated. This was also hidden from the user even though this
could result in data races and the functionality being broken.
Native functionality is, however, not support on macos/ios.
Rather than enable this emulation solely for this platform, it
should be removed as this is a dangerous abstraction.
Diffstat (limited to 'test')
-rw-r--r-- | test/sys/test_socket.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index a997fbca..5d57ac17 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -255,3 +255,65 @@ pub fn test_syscontrol() { // requires root privileges // connect(fd, &sockaddr).expect("connect failed"); } + +/// Test non-blocking mode on new sockets via SockFlag::O_NONBLOCK +#[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] +#[test] +pub fn test_sockflag_nonblock() { + use libc; + use nix::fcntl::{fcntl}; + use nix::fcntl::FcntlArg::{F_GETFL}; + use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag}; + + /* first, try without SockFlag::SOCK_NONBLOCK */ + let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None) + .expect("socket failed"); + + let fcntl_res = fcntl(sock, F_GETFL).expect("fcntl failed"); + + assert!(fcntl_res & libc::O_NONBLOCK == 0); + + /* next, try with SockFlag::SOCK_NONBLOCK */ + let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::SOCK_NONBLOCK, None) + .expect("socket failed"); + + let fcntl_res = fcntl(sock, F_GETFL).expect("fcntl failed"); + + assert!(fcntl_res & libc::O_NONBLOCK == libc::O_NONBLOCK); +} + +/// Test close-on-exec on new sockets via SockFlag::SOCK_CLOEXEC +#[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] +#[test] +pub fn test_sockflag_cloexec() { + use libc; + use nix::fcntl::{fcntl}; + use nix::fcntl::FcntlArg::{F_GETFD}; + use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag}; + + /* first, test without SockFlag::SOCK_CLOEXEC */ + let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None) + .expect("socket failed"); + + let fcntl_res = fcntl(sock, F_GETFD).expect("fcntl failed"); + + assert!(fcntl_res & libc::FD_CLOEXEC == 0); + + /* next, test without SockFlag::SOCK_CLOEXEC */ + let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::SOCK_CLOEXEC, None) + .expect("socket failed"); + + let fcntl_res = fcntl(sock, F_GETFD).expect("fcntl failed"); + + assert!(fcntl_res & libc::FD_CLOEXEC == libc::FD_CLOEXEC); +} |