diff --git a/adb_shell/adb_device.py b/adb_shell/adb_device.py index 84a4827..a14c946 100644 --- a/adb_shell/adb_device.py +++ b/adb_shell/adb_device.py @@ -209,7 +209,7 @@ def shell(self, command, timeout_s=None, total_timeout_s=constants.DEFAULT_TOTAL The output of the ADB shell command """ - return ''.join(self._streaming_command(b'shell', command.encode('utf8'), timeout_s, total_timeout_s)) + return b''.join(self._streaming_command(b'shell', command.encode('utf8'), timeout_s, total_timeout_s)).decode('utf8') def _okay(self, local_id, remote_id, timeout_s): """Send an ``b'OKAY'`` mesage. @@ -317,7 +317,7 @@ def _read(self, expected_cmds, timeout_s, total_timeout_s): while True: msg = self._handle.bulk_read(constants.MESSAGE_SIZE, timeout_s) - _LOGGER.debug("bulk_read(%d): %s", constants.MESSAGE_SIZE, msg) + _LOGGER.debug("bulk_read(%d): %s", constants.MESSAGE_SIZE, repr(msg)) cmd, arg0, arg1, data_length, data_checksum = unpack(msg) command = constants.WIRE_TO_ID.get(cmd) @@ -334,7 +334,7 @@ def _read(self, expected_cmds, timeout_s, total_timeout_s): data = bytearray() while data_length > 0: temp = self._handle.bulk_read(data_length, timeout_s) - _LOGGER.debug("bulk_read(%d): %s", data_length, temp) + _LOGGER.debug("bulk_read(%d): %s", data_length, repr(temp)) data += temp data_length -= len(temp) @@ -468,9 +468,9 @@ def _send(self, msg, timeout_s): Timeout in seconds for TCP packets, or ``None``; see :meth:`TcpHandle.bulk_write() ` """ - _LOGGER.debug("bulk_write: %s", msg.pack()) + _LOGGER.debug("bulk_write: %s", repr(msg.pack())) self._handle.bulk_write(msg.pack(), timeout_s) - _LOGGER.debug("bulk_write: %s", msg.data) + _LOGGER.debug("bulk_write: %s", repr(msg.data)) self._handle.bulk_write(msg.data, timeout_s) def _streaming_command(self, service, command, timeout_s, total_timeout_s): @@ -506,4 +506,4 @@ def _streaming_command(self, service, command, timeout_s, total_timeout_s): local_id, remote_id = self._open(b'%s:%s' % (service, command), timeout_s, total_timeout_s) for data in self._read_until_close(local_id, remote_id, timeout_s, total_timeout_s): - yield data.decode('utf8') + yield data diff --git a/tests/test_adb_device.py b/tests/test_adb_device.py index ea7102b..2698585 100644 --- a/tests/test_adb_device.py +++ b/tests/test_adb_device.py @@ -1,13 +1,11 @@ import logging from mock import patch -import os -import struct import sys import unittest from adb_shell import constants, exceptions from adb_shell.adb_device import AdbDevice -from adb_shell.adb_message import AdbMessage, unpack +from adb_shell.adb_message import AdbMessage from adb_shell.auth.keygen import keygen from adb_shell.auth.sign_pythonrsa import PythonRSASigner @@ -149,6 +147,32 @@ def test_shell_data_length_exceeds_max(self): self.device.shell('TEST') self.assertTrue(True) + def test_shell_multibytes_sequence_exceeds_max(self): + self.assertTrue(self.device.connect()) + + # Provide the `bulk_read` return values + msg1 = AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b'\x00') + msg2 = AdbMessage(command=constants.WRTE, arg0=1, arg1=1, data=b'0'*(constants.MAX_ADB_DATA-1) + b'\xe3\x81\x82') + msg3 = AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b'') + self.device._handle._bulk_read = b''.join([msg1.pack(), msg1.data, msg2.pack(), msg2.data, msg3.pack()]) + + res = self.device.shell('TEST') + self.assertEqual(u'0'*(constants.MAX_ADB_DATA-1) + u'\u3042', res) + + def test_shell_with_multibytes_sequence_over_two_messages(self): + self.assertTrue(self.device.connect()) + + # Provide the `bulk_read` return values + msg1 = AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b'\x00') + msg2 = AdbMessage(command=constants.WRTE, arg0=1, arg1=1, data=b'\xe3') + msg3 = AdbMessage(command=constants.WRTE, arg0=1, arg1=1, data=b'\x81\x82') + msg4 = AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b'') + self.device._handle._bulk_read = b''.join([msg1.pack(), msg1.data, msg2.pack(), msg2.data, + msg3.pack(), msg3.data, msg4.pack()]) + + res = self.device.shell('TEST') + self.assertEqual(u'\u3042', res) + def test_shell_multiple_clse(self): # https://github.com/JeffLIrion/adb_shell/issues/15#issuecomment-536795938 self.assertTrue(self.device.connect())