def test_init(self): device_with_banner = AdbDevice('IP:5555', 'banner') self.assertEqual(device_with_banner._banner, 'banner') with patch('socket.gethostname', side_effect=Exception): device_banner_unknown = AdbDevice('IP:5555') self.assertEqual(device_banner_unknown._banner, 'unknown') self.device._handle._bulk_read = b''
def test_init_banner(self): device_with_banner = AdbDevice(handle=patchers.FakeTcpHandle('host', 5555), banner='banner') self.assertEqual(device_with_banner._banner, b'banner') device_with_banner2 = AdbDevice(handle=patchers.FakeTcpHandle('host', 5555), banner=bytearray('banner2', 'utf-8')) self.assertEqual(device_with_banner2._banner, b'banner2') device_with_banner3 = AdbDevice(handle=patchers.FakeTcpHandle('host', 5555), banner=u'banner3') self.assertEqual(device_with_banner3._banner, b'banner3') with patch('socket.gethostname', side_effect=Exception): device_banner_unknown = AdbDevice(handle=patchers.FakeTcpHandle('host', 5555)) self.assertEqual(device_banner_unknown._banner, b'unknown') # Clear the `_bulk_read` buffer so that `self.tearDown()` passes self.device._handle._bulk_read = b''
def __init__(self, host, adbkey=''): self.host = host self.adbkey = adbkey self._adb = AdbDevice(serial=self.host, default_timeout_s=9.) # keep track of whether the ADB connection is intact self._available = False # use a lock to make sure that ADB commands don't overlap self._adb_lock = threading.Lock()
def _run_in_reactor(self, func, *args, **kwargs): handle = self._connect_adb() adb = AdbDevice(handle) def auth_cb(device): self._awaiting_auth = True self._logger.info( "Waiting for connection to be accepted on the device..") def on_running(): try: signer = RSA_SIGNER(self._adbkey) if adb.connect(rsa_keys=[signer], auth_timeout_s=30, timeout_s=10, auth_callback=auth_cb): func(adb, *args, **kwargs) except WSHandleShutdown: pass except TcpTimeoutException: if self._awaiting_auth: self._logger.error( "Connection was not accepted on the device " "within 30 seconds.") else: self._logger.error("Connection to the device timed out") except Exception as exc: self._logger.debug(exc, exc_info=True) if len(exc.args) >= 1 and type(exc.args[0]) is bytearray: err = str(exc.args[0].decode('utf-8')) else: err = str(exc) if 'Read-only file system' in err: self._logger.error('Permission denied.') else: self._logger.error(err) finally: adb.close() return handle.run(on_running)
def connect(self, always_log_errors=True): """Connect to an Amazon Fire TV device. Will attempt to establish ADB connection to the given host. Failure sets state to UNKNOWN and disables sending actions. :returns: True if successful, False otherwise """ self._adb_lock.acquire(**LOCK_KWARGS) signer = None if self.adbkey: signer = Signer(self.adbkey) try: if USE_ADB_SHELL: # adb_shell self._adb_device = AdbDevice(serial=self.host) # Connect to the device connected = False if signer: connected = self._adb_device.connect(rsa_keys=[signer]) else: connected = self._adb_device.connect() self._available = connected elif not self.adb_server_ip: # python-adb try: if self.adbkey: signer = Signer(self.adbkey) # Connect to the device self._adb = adb_commands.AdbCommands().ConnectDevice( serial=self.host, rsa_keys=[signer], default_timeout_ms=9000) else: self._adb = adb_commands.AdbCommands().ConnectDevice( serial=self.host, default_timeout_ms=9000) # ADB connection successfully established self._available = True except socket_error as serr: if self._available or always_log_errors: if serr.strerror is None: serr.strerror = "Timed out trying to connect to ADB device." logging.warning( "Couldn't connect to host: %s, error: %s", self.host, serr.strerror) # ADB connection attempt failed self._adb = None self._available = False finally: return self._available else: # pure-python-adb try: self._adb_client = AdbClient(host=self.adb_server_ip, port=self.adb_server_port) self._adb_device = self._adb_client.device(self.host) self._available = bool(self._adb_device) except: self._available = False finally: return self._available finally: self._adb_lock.release()
def test_init_invalid_handle(self): with self.assertRaises(exceptions.InvalidHandleError): device = AdbDevice(handle=123) # Clear the `_bulk_read` buffer so that `self.tearDown()` passes self.device._handle._bulk_read = b''
def setUp(self): self.device = AdbDevice(handle=patchers.FakeTcpHandle('host', 5555)) self.device._handle._bulk_read = b''.join(patchers.BULK_READ_LIST)
def setUp(self): with patch('socket.gethostname', side_effect=Exception): with patchers.patch_tcp_handle: self.device = AdbDevice('IP:5555') self.device._handle._bulk_read = b''.join( patchers.BULK_READ_LIST)
def setUp(self): with patchers.patch_tcp_handle: self.device = AdbDevice('IP:5555', 'banner') self.device._handle._bulk_read = b''.join(patchers.BULK_READ_LIST)
def setUp(self): self.device = AdbDevice(transport=patchers.FakeTcpTransport('host', 5555)) self.device._transport._bulk_read = b''.join(patchers.BULK_READ_LIST)
def test_init_invalid_transport(self): with self.assertRaises(exceptions.InvalidTransportError): device = AdbDevice(transport=123) # Clear the `_bulk_read` buffer so that `self.tearDown()` passes self.device._transport._bulk_read = b''