OSDN Git Service

{Py,Cert}Security: Add function to input pin code
authorMartin Brabham <optedoblivion@google.com>
Wed, 2 Dec 2020 19:39:41 +0000 (11:39 -0800)
committerMartin Brabham <optedoblivion@google.com>
Wed, 2 Dec 2020 23:35:59 +0000 (15:35 -0800)
Additionally, update the send_ui_callback to be internal
only.

Bug: 162984360
Tag: #gd-refactor
Test: cert/run --host SecurityTest
Change-Id: I088e55c883412e78accfb0d478ed0b2080871881

gd/cert/py_security.py
gd/security/cert/cert_security.py

index f70d736..98b8eb7 100644 (file)
@@ -125,7 +125,7 @@ class PySecurity(Closable):
             auth_reqs, "ERROR"))
         self._device.security.SetAuthenticationRequirements(AuthenticationRequirementsMessage(requirement=auth_reqs))
 
-    def send_ui_callback(self, address, callback_type, b, uid):
+    def __send_ui_callback(self, address, callback_type, b, uid, pin):
         """
             Send a callback from the UI as if the user pressed a button on the dialog
         """
@@ -135,6 +135,7 @@ class PySecurity(Closable):
                 message_type=callback_type,
                 boolean=b,
                 unique_id=uid,
+                pin=bytes(pin),
                 address=common.BluetoothAddressWithType(
                     address=common.BluetoothAddress(address=address),
                     type=common.BluetoothAddressTypeEnum.PUBLIC_DEVICE_ADDRESS)))
@@ -183,7 +184,14 @@ class PySecurity(Closable):
         assertThat(self._ui_event_stream).emits(get_unique_id)
         return passkey
 
-    def on_user_input(self, cert_address, reply_boolean, expected_ui_event):
+    def input_pin(self, cert_address, pin):
+        """
+            Respond to the UI event
+        """
+        self.on_user_input(
+            cert_address=cert_address, reply_boolean=True, expected_ui_event=UiMsgType.DISPLAY_PIN_ENTRY, pin=pin)
+
+    def on_user_input(self, cert_address, reply_boolean, expected_ui_event, pin=[]):
         """
             Respond to the UI event
         """
@@ -201,8 +209,8 @@ class PySecurity(Closable):
 
         logging.debug("DUT: Waiting for expected UI event")
         assertThat(self._ui_event_stream).emits(get_unique_id)
-        # TODO(optedoblivion): Make UiCallbackType dynamic for PASSKEY when added
-        self.send_ui_callback(cert_address, UiCallbackType.YES_NO, reply_boolean, ui_id)
+        callback_type = UiCallbackType.YES_NO if len(pin) == 0 else UiCallbackType.PIN
+        self.__send_ui_callback(cert_address, callback_type, reply_boolean, ui_id, pin)
 
     def get_address(self):
         return self._device.address
index e48d1bc..1446b96 100644 (file)
@@ -74,6 +74,9 @@ class CertSecurity(PySecurity):
 
     _hci = None
 
+    MAX_PIN_LENGTH = 16
+    MIN_PIN_LENGTH = 1
+
     def _enqueue_hci_command(self, command, expect_complete):
         if (expect_complete):
             self._hci.send_command_with_complete(command)
@@ -230,7 +233,23 @@ class CertSecurity(PySecurity):
             True)
         self._enqueue_hci_command(hci_packets.UserPasskeyRequestReplyBuilder(peer, passkey), True)
 
-    def send_ui_callback(self, address, callback_type, b, uid):
+    def input_pin(self, address, pin):
+        """
+            Pretend to answer the pairing dialog as a user
+        """
+        if len(pin) > self.MAX_PIN_LENGTH or len(pin) < self.MIN_PIN_LENGTH:
+            raise Exception("Pin code must be within range")
+        logging.info("Cert: Waiting for PIN request")
+        assertThat(self._hci_event_stream).emits(HciMatchers.EventWithCode(hci_packets.EventCode.PIN_CODE_REQUEST))
+        logging.info("Cert: Send user input PIN %s for %s" % (pin.decode(), address))
+        peer = address.decode('utf-8')
+        pin_list = list(pin)
+        # Pad
+        for i in range(self.MAX_PIN_LENGTH - len(pin_list)):
+            pin_list.append(0)
+        self._enqueue_hci_command(hci_packets.PinCodeRequestReplyBuilder(peer, len(pin), pin_list), True)
+
+    def __send_ui_callback(self, address, callback_type, b, uid, pin):
         """
             Pretend to answer the pairing dailog as a user
         """