fuchsia_fuzzctl_test/
test.rs1use crate::controller::FakeController;
6use crate::writer::BufferSink;
7use anyhow::{Context as _, Result, anyhow, bail};
8use flex_fuchsia_fuzzer as fuzz;
9use fuchsia_fuzzctl::{Writer, create_artifact_dir, create_corpus_dir};
10use serde_json::json;
11use std::cell::RefCell;
12use std::fmt::{Debug, Display};
13use std::path::{Path, PathBuf};
14use std::rc::Rc;
15use std::{env, fs};
16use tempfile::{TempDir, tempdir};
17
18#[cfg(feature = "fdomain")]
19use std::sync::Arc;
20
21pub const TEST_URL: &str = "fuchsia-pkg://fuchsia.com/fake#meta/foo-fuzzer.cm";
22
23#[derive(Clone, Debug)]
36pub struct Test {
37 _tmp_dir: Rc<Option<TempDir>>,
40 root_dir: PathBuf,
41 url: Rc<RefCell<Option<String>>>,
42 controller: FakeController,
43 requests: Rc<RefCell<Vec<String>>>,
44 expected: Vec<Expectation>,
45 actual: Rc<RefCell<Vec<u8>>>,
46 writer: Writer<BufferSink>,
47 #[cfg(feature = "fdomain")]
48 client: Arc<flex_client::Client>,
49}
50
51#[derive(Clone, Debug)]
53pub enum Expectation {
54 Equals(String),
55 Contains(String),
56}
57
58impl Test {
59 pub fn try_new() -> Result<Self> {
65 let (tmp_dir, root_dir) = match env::var("FFX_FUZZ_TEST_ROOT_DIR") {
66 Ok(root_dir) => (None, PathBuf::from(root_dir)),
67 Err(_) => {
68 let tmp_dir = tempdir().context("failed to create test directory")?;
69 let root_dir = PathBuf::from(tmp_dir.path());
70 (Some(tmp_dir), root_dir)
71 }
72 };
73 let actual = Rc::new(RefCell::new(Vec::new()));
74 let mut writer = Writer::new(BufferSink::new(Rc::clone(&actual)));
75 writer.use_colors(false);
76
77 #[cfg(feature = "fdomain")]
78 let client = fdomain_local::local_client_empty();
79
80 Ok(Self {
81 _tmp_dir: Rc::new(tmp_dir),
82 root_dir,
83 url: Rc::new(RefCell::new(None)),
84 controller: FakeController::new(),
85 requests: Rc::new(RefCell::new(Vec::new())),
86 expected: Vec::new(),
87 actual,
88 writer,
89 #[cfg(feature = "fdomain")]
90 client,
91 })
92 }
93
94 pub fn domain(&self) -> flex_client::ClientArg {
99 #[cfg(feature = "fdomain")]
100 return Arc::clone(&self.client);
101 #[cfg(not(feature = "fdomain"))]
102 return flex_client::fidl::ZirconClient;
103 }
104
105 pub fn create_proxy<P: flex_client::fidl::ProtocolMarker>(
107 &self,
108 ) -> (P::Proxy, flex_client::fidl::ServerEnd<P>) {
109 #[cfg(feature = "fdomain")]
110 return self.client.create_proxy::<P>();
111 #[cfg(not(feature = "fdomain"))]
112 return flex_client::fidl::create_proxy::<P>();
113 }
114
115 pub fn root_dir(&self) -> &Path {
117 self.root_dir.as_path()
118 }
119
120 pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf> {
129 let path = path.as_ref();
130 let mut abspath = PathBuf::from(self.root_dir());
131 if path.is_relative() {
132 abspath.push(path);
133 } else if path.starts_with(self.root_dir()) {
134 abspath = PathBuf::from(path);
135 } else {
136 bail!(
137 "cannot create test directories outside the test root: {}",
138 path.to_string_lossy()
139 );
140 }
141 fs::create_dir_all(&abspath).with_context(|| {
142 format!("failed to create '{}' directory", abspath.to_string_lossy())
143 })?;
144 Ok(abspath)
145 }
146
147 pub fn artifact_dir(&self) -> PathBuf {
149 create_artifact_dir(&self.root_dir).unwrap()
150 }
151
152 pub fn corpus_dir(&self, corpus_type: fuzz::Corpus) -> PathBuf {
154 create_corpus_dir(&self.root_dir, corpus_type).unwrap()
155 }
156
157 pub fn write_tests_json<P: AsRef<Path>, S: AsRef<str>>(
166 &self,
167 build_dir: P,
168 contents: S,
169 ) -> Result<PathBuf> {
170 let build_dir = build_dir.as_ref();
171 let mut tests_json = PathBuf::from(build_dir);
172 tests_json.push("tests.json");
173 fs::write(&tests_json, contents.as_ref())
174 .with_context(|| format!("failed to write to '{}'", tests_json.to_string_lossy()))?;
175 Ok(tests_json)
176 }
177
178 pub fn create_tests_json<D: Display>(&self, urls: impl Iterator<Item = D>) -> Result<PathBuf> {
185 let build_dir = self
186 .create_dir("out/default")
187 .context("failed to create build directory for 'tests.json'")?;
188
189 let json_data: Vec<_> = urls
190 .map(|url| {
191 json!({
192 "test": {
193 "build_rule": "fuchsia_fuzzer_package",
194 "package_url": url.to_string()
195 }
196 })
197 })
198 .collect();
199 let json_data = json!(json_data);
200 self.write_tests_json(&build_dir, json_data.to_string()).with_context(|| {
201 format!("failed to create '{}/tests.json'", build_dir.to_string_lossy())
202 })
203 }
204
205 pub fn create_test_files<P: AsRef<Path>, D: Display>(
212 &self,
213 test_dir: P,
214 files: impl Iterator<Item = D>,
215 ) -> Result<()> {
216 let test_dir = self.create_dir(test_dir)?;
217 for filename in files {
218 let filename = filename.to_string();
219 fs::write(test_dir.join(&filename), filename.as_bytes())
220 .with_context(|| format!("failed to write to '{}'", filename))?;
221 }
222 Ok(())
223 }
224
225 pub fn url(&self) -> Rc<RefCell<Option<String>>> {
227 self.url.clone()
228 }
229
230 pub fn controller(&self) -> FakeController {
232 self.controller.clone()
233 }
234
235 pub fn record<S: AsRef<str>>(&mut self, request: S) {
237 let mut requests_mut = self.requests.borrow_mut();
238 requests_mut.push(request.as_ref().to_string());
239 }
240
241 pub fn requests(&mut self) -> Vec<String> {
246 let mut requests_mut = self.requests.borrow_mut();
247 let requests = requests_mut.clone();
248 *requests_mut = Vec::new();
249 requests
250 }
251
252 pub fn output_matches<T: AsRef<str> + Display>(&mut self, msg: T) {
254 let msg = msg.as_ref().trim().to_string();
255 if !msg.is_empty() {
256 self.expected.push(Expectation::Equals(msg));
257 }
258 }
259
260 pub fn output_includes<T: AsRef<str> + Display>(&mut self, msg: T) {
262 let msg = msg.as_ref().trim().to_string();
263 if !msg.is_empty() {
264 self.expected.push(Expectation::Contains(msg));
265 }
266 }
267
268 pub fn verify_output(&mut self) -> Result<()> {
270 let actual: Vec<u8> = {
271 let mut actual = self.actual.borrow_mut();
272 actual.drain(..).collect()
273 };
274 let actual = String::from_utf8_lossy(&actual);
275 let mut actual: Vec<String> = actual.split("\n").map(|s| s.trim().to_string()).collect();
276 actual.retain(|s| !s.is_empty());
277 let mut actual = actual.into_iter();
278
279 let mut extra = false;
281 for expectation in self.expected.drain(..) {
282 loop {
283 let line =
284 actual.next().ok_or_else(|| anyhow!("unmet expectation: {:?}", expectation))?;
285 match &expectation {
286 Expectation::Equals(msg) if line == *msg => {
287 extra = false;
288 break;
289 }
290 Expectation::Equals(_msg) if extra => continue,
291 Expectation::Equals(msg) => {
292 bail!("mismatch:\n actual=`{}`\nexpected=`{}`", line, msg)
293 }
294 Expectation::Contains(msg) => {
295 extra = true;
296 if line.contains(msg) {
297 break;
298 }
299 }
300 }
301 }
302 }
303 if !extra {
304 if let Some(line) = actual.next() {
305 bail!("unexpected line: {}", line);
306 }
307 }
308 Ok(())
309 }
310
311 pub fn writer(&self) -> &Writer<BufferSink> {
313 &self.writer
314 }
315}