summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2016-11-18 21:49:49 -0700
committerAlan Somers <asomers@gmail.com>2016-11-18 21:56:08 -0700
commit34a5c280ed5a0f7fcbcffcfa7cda012603ace44c (patch)
treebba7b5e49aeb069929ec95e597a7084845bd72b3
parent80a34a57313519272428f3f19f54b62261ee8001 (diff)
downloadnix-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.
-rw-r--r--test/sys/test_socket.rs20
-rw-r--r--test/test.rs24
2 files changed, 12 insertions, 32 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]
diff --git a/test/test.rs b/test/test.rs
index 045f8f18..d10970d6 100644
--- a/test/test.rs
+++ b/test/test.rs
@@ -22,30 +22,6 @@ mod test_mq;
#[cfg(any(target_os = "linux", target_os = "macos"))]
mod test_poll;
-mod ports {
- use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
- use std::sync::atomic::Ordering::SeqCst;
-
- // Helper for getting a unique port for the task run
- // TODO: Reuse ports to not spam the system
- static mut NEXT_PORT: AtomicUsize = ATOMIC_USIZE_INIT;
- const FIRST_PORT: usize = 18080;
-
- pub fn next_port() -> usize {
- unsafe {
- // If the atomic was never used, set it to the initial port
- NEXT_PORT.compare_and_swap(0, FIRST_PORT, SeqCst);
-
- // Get and increment the port list
- NEXT_PORT.fetch_add(1, SeqCst)
- }
- }
-
- pub fn localhost() -> String {
- format!("127.0.0.1:{}", next_port())
- }
-}
-
use nixtest::assert_size_of;
#[test]