summaryrefslogtreecommitdiffstats
path: root/tests/ui-fulldeps/std
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui-fulldeps/std')
-rw-r--r--tests/ui-fulldeps/std/issue-15149.rs57
-rw-r--r--tests/ui-fulldeps/std/issue-81357-unsound-file-methods.rs81
-rw-r--r--tests/ui-fulldeps/std/stdio-from.rs69
-rw-r--r--tests/ui-fulldeps/std/switch-stdout.rs51
4 files changed, 258 insertions, 0 deletions
diff --git a/tests/ui-fulldeps/std/issue-15149.rs b/tests/ui-fulldeps/std/issue-15149.rs
new file mode 100644
index 000000000..064472f57
--- /dev/null
+++ b/tests/ui-fulldeps/std/issue-15149.rs
@@ -0,0 +1,57 @@
+// run-pass
+
+#![allow(unused_variables)]
+// no-prefer-dynamic
+// ignore-cross-compile
+
+use std::env;
+use std::ffi::OsStr;
+use std::fs;
+use std::path::PathBuf;
+use std::process;
+use std::str;
+
+fn main() {
+ // If we're the child, make sure we were invoked correctly
+ let args: Vec<String> = env::args().collect();
+ if args.len() > 1 && args[1] == "child" {
+ // FIXME: This should check the whole `args[0]` instead of just
+ // checking that it ends_with the executable name. This
+ // is needed because of Windows, which has a different behavior.
+ // See #15149 for more info.
+ let my_path = env::current_exe().unwrap();
+ return assert_eq!(my_path.file_stem(), Some(OsStr::new("mytest")));
+ }
+
+ test();
+}
+
+fn test() {
+ // If we're the parent, copy our own binary to a new directory.
+ let my_path = env::current_exe().unwrap();
+ let my_dir = my_path.parent().unwrap();
+
+ let child_dir = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
+ let child_dir = child_dir.join("issue-15140-child");
+ fs::create_dir_all(&child_dir).unwrap();
+
+ let child_path = child_dir.join(&format!("mytest{}", env::consts::EXE_SUFFIX));
+ fs::copy(&my_path, &child_path).unwrap();
+
+ // Append the new directory to our own PATH.
+ let path = {
+ let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap()).collect();
+ paths.push(child_dir.to_path_buf());
+ env::join_paths(paths).unwrap()
+ };
+
+ let child_output =
+ process::Command::new("mytest").env("PATH", &path).arg("child").output().unwrap();
+
+ assert!(
+ child_output.status.success(),
+ "child assertion failed\n child stdout:\n {}\n child stderr:\n {}",
+ str::from_utf8(&child_output.stdout).unwrap(),
+ str::from_utf8(&child_output.stderr).unwrap()
+ );
+}
diff --git a/tests/ui-fulldeps/std/issue-81357-unsound-file-methods.rs b/tests/ui-fulldeps/std/issue-81357-unsound-file-methods.rs
new file mode 100644
index 000000000..fdf1150f8
--- /dev/null
+++ b/tests/ui-fulldeps/std/issue-81357-unsound-file-methods.rs
@@ -0,0 +1,81 @@
+// run-fail
+// only-windows
+
+fn main() {
+ use std::fs;
+ use std::io::prelude::*;
+ use std::os::windows::prelude::*;
+ use std::ptr;
+ use std::sync::Arc;
+ use std::thread;
+ use std::time::Duration;
+
+ const FILE_FLAG_OVERLAPPED: u32 = 0x40000000;
+
+ fn create_pipe_server(path: &str) -> fs::File {
+ let mut path0 = path.as_bytes().to_owned();
+ path0.push(0);
+ extern "system" {
+ fn CreateNamedPipeA(
+ lpName: *const u8,
+ dwOpenMode: u32,
+ dwPipeMode: u32,
+ nMaxInstances: u32,
+ nOutBufferSize: u32,
+ nInBufferSize: u32,
+ nDefaultTimeOut: u32,
+ lpSecurityAttributes: *mut u8,
+ ) -> RawHandle;
+ }
+
+ unsafe {
+ let h = CreateNamedPipeA(path0.as_ptr(), 3, 0, 1, 0, 0, 0, ptr::null_mut());
+ assert_ne!(h as isize, -1);
+ fs::File::from_raw_handle(h)
+ }
+ }
+
+ let path = "\\\\.\\pipe\\repro";
+ let mut server = create_pipe_server(path);
+
+ let client = Arc::new(
+ fs::OpenOptions::new().custom_flags(FILE_FLAG_OVERLAPPED).read(true).open(path).unwrap(),
+ );
+
+ let spawn_read = |is_first: bool| {
+ thread::spawn({
+ let f = client.clone();
+ move || {
+ let mut buf = [0xcc; 1];
+ let mut f = f.as_ref();
+ f.read(&mut buf).unwrap();
+ if is_first {
+ assert_ne!(buf[0], 0xcc);
+ } else {
+ let b = buf[0]; // capture buf[0]
+ thread::sleep(Duration::from_millis(200));
+
+ // Check the buffer hasn't been written to after read.
+ dbg!(buf[0], b);
+ assert_eq!(buf[0], b);
+ }
+ }
+ })
+ };
+
+ let t1 = spawn_read(true);
+ thread::sleep(Duration::from_millis(20));
+ let t2 = spawn_read(false);
+ thread::sleep(Duration::from_millis(100));
+ let _ = server.write(b"x");
+ thread::sleep(Duration::from_millis(100));
+ let _ = server.write(b"y");
+
+ // This is run fail because we need to test for the `abort`.
+ // That failing to run is the success case.
+ if t1.join().is_err() || t2.join().is_err() {
+ return;
+ } else {
+ panic!("success");
+ }
+}
diff --git a/tests/ui-fulldeps/std/stdio-from.rs b/tests/ui-fulldeps/std/stdio-from.rs
new file mode 100644
index 000000000..fef9f27fc
--- /dev/null
+++ b/tests/ui-fulldeps/std/stdio-from.rs
@@ -0,0 +1,69 @@
+// run-pass
+// ignore-cross-compile
+
+use std::env;
+use std::fs::File;
+use std::io;
+use std::io::{Read, Write};
+use std::process::{Command, Stdio};
+use std::path::PathBuf;
+
+fn main() {
+ if env::args().len() > 1 {
+ child().unwrap()
+ } else {
+ parent().unwrap()
+ }
+}
+
+fn parent() -> io::Result<()> {
+ let td = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
+ let input = td.join("stdio-from-input");
+ let output = td.join("stdio-from-output");
+
+ File::create(&input)?.write_all(b"foo\n")?;
+
+ // Set up this chain:
+ // $ me <file | me | me >file
+ // ... to duplicate each line 8 times total.
+
+ let mut child1 = Command::new(env::current_exe()?)
+ .arg("first")
+ .stdin(File::open(&input)?) // tests File::into()
+ .stdout(Stdio::piped())
+ .spawn()?;
+
+ let mut child3 = Command::new(env::current_exe()?)
+ .arg("third")
+ .stdin(Stdio::piped())
+ .stdout(File::create(&output)?) // tests File::into()
+ .spawn()?;
+
+ // Started out of order so we can test both `ChildStdin` and `ChildStdout`.
+ let mut child2 = Command::new(env::current_exe()?)
+ .arg("second")
+ .stdin(child1.stdout.take().unwrap()) // tests ChildStdout::into()
+ .stdout(child3.stdin.take().unwrap()) // tests ChildStdin::into()
+ .spawn()?;
+
+ assert!(child1.wait()?.success());
+ assert!(child2.wait()?.success());
+ assert!(child3.wait()?.success());
+
+ let mut data = String::new();
+ File::open(&output)?.read_to_string(&mut data)?;
+ for line in data.lines() {
+ assert_eq!(line, "foo");
+ }
+ assert_eq!(data.lines().count(), 8);
+ Ok(())
+}
+
+fn child() -> io::Result<()> {
+ // double everything
+ let mut input = vec![];
+ io::stdin().read_to_end(&mut input)?;
+ io::stdout().write_all(&input)?;
+ io::stdout().write_all(&input)?;
+ Ok(())
+}
diff --git a/tests/ui-fulldeps/std/switch-stdout.rs b/tests/ui-fulldeps/std/switch-stdout.rs
new file mode 100644
index 000000000..e9501a809
--- /dev/null
+++ b/tests/ui-fulldeps/std/switch-stdout.rs
@@ -0,0 +1,51 @@
+// run-pass
+
+use std::env;
+use std::fs::File;
+use std::io::{Read, Write};
+use std::path::PathBuf;
+
+#[cfg(unix)]
+fn switch_stdout_to(file: File) {
+ use std::os::unix::prelude::*;
+
+ extern "C" {
+ fn dup2(old: i32, new: i32) -> i32;
+ }
+
+ unsafe {
+ assert_eq!(dup2(file.as_raw_fd(), 1), 1);
+ }
+}
+
+#[cfg(windows)]
+fn switch_stdout_to(file: File) {
+ use std::os::windows::prelude::*;
+
+ extern "system" {
+ fn SetStdHandle(nStdHandle: u32, handle: *mut u8) -> i32;
+ }
+
+ const STD_OUTPUT_HANDLE: u32 = (-11i32) as u32;
+
+ unsafe {
+ let rc = SetStdHandle(STD_OUTPUT_HANDLE, file.into_raw_handle() as *mut _);
+ assert!(rc != 0);
+ }
+}
+
+fn main() {
+ let path = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
+ let path = path.join("switch-stdout-output");
+ let f = File::create(&path).unwrap();
+
+ println!("foo");
+ std::io::stdout().flush().unwrap();
+ switch_stdout_to(f);
+ println!("bar");
+ std::io::stdout().flush().unwrap();
+
+ let mut contents = String::new();
+ File::open(&path).unwrap().read_to_string(&mut contents).unwrap();
+ assert_eq!(contents, "bar\n");
+}