Skip to content

Commit

Permalink
Use '_available' attribute to track ADB connection (#18)
Browse files Browse the repository at this point in the history
* Use '_available' attribute to track ADB connection

* Remove 'TcpHandle.available' property

* Check that 'available' property is False after failed connect attempts
  • Loading branch information
JeffLIrion authored Oct 5, 2019
1 parent da4542c commit 0fd2934
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 11 additions & 3 deletions adb_shell/adb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class AdbDevice(object):
Attributes
----------
_available : bool
Whether an ADB connection to the device has been established
_banner : str
The hostname of the machine where the Python interpreter is currently running
_banner_bytes : bytearray
Expand All @@ -71,22 +73,25 @@ def __init__(self, serial, banner=None, default_timeout_s=None):

self._handle = TcpHandle(self._serial, default_timeout_s)

self._available = False

@property
def available(self):
"""Whether or not the socket connection is established.
"""Whether or not an ADB connection to the device has been established.
Returns
-------
bool
:attr:`adb_shell.tcp_handle.TcpHandle.available`
``self._available``
"""
return self._handle.available
return self._available

def close(self):
"""Close the socket connection via :meth:`adb_shell.tcp_handle.TcpHandle.close`.
"""
self._available = False
self._handle.close()

def connect(self, rsa_keys=None, timeout_s=None, auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S, total_timeout_s=constants.DEFAULT_TOTAL_TIMEOUT_S):
Expand Down Expand Up @@ -146,6 +151,7 @@ def connect(self, rsa_keys=None, timeout_s=None, auth_timeout_s=constants.DEFAUL

# 4. If ``cmd`` is not ``b'AUTH'``, then authentication is not necesary and so we are done
if cmd != constants.AUTH:
self._available = True
return True # return banner

# 5. If no ``rsa_keys`` are provided, raise an exception
Expand All @@ -168,6 +174,7 @@ def connect(self, rsa_keys=None, timeout_s=None, auth_timeout_s=constants.DEFAUL

# 6.4. If ``cmd`` is ``b'CNXN'``, return ``banner``
if cmd == constants.CNXN:
self._available = True
return True # return banner

# 7. None of the keys worked, so send ``rsa_keys[0]``'s public key; if the response does not time out, we must have connected successfully
Expand All @@ -178,6 +185,7 @@ def connect(self, rsa_keys=None, timeout_s=None, auth_timeout_s=constants.DEFAUL
self._send(msg, timeout_s)

cmd, arg0, _, banner = self._read([constants.CNXN], auth_timeout_s, total_timeout_s)
self._available = True
return True # return banner

def shell(self, command, timeout_s=None, total_timeout_s=constants.DEFAULT_TOTAL_TIMEOUT_S):
Expand Down
12 changes: 0 additions & 12 deletions adb_shell/tcp_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ def __init__(self, serial, default_timeout_s=None):

self._connection = None

@property
def available(self):
"""Whether the socket connection has been created.
Returns
-------
bool
Whether the connection has been created
"""
return bool(self._connection)

def close(self):
"""Close the socket connection.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_adb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def test_connect_no_keys(self):
with self.assertRaises(exceptions.DeviceAuthError):
self.device.connect()

self.assertFalse(self.device.available)

def test_connect_with_key_invalid_response(self):
with patch('adb_shell.auth.sign_pythonrsa.open', open_priv_pub), patch('adb_shell.auth.keygen.open', open_priv_pub):
keygen('tests/adbkey')
Expand All @@ -88,6 +90,8 @@ def test_connect_with_key_invalid_response(self):
with self.assertRaises(exceptions.InvalidResponseError):
self.device.connect([signer])

self.assertFalse(self.device.available)

def test_connect_with_key(self):
with patch('adb_shell.auth.sign_pythonrsa.open', open_priv_pub), patch('adb_shell.auth.keygen.open', open_priv_pub):
keygen('tests/adbkey')
Expand Down

0 comments on commit 0fd2934

Please sign in to comment.