Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions adb_shell/adb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -468,9 +468,9 @@ def _send(self, msg, timeout_s):
Timeout in seconds for TCP packets, or ``None``; see :meth:`TcpHandle.bulk_write() <adb_shell.tcp_handle.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):
Expand Down Expand Up @@ -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
30 changes: 27 additions & 3 deletions tests/test_adb_device.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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())
Expand Down