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
// 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::expect::{expect_call, Status};
use anyhow::Error;
use fidl::endpoints::{ClientEnd, ServerEnd};
use fidl_fuchsia_bluetooth::Uuid as FidlUuid;
use fidl_fuchsia_bluetooth_gatt2::{
    self as gatt2, Characteristic, CharacteristicNotifierMarker, ClientControlHandle, ClientMarker,
    ClientProxy, ClientRequest, ClientRequestStream, Handle, ReadByTypeResult, RemoteServiceMarker,
    RemoteServiceProxy, RemoteServiceRequest, RemoteServiceRequestStream, ServiceHandle,
};
use fuchsia_bluetooth::types::Uuid;
use fuchsia_zircon::Duration;

/// Provides a simple mock implementation of `fuchsia.bluetooth.gatt2.RemoteService`.
pub struct RemoteServiceMock {
    stream: RemoteServiceRequestStream,
    timeout: Duration,
}

impl RemoteServiceMock {
    pub fn new(timeout: Duration) -> Result<(RemoteServiceProxy, RemoteServiceMock), Error> {
        let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<RemoteServiceMarker>()?;
        Ok((proxy, RemoteServiceMock { stream, timeout }))
    }

    pub fn from_stream(stream: RemoteServiceRequestStream, timeout: Duration) -> RemoteServiceMock {
        RemoteServiceMock { stream, timeout }
    }

    pub async fn expect_discover_characteristics(
        &mut self,
        characteristics: &Vec<Characteristic>,
    ) -> Result<(), Error> {
        expect_call(&mut self.stream, self.timeout, move |req| match req {
            RemoteServiceRequest::DiscoverCharacteristics { responder } => {
                match responder.send(characteristics) {
                    Ok(_) => Ok(Status::Satisfied(())),
                    Err(e) => Err(e.into()),
                }
            }
            _ => Ok(Status::Pending),
        })
        .await
    }

    /// Wait until a Read By Type message is received with the given `uuid`. `result` will be sent
    /// in response to the matching FIDL request.
    pub async fn expect_read_by_type(
        &mut self,
        expected_uuid: Uuid,
        result: Result<&[ReadByTypeResult], gatt2::Error>,
    ) -> Result<(), Error> {
        let expected_uuid: FidlUuid = expected_uuid.into();
        expect_call(&mut self.stream, self.timeout, move |req| {
            if let RemoteServiceRequest::ReadByType { uuid, responder } = req {
                if uuid == expected_uuid {
                    responder.send(result)?;
                    Ok(Status::Satisfied(()))
                } else {
                    // Send error to unexpected request.
                    responder.send(Err(fidl_fuchsia_bluetooth_gatt2::Error::UnlikelyError))?;
                    Ok(Status::Pending)
                }
            } else {
                Ok(Status::Pending)
            }
        })
        .await
    }

    pub async fn expect_register_characteristic_notifier(
        &mut self,
        handle: Handle,
    ) -> Result<ClientEnd<CharacteristicNotifierMarker>, Error> {
        expect_call(&mut self.stream, self.timeout, move |req| match req {
            RemoteServiceRequest::RegisterCharacteristicNotifier {
                handle: h,
                notifier,
                responder,
            } => {
                if h == handle {
                    responder.send(Ok(()))?;
                    Ok(Status::Satisfied(notifier))
                } else {
                    responder.send(Err(gatt2::Error::InvalidHandle))?;
                    Ok(Status::Pending)
                }
            }
            _ => Ok(Status::Pending),
        })
        .await
    }
}

/// Mock for the fuchsia.bluetooth.gatt2/Client server. Can be used to expect and intercept requests
/// to connect to GATT services.
pub struct ClientMock {
    stream: ClientRequestStream,
    timeout: Duration,
}

impl ClientMock {
    pub fn new(timeout: Duration) -> Result<(ClientProxy, ClientMock), Error> {
        let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<ClientMarker>()?;
        Ok((proxy, ClientMock { stream, timeout }))
    }

    pub async fn expect_connect_to_service(
        &mut self,
        handle: ServiceHandle,
    ) -> Result<(ClientControlHandle, ServerEnd<RemoteServiceMarker>), Error> {
        expect_call(&mut self.stream, self.timeout, move |req| match req {
            ClientRequest::ConnectToService { handle: h, service, control_handle }
                if h == handle =>
            {
                Ok(Status::Satisfied((control_handle, service)))
            }
            _ => Ok(Status::Pending),
        })
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use {crate::timeout_duration, futures::join};

    #[fuchsia_async::run_until_stalled(test)]
    async fn test_expect_read_by_type() {
        let (proxy, mut mock) =
            RemoteServiceMock::new(timeout_duration()).expect("failed to create mock");
        let uuid = Uuid::new16(0x180d);
        let result = Ok(&[][..]);

        let fidl_uuid: FidlUuid = uuid.clone().into();
        let read_by_type_fut = proxy.read_by_type(&fidl_uuid);
        let expect_fut = mock.expect_read_by_type(uuid, result);

        let (read_by_type_result, expect_result) = join!(read_by_type_fut, expect_fut);
        let _ = read_by_type_result.expect("read by type request failed");
        let _ = expect_result.expect("expectation not satisfied");
    }
}