fuchsia_fuzzctl/
manager.rsuse anyhow::{bail, Context as _, Result};
use fidl::endpoints::{create_proxy, ProtocolMarker};
use url::Url;
use {fidl_fuchsia_fuzzer as fuzz, zx_status as zx};
pub struct Manager {
proxy: fuzz::ManagerProxy,
}
impl Manager {
pub fn new(proxy: fuzz::ManagerProxy) -> Self {
Self { proxy }
}
pub async fn connect(&self, url: &Url) -> Result<fuzz::ControllerProxy> {
let (proxy, server_end) = create_proxy::<fuzz::ControllerMarker>();
let result = self
.proxy
.connect(url.as_str(), server_end)
.await
.context("fuchsia.fuzzer/Manager.Connect")?;
if let Err(e) = result {
bail!("fuchsia.fuzzer/Manager.Connect returned ZX_ERR_{}", zx::Status::from_raw(e));
}
Ok(proxy)
}
pub async fn get_output(&self, url: &Url, output: fuzz::TestOutput) -> Result<fidl::Socket> {
let (rx, tx) = fidl::Socket::create_stream();
let result = self
.proxy
.get_output(url.as_str(), output, tx)
.await
.context("failed to get output")?;
if let Err(e) = result {
bail!("fuchsia.fuzzer/Manager.GetOutput returned ZX_ERR_{}", zx::Status::from_raw(e));
}
Ok(rx)
}
pub async fn stop(&self, url: &Url) -> Result<bool> {
let result = self.proxy.stop(url.as_str()).await.context(fidl_name("Stop"))?;
match result {
Ok(()) => Ok(true),
Err(e) if e == zx::Status::NOT_FOUND.into_raw() => Ok(false),
Err(e) => {
bail!("fuchsia.fuzzer/Manager.Stop returned ZX_ERR_{}", zx::Status::from_raw(e))
}
}
}
}
fn fidl_name(method: &str) -> String {
format!("{}/{}", fuzz::ManagerMarker::DEBUG_NAME, method)
}
#[cfg(test)]
mod tests {
use super::Manager;
use anyhow::Result;
use fidl::endpoints::create_proxy;
use fidl_fuchsia_fuzzer as fuzz;
use fuchsia_fuzzctl_test::{create_task, serve_manager, Test, TEST_URL};
use url::Url;
#[fuchsia::test]
async fn test_connect() -> Result<()> {
let test = Test::try_new()?;
let (proxy, server_end) = create_proxy::<fuzz::ManagerMarker>();
let _task = create_task(serve_manager(server_end, test.clone()), test.writer());
let manager = Manager::new(proxy);
let url = Url::parse(TEST_URL)?;
manager.connect(&url).await?;
let actual = test.url().borrow().as_ref().map(|url| url.to_string());
let expected = Some(url.to_string());
assert_eq!(actual, expected);
Ok(())
}
#[fuchsia::test]
async fn test_stop() -> Result<()> {
let test = Test::try_new()?;
let (proxy, server_end) = create_proxy::<fuzz::ManagerMarker>();
let _task = create_task(serve_manager(server_end, test.clone()), test.writer());
let manager = Manager::new(proxy);
let url = Url::parse(TEST_URL)?;
manager.stop(&url).await?;
let actual = test.url().borrow().as_ref().map(|url| url.to_string());
let expected = Some(url.to_string());
assert_eq!(actual, expected);
Ok(())
}
}