diff options
author | Luca Bruno <lucab@debian.org> | 2017-10-26 09:30:55 +0000 |
---|---|---|
committer | Luca Bruno <lucab@debian.org> | 2017-11-19 13:46:43 +0000 |
commit | 02f2edebafd3929fc0ba096e3fe38123e205bb37 (patch) | |
tree | 90fc28f7e72518fe8295eb3bdb7a5e9b7e867737 /test/sys/test_socket.rs | |
parent | 2d270c964ca342764dc9bebc0bb404cae8022a51 (diff) | |
download | nix-02f2edebafd3929fc0ba096e3fe38123e205bb37.zip |
sys/socket: add UnixAddr abstract name getter
This introduces an `as_abstract()` getter to `UnixAddr` in order to
retrieve the name of an abstract unix socket.
This also adds tests around abstract addresses and clarify docs,
adding explicit semantics.
Diffstat (limited to 'test/sys/test_socket.rs')
-rw-r--r-- | test/sys/test_socket.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 5f9c6fff..b3d983d3 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -64,6 +64,28 @@ pub fn test_path_to_sock_addr() { assert_eq!(addr.path(), Some(actual)); } +// Test getting/setting abstract addresses (without unix socket creation) +#[cfg(target_os = "linux")] +#[test] +pub fn test_abstract_uds_addr() { + let empty = String::new(); + let addr = UnixAddr::new_abstract(empty.as_bytes()).unwrap(); + assert_eq!(addr.as_abstract(), Some(empty.as_bytes())); + + let name = String::from("nix\0abstract\0test"); + let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap(); + assert_eq!(addr.as_abstract(), Some(name.as_bytes())); + assert_eq!(addr.path(), None); + + // Internally, name is null-prefixed (abstract namespace) + let internal: &[u8] = unsafe { + slice::from_raw_parts(addr.0.sun_path.as_ptr() as *const u8, addr.1) + }; + let mut abstract_name = name.clone(); + abstract_name.insert(0, '\0'); + assert_eq!(internal, abstract_name.as_bytes()); +} + #[test] pub fn test_getsockname() { use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag}; |