diff options
author | Alan Somers <asomers@gmail.com> | 2016-11-18 21:49:49 -0700 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2016-11-18 21:56:08 -0700 |
commit | 34a5c280ed5a0f7fcbcffcfa7cda012603ace44c (patch) | |
tree | bba7b5e49aeb069929ec95e597a7084845bd72b3 /test/sys/test_socket.rs | |
parent | 80a34a57313519272428f3f19f54b62261ee8001 (diff) | |
download | nix-34a5c280ed5a0f7fcbcffcfa7cda012603ace44c.zip |
Improve portability of test_getsockname
test_getsockname used an IPv4 socket and assumed that localhost was
"127.0.0.1". But that assumption doesn't hold on IPv6-only hosts or on
shared-IP FreeBSD jails. Unfortunately, the Rust standard library
doesn't provide a good way to resolve localhost. So change the test to
use a unix-domain socket instead.
Diffstat (limited to 'test/sys/test_socket.rs')
-rw-r--r-- | test/sys/test_socket.rs | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 8b713c2b..9f4b4278 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -3,8 +3,7 @@ use std::mem; use std::net::{self, Ipv6Addr, SocketAddr, SocketAddrV6}; use std::path::Path; use std::str::FromStr; -use std::os::unix::io::{AsRawFd, RawFd}; -use ports::localhost; +use std::os::unix::io::RawFd; use libc::c_char; #[test] @@ -64,13 +63,18 @@ pub fn test_path_to_sock_addr() { #[test] pub fn test_getsockname() { - use std::net::TcpListener; - - let addr = localhost(); - let sock = TcpListener::bind(&*addr).unwrap(); - let res = getsockname(sock.as_raw_fd()).unwrap(); + use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag}; + use nix::sys::socket::{bind, SockAddr}; + use tempdir::TempDir; - assert_eq!(addr, res.to_str()); + let tempdir = TempDir::new("test_getsockname").unwrap(); + let sockname = tempdir.path().join("sock"); + let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), + 0).expect("socket failed"); + let sockaddr = SockAddr::new_unix(&sockname).unwrap(); + bind(sock, &sockaddr).expect("bind failed"); + assert_eq!(sockaddr.to_str(), + getsockname(sock).expect("getsockname failed").to_str()); } #[test] |