sl4f_lib/weave/
facade.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2020 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::common_utils::common::LazyProxy;
use crate::weave::types::{PairingState, ResetConfig};
use anyhow::Error;
use fidl::endpoints::create_proxy;
use fidl_fuchsia_weave::{
    ErrorCode, FactoryDataManagerMarker, FactoryDataManagerProxy, PairingStateWatcherMarker,
    PairingStateWatcherProxy, ResetConfigFlags, StackMarker, StackProxy,
};
use serde_json::Value;

/// Perform Weave FIDL operations.
///
/// Note this object is shared among all threads created by server.
#[derive(Debug)]
pub struct WeaveFacade {
    factory_data_manager: LazyProxy<FactoryDataManagerMarker>,
    stack: LazyProxy<StackMarker>,
}

impl WeaveFacade {
    pub fn new() -> WeaveFacade {
        WeaveFacade { factory_data_manager: Default::default(), stack: Default::default() }
    }

    /// Returns the FactoryDataManager proxy provided on instantiation
    /// or establishes a new connection.
    fn factory_data_manager(&self) -> Result<FactoryDataManagerProxy, Error> {
        self.factory_data_manager.get_or_connect()
    }

    /// Returns the Stack proxy provided on instantiation or establishes a new connection.
    fn stack(&self) -> Result<StackProxy, Error> {
        self.stack.get_or_connect()
    }

    /// Returns the PairingStateWatcher proxy provided on instantiation.
    fn pairing_state_watcher(&self) -> Result<PairingStateWatcherProxy, Error> {
        let (pairing_proxy, pairing_server_end) = create_proxy::<PairingStateWatcherMarker>();
        self.stack()?.get_pairing_state_watcher(pairing_server_end)?;
        Ok(pairing_proxy)
    }

    /// Returns a string mapped from the provided Weave error code.
    fn map_weave_err(&self, code: ErrorCode) -> anyhow::Error {
        anyhow!(match code {
            ErrorCode::FileNotFound => "FileNotFound",
            ErrorCode::CryptoError => "CryptoError",
            ErrorCode::InvalidArgument => "InvalidArgument",
            ErrorCode::InvalidState => "InvalidState",
            ErrorCode::UnspecifiedError => "UnspecifiedError",
        })
    }

    /// Returns the pairing code from the FactoryDataManager proxy service.
    pub async fn get_pairing_code(&self) -> Result<Vec<u8>, Error> {
        self.factory_data_manager()?.get_pairing_code().await?.map_err(|e| self.map_weave_err(e))
    }

    /// Returns the qr code from the StackManager proxy service.
    pub async fn get_qr_code(&self) -> Result<String, Error> {
        self.stack()?
            .get_qr_code()
            .await?
            .map(|qr_code| qr_code.data)
            .map_err(|e| self.map_weave_err(e))
    }

    /// Returns the pairing state from the PairingStateWatcher service.
    pub async fn get_pairing_state(&self) -> Result<PairingState, Error> {
        let watch = self.pairing_state_watcher()?.watch_pairing_state().await;
        watch.map(|pairing_state| pairing_state.into()).map_err(anyhow::Error::from)
    }

    /// Resets Weave state by wiping the provided configurations.
    ///
    /// # Arguments
    /// * `args`: The JSON indicating the configurations to reset, in the form of a list of bytes.
    ///
    /// # JSON Format
    /// All fields are optional. Fields set to 'true' reset the corresponding configuration in
    /// weave, and fields left unset default to 'false'.
    ///
    /// {
    ///   network_config: true,
    ///   fabric_config: false,
    ///   service_config: true,
    ///   operational_credentials: false
    /// }
    pub async fn reset_config(&self, args: Value) -> Result<(), Error> {
        let flags: ResetConfig = serde_json::from_value(args)?;
        self.stack()?
            .reset_config(ResetConfigFlags::from(flags))
            .await?
            .map_err(|e| self.map_weave_err(e))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;
    use fidl::endpoints::create_proxy_and_stream;
    use fidl_fuchsia_weave::{
        FactoryDataManagerRequest, PairingStateWatcherRequest, QrCode, StackGetQrCodeResult,
        StackRequest, StackResetConfigResult,
    };
    use fuchsia_async as fasync;
    use futures::prelude::*;
    use lazy_static::lazy_static;
    use serde_json::json;

    const PAIRING_CODE: &'static [u8] = b"ABC1234";

    lazy_static! {
        static ref QR_CODE: QrCode = QrCode { data: String::from("qrcodedata") };
        static ref PAIRING_STATE: PairingState = PairingState {
            is_wlan_provisioned: Some(true),
            is_fabric_provisioned: Some(true),
            is_service_provisioned: Some(false),
            is_weave_fully_provisioned: Some(false),
            is_thread_provisioned: Some(true)
        };
        static ref RESET_CONFIG: Value = json!({
            "network_config": true,
            // "fabric_config" unset
            "service_config": true,
            // "operational_credentials" unset
        });
    }

    struct MockStackBuilder {
        expected_stack: Vec<Box<dyn FnOnce(StackRequest) + Send + 'static>>,
        expected_pair: Vec<Box<dyn FnOnce(PairingStateWatcherRequest) + Send + 'static>>,
    }

    impl MockStackBuilder {
        fn new() -> Self {
            Self { expected_stack: vec![], expected_pair: vec![] }
        }

        fn push_stack(mut self, request: impl FnOnce(StackRequest) + Send + 'static) -> Self {
            self.expected_stack.push(Box::new(request));
            self
        }

        fn push_pair(
            mut self,
            request: impl FnOnce(PairingStateWatcherRequest) + Send + 'static,
        ) -> Self {
            self.expected_pair.push(Box::new(request));
            self
        }

        fn expect_get_qr_code(self, result: StackGetQrCodeResult) -> Self {
            self.push_stack(move |req| match req {
                StackRequest::GetQrCode { responder } => {
                    responder.send(result.as_ref().map_err(|e| *e)).unwrap()
                }
                req => panic!("unexpected request: {:?}", req),
            })
        }

        fn expect_get_pairing_state(self, result: PairingState) -> Self {
            self.push_pair(move |req| match req {
                PairingStateWatcherRequest::WatchPairingState { responder } => {
                    responder.send(&fidl_fuchsia_weave::PairingState::from(result)).unwrap();
                }
            })
        }

        fn expect_reset_config(
            self,
            _expected_flags: fidl_fuchsia_weave::ResetConfigFlags,
            result: StackResetConfigResult,
        ) -> Self {
            self.push_stack(move |req| match req {
                StackRequest::ResetConfig { responder, flags } => {
                    assert_matches!(flags, _expected_flags);
                    responder.send(result).unwrap()
                }
                req => panic!("unexpected request: {:?}", req),
            })
        }

        fn build_stack(self) -> (WeaveFacade, impl Future<Output = ()>) {
            let (proxy, mut stream) = create_proxy_and_stream::<StackMarker>();
            let fut = async move {
                let _ = &self;
                for expected in self.expected_stack {
                    expected(stream.next().await.unwrap().unwrap());
                }
                assert_matches!(stream.next().await, None);
            };
            let facade = WeaveFacade::new();
            facade.stack.set(proxy).expect("just-created facade should have empty stack");
            (facade, fut)
        }
        fn build_stack_and_pairing_state_watcher(self) -> (WeaveFacade, impl Future<Output = ()>) {
            let (proxy, mut stream) = create_proxy_and_stream::<StackMarker>();
            let stream_fut = async move {
                let _ = &self;
                match stream.next().await {
                    Some(Ok(StackRequest::GetPairingStateWatcher {
                        watcher,
                        control_handle: _,
                    })) => {
                        let mut into_stream = watcher.into_stream();
                        for expected in self.expected_pair {
                            expected(into_stream.next().await.unwrap().unwrap());
                        }
                        assert_matches!(into_stream.next().await, None);
                    }
                    err => panic!("Error in request handler: {:?}", err),
                }
            };
            let facade = WeaveFacade::new();
            facade.stack.set(proxy).expect("just-created facade should have empty stack");
            (facade, stream_fut)
        }
    }

    struct MockFactoryDataManagerBuilder {
        expected: Vec<Box<dyn FnOnce(FactoryDataManagerRequest) + Send + 'static>>,
    }

    impl MockFactoryDataManagerBuilder {
        fn new() -> Self {
            Self { expected: vec![] }
        }

        fn push(
            mut self,
            request: impl FnOnce(FactoryDataManagerRequest) + Send + 'static,
        ) -> Self {
            self.expected.push(Box::new(request));
            self
        }

        fn expect_get_pairing_code(self, result: Result<&'static [u8], ErrorCode>) -> Self {
            self.push(move |req| match req {
                FactoryDataManagerRequest::GetPairingCode { responder } => {
                    responder.send(result).unwrap()
                }
                _ => {}
            })
        }

        fn build(self) -> (WeaveFacade, impl Future<Output = ()>) {
            let (proxy, mut stream) =
                fidl::endpoints::create_proxy_and_stream::<FactoryDataManagerMarker>();
            let fut = async move {
                for expected in self.expected {
                    expected(stream.next().await.unwrap().unwrap());
                }
                assert_matches!(stream.next().await, None);
            };
            let facade = WeaveFacade::new();
            facade
                .factory_data_manager
                .set(proxy)
                .expect("just-created facade should have empty fdm");
            (facade, fut)
        }
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_get_pairing_code() {
        let (facade, pairing_code_fut) =
            MockFactoryDataManagerBuilder::new().expect_get_pairing_code(Ok(PAIRING_CODE)).build();

        let facade_fut = async move {
            assert_eq!(facade.get_pairing_code().await.unwrap(), PAIRING_CODE);
        };

        future::join(facade_fut, pairing_code_fut).await;
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_get_qr_code() {
        let qr_clone: StackGetQrCodeResult = Ok((*QR_CODE).clone());
        let (facade, qr_code_fut) =
            MockStackBuilder::new().expect_get_qr_code(qr_clone).build_stack();

        let facade_fut = async move {
            assert_eq!(facade.get_qr_code().await.unwrap(), *QR_CODE.data);
        };

        future::join(facade_fut, qr_code_fut).await;
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_get_pairing_state() {
        let (facade, pairing_state_fut) = MockStackBuilder::new()
            .expect_get_pairing_state(*PAIRING_STATE)
            .build_stack_and_pairing_state_watcher();

        let facade_fut = async move {
            let pairing_state = facade.get_pairing_state().await.unwrap();
            assert_eq!(pairing_state, *PAIRING_STATE);
        };

        future::join(facade_fut, pairing_state_fut).await;
    }

    #[allow(clippy::unit_cmp)] // TODO(https://fxbug.dev/42176996)
    #[fasync::run_singlethreaded(test)]
    async fn test_reset_config() {
        let flags: ResetConfigFlags = fidl_fuchsia_weave::ResetConfigFlags::NETWORK_CONFIG
            | fidl_fuchsia_weave::ResetConfigFlags::SERVICE_CONFIG;
        let (facade, reset_config_fut) =
            MockStackBuilder::new().expect_reset_config(flags, Ok(())).build_stack();

        let facade_fut =
            async move { assert_eq!(facade.reset_config(RESET_CONFIG.clone()).await.unwrap(), ()) };

        future::join(facade_fut, reset_config_fut).await;
    }
}