1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use 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;
1718// #! Library for common utilities (mocks, definitions) for the manifest integration tests.
1920/// Process requests received in the `stream` and relay them to the provided `sender`.
21/// Logs incoming requests prefixed with the `tag`.
22pub async fn process_request_stream<S, Event>(
23mut stream: S::RequestStream,
24mut sender: mpsc::Sender<Event>,
25) where
26S: DiscoverableProtocolMarker,
27 Event: std::convert::From<<S::RequestStream as TryStream>::Ok>,
28 <S::RequestStream as TryStream>::Ok: std::fmt::Debug,
29{
30while let Some(request) = stream.try_next().await.expect("serving request stream failed") {
31info!("Received {} service request: {:?}", S::PROTOCOL_NAME, request);
32 sender.send(request.into()).await.expect("should send");
33 }
34}
3536/// Adds a handler for the FIDL service `S` which relays the ServerEnd of the service
37/// connection request to the provided `sender`.
38/// Note: This method does not process requests from the service connection. It only relays
39/// the stream to the `sender.
40pub fn add_fidl_service_handler<S, Event: 'static>(
41 fs: &mut ServiceFs<ServiceObj<'_, ()>>,
42 sender: mpsc::Sender<Event>,
43) where
44S: DiscoverableProtocolMarker,
45 Event: std::convert::From<S::RequestStream> + std::marker::Send,
46{
47let _ = fs.dir("svc").add_fidl_service(move |req_stream: S::RequestStream| {
48let mut s = sender.clone();
49 fasync::Task::local(async move {
50info!("Received connection for {}", S::PROTOCOL_NAME);
51 s.send(req_stream.into()).await.expect("should send");
52 })
53 .detach()
54 });
55}
5657/// A mock component that provides the generic service `S`. The request stream
58/// of the service is processed and any requests relayed to the provided `sender`.
59pub async fn mock_component<S, Event: 'static>(
60 sender: mpsc::Sender<Event>,
61 handles: LocalComponentHandles,
62) -> Result<(), Error>
63where
64S: 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{
69let mut fs = ServiceFs::new();
70let _ = fs.dir("svc").add_fidl_service(move |req_stream: S::RequestStream| {
71let sender_clone = sender.clone();
72info!("Received connection for {}", S::PROTOCOL_NAME);
73 fasync::Task::local(process_request_stream::<S, _>(req_stream, sender_clone)).detach();
74 });
7576let _ = fs.serve_connection(handles.outgoing_dir)?;
77 fs.collect::<()>().await;
78Ok(())
79}
8081/// Sets up a mock dev/ directory with the provided `dev_directory` topology.
82pub async fn mock_dev(
83 handles: LocalComponentHandles,
84 dev_directory: Arc<dyn Directory>,
85) -> Result<(), Error> {
86let mut fs = ServiceFs::new();
87let _ = fs.add_remote("dev", vfs::directory::serve_read_only(dev_directory));
88let _ = fs.serve_connection(handles.outgoing_dir)?;
89 fs.collect::<()>().await;
90Ok(())
91}
9293/// A mock component serving a protocol `S` on `handles`. Specifically, this services S by calling
94/// `responder` for every request of every client connection to S.
95pub async fn stateless_mock_responder<S, F>(
96 handles: LocalComponentHandles,
97 responder: F,
98) -> Result<(), anyhow::Error>
99where
100S: 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{
107let mut fs = ServiceFs::new();
108// The FIDL service's task is generated for every client connection, hence the `F: Copy` bound
109 // in order to use `responder` inside the task. F's bound could be changed to `Clone` in the
110 // future, but we chose not to do so for now to avoid unexpected implicit `clone`s.
111let _ = fs.dir("svc").add_fidl_service(
112move |mut req_stream: <S as ProtocolMarker>::RequestStream| {
113 fasync::Task::local(async move {
114let failure_msg = format!("serving {} request stream failed", S::DEBUG_NAME);
115while let Some(req) = req_stream.try_next().await.expect(&failure_msg) {
116let failed_to_respond = format!("failed to respond to req {:?}", req);
117 responder(req).expect(&failed_to_respond);
118 }
119 })
120 .detach()
121 },
122 );
123let _ = fs.serve_connection(handles.outgoing_dir)?;
124 fs.collect::<()>().await;
125Ok(())
126}
127128/// Exposes implementations of the the services used by bt-gap in the provided `ServiceFs`.
129pub fn provide_bt_gap_uses<Event>(
130 fs: &mut ServiceFs<ServiceObj<'_, ()>>,
131 sender: &mpsc::Sender<Event>,
132 handles: &LocalComponentHandles,
133) -> Result<(), Error>
134where
135Event: From<SecureStoreMarker> + From<NameProviderRequestStream> + Send + 'static,
136{
137let svc_dir = handles.clone_from_namespace("svc")?;
138let sender_clone = Some(sender.clone());
139let _ = fs.dir("svc").add_service_at(SecureStoreMarker::PROTOCOL_NAME, move |chan| {
140let mut s = sender_clone.clone();
141let svc_dir = Clone::clone(&svc_dir);
142 fasync::Task::local(async move {
143info!(
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// We only care that the Secure Store is routed correctly, so if a client connects
154 // to it more than once, we only want to report it the first time.
155if let Some(mut sender) = s.take() {
156 sender.send(Event::from(SecureStoreMarker)).await.expect("should send");
157 }
158 })
159 .detach();
160None
161});
162 add_fidl_service_handler::<NameProviderMarker, _>(fs, sender.clone());
163Ok(())
164}