]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unix/process/process_unix/tests.rs
New upstream version 1.60.0+dfsg1
[rustc.git] / library / std / src / sys / unix / process / process_unix / tests.rs
1 use crate::os::unix::process::{CommandExt, ExitStatusExt};
2 use crate::panic::catch_unwind;
3 use crate::process::Command;
4
5 // Many of the other aspects of this situation, including heap alloc concurrency
6 // safety etc., are tested in src/test/ui/process/process-panic-after-fork.rs
7
8 #[test]
9 fn exitstatus_display_tests() {
10 // In practice this is the same on every Unix.
11 // If some weird platform turns out to be different, and this test fails, use #[cfg].
12 use crate::os::unix::process::ExitStatusExt;
13 use crate::process::ExitStatus;
14
15 let t = |v, s| assert_eq!(s, format!("{}", <ExitStatus as ExitStatusExt>::from_raw(v)));
16
17 t(0x0000f, "signal: 15");
18 t(0x0008b, "signal: 11 (core dumped)");
19 t(0x00000, "exit status: 0");
20 t(0x0ff00, "exit status: 255");
21
22 // On MacOS, 0x0137f is WIFCONTINUED, not WIFSTOPPED. Probably *BSD is similar.
23 // https://github.com/rust-lang/rust/pull/82749#issuecomment-790525956
24 // The purpose of this test is to test our string formatting, not our understanding of the wait
25 // status magic numbers. So restrict these to Linux.
26 if cfg!(target_os = "linux") {
27 t(0x0137f, "stopped (not terminated) by signal: 19");
28 t(0x0ffff, "continued (WIFCONTINUED)");
29 }
30
31 // Testing "unrecognised wait status" is hard because the wait.h macros typically
32 // assume that the value came from wait and isn't mad. With the glibc I have here
33 // this works:
34 if cfg!(all(target_os = "linux", target_env = "gnu")) {
35 t(0x000ff, "unrecognised wait status: 255 0xff");
36 }
37 }
38
39 #[test]
40 #[cfg_attr(target_os = "emscripten", ignore)]
41 fn test_command_fork_no_unwind() {
42 let got = catch_unwind(|| {
43 let mut c = Command::new("echo");
44 c.arg("hi");
45 unsafe {
46 c.pre_exec(|| panic!("{}", "crash now!"));
47 }
48 let st = c.status().expect("failed to get command status");
49 dbg!(st);
50 st
51 });
52 dbg!(&got);
53 let status = got.expect("panic unexpectedly propagated");
54 dbg!(status);
55 let signal = status.signal().expect("expected child process to die of signal");
56 assert!(
57 signal == libc::SIGABRT
58 || signal == libc::SIGILL
59 || signal == libc::SIGTRAP
60 || signal == libc::SIGSEGV
61 );
62 }