Skip to content

Commit

Permalink
test_socket: add AddressFamily::Vsock test
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stefano-garzarella committed Jul 1, 2019
1 parent ed495c0 commit deb9d66
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit deb9d66

Please sign in to comment.