Debian Codegraph @

third_party/rust/minidump-writer/tests/common/mod.rs

raw
1 use std::error;
2 use std::io::{BufRead, BufReader, Write};
3 use std::process::{Child, Command, Stdio};
4 use std::result;
5
6 #[allow(unused)]
7 type Error = Box<dyn error::Error + std::marker::Send + std::marker::Sync>;
8 #[allow(unused)]
9 pub type Result<T> = result::Result<T, Error>;
10
11 fn build_command() -> Command {
12 let mut cmd;
13 if let Some(binary) = std::env::var_os("TEST_HELPER") {
14 cmd = Command::new(binary);
15 } else {
16 cmd = Command::new("cargo");
17 cmd.args(["run", "-q", "--bin", "test"]);
18
19 // In normal cases where the host and target are the same this won't matter,
20 // but tests will fail if you are eg running in a cross container which will
21 // likely be x86_64 but may be targetting aarch64 or i686, which will result
22 // in tests failing, or at the least not testing what you think
23 cmd.args(["--target", current_platform::CURRENT_PLATFORM, "--"]);
24 }
25
26 cmd.env("RUST_BACKTRACE", "1");
27
28 cmd
29 }
30
31 #[allow(unused)]
32 pub fn spawn_child(command: &str, args: &[&str]) {
33 let mut cmd = build_command();
34 cmd.arg(command).args(args);
35
36 let child = cmd.output().expect("failed to execute child");
37
38 println!("Child output:");
39 std::io::stdout().write_all(&child.stdout).unwrap();
40 std::io::stdout().write_all(&child.stderr).unwrap();
41 assert_eq!(child.status.code().expect("No return value"), 0);
42 }
43
44 fn start_child_and_wait_for_threads_helper(command: &str, num: usize) -> Child {
45 let mut cmd = build_command();
46 cmd.arg(command).arg(num.to_string());
47 cmd.stdout(Stdio::piped());
48
49 let mut child = cmd.spawn().expect("failed to spawn cargo");
50 wait_for_threads(&mut child, num);
51 child
52 }
53
54 #[allow(unused)]
55 pub fn start_child_and_wait_for_threads(num: usize) -> Child {
56 start_child_and_wait_for_threads_helper("spawn_and_wait", num)
57 }
58
59 #[allow(unused)]
60 pub fn start_child_and_wait_for_named_threads(num: usize) -> Child {
61 start_child_and_wait_for_threads_helper("spawn_name_wait", num)
62 }
63
64 #[allow(unused)]
65 pub fn start_child_and_wait_for_create_files(num: usize) -> Child {
66 start_child_and_wait_for_threads_helper("create_files_wait", num)
67 }
68
69 #[allow(unused)]
70 pub fn wait_for_threads(child: &mut Child, num: usize) {
71 let mut f = BufReader::new(child.stdout.as_mut().expect("Can't open stdout"));
72 let mut lines = 0;
73 while lines < num {
74 let mut buf = String::new();
75 match f.read_line(&mut buf) {
76 Ok(_) => {
77 if buf == "1\n" {
78 lines += 1;
79 }
80 }
81 Err(e) => {
82 std::panic::panic_any(e);
83 }
84 }
85 }
86 }
87
88 #[allow(unused)]
89 pub fn start_child_and_return(args: &[&str]) -> Child {
90 let mut cmd = build_command();
91 cmd.args(args);
92
93 cmd.stdout(Stdio::piped())
94 .spawn()
95 .expect("failed to execute child")
96 }
97
98 #[allow(unused)]
99 pub fn read_minidump_soft_errors_or_panic<'a, T>(
100 dump: &minidump::Minidump<'a, T>,
101 ) -> serde_json::Value
102 where
103 T: std::ops::Deref<Target = [u8]> + 'a,
104 {
105 let contents = std::str::from_utf8(
106 dump.get_raw_stream(minidump_common::format::MINIDUMP_STREAM_TYPE::MozSoftErrors.into())
107 .expect("missing soft error stream"),
108 )
109 .expect("expected utf-8 stream");
110
111 serde_json::from_str(contents).expect("expected json")
112 }
113
114 #[allow(unused)]
115 pub fn assert_soft_errors_in_minidump<'a, 'b, T, I>(
116 dump: &minidump::Minidump<'a, T>,
117 expected_errors: I,
118 ) where
119 T: std::ops::Deref<Target = [u8]> + 'a,
120 I: IntoIterator<Item = &'b serde_json::Value>,
121 {
122 let actual_json = read_minidump_soft_errors_or_panic(dump);
123 let actual_errors = actual_json.as_array().unwrap();
124
125 // Ensure that every error we expect is in the actual list somewhere
126 for expected_error in expected_errors {
127 assert!(actual_errors
128 .iter()
129 .any(|actual_error| actual_error == expected_error));
130 }
131 }