libafl_inline_c/
assert.rs

1use std::{fs, path::PathBuf, process::Command};
2
3/// `Assert` is a wrapper around the [`assert_cmd::assert::Assert`]
4/// struct.
5pub struct Assert {
6    command: assert_cmd::Command,
7    files_to_remove: Option<Vec<PathBuf>>,
8    output_path: PathBuf,
9}
10
11impl Assert {
12    pub(crate) fn new(
13        command: Command,
14        files_to_remove: Option<Vec<PathBuf>>,
15        output_path: PathBuf,
16    ) -> Self {
17        Self {
18            command: assert_cmd::Command::from_std(command),
19            files_to_remove: files_to_remove,
20            output_path: output_path,
21        }
22    }
23
24    pub fn assert(&mut self) -> assert_cmd::assert::Assert {
25        self.command.assert()
26    }
27
28    /// Shortcut to `self.assert().success()`.
29    pub fn success(&mut self) -> assert_cmd::assert::Assert {
30        self.assert().success()
31    }
32
33    /// Shortcut to `self.assert().failure()`.
34    pub fn failure(&mut self) -> assert_cmd::assert::Assert {
35        self.assert().failure()
36    }
37
38    /// Return the path that the executable was compiled to. Useful for shared object/dll compilation. 
39    pub fn output_path(&self) -> &PathBuf {
40        &self.output_path
41    }
42}
43
44impl Drop for Assert {
45    fn drop(&mut self) {
46        if let Some(files_to_remove) = &self.files_to_remove {
47            for file in files_to_remove.iter() {
48                if fs::metadata(file).is_ok() {
49                    fs::remove_file(file)
50                        .unwrap_or_else(|_| panic!("Failed to remove `{:?}`", file));
51                }
52            }
53        }
54    }
55}