| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 # pylint: disable=C0321 | 10 # pylint: disable=C0321 |
| 11 # pylint: disable=W0212 | 11 # pylint: disable=W0212 |
| 12 # pylint: disable=W0613 | 12 # pylint: disable=W0613 |
| 13 | 13 |
| 14 import collections | 14 import collections |
| 15 import datetime | 15 import datetime |
| 16 import logging | 16 import logging |
| 17 import os | 17 import os |
| 18 import re | 18 import re |
| 19 import signal | 19 import signal |
| 20 import sys | 20 import sys |
| 21 import unittest | 21 import unittest |
| 22 | 22 |
| 23 from pylib import android_commands | 23 from pylib import android_commands |
| 24 from pylib import cmd_helper |
| 24 from pylib import constants | 25 from pylib import constants |
| 25 from pylib.device import adb_wrapper | 26 from pylib.device import adb_wrapper |
| 26 from pylib.device import device_errors | 27 from pylib.device import device_errors |
| 27 from pylib.device import device_utils | 28 from pylib.device import device_utils |
| 28 from pylib.device import intent | 29 from pylib.device import intent |
| 29 from pylib.utils import mock_calls | 30 from pylib.utils import mock_calls |
| 30 | 31 |
| 31 # RunCommand from third_party/android_testrunner/run_command.py is mocked | 32 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
| 32 # below, so its path needs to be in sys.path. | 33 # below, so its path needs to be in sys.path. |
| 33 sys.path.append(os.path.join( | 34 sys.path.append(os.path.join( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 def testInitWithNone(self): | 66 def testInitWithNone(self): |
| 66 d = device_utils.DeviceUtils(None) | 67 d = device_utils.DeviceUtils(None) |
| 67 self.assertIsNone(d.old_interface.GetDevice()) | 68 self.assertIsNone(d.old_interface.GetDevice()) |
| 68 | 69 |
| 69 | 70 |
| 70 class MockTempFile(object): | 71 class MockTempFile(object): |
| 71 | 72 |
| 72 def __init__(self, name='/tmp/some/file'): | 73 def __init__(self, name='/tmp/some/file'): |
| 73 self.file = mock.MagicMock(spec=file) | 74 self.file = mock.MagicMock(spec=file) |
| 74 self.file.name = name | 75 self.file.name = name |
| 76 self.file.name_quoted = cmd_helper.SingleQuote(name) |
| 75 | 77 |
| 76 def __enter__(self): | 78 def __enter__(self): |
| 77 return self.file | 79 return self.file |
| 78 | 80 |
| 79 def __exit__(self, exc_type, exc_val, exc_tb): | 81 def __exit__(self, exc_type, exc_val, exc_tb): |
| 80 pass | 82 pass |
| 81 | 83 |
| 82 | 84 |
| 83 class _PatchedFunction(object): | 85 class _PatchedFunction(object): |
| 84 def __init__(self, patched=None, mocked=None): | 86 def __init__(self, patched=None, mocked=None): |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 | 616 |
| 615 def testNewRunShellImpl_withCwd(self): | 617 def testNewRunShellImpl_withCwd(self): |
| 616 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): | 618 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): |
| 617 self.device.RunShellCommand('ls', cwd='/some/test/path') | 619 self.device.RunShellCommand('ls', cwd='/some/test/path') |
| 618 | 620 |
| 619 def testNewRunShellImpl_withCwdQuoted(self): | 621 def testNewRunShellImpl_withCwdQuoted(self): |
| 620 with self.assertCall( | 622 with self.assertCall( |
| 621 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 623 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
| 622 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 624 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
| 623 | 625 |
| 626 def testRunShellCommand_withHugeCmd(self): |
| 627 payload = 'hi! ' * 1024 |
| 628 expected_cmd = "echo '%s'" % payload |
| 629 with self.assertCalls( |
| 630 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 631 self.device, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 632 self.call.device.PushContents(expected_cmd, '/sdcard/temp-123.sh'), |
| 633 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 634 self.assertEquals([payload], |
| 635 self.device.RunShellCommand(['echo', payload])) |
| 636 |
| 637 def testRunShellCommand_withHugeCmdAmdSU(self): |
| 638 payload = 'hi! ' * 1024 |
| 639 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload |
| 640 with self.assertCalls( |
| 641 (self.call.device.NeedsSU(), True), |
| 642 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 643 self.device, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 644 self.call.device.PushContents(expected_cmd, '/sdcard/temp-123.sh'), |
| 645 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 646 self.assertEquals( |
| 647 [payload], |
| 648 self.device.RunShellCommand(['echo', payload], as_root=True)) |
| 649 |
| 624 def testRunShellCommand_withSu(self): | 650 def testRunShellCommand_withSu(self): |
| 625 with self.assertCalls( | 651 with self.assertCalls( |
| 626 (self.call.device.NeedsSU(), True), | 652 (self.call.device.NeedsSU(), True), |
| 627 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): | 653 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): |
| 628 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 654 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
| 629 | 655 |
| 630 def testRunShellCommand_manyLines(self): | 656 def testRunShellCommand_manyLines(self): |
| 631 cmd = 'ls /some/path' | 657 cmd = 'ls /some/path' |
| 632 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 658 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
| 633 self.assertEquals(['file1', 'file2', 'file3'], | 659 self.assertEquals(['file1', 'file2', 'file3'], |
| (...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1193 as_root=True)) | 1219 as_root=True)) |
| 1194 | 1220 |
| 1195 def testReadFile_asRoot_rejected(self): | 1221 def testReadFile_asRoot_rejected(self): |
| 1196 self.device.old_interface._privileged_command_runner = None | 1222 self.device.old_interface._privileged_command_runner = None |
| 1197 self.device.old_interface._protected_file_access_method_initialized = True | 1223 self.device.old_interface._protected_file_access_method_initialized = True |
| 1198 with self.assertRaises(device_errors.CommandFailedError): | 1224 with self.assertRaises(device_errors.CommandFailedError): |
| 1199 self.device.ReadFile('/this/file/cannot.be.read.by.user', | 1225 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
| 1200 as_root=True) | 1226 as_root=True) |
| 1201 | 1227 |
| 1202 | 1228 |
| 1203 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1229 class DeviceUtilsPushContentsTest(DeviceUtilsNewImplTest): |
| 1204 | 1230 def testPushContents_success(self): |
| 1205 def testWriteFile_withPush(self): | |
| 1206 tmp_host = MockTempFile('/tmp/file/on.host') | 1231 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1207 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1232 contents = 'some interesting contents' |
| 1208 with self.assertCalls( | 1233 with self.assertCalls( |
| 1209 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1234 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1210 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1235 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1211 self.device.WriteFile('/path/to/device/file', contents) | 1236 self.device.PushContents(contents, '/path/to/device/file') |
| 1212 tmp_host.file.write.assert_called_once_with(contents) | 1237 tmp_host.file.write.assert_called_once_with(contents) |
| 1213 | 1238 |
| 1214 def testWriteFile_withPushForced(self): | 1239 def testPushContents_rejected(self): |
| 1215 tmp_host = MockTempFile('/tmp/file/on.host') | |
| 1216 contents = 'tiny contents' | |
| 1217 with self.assertCalls( | |
| 1218 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
| 1219 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | |
| 1220 self.device.WriteFile('/path/to/device/file', contents, force_push=True) | |
| 1221 tmp_host.file.write.assert_called_once_with(contents) | |
| 1222 | |
| 1223 def testWriteFile_withPushAndSU(self): | |
| 1224 tmp_host = MockTempFile('/tmp/file/on.host') | |
| 1225 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
| 1226 with self.assertCalls( | |
| 1227 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
| 1228 (self.call.device.NeedsSU(), True), | |
| 1229 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), | |
| 1230 MockTempFile('/external/path/tmp/on.device')), | |
| 1231 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), | |
| 1232 self.call.device.RunShellCommand( | |
| 1233 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], | |
| 1234 as_root=True, check_return=True)): | |
| 1235 self.device.WriteFile('/path/to/device/file', contents, as_root=True) | |
| 1236 tmp_host.file.write.assert_called_once_with(contents) | |
| 1237 | |
| 1238 def testWriteFile_withPush_rejected(self): | |
| 1239 tmp_host = MockTempFile('/tmp/file/on.host') | 1240 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1240 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1241 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1241 with self.assertCalls( | 1242 with self.assertCalls( |
| 1242 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1243 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1243 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), | 1244 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
| 1244 self.CommandError())): | 1245 self.CommandError())): |
| 1245 with self.assertRaises(device_errors.CommandFailedError): | 1246 with self.assertRaises(device_errors.CommandFailedError): |
| 1246 self.device.WriteFile('/path/to/device/file', contents) | 1247 self.device.PushContents(contents, '/path/to/device/file') |
| 1248 |
| 1249 |
| 1250 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
| 1251 |
| 1252 def testWriteFile_withPush(self): |
| 1253 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1254 with self.assertCalls( |
| 1255 self.call.device.PushContents(contents, '/path/to/device/file')): |
| 1256 self.device.WriteFile('/path/to/device/file', contents) |
| 1257 |
| 1258 def testWriteFile_withPushForced(self): |
| 1259 contents = 'tiny contents' |
| 1260 with self.assertCalls( |
| 1261 self.call.device.PushContents(contents, '/path/to/device/file')): |
| 1262 self.device.WriteFile('/path/to/device/file', contents, force_push=True) |
| 1263 |
| 1264 def testWriteFile_withPushAndSU(self): |
| 1265 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1266 with self.assertCalls( |
| 1267 (self.call.device.NeedsSU(), True), |
| 1268 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), |
| 1269 MockTempFile('/sdcard/tmp/on.device')), |
| 1270 self.call.device.PushContents(contents, '/sdcard/tmp/on.device'), |
| 1271 self.call.device.RunShellCommand( |
| 1272 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'], |
| 1273 as_root=True, check_return=True)): |
| 1274 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
| 1247 | 1275 |
| 1248 def testWriteFile_withEcho(self): | 1276 def testWriteFile_withEcho(self): |
| 1249 with self.assertCall(self.call.adb.Shell( | 1277 with self.assertCall(self.call.adb.Shell( |
| 1250 "echo -n the.contents > /test/file/to.write"), ''): | 1278 "echo -n the.contents > /test/file/to.write"), ''): |
| 1251 self.device.WriteFile('/test/file/to.write', 'the.contents') | 1279 self.device.WriteFile('/test/file/to.write', 'the.contents') |
| 1252 | 1280 |
| 1253 def testWriteFile_withEchoAndQuotes(self): | 1281 def testWriteFile_withEchoAndQuotes(self): |
| 1254 with self.assertCall(self.call.adb.Shell( | 1282 with self.assertCall(self.call.adb.Shell( |
| 1255 "echo -n 'the contents' > '/test/file/to write'"), ''): | 1283 "echo -n 'the contents' > '/test/file/to write'"), ''): |
| 1256 self.device.WriteFile('/test/file/to write', 'the contents') | 1284 self.device.WriteFile('/test/file/to write', 'the contents') |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1567 self.device = device_utils.DeviceUtils(None) | 1595 self.device = device_utils.DeviceUtils(None) |
| 1568 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1596 with self.assertCalls('adb get-serialno', 'unknown'), ( |
| 1569 self.assertRaises(device_errors.NoDevicesError)): | 1597 self.assertRaises(device_errors.NoDevicesError)): |
| 1570 str(self.device) | 1598 str(self.device) |
| 1571 | 1599 |
| 1572 | 1600 |
| 1573 if __name__ == '__main__': | 1601 if __name__ == '__main__': |
| 1574 logging.getLogger().setLevel(logging.DEBUG) | 1602 logging.getLogger().setLevel(logging.DEBUG) |
| 1575 unittest.main(verbosity=2) | 1603 unittest.main(verbosity=2) |
| 1576 | 1604 |
| OLD | NEW |