component_debug/storage/
mod.rs
1mod copy;
6mod delete;
7mod delete_all;
8mod list;
9mod make_directory;
10
11pub use copy::copy;
12pub use delete::delete;
13pub use delete_all::delete_all;
14pub use list::list;
15pub use make_directory::make_directory;
16
17#[cfg(test)]
18pub mod test {
19 use std::collections::HashMap;
20
21 use fidl::endpoints::{RequestStream, ServerEnd};
22 use fidl::handle::AsyncChannel;
23 use fidl_fuchsia_io as fio;
24 use fidl_fuchsia_sys2::{StorageAdminProxy, StorageAdminRequest};
25 use futures::TryStreamExt;
26 use std::fs::{create_dir, write};
27 use tempfile::tempdir;
28
29 fn setup_oneshot_fake_storage_admin<R: 'static>(mut handle_request: R) -> StorageAdminProxy
30 where
31 R: FnMut(fidl::endpoints::Request<<StorageAdminProxy as fidl::endpoints::Proxy>::Protocol>),
32 {
33 let (proxy, mut stream) = fidl::endpoints::create_proxy_and_stream::<
34 <StorageAdminProxy as fidl::endpoints::Proxy>::Protocol,
35 >();
36 fuchsia_async::Task::local(async move {
37 if let Ok(Some(req)) = stream.try_next().await {
38 handle_request(req);
39 }
40 })
41 .detach();
42 proxy
43 }
44
45 pub fn node_to_directory(object: ServerEnd<fio::NodeMarker>) -> fio::DirectoryRequestStream {
46 fio::DirectoryRequestStream::from_channel(AsyncChannel::from_channel(object.into_channel()))
47 }
48
49 pub fn node_to_file(object: ServerEnd<fio::NodeMarker>) -> fio::FileRequestStream {
50 fio::FileRequestStream::from_channel(AsyncChannel::from_channel(object.into_channel()))
51 }
52
53 pub fn setup_fake_storage_admin(
54 expected_id: &'static str,
55 setup_fake_directory_fn: fn(fio::DirectoryRequestStream),
56 ) -> StorageAdminProxy {
57 setup_oneshot_fake_storage_admin(move |req| match req {
58 StorageAdminRequest::OpenComponentStorageById { id, object, responder, .. } => {
59 assert_eq!(expected_id, id);
60 setup_fake_directory_fn(node_to_directory(object));
61 responder.send(Ok(())).unwrap();
62 }
63 _ => panic!("got unexpected {:?}", req),
64 })
65 }
66
67 pub fn create_tmp_path_string() -> String {
69 let tmp_dir = tempdir();
70 let dir = tmp_dir.as_ref().unwrap();
71 let tmp_path = String::from(dir.path().to_str().unwrap());
72 tmp_dir.expect("Could not close file").close().unwrap();
73 return tmp_path;
74 }
75
76 pub fn setup_fake_storage_admin_with_tmp(
82 expected_id: &'static str,
83 seed_files: HashMap<&'static str, &'static str>,
84 ) -> StorageAdminProxy {
85 setup_oneshot_fake_storage_admin(move |req| match req {
86 StorageAdminRequest::OpenComponentStorageById { id, object, responder, .. } => {
87 assert_eq!(expected_id, id);
88 let tmp_path = create_tmp_path_string();
89 let () = create_dir(&tmp_path).unwrap();
90
91 for (new_file, new_file_contents) in seed_files.iter() {
92 let new_file_path = format!("{}/{}", tmp_path, new_file);
93 write(&new_file_path, new_file_contents).unwrap();
94 }
95
96 fuchsia_fs::directory::open_channel_in_namespace(
97 &tmp_path,
98 fio::PERM_READABLE | fio::PERM_WRITABLE | fio::Flags::PROTOCOL_DIRECTORY,
99 ServerEnd::new(object.into_channel()),
100 )
101 .unwrap();
102 responder.send(Ok(())).unwrap();
103 }
104 _ => panic!("got unexpected {:?}", req),
105 })
106 }
107}