1use std::any::Any;
2#[cfg(unix)]
3use std::os::unix::process::ExitStatusExt;
4use std::process::ExitStatus;
56pub use self::TestResult::*;
7use super::bench::BenchSamples;
8use super::options::ShouldPanic;
9use super::time;
10use super::types::TestDesc;
1112// Return code for secondary process.
13// Start somewhere other than 0 so we know the return code means what we think
14// it means.
15pub(crate) const TR_OK: i32 = 50;
1617// On Windows we use __fastfail to abort, which is documented to use this
18// exception code.
19#[cfg(windows)]
20const STATUS_FAIL_FAST_EXCEPTION: i32 = 0xC0000409u32 as i32;
2122// On Zircon (the Fuchsia kernel), an abort from userspace calls the
23// LLVM implementation of __builtin_trap(), e.g., ud2 on x86, which
24// raises a kernel exception. If a userspace process does not
25// otherwise arrange exception handling, the kernel kills the process
26// with this return code.
27#[cfg(target_os = "fuchsia")]
28const ZX_TASK_RETCODE_EXCEPTION_KILL: i32 = -1028;
2930#[derive(Debug, Clone, PartialEq)]
31pub enum TestResult {
32 TrOk,
33 TrFailed,
34 TrFailedMsg(String),
35 TrIgnored,
36 TrBench(BenchSamples),
37 TrTimedFail,
38}
3940/// Creates a `TestResult` depending on the raw result of test execution
41/// and associated data.
42pub(crate) fn calc_result(
43 desc: &TestDesc,
44 panic_payload: Option<&(dyn Any + Send)>,
45 time_opts: Option<&time::TestTimeOptions>,
46 exec_time: Option<&time::TestExecTime>,
47) -> TestResult {
48let result = match (desc.should_panic, panic_payload) {
49// The test did or didn't panic, as expected.
50(ShouldPanic::No, None) | (ShouldPanic::Yes, Some(_)) => TestResult::TrOk,
5152// Check the actual panic message against the expected message.
53(ShouldPanic::YesWithMessage(msg), Some(err)) => {
54let maybe_panic_str = err
55 .downcast_ref::<String>()
56 .map(|e| &**e)
57 .or_else(|| err.downcast_ref::<&'static str>().copied());
5859if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
60 TestResult::TrOk
61 } else if let Some(panic_str) = maybe_panic_str {
62 TestResult::TrFailedMsg(format!(
63r#"panic did not contain expected string
64 panic message: {panic_str:?}
65 expected substring: {msg:?}"#
66))
67 } else {
68 TestResult::TrFailedMsg(format!(
69r#"expected panic with string value,
70 found non-string value: `{:?}`
71 expected substring: {msg:?}"#,
72 (*err).type_id()
73 ))
74 }
75 }
7677// The test should have panicked, but didn't panic.
78(ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => {
79let fn_location = if !desc.source_file.is_empty() {
80&format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
81 } else {
82""
83};
84 TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
85 }
8687// The test should not have panicked, but did panic.
88(ShouldPanic::No, Some(_)) => TestResult::TrFailed,
89 };
9091// If test is already failed (or allowed to fail), do not change the result.
92if result != TestResult::TrOk {
93return result;
94 }
9596// Check if test is failed due to timeout.
97if let (Some(opts), Some(time)) = (time_opts, exec_time) {
98if opts.error_on_excess && opts.is_critical(desc, time) {
99return TestResult::TrTimedFail;
100 }
101 }
102103 result
104}
105106/// Creates a `TestResult` depending on the exit code of test subprocess.
107pub(crate) fn get_result_from_exit_code(
108 desc: &TestDesc,
109 status: ExitStatus,
110 time_opts: Option<&time::TestTimeOptions>,
111 exec_time: Option<&time::TestExecTime>,
112) -> TestResult {
113let result = match status.code() {
114Some(TR_OK) => TestResult::TrOk,
115#[cfg(windows)]
116Some(STATUS_FAIL_FAST_EXCEPTION) => TestResult::TrFailed,
117#[cfg(unix)]
118None => match status.signal() {
119Some(libc::SIGABRT) => TestResult::TrFailed,
120Some(signal) => {
121 TestResult::TrFailedMsg(format!("child process exited with signal {signal}"))
122 }
123None => unreachable!("status.code() returned None but status.signal() was None"),
124 },
125// Upon an abort, Fuchsia returns the status code ZX_TASK_RETCODE_EXCEPTION_KILL.
126#[cfg(target_os = "fuchsia")]
127Some(ZX_TASK_RETCODE_EXCEPTION_KILL) => TestResult::TrFailed,
128#[cfg(not(unix))]
129None => TestResult::TrFailedMsg(format!("unknown return code")),
130#[cfg(any(windows, unix))]
131Some(code) => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
132#[cfg(not(any(windows, unix)))]
133Some(_) => TestResult::TrFailed,
134 };
135136// If test is already failed (or allowed to fail), do not change the result.
137if result != TestResult::TrOk {
138return result;
139 }
140141// Check if test is failed due to timeout.
142if let (Some(opts), Some(time)) = (time_opts, exec_time) {
143if opts.error_on_excess && opts.is_critical(desc, time) {
144return TestResult::TrTimedFail;
145 }
146 }
147148 result
149}