Skip to content

Commit

Permalink
Merge #993
Browse files Browse the repository at this point in the history
993: Don't reference packed structs. r=asomers a=pusateri

Fixes #992.
Don't merge this yet. I have more testing to do. I just am pushing it up for others.

Co-authored-by: Tom Pusateri <pusateri@bangj.com>
  • Loading branch information
bors[bot] and pusateri committed Dec 17, 2018
2 parents f404d9d + d139551 commit e1bb80e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ impl Eq for Ipv4Addr {

impl hash::Hash for Ipv4Addr {
fn hash<H: hash::Hasher>(&self, s: &mut H) {
self.0.s_addr.hash(s)
let saddr = self.0.s_addr;
saddr.hash(s)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,13 @@ impl Eq for IpMembershipRequest {}

impl fmt::Debug for IpMembershipRequest {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mref = &self.0.imr_multiaddr;
let maddr = mref.s_addr;
let iref = &self.0.imr_interface;
let ifaddr = iref.s_addr;
f.debug_struct("IpMembershipRequest")
.field("imr_multiaddr", &self.0.imr_multiaddr.s_addr)
.field("imr_interface", &self.0.imr_interface.s_addr)
.field("imr_multiaddr", &maddr)
.field("imr_interface", &ifaddr)
.finish()
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pub fn test_inetv4_addr_to_sock_addr() {
InetAddr::V4(addr) => {
let ip: u32 = 0x7f00_0001;
let port: u16 = 3000;
let saddr = addr.sin_addr.s_addr;

assert_eq!(addr.sin_addr.s_addr, ip.to_be());
assert_eq!(saddr, ip.to_be());
assert_eq!(addr.sin_port, port.to_be());
}
_ => panic!("nope"),
Expand Down
10 changes: 9 additions & 1 deletion test/test_mq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str;

use nix::errno::Errno::*;
use nix::Error::Sys;
use nix::mqueue::{mq_open, mq_close, mq_send, mq_receive, mq_getattr, mq_setattr, mq_unlink, mq_set_nonblock, mq_remove_nonblock};
use nix::mqueue::{mq_open, mq_close, mq_send, mq_receive};
use nix::mqueue::{MqAttr, MQ_OFlag};
use nix::sys::stat::Mode;

Expand Down Expand Up @@ -40,7 +40,9 @@ fn test_mq_send_and_receive() {


#[test]
#[cfg(not(any(target_os = "netbsd")))]
fn test_mq_getattr() {
use nix::mqueue::mq_getattr;
const MSG_SIZE: c_long = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap();
Expand All @@ -60,8 +62,10 @@ fn test_mq_getattr() {

// FIXME: Fix failures for mips in QEMU
#[test]
#[cfg(not(any(target_os = "netbsd")))]
#[cfg_attr(any(target_arch = "mips", target_arch = "mips64"), ignore)]
fn test_mq_setattr() {
use nix::mqueue::{mq_getattr, mq_setattr};
const MSG_SIZE: c_long = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap();
Expand Down Expand Up @@ -95,8 +99,10 @@ fn test_mq_setattr() {

// FIXME: Fix failures for mips in QEMU
#[test]
#[cfg(not(any(target_os = "netbsd")))]
#[cfg_attr(any(target_arch = "mips", target_arch = "mips64"), ignore)]
fn test_mq_set_nonblocking() {
use nix::mqueue::{mq_getattr, mq_set_nonblock, mq_remove_nonblock};
const MSG_SIZE: c_long = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = &CString::new(b"/attr_test_get_attr".as_ref()).unwrap();
Expand All @@ -118,7 +124,9 @@ fn test_mq_set_nonblocking() {
}

#[test]
#[cfg(not(any(target_os = "netbsd")))]
fn test_mq_unlink() {
use nix::mqueue::mq_unlink;
const MSG_SIZE: c_long = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name_opened = &CString::new(b"/mq_unlink_test".as_ref()).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use nix::poll::{EventFlags, poll, PollFd};
use nix::sys::signal::SigSet;
use nix::sys::time::{TimeSpec, TimeValLike};
use nix::unistd::{write, pipe, close};

#[test]
Expand Down Expand Up @@ -49,6 +47,9 @@ fn test_poll_debug() {
#[test]
fn test_ppoll() {
use nix::poll::ppoll;
use nix::sys::signal::SigSet;
use nix::sys::time::{TimeSpec, TimeValLike};

let timeout = TimeSpec::milliseconds(1);
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(r, EventFlags::POLLIN)];
Expand Down
21 changes: 19 additions & 2 deletions test/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ use std::os::unix::fs::symlink;
use std::os::unix::prelude::AsRawFd;
use std::time::{Duration, UNIX_EPOCH};

#[cfg(not(any(target_os = "netbsd")))]
use libc::{S_IFMT, S_IFLNK};

use nix::fcntl;
use nix::sys::stat::{self, fchmod, fchmodat, fstat, futimens, lstat, lutimes, stat, utimes, utimensat};
use nix::sys::stat::{FileStat, Mode, FchmodatFlags, UtimensatFlags};
use nix::sys::stat::{self, fchmod, fchmodat, futimens, lutimes, stat, utimes, utimensat};
use nix::sys::stat::{Mode, FchmodatFlags, UtimensatFlags};

#[cfg(not(any(target_os = "netbsd")))]
use nix::sys::stat::FileStat;

use nix::sys::time::{TimeSpec, TimeVal, TimeValLike};
use nix::unistd::chdir;

#[cfg(not(any(target_os = "netbsd")))]
use nix::Result;
use tempfile;

#[allow(unused_comparisons)]
// uid and gid are signed on Windows, but not on other platforms. This function
// allows warning free compiles on all platforms, and can be removed when
// expression-level #[allow] is available.
#[cfg(not(any(target_os = "netbsd")))]
fn valid_uid_gid(stat: FileStat) -> bool {
// uid could be 0 for the `root` user. This quite possible when
// the tests are being run on a rooted Android device.
stat.st_uid >= 0 && stat.st_gid >= 0
}

#[cfg(not(any(target_os = "netbsd")))]
fn assert_stat_results(stat_result: Result<FileStat>) {
let stats = stat_result.expect("stat call failed");
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
Expand All @@ -35,6 +44,7 @@ fn assert_stat_results(stat_result: Result<FileStat>) {
assert!(stats.st_blocks <= 16); // Up to 16 blocks can be allocated for a blank file
}

#[cfg(not(any(target_os = "netbsd")))]
fn assert_lstat_results(stat_result: Result<FileStat>) {
let stats = stat_result.expect("stat call failed");
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
Expand All @@ -57,7 +67,10 @@ fn assert_lstat_results(stat_result: Result<FileStat>) {
}

#[test]
#[cfg(not(any(target_os = "netbsd")))]
fn test_stat_and_fstat() {
use nix::sys::stat::fstat;

let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("foo.txt");
let file = File::create(&filename).unwrap();
Expand All @@ -70,6 +83,7 @@ fn test_stat_and_fstat() {
}

#[test]
#[cfg(not(any(target_os = "netbsd")))]
fn test_fstatat() {
let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("foo.txt");
Expand All @@ -85,7 +99,10 @@ fn test_fstatat() {
}

#[test]
#[cfg(not(any(target_os = "netbsd")))]
fn test_stat_fstat_lstat() {
use nix::sys::stat::{fstat, lstat};

let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("bar.txt");
let linkname = tempdir.path().join("barlink");
Expand Down
7 changes: 4 additions & 3 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tempfile::{self, tempfile};
use libc::{self, _exit, off_t};

#[test]
#[cfg(not(any(target_os = "netbsd")))]
fn test_fork_and_waitpid() {
let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");

Expand Down Expand Up @@ -230,15 +231,15 @@ cfg_if!{
execve_test_factory!(test_fexecve, fexecve, File::open("/system/bin/sh").unwrap().into_raw_fd());
} else if #[cfg(any(target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))] {
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
execve_test_factory!(test_fexecve, fexecve, File::open("/bin/sh").unwrap().into_raw_fd());
} else if #[cfg(any(target_os = "dragonfly",
target_os = "ios",
target_os = "macos"))] {
target_os = "macos",
target_os = "netbsd"))] {
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
// No fexecve() on macos/ios and DragonFly.
// No fexecve() on DragonFly, ios, macos, and NetBSD.
}
}

Expand Down

0 comments on commit e1bb80e

Please sign in to comment.