diff options
author | Bryant Mairs <bryant@mai.rs> | 2019-03-10 08:29:23 -0700 |
---|---|---|
committer | Bryant Mairs <bryant@mai.rs> | 2019-06-09 11:31:46 -0700 |
commit | c50e987b4e169e2d7dc7089c91407e1e55c550f1 (patch) | |
tree | 4892e791f8809dfa1501e9fa4ff999a106a8dd04 /test | |
parent | 2075ac70bdd4848b08213a237feb32f5506096e0 (diff) | |
download | nix-c50e987b4e169e2d7dc7089c91407e1e55c550f1.zip |
Add extra traits for all types
Derive Clone, Copy, Eq, Hash, and PartialEq for all types. Not all
traits are supported by all types, which is why many are missing
some.
Diffstat (limited to 'test')
-rw-r--r-- | test/sys/test_socket.rs | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 9cbf0257..7d945397 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -1,9 +1,11 @@ use nix::sys::socket::{InetAddr, UnixAddr, getsockname}; -use std::slice; +use std::collections::hash_map::DefaultHasher; +use std::hash::{Hash, Hasher}; use std::net::{self, Ipv6Addr, SocketAddr, SocketAddrV6}; +use std::os::unix::io::RawFd; use std::path::Path; +use std::slice; use std::str::FromStr; -use std::os::unix::io::RawFd; use libc::c_char; use tempfile; @@ -66,6 +68,37 @@ pub fn test_path_to_sock_addr() { assert_eq!(addr.path(), Some(actual)); } +fn calculate_hash<T: Hash>(t: &T) -> u64 { + let mut s = DefaultHasher::new(); + t.hash(&mut s); + s.finish() +} + +#[test] +pub fn test_addr_equality_path() { + let path = "/foo/bar"; + let actual = Path::new(path); + let addr1 = UnixAddr::new(actual).unwrap(); + let mut addr2 = addr1.clone(); + + addr2.0.sun_path[10] = 127; + + assert_eq!(addr1, addr2); + assert_eq!(calculate_hash(&addr1), calculate_hash(&addr2)); +} + +#[cfg(target_os = "linux")] +#[test] +pub fn test_addr_equality_abstract() { + let name = String::from("nix\0abstract\0test"); + let addr1 = UnixAddr::new_abstract(name.as_bytes()).unwrap(); + let mut addr2 = addr1.clone(); + addr2.0.sun_path[18] = 127; + + assert_eq!(addr1, addr2); + assert_eq!(calculate_hash(&addr1), calculate_hash(&addr2)); +} + // Test getting/setting abstract addresses (without unix socket creation) #[cfg(target_os = "linux")] #[test] |