system_update_configurator/
service.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::bridge::{Bridge, OptOutPreference};
use anyhow::anyhow;
use fidl_fuchsia_update_config::{
    OptOutAdminError, OptOutAdminRequest, OptOutAdminRequestStream, OptOutRequest,
    OptOutRequestStream,
};
use fuchsia_component::server::{ServiceFs, ServiceObjLocal};
use futures::channel::mpsc;
use futures::prelude::*;
use tracing::warn;

/// ServiceFs, configured for single-threaded execution and handling services listed in
/// [`IncomingServices`].
pub type Fs = ServiceFs<ServiceObjLocal<'static, IncomingService>>;

/// FIDL services served by this component.
pub enum IncomingService {
    /// Read-only opt-out protocol.
    OptOut(OptOutRequestStream),

    /// Write-only opt-out protocol.
    OptOutAdmin(OptOutAdminRequestStream),
}

enum IncomingRequest {
    OptOut(OptOutRequest),
    OptOutAdmin(OptOutAdminRequest),
}

/// Register the exported FIDL services and serve incoming connections.
pub async fn serve(mut fs: Fs, storage: &mut dyn Bridge) {
    fs.dir("svc")
        .add_fidl_service(IncomingService::OptOut)
        .add_fidl_service(IncomingService::OptOutAdmin);

    serve_connections(fs, storage).await
}

async fn handle_request(req: IncomingRequest, storage: &mut dyn Bridge) {
    match req {
        IncomingRequest::OptOut(OptOutRequest::Get { responder }) => {
            let res = match storage.get_opt_out().await {
                Ok(value) => value.into(),
                Err(e) => {
                    warn!(
                        "Could not determine opt-out status, closing the request channel: {:#}",
                        anyhow!(e)
                    );
                    return;
                }
            };

            if let Err(e) = responder.send(res) {
                warn!("Could not respond to OptOut::Get request: {:#}", anyhow!(e));
            }
        }
        IncomingRequest::OptOutAdmin(OptOutAdminRequest::Set { value, responder }) => {
            let res = match storage.set_opt_out(value.into()).await {
                Ok(()) => Ok(()),
                Err(_) => Err(OptOutAdminError::Internal),
            };

            if let Err(e) = responder.send(res) {
                warn!("Could not respond to OptOut::Set request: {:#}", anyhow!(e));
            }
        }
    }
}

/// Serve incoming connections using the provided backing `storage`.
async fn serve_connections(
    connections: impl Stream<Item = IncomingService> + 'static,
    storage: &mut dyn Bridge,
) {
    let (send_requests, mut recv_requests) = mpsc::channel(1);

    // Demux N FIDL connections into a single request stream.
    let forward_requests =
        fuchsia_async::Task::local(connections.for_each_concurrent(None, move |conn| {
            let send_requests = send_requests.clone().sink_map_err(SinkError::Forward);
            async move {
                let res = match conn {
                    IncomingService::OptOut(conn) => {
                        conn.map_ok(IncomingRequest::OptOut)
                            .map_err(SinkError::Read)
                            .forward(send_requests)
                            .await
                    }
                    IncomingService::OptOutAdmin(conn) => {
                        conn.map_ok(IncomingRequest::OptOutAdmin)
                            .map_err(SinkError::Read)
                            .forward(send_requests)
                            .await
                    }
                };
                match res {
                    Ok(()) => {}
                    Err(e @ SinkError::Read(_)) => {
                        warn!("Closing request channel: {:#}", anyhow!(e))
                    }
                    Err(SinkError::Forward(_)) => {
                        // unreachable. The receive side is only closed after this task finishes.
                    }
                }
            }
        }));

    // Serve that single request stream 1 request at a time.
    while let Some(request) = recv_requests.next().await {
        handle_request(request, storage).await;
    }

    forward_requests.await
}

/// An error encountered while reading a fidl request or forwarding it to the single stream of
/// requests.
#[derive(Debug, thiserror::Error)]
enum SinkError {
    #[error("while reading the request")]
    Read(#[source] fidl::Error),

    #[error("while forwarding the request to the handler")]
    Forward(#[source] mpsc::SendError),
}

impl From<fidl_fuchsia_update_config::OptOutPreference> for OptOutPreference {
    fn from(x: fidl_fuchsia_update_config::OptOutPreference) -> Self {
        use fidl_fuchsia_update_config::OptOutPreference::*;
        match x {
            AllowAllUpdates => OptOutPreference::AllowAllUpdates,
            AllowOnlySecurityUpdates => OptOutPreference::AllowOnlySecurityUpdates,
        }
    }
}

impl From<OptOutPreference> for fidl_fuchsia_update_config::OptOutPreference {
    fn from(x: OptOutPreference) -> Self {
        use fidl_fuchsia_update_config::OptOutPreference::*;
        match x {
            OptOutPreference::AllowAllUpdates => AllowAllUpdates,
            OptOutPreference::AllowOnlySecurityUpdates => AllowOnlySecurityUpdates,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bridge;
    use assert_matches::assert_matches;
    use fidl_fuchsia_update_config::{
        OptOutAdminMarker, OptOutAdminProxy, OptOutMarker,
        OptOutPreference as FidlOptOutPreference, OptOutProxy,
    };
    use fuchsia_async::Task;

    fn spawn_serve(mut storage: impl Bridge + 'static) -> (Connector, Task<()>) {
        let (send, recv) = mpsc::unbounded();

        let svc = async move { serve_connections(recv, &mut storage).await };

        (Connector(send), Task::local(svc))
    }

    struct Connector(mpsc::UnboundedSender<IncomingService>);

    impl Connector {
        fn opt_out(&self) -> OptOutProxy {
            let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<OptOutMarker>();
            self.0.unbounded_send(IncomingService::OptOut(stream)).unwrap();
            proxy
        }
        fn opt_out_admin(&self) -> OptOutAdminProxy {
            let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<OptOutAdminMarker>();
            self.0.unbounded_send(IncomingService::OptOutAdmin(stream)).unwrap();
            proxy
        }
    }

    #[fuchsia::test]
    async fn start_stop() {
        let fs = ServiceFs::new_local();

        serve(fs, &mut bridge::testing::Error).await
    }

    #[fuchsia::test]
    async fn get_initial_state() {
        let (connector, svc) =
            spawn_serve(bridge::testing::Fake::new(OptOutPreference::AllowAllUpdates));
        assert_eq!(connector.opt_out().get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);
        drop(connector);
        svc.await;

        let (connector, svc) =
            spawn_serve(bridge::testing::Fake::new(OptOutPreference::AllowOnlySecurityUpdates));
        assert_eq!(
            connector.opt_out().get().await.unwrap(),
            FidlOptOutPreference::AllowOnlySecurityUpdates
        );
        drop(connector);
        svc.await;
    }

    #[fuchsia::test]
    async fn uses_storage_to_provide_preference() {
        let storage = bridge::testing::Fake::new(OptOutPreference::AllowAllUpdates);
        let (connector, svc) = spawn_serve(storage);

        let get1 = connector.opt_out();
        let get2 = connector.opt_out();
        let set1 = connector.opt_out_admin();

        assert_eq!(get1.get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);
        assert_eq!(get2.get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);

        assert_matches!(set1.set(FidlOptOutPreference::AllowOnlySecurityUpdates).await, Ok(Ok(())));

        assert_eq!(get1.get().await.unwrap(), FidlOptOutPreference::AllowOnlySecurityUpdates);
        assert_eq!(get2.get().await.unwrap(), FidlOptOutPreference::AllowOnlySecurityUpdates);

        assert_matches!(set1.set(FidlOptOutPreference::AllowAllUpdates).await, Ok(Ok(())));

        assert_eq!(get1.get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);
        assert_eq!(connector.opt_out().get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);

        // close proxy connections and our ability to open new ones, allowing svc to finish its
        // work and complete.
        drop(get1);
        drop(get2);
        drop(set1);
        drop(connector);
        svc.await;
    }

    #[fuchsia::test]
    async fn closes_connection_on_read_error() {
        let (storage, fail_requests) =
            bridge::testing::Fake::new_with_error_toggle(OptOutPreference::AllowAllUpdates);
        let (connector, svc) = spawn_serve(storage);

        let proxy1 = connector.opt_out();
        let proxy2 = connector.opt_out();

        // Make sure the connections are being served before we break things.
        assert_eq!(proxy1.get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);
        assert_eq!(proxy2.get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);

        fail_requests.set(true);
        assert_matches!(proxy1.get().await, Err(_));

        // The proxy that observed should be closed, and the proxy that did not should still be
        // open.
        fail_requests.set(false);
        assert_matches!(proxy1.get().await, Err(_));
        assert_eq!(proxy2.get().await.unwrap(), FidlOptOutPreference::AllowAllUpdates);

        // close proxy connections and our ability to open new ones, allowing svc to finish its
        // work and complete.
        drop(proxy2);

        drop(connector);
        svc.await;
    }

    #[fuchsia::test]
    async fn responds_with_error_on_write_error() {
        let (storage, fail_requests) =
            bridge::testing::Fake::new_with_error_toggle(OptOutPreference::AllowAllUpdates);
        let (connector, svc) = spawn_serve(storage);

        let proxy = connector.opt_out_admin();

        fail_requests.set(true);
        assert_matches!(
            proxy.set(FidlOptOutPreference::AllowAllUpdates).await,
            Ok(Err(OptOutAdminError::Internal))
        );

        // The channel should still be open.
        fail_requests.set(false);
        assert_matches!(proxy.set(FidlOptOutPreference::AllowAllUpdates).await, Ok(Ok(())));

        // close proxy connections and our ability to open new ones, allowing svc to finish its
        // work and complete.
        drop(proxy);
        drop(connector);
        svc.await;
    }
}