Skip to content

Commit

Permalink
Write more tests. (#733)
Browse files Browse the repository at this point in the history
* Write more tests.

* Miscellaneous cleanups.

* Fix a warning.
  • Loading branch information
sunfishcode authored Jul 8, 2023
1 parent 0ab3e8d commit 7cb06d9
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 7 deletions.
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ fn main() {
// Features only used in no-std configurations.
#[cfg(not(feature = "std"))]
{
use_feature_or_nothing("const_raw_ptr_deref");
use_feature_or_nothing("core_ffi_c");
use_feature_or_nothing("core_c_str");
use_feature_or_nothing("core_ffi_c");
use_feature_or_nothing("alloc_c_string");
use_feature_or_nothing("alloc_ffi");
}

// Gather target information.
Expand Down Expand Up @@ -167,8 +167,8 @@ fn can_compile<T: AsRef<str>>(test: T) -> bool {
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();

// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
// as documented [here].
// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string, as
// documented [here].
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
let wrapper = var("RUSTC_WRAPPER")
.ok()
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
#![cfg_attr(all(wasi_ext, target_os = "wasi", feature = "std"), feature(wasi_ext))]
#![cfg_attr(core_ffi_c, feature(core_ffi_c))]
#![cfg_attr(core_c_str, feature(core_c_str))]
#![cfg_attr(alloc_c_string, feature(alloc_ffi))]
#![cfg_attr(alloc_c_string, feature(alloc_c_string))]
#![cfg_attr(alloc_ffi, feature(alloc_ffi))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "rustc-dep-of-std", feature(ip))]
#![cfg_attr(
Expand Down
13 changes: 12 additions & 1 deletion src/net/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,14 +1286,25 @@ bitflags! {
#[test]
fn test_sizes() {
use c::c_int;
use core::mem::size_of;
use core::mem::{size_of, transmute};

// Backend code needs to cast these to `c_int` so make sure that cast
// isn't lossy.
assert_eq!(size_of::<RawProtocol>(), size_of::<c_int>());
assert_eq!(size_of::<Protocol>(), size_of::<c_int>());
assert_eq!(size_of::<Option<RawProtocol>>(), size_of::<c_int>());
assert_eq!(size_of::<Option<Protocol>>(), size_of::<c_int>());
assert_eq!(size_of::<RawSocketType>(), size_of::<c_int>());
assert_eq!(size_of::<SocketType>(), size_of::<c_int>());
assert_eq!(size_of::<SocketFlags>(), size_of::<c_int>());

// Rustix doesn't depend on `Option<Protocol>` matching the ABI of
// a raw integer for correctness, but it should work nonetheless.
unsafe {
let t: Option<Protocol> = None;
assert_eq!(0_u32, transmute(t));

let t: Option<Protocol> = Some(Protocol::from_raw(RawProtocol::new(4567).unwrap()));
assert_eq!(4567_u32, transmute(t));
}
}
13 changes: 12 additions & 1 deletion src/pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,19 @@ impl Pid {

#[test]
fn test_sizes() {
use core::mem::size_of;
use core::mem::{size_of, transmute};

assert_eq!(size_of::<RawPid>(), size_of::<NonZeroI32>());
assert_eq!(size_of::<RawPid>(), size_of::<Pid>());
assert_eq!(size_of::<RawPid>(), size_of::<Option<Pid>>());

// Rustix doesn't depend on `Option<Pid>` matching the ABI of a raw integer
// for correctness, but it should work nonetheless.
unsafe {
let t: Option<Pid> = None;
assert_eq!(0 as RawPid, transmute(t));

let t: Option<Pid> = Some(Pid::from_raw_unchecked(4567));
assert_eq!(4567 as RawPid, transmute(t));
}
}
22 changes: 22 additions & 0 deletions tests/mm/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,25 @@ fn test_msync() {
munmap(addr, 8192).unwrap();
}
}

#[cfg(any(target_os = "emscripten", target_os = "linux"))]
#[test]
fn test_mremap() {
use rustix::mm::{mmap_anonymous, mremap, munmap, MapFlags, MremapFlags, ProtFlags};
use std::ptr::null_mut;

unsafe {
let addr = mmap_anonymous(null_mut(), 8192, ProtFlags::READ, MapFlags::PRIVATE).unwrap();

assert_eq!(
mremap(addr, 4096, 16384, MremapFlags::empty()),
Err(rustix::io::Errno::NOMEM)
);
let new = mremap(addr, 4096, 16384, MremapFlags::MAYMOVE).unwrap();
assert_ne!(new, addr);
assert!(!new.is_null());

munmap(new, 16384).unwrap();
munmap(addr.offset(4096), 4096).unwrap();
}
}
23 changes: 23 additions & 0 deletions tests/net/connect_bind_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,26 @@ fn net_v6_acceptfrom() {

assert_eq!(request, &response[..n]);
}

/// Test `shutdown`.
#[test]
fn net_shutdown() {
let localhost = IpAddr::V4(Ipv4Addr::LOCALHOST);
let addr = SocketAddr::new(localhost, 0);
let listener = rustix::net::socket(AddressFamily::INET, SocketType::STREAM, None).unwrap();
rustix::net::bind(&listener, &addr).expect("bind");
rustix::net::listen(&listener, 1).expect("listen");

let local_addr = rustix::net::getsockname(&listener).unwrap();

let sender = rustix::net::socket(AddressFamily::INET, SocketType::STREAM, None).unwrap();
rustix::net::connect_any(&sender, &local_addr).expect("connect");
rustix::net::shutdown(&sender, rustix::net::Shutdown::Write).expect("shutdown");

let accepted = rustix::net::accept(&listener).expect("accept");
let mut response = [0_u8; 128];
let n = rustix::net::recv(&accepted, &mut response, RecvFlags::empty()).expect("recv");
assert_eq!(n, 0);

drop(sender);
}
1 change: 1 addition & 0 deletions tests/process/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn test_getpid() {
#[test]
fn test_getppid() {
assert_eq!(process::getppid(), process::getppid());
assert_ne!(process::getppid(), Some(process::getpid()));
unsafe {
assert_eq!(
process::Pid::as_raw(process::getppid()) as libc::pid_t,
Expand Down
2 changes: 2 additions & 0 deletions tests/system/sysinfo.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#[test]
#[allow(unused_comparisons)]
fn test_sysinfo() {
let sysinfo: rustix::system::Sysinfo = rustix::system::sysinfo();

// Values can vary, but we can test a few simple things.
assert!(sysinfo.uptime >= 0);
assert!(sysinfo.totalram > 0);
assert!(sysinfo.procs > 0);
}
3 changes: 3 additions & 0 deletions tests/system/uname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ fn test_uname() {

#[cfg(linux_kernel)]
assert!(!name.domainname().to_bytes().is_empty());

#[cfg(linux_kernel)]
assert_eq!(name.sysname(), rustix::cstr!("Linux"));
}

0 comments on commit 7cb06d9

Please sign in to comment.