diff options
author | Stefano Garzarella <sgarzare@redhat.com> | 2019-07-01 12:08:23 +0200 |
---|---|---|
committer | Stefano Garzarella <sgarzare@redhat.com> | 2019-07-12 08:50:28 +0200 |
commit | db72b7ea35c8200d79f4af324349fc3b49074079 (patch) | |
tree | f812d83361991dfd1714189951db770f5c8c6d67 /test | |
parent | b1b0e0771bde7905186fc5ac37787297621d42fd (diff) | |
download | nix-db72b7ea35c8200d79f4af324349fc3b49074079.zip |
test_socket: add AddressFamily::Vsock test
The current VSOCK implementation does not support loopback devices,
so, for now, we expect a failure in the spawned thread when it
tries to connect.
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/sys/test_socket.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 12afc84c..bbe067f8 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -1013,3 +1013,55 @@ pub fn test_recv_ipv6pktinfo() { ); } } + +#[cfg(target_os = "linux")] +#[test] +pub fn test_vsock() { + use libc; + use nix::Error; + use nix::errno::Errno; + use nix::sys::socket::{AddressFamily, socket, bind, connect, listen, + SockAddr, SockType, SockFlag}; + use nix::unistd::{close}; + use std::thread; + + let port: u32 = 3000; + + let s1 = socket(AddressFamily::Vsock, SockType::Stream, + SockFlag::empty(), None) + .expect("socket failed"); + + // VMADDR_CID_HYPERVISOR and VMADDR_CID_RESERVED are reserved, so we expect + // an EADDRNOTAVAIL error. + let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_HYPERVISOR, port); + assert_eq!(bind(s1, &sockaddr).err(), + Some(Error::Sys(Errno::EADDRNOTAVAIL))); + + let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_RESERVED, port); + assert_eq!(bind(s1, &sockaddr).err(), + Some(Error::Sys(Errno::EADDRNOTAVAIL))); + + + let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_ANY, port); + assert_eq!(bind(s1, &sockaddr), Ok(())); + listen(s1, 10).expect("listen failed"); + + let thr = thread::spawn(move || { + let cid: u32 = libc::VMADDR_CID_HOST; + + let s2 = socket(AddressFamily::Vsock, SockType::Stream, + SockFlag::empty(), None) + .expect("socket failed"); + + let sockaddr = SockAddr::new_vsock(cid, port); + + // The current implementation does not support loopback devices, so, + // for now, we expect a failure on the connect. + assert_ne!(connect(s2, &sockaddr), Ok(())); + + close(s2).unwrap(); + }); + + close(s1).unwrap(); + thr.join().unwrap(); +} |