• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2020 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import logging
18
19from bluetooth_packets_python3 import hci_packets
20from blueberry.tests.gd.cert.captures import SecurityCaptures
21from blueberry.tests.gd.cert.closable import Closable
22from blueberry.tests.gd.cert.closable import safeClose
23from blueberry.tests.gd.cert.event_stream import EventStream
24from blueberry.tests.gd.cert.matchers import SecurityMatchers
25from blueberry.tests.gd.cert.truth import assertThat
26from datetime import timedelta
27from google.protobuf import empty_pb2 as empty_proto
28from blueberry.facade.security.facade_pb2 import LeAuthRequirementsMessage
29from blueberry.facade.security.facade_pb2 import HelperMsgType
30
31
32class PyLeSecurity(Closable):
33    """
34        Abstraction for security tasks and GRPC calls
35    """
36
37    _ui_event_stream = None
38    _bond_event_stream = None
39    _helper_event_stream = None
40
41    def __init__(self, device):
42        logging.info("DUT: Init")
43        self._device = device
44        self._device.wait_channel_ready()
45        self._ui_event_stream = EventStream(self._device.security.FetchUiEvents(empty_proto.Empty()))
46        self._bond_event_stream = EventStream(self._device.security.FetchBondEvents(empty_proto.Empty()))
47        self._helper_event_stream = EventStream(self._device.security.FetchHelperEvents(empty_proto.Empty()))
48        self._advertising_callback_event_stream = EventStream(
49            self._device.security.FetchAdvertisingCallbackEvents(empty_proto.Empty()))
50
51    def get_ui_stream(self):
52        return self._ui_event_stream
53
54    def get_bond_stream(self):
55        return self._bond_event_stream
56
57    def get_advertising_callback_event_stream(self):
58        return self._advertising_callback_event_stream
59
60    def wait_for_ui_event_passkey(self, timeout=timedelta(seconds=3)):
61        display_passkey_capture = SecurityCaptures.DisplayPasskey()
62        assertThat(self._ui_event_stream).emits(display_passkey_capture, timeout=timeout)
63        return display_passkey_capture.get()
64
65    def wait_device_disconnect(self, address):
66        assertThat(self._helper_event_stream).emits(
67            SecurityMatchers.HelperMsg(HelperMsgType.DEVICE_DISCONNECTED, address))
68
69    def SetLeAuthRequirements(self, *args, **kwargs):
70        return self._device.security.SetLeAuthRequirements(LeAuthRequirementsMessage(*args, **kwargs))
71
72    def close(self):
73        if self._ui_event_stream is not None:
74            safeClose(self._ui_event_stream)
75        else:
76            logging.info("DUT: UI Event Stream is None!")
77
78        if self._bond_event_stream is not None:
79            safeClose(self._bond_event_stream)
80        else:
81            logging.info("DUT: Bond Event Stream is None!")
82
83        if self._helper_event_stream is not None:
84            safeClose(self._helper_event_stream)
85        else:
86            logging.info("DUT: Helper Event Stream is None!")
87
88        if self._advertising_callback_event_stream is not None:
89            safeClose(self._advertising_callback_event_stream)
90        else:
91            logging.info("DUT: Advertising Callback Event Stream is None!")
92