vfs/directory/
test_utils.rs#[doc(hidden)]
pub mod reexport {
pub use fuchsia_async::Channel;
pub use zx_status::Status;
pub use {fidl, fidl_fuchsia_io as fio};
#[cfg(not(target_os = "fuchsia"))]
pub use fuchsia_async::emulated_handle::MessageBuf;
#[cfg(target_os = "fuchsia")]
pub use zx::MessageBuf;
}
use crate::directory::entry::DirectoryEntry;
use crate::test_utils::run::{self, AsyncServerClientTestParams};
use byteorder::{LittleEndian, WriteBytesExt};
use fidl_fuchsia_io as fio;
use futures::Future;
use std::convert::TryInto as _;
use std::io::Write;
use std::sync::Arc;
pub use run::{run_client, test_client};
pub fn run_server_client<GetClientRes>(
flags: fio::OpenFlags,
server: Arc<dyn DirectoryEntry>,
get_client: impl FnOnce(fio::DirectoryProxy) -> GetClientRes,
) where
GetClientRes: Future<Output = ()>,
{
run::run_server_client::<fio::DirectoryMarker, _, _>(flags, server, get_client)
}
pub fn test_server_client<'test_refs, GetClientRes>(
flags: fio::OpenFlags,
server: Arc<dyn DirectoryEntry>,
get_client: impl FnOnce(fio::DirectoryProxy) -> GetClientRes + 'test_refs,
) -> AsyncServerClientTestParams<'test_refs, fio::DirectoryMarker>
where
GetClientRes: Future<Output = ()> + 'test_refs,
{
run::test_server_client::<fio::DirectoryMarker, _, _>(flags, server, get_client)
}
pub struct DirentsSameInodeBuilder {
expected: Vec<u8>,
inode: u64,
}
impl DirentsSameInodeBuilder {
pub fn new(inode: u64) -> Self {
DirentsSameInodeBuilder { expected: vec![], inode }
}
pub fn add(&mut self, type_: fio::DirentType, name: &[u8]) -> &mut Self {
assert!(
name.len() <= fio::MAX_FILENAME as usize,
"Expected entry name should not exceed MAX_FILENAME ({}) bytes.\n\
Got: {:?}\n\
Length: {} bytes",
fio::MAX_FILENAME,
name,
name.len()
);
self.expected.write_u64::<LittleEndian>(self.inode).unwrap();
self.expected.write_u8(name.len().try_into().unwrap()).unwrap();
self.expected.write_u8(type_.into_primitive()).unwrap();
self.expected.write_all(name).unwrap();
self
}
pub fn into_vec(self) -> Vec<u8> {
self.expected
}
}
#[macro_export]
macro_rules! assert_rewind {
($proxy:expr) => {{
use $crate::directory::test_utils::reexport::Status;
let status = $proxy.rewind().await.expect("rewind failed");
assert_eq!(Status::from_raw(status), Status::OK);
}};
}
#[macro_export]
macro_rules! open_as_vmo_file_assert_content {
($proxy:expr, $flags:expr, $path:expr, $expected_content:expr) => {{
let file = open_get_vmo_file_proxy_assert_ok!($proxy, $flags, $path);
assert_read!(file, $expected_content);
assert_close!(file);
}};
}
#[macro_export]
macro_rules! assert_watch {
($proxy:expr, $mask:expr) => {{
use $crate::directory::test_utils::reexport::{fidl, Channel, Status};
let (client, server) = fidl::endpoints::create_endpoints();
let status = $proxy.watch($mask, 0, server).await.expect("watch failed");
assert_eq!(Status::from_raw(status), Status::OK);
Channel::from_channel(client.into_channel())
}};
}
#[macro_export]
macro_rules! assert_watch_err {
($proxy:expr, $mask:expr, $expected_status:expr) => {{
use $crate::directory::test_utils::reexport::{fidl, Status};
let (_client, server) = fidl::endpoints::create_endpoints();
let status = $proxy.watch($mask, 0, server).await.expect("watch failed");
assert_eq!(Status::from_raw(status), $expected_status);
}};
}
#[macro_export]
macro_rules! assert_watcher_one_message_watched_events {
($watcher:expr, $( { $type:tt, $name:expr $(,)* } ),* $(,)*) => {{
#[allow(unused)]
use $crate::directory::test_utils::reexport::{MessageBuf, fio::WatchEvent};
use std::convert::TryInto as _;
let mut buf = MessageBuf::new();
$watcher.recv_msg(&mut buf).await.unwrap();
let (bytes, handles) = buf.split();
assert_eq!(
handles.len(),
0,
"Received buffer with handles.\n\
Handle count: {}\n\
Buffer: {:X?}",
handles.len(),
bytes
);
let expected = &mut vec![];
$({
let type_ = assert_watcher_one_message_watched_events!(@expand_event_type $type);
let name = Vec::<u8>::from($name);
assert!(name.len() <= std::u8::MAX as usize);
expected.push(type_.into_primitive());
expected.push(name.len().try_into().unwrap());
expected.extend_from_slice(&name);
})*
assert!(bytes == *expected,
"Received buffer does not match the expectation.\n\
Expected: {:X?}\n\
Received: {:X?}\n\
Expected as UTF-8 lossy: {:?}\n\
Received as UTF-8 lossy: {:?}",
*expected, bytes,
String::from_utf8_lossy(expected), String::from_utf8_lossy(&bytes));
}};
(@expand_event_type EXISTING) => { fio::WatchEvent::Existing };
(@expand_event_type IDLE) => { fio::WatchEvent::Idle };
(@expand_event_type ADDED) => { fio::WatchEvent::Added };
(@expand_event_type REMOVED) => { fio::WatchEvent::Removed };
}