Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SGX target: fix std unit tests #59136

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ wasm-bindgen-threads = []
# https://github.com/rust-lang-nursery/stdsimd/blob/master/crates/std_detect/Cargo.toml
std_detect_file_io = []
std_detect_dlsym_getauxval = []

[package.metadata.fortanix-sgx]
# Maximum possible number of threads when testing
threads = 125
3 changes: 2 additions & 1 deletion src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ mod tests {
use crate::path::Path;

#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
#[cfg_attr(any(target_os = "emscripten", target_env = "sgx"), ignore)]
fn test_self_exe_path() {
let path = current_exe();
assert!(path.is_ok());
Expand All @@ -989,6 +989,7 @@ mod tests {
fn test() {
assert!((!Path::new("test-path").is_absolute()));

#[cfg(not(target_env = "sgx"))]
current_dir().unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2095,7 +2095,7 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
}
}

#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten", target_env = "sgx"))))]
mod tests {
use crate::io::prelude::*;

Expand Down
6 changes: 6 additions & 0 deletions src/libstd/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,10 @@ mod tests {
assert_eq!(Ok(vec![a]), tsa(("2a02:6b8:0:1::1", 53)));

let a = sa4(Ipv4Addr::new(127, 0, 0, 1), 23924);
#[cfg(not(target_env = "sgx"))]
assert!(tsa(("localhost", 23924)).unwrap().contains(&a));
#[cfg(target_env = "sgx")]
let _ = a;
}

#[test]
Expand All @@ -953,7 +956,10 @@ mod tests {
assert_eq!(Ok(vec![a]), tsa("[2a02:6b8:0:1::1]:53"));

let a = sa4(Ipv4Addr::new(127, 0, 0, 1), 23924);
#[cfg(not(target_env = "sgx"))]
assert!(tsa("localhost:23924").unwrap().contains(&a));
#[cfg(target_env = "sgx")]
let _ = a;
}

#[test]
Expand Down
62 changes: 47 additions & 15 deletions src/libstd/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,12 @@ impl fmt::Debug for TcpListener {

#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
mod tests {
use crate::fmt;
use crate::io::{ErrorKind, IoVec, IoVecMut};
use crate::io::prelude::*;
use crate::net::*;
use crate::net::test::{next_test_ip4, next_test_ip6};
use crate::sync::mpsc::channel;
use crate::sys_common::AsInner;
use crate::time::{Instant, Duration};
use crate::thread;

Expand Down Expand Up @@ -1129,7 +1129,7 @@ mod tests {
connect(i + 1, addr);
t!(stream.write(&[i as u8]));
});
t.join().ok().unwrap();
t.join().ok().expect("thread panicked");
}
}

Expand Down Expand Up @@ -1162,7 +1162,7 @@ mod tests {
connect(i + 1, addr);
t!(stream.write(&[99]));
});
t.join().ok().unwrap();
t.join().ok().expect("thread panicked");
}
}

Expand Down Expand Up @@ -1377,6 +1377,8 @@ mod tests {
}

#[test]
// FIXME: https://github.com/fortanix/rust-sgx/issues/110
#[cfg_attr(target_env = "sgx", ignore)]
fn shutdown_smoke() {
each_ip(&mut |addr| {
let a = t!(TcpListener::bind(&addr));
Expand All @@ -1397,6 +1399,8 @@ mod tests {
}

#[test]
// FIXME: https://github.com/fortanix/rust-sgx/issues/110
#[cfg_attr(target_env = "sgx", ignore)]
fn close_readwrite_smoke() {
each_ip(&mut |addr| {
let a = t!(TcpListener::bind(&addr));
Expand Down Expand Up @@ -1550,30 +1554,51 @@ mod tests {

#[test]
fn debug() {
let name = if cfg!(windows) {"socket"} else {"fd"};
#[cfg(not(target_env = "sgx"))]
fn render_socket_addr<'a>(addr: &'a SocketAddr) -> impl fmt::Debug + 'a {
addr
}
#[cfg(target_env = "sgx")]
fn render_socket_addr<'a>(addr: &'a SocketAddr) -> impl fmt::Debug + 'a {
addr.to_string()
}

#[cfg(unix)]
use crate::os::unix::io::AsRawFd;
#[cfg(target_env = "sgx")]
use crate::os::fortanix_sgx::io::AsRawFd;
#[cfg(not(windows))]
fn render_inner(addr: &dyn AsRawFd) -> impl fmt::Debug {
addr.as_raw_fd()
}
#[cfg(windows)]
fn render_inner(addr: &dyn crate::os::windows::io::AsRawSocket) -> impl fmt::Debug {
addr.as_raw_socket()
}

let inner_name = if cfg!(windows) {"socket"} else {"fd"};
let socket_addr = next_test_ip4();

let listener = t!(TcpListener::bind(&socket_addr));
let listener_inner = listener.0.socket().as_inner();
let compare = format!("TcpListener {{ addr: {:?}, {}: {:?} }}",
socket_addr, name, listener_inner);
render_socket_addr(&socket_addr),
inner_name,
render_inner(&listener));
assert_eq!(format!("{:?}", listener), compare);

let stream = t!(TcpStream::connect(&("localhost",
socket_addr.port())));
let stream_inner = stream.0.socket().as_inner();
let compare = format!("TcpStream {{ addr: {:?}, \
peer: {:?}, {}: {:?} }}",
stream.local_addr().unwrap(),
stream.peer_addr().unwrap(),
name,
stream_inner);
let stream = t!(TcpStream::connect(&("localhost", socket_addr.port())));
let compare = format!("TcpStream {{ addr: {:?}, peer: {:?}, {}: {:?} }}",
render_socket_addr(&stream.local_addr().unwrap()),
render_socket_addr(&stream.peer_addr().unwrap()),
inner_name,
render_inner(&stream));
assert_eq!(format!("{:?}", stream), compare);
}

// FIXME: re-enabled bitrig/openbsd tests once their socket timeout code
// no longer has rounding errors.
#[cfg_attr(any(target_os = "bitrig", target_os = "netbsd", target_os = "openbsd"), ignore)]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
#[test]
fn timeouts() {
let addr = next_test_ip4();
Expand Down Expand Up @@ -1601,6 +1626,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn test_read_timeout() {
let addr = next_test_ip4();
let listener = t!(TcpListener::bind(&addr));
Expand All @@ -1618,6 +1644,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn test_read_with_timeout() {
let addr = next_test_ip4();
let listener = t!(TcpListener::bind(&addr));
Expand Down Expand Up @@ -1661,6 +1688,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)]
fn nodelay() {
let addr = next_test_ip4();
let _listener = t!(TcpListener::bind(&addr));
Expand All @@ -1675,6 +1703,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)]
fn ttl() {
let ttl = 100;

Expand All @@ -1691,6 +1720,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)]
fn set_nonblocking() {
let addr = next_test_ip4();
let listener = t!(TcpListener::bind(&addr));
Expand All @@ -1712,6 +1742,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn peek() {
each_ip(&mut |addr| {
let (txdone, rxdone) = channel();
Expand Down Expand Up @@ -1743,6 +1774,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn connect_timeout_valid() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
Expand Down
10 changes: 7 additions & 3 deletions src/libstd/net/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ pub fn tsa<A: ToSocketAddrs>(a: A) -> Result<Vec<SocketAddr>, String> {
// all want to use ports. This function figures out which workspace
// it is running in and assigns a port range based on it.
fn base_port() -> u16 {
let cwd = env::current_dir().unwrap();
let cwd = if cfg!(target_env = "sgx") {
String::from("sgx")
} else {
env::current_dir().unwrap().into_os_string().into_string().unwrap()
};
let dirs = ["32-opt", "32-nopt",
"musl-64-opt", "cross-opt",
"64-opt", "64-nopt", "64-opt-vg", "64-debug-opt",
"all-opt", "snap3", "dist"];
"all-opt", "snap3", "dist", "sgx"];
dirs.iter().enumerate().find(|&(_, dir)| {
cwd.to_str().unwrap().contains(dir)
cwd.contains(dir)
}).map(|p| p.0).unwrap_or(0) as u16 * 1000 + 19600
}
2 changes: 1 addition & 1 deletion src/libstd/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ impl fmt::Debug for UdpSocket {
}
}

#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten", target_env = "sgx"))))]
mod tests {
use crate::io::ErrorKind;
use crate::net::*;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3801,7 +3801,7 @@ mod tests {
});
);

if cfg!(unix) {
if cfg!(unix) || cfg!(all(target_env = "sgx", target_vendor = "fortanix")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me all other places check target_env, but only in this file target_vendor too is checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other places where target_vendor is used already, like https://github.com/rust-lang/rust/blob/master/src/libstd/sys_common/mod.rs#L54

I choose all(target_vendor = "fortanix", target_env = "sgx") or target_env = "sgx" on a best-effort basis depending on the particular functionality. However, since there are no upstream targets that are all(not(target_vendor = "fortanix"), target_env = "sgx"), I can't really say with certainty that the conditions are correct everywhere.

tp!("", "foo", "foo");
tp!("foo", "bar", "foo/bar");
tp!("foo/", "bar", "foo/bar");
Expand Down Expand Up @@ -3960,7 +3960,7 @@ mod tests {
tfn!("foo", "bar", "bar");
tfn!("foo", "", "");
tfn!("", "foo", "foo");
if cfg!(unix) {
if cfg!(unix) || cfg!(all(target_env = "sgx", target_vendor = "fortanix")) {
tfn!(".", "foo", "./foo");
tfn!("foo/", "bar", "bar");
tfn!("foo/.", "bar", "bar");
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ impl Termination for ExitCode {
}
}

#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten", target_env = "sgx"))))]
mod tests {
use crate::io::prelude::*;

Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ mod tests {

#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn wait_timeout_wait() {
let m = Arc::new(Mutex::new(()));
let c = Arc::new(Condvar::new());
Expand All @@ -724,6 +725,7 @@ mod tests {

#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn wait_timeout_until_wait() {
let m = Arc::new(Mutex::new(()));
let c = Arc::new(Condvar::new());
Expand All @@ -748,6 +750,7 @@ mod tests {

#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn wait_timeout_until_wake() {
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair_copy = pair.clone();
Expand All @@ -771,6 +774,7 @@ mod tests {

#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31
fn wait_timeout_wake() {
let m = Arc::new(Mutex::new(()));
let c = Arc::new(Condvar::new());
Expand Down
Loading