diff options
author | Bryant Mairs <bryant@mai.rs> | 2019-06-09 10:33:47 -0700 |
---|---|---|
committer | Bryant Mairs <bryant@mai.rs> | 2019-06-11 19:42:51 -0700 |
commit | 3fdf3fe02d0a5816afc7b31db942cfe507e828d1 (patch) | |
tree | 70e261437de685b92d91e695ab0a123848f16211 /src/sys/socket | |
parent | 07420b59e38c04a14b78c634953c9c5687e82e81 (diff) | |
download | nix-3fdf3fe02d0a5816afc7b31db942cfe507e828d1.zip |
Fix tests for abstract sockets
Abstract paths should always be N-1 in length where N is the length of
the `sun_path` field (first byte is \0). Given that,
`UnixAddr::new_abstract()` should always return this N-1 length, not
just the length of the string provided (the rest of the array will be
\0s).
Diffstat (limited to 'src/sys/socket')
-rw-r--r-- | src/sys/socket/addr.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index c6988227..e6c5f8ae 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -551,7 +551,7 @@ impl UnixAddr { ret.sun_path.as_mut_ptr().offset(1) as *mut u8, path.len()); - Ok(UnixAddr(ret, path.len() + 1)) + Ok(UnixAddr(ret, ret.sun_path.len())) } } @@ -1126,9 +1126,11 @@ mod datalink { #[cfg(test)] mod tests { - #[cfg(any(target_os = "dragonfly", + #[cfg(any(target_os = "android", + target_os = "dragonfly", target_os = "freebsd", target_os = "ios", + target_os = "linux", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] @@ -1174,4 +1176,18 @@ mod tests { _ => { unreachable!() } }; } + + #[cfg(any(target_os = "android", target_os = "linux"))] + #[test] + fn test_abstract_sun_path() { + let name = String::from("nix\0abstract\0test"); + let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap(); + + let sun_path1 = addr.sun_path(); + let sun_path2 = [0u8, 110, 105, 120, 0, 97, 98, 115, 116, 114, 97, 99, 116, 0, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + assert_eq!(sun_path1.len(), sun_path2.len()); + for i in 0..sun_path1.len() { + assert_eq!(sun_path1[i], sun_path2[i]); + } + } } |