realmbuilder_mock_helpers/
lib.rs1use anyhow::Error;
6use fidl::endpoints::{DiscoverableProtocolMarker, ProtocolMarker, Proxy};
7use fidl_fuchsia_device::{NameProviderMarker, NameProviderRequestStream};
8use fidl_fuchsia_stash::SecureStoreMarker;
9use fuchsia_async as fasync;
10use fuchsia_component::server::{ServiceFs, ServiceObj};
11use fuchsia_component_test::LocalComponentHandles;
12use futures::channel::mpsc;
13use futures::{SinkExt, StreamExt, TryStream, TryStreamExt};
14use log::info;
15use std::sync::Arc;
16use vfs::directory::entry_container::Directory;
17
18pub async fn process_request_stream<S, Event>(
23 mut stream: S::RequestStream,
24 mut sender: mpsc::Sender<Event>,
25) where
26 S: DiscoverableProtocolMarker,
27 Event: std::convert::From<<S::RequestStream as TryStream>::Ok>,
28 <S::RequestStream as TryStream>::Ok: std::fmt::Debug,
29{
30 while let Some(request) = stream.try_next().await.expect("serving request stream failed") {
31 info!("Received {} service request: {:?}", S::PROTOCOL_NAME, request);
32 sender.send(request.into()).await.expect("should send");
33 }
34}
35
36pub fn add_fidl_service_handler<S, Event: 'static>(
41 fs: &mut ServiceFs<ServiceObj<'_, ()>>,
42 sender: mpsc::Sender<Event>,
43) where
44 S: DiscoverableProtocolMarker,
45 Event: std::convert::From<S::RequestStream> + std::marker::Send,
46{
47 let _ = fs.dir("svc").add_fidl_service(move |req_stream: S::RequestStream| {
48 let mut s = sender.clone();
49 fasync::Task::local(async move {
50 info!("Received connection for {}", S::PROTOCOL_NAME);
51 s.send(req_stream.into()).await.expect("should send");
52 })
53 .detach()
54 });
55}
56
57pub async fn mock_component<S, Event: 'static>(
60 sender: mpsc::Sender<Event>,
61 handles: LocalComponentHandles,
62) -> Result<(), Error>
63where
64 S: DiscoverableProtocolMarker,
65 Event: std::convert::From<<<S as ProtocolMarker>::RequestStream as TryStream>::Ok>
66 + std::marker::Send,
67 <<S as ProtocolMarker>::RequestStream as TryStream>::Ok: std::fmt::Debug,
68{
69 let mut fs = ServiceFs::new();
70 let _ = fs.dir("svc").add_fidl_service(move |req_stream: S::RequestStream| {
71 let sender_clone = sender.clone();
72 info!("Received connection for {}", S::PROTOCOL_NAME);
73 fasync::Task::local(process_request_stream::<S, _>(req_stream, sender_clone)).detach();
74 });
75
76 let _ = fs.serve_connection(handles.outgoing_dir)?;
77 fs.collect::<()>().await;
78 Ok(())
79}
80
81pub async fn mock_dev(
83 handles: LocalComponentHandles,
84 dev_directory: Arc<dyn Directory>,
85) -> Result<(), Error> {
86 let mut fs = ServiceFs::new();
87 let _ = fs.add_remote("dev", vfs::directory::serve_read_only(dev_directory));
88 let _ = fs.serve_connection(handles.outgoing_dir)?;
89 fs.collect::<()>().await;
90 Ok(())
91}
92
93pub async fn stateless_mock_responder<S, F>(
96 handles: LocalComponentHandles,
97 responder: F,
98) -> Result<(), anyhow::Error>
99where
100 S: DiscoverableProtocolMarker,
101 <<S as ProtocolMarker>::RequestStream as TryStream>::Ok: std::fmt::Debug,
102 F: Fn(<<S as ProtocolMarker>::RequestStream as TryStream>::Ok) -> Result<(), Error>
103 + Copy
104 + Send
105 + 'static,
106{
107 let mut fs = ServiceFs::new();
108 let _ = fs.dir("svc").add_fidl_service(
112 move |mut req_stream: <S as ProtocolMarker>::RequestStream| {
113 fasync::Task::local(async move {
114 let failure_msg = format!("serving {} request stream failed", S::DEBUG_NAME);
115 while let Some(req) = req_stream.try_next().await.expect(&failure_msg) {
116 let failed_to_respond = format!("failed to respond to req {:?}", req);
117 responder(req).expect(&failed_to_respond);
118 }
119 })
120 .detach()
121 },
122 );
123 let _ = fs.serve_connection(handles.outgoing_dir)?;
124 fs.collect::<()>().await;
125 Ok(())
126}
127
128pub fn provide_bt_gap_uses<Event>(
130 fs: &mut ServiceFs<ServiceObj<'_, ()>>,
131 sender: &mpsc::Sender<Event>,
132 handles: &LocalComponentHandles,
133) -> Result<(), Error>
134where
135 Event: From<SecureStoreMarker> + From<NameProviderRequestStream> + Send + 'static,
136{
137 let svc_dir = handles.clone_from_namespace("svc")?;
138 let sender_clone = Some(sender.clone());
139 let _ = fs.dir("svc").add_service_at(SecureStoreMarker::PROTOCOL_NAME, move |chan| {
140 let mut s = sender_clone.clone();
141 let svc_dir = Clone::clone(&svc_dir);
142 fasync::Task::local(async move {
143 info!(
144 "Proxying {} connection to real implementation",
145 SecureStoreMarker::PROTOCOL_NAME
146 );
147 fdio::service_connect_at(
148 svc_dir.as_channel().as_ref(),
149 SecureStoreMarker::PROTOCOL_NAME,
150 chan,
151 )
152 .expect("unable to forward secure store");
153 if let Some(mut sender) = s.take() {
156 sender.send(Event::from(SecureStoreMarker)).await.expect("should send");
157 }
158 })
159 .detach();
160 None
161 });
162 add_fidl_service_handler::<NameProviderMarker, _>(fs, sender.clone());
163 Ok(())
164}