| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), | 99 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), |
| 99 (0, '123\n'))): | 100 (0, '123\n'))): |
| 100 device_utils.RestartServer() | 101 device_utils.RestartServer() |
| 101 | 102 |
| 102 | 103 |
| 103 class MockTempFile(object): | 104 class MockTempFile(object): |
| 104 | 105 |
| 105 def __init__(self, name='/tmp/some/file'): | 106 def __init__(self, name='/tmp/some/file'): |
| 106 self.file = mock.MagicMock(spec=file) | 107 self.file = mock.MagicMock(spec=file) |
| 107 self.file.name = name | 108 self.file.name = name |
| 109 self.file.name_quoted = cmd_helper.SingleQuote(name) |
| 108 | 110 |
| 109 def __enter__(self): | 111 def __enter__(self): |
| 110 return self.file | 112 return self.file |
| 111 | 113 |
| 112 def __exit__(self, exc_type, exc_val, exc_tb): | 114 def __exit__(self, exc_type, exc_val, exc_tb): |
| 113 pass | 115 pass |
| 114 | 116 |
| 115 | 117 |
| 116 class _PatchedFunction(object): | 118 class _PatchedFunction(object): |
| 117 def __init__(self, patched=None, mocked=None): | 119 def __init__(self, patched=None, mocked=None): |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 | 552 |
| 551 def testNewRunShellImpl_withCwd(self): | 553 def testNewRunShellImpl_withCwd(self): |
| 552 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): | 554 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): |
| 553 self.device.RunShellCommand('ls', cwd='/some/test/path') | 555 self.device.RunShellCommand('ls', cwd='/some/test/path') |
| 554 | 556 |
| 555 def testNewRunShellImpl_withCwdQuoted(self): | 557 def testNewRunShellImpl_withCwdQuoted(self): |
| 556 with self.assertCall( | 558 with self.assertCall( |
| 557 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 559 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
| 558 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 560 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
| 559 | 561 |
| 562 def testRunShellCommand_withHugeCmd(self): |
| 563 payload = 'hi! ' * 1024 |
| 564 expected_cmd = "echo '%s'" % payload |
| 565 with self.assertCalls( |
| 566 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 567 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 568 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), |
| 569 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 570 self.assertEquals([payload], |
| 571 self.device.RunShellCommand(['echo', payload])) |
| 572 |
| 573 def testRunShellCommand_withHugeCmdAmdSU(self): |
| 574 payload = 'hi! ' * 1024 |
| 575 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload |
| 576 with self.assertCalls( |
| 577 (self.call.device.NeedsSU(), True), |
| 578 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 579 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 580 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), |
| 581 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 582 self.assertEquals( |
| 583 [payload], |
| 584 self.device.RunShellCommand(['echo', payload], as_root=True)) |
| 585 |
| 560 def testRunShellCommand_withSu(self): | 586 def testRunShellCommand_withSu(self): |
| 561 with self.assertCalls( | 587 with self.assertCalls( |
| 562 (self.call.device.NeedsSU(), True), | 588 (self.call.device.NeedsSU(), True), |
| 563 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): | 589 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): |
| 564 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 590 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
| 565 | 591 |
| 566 def testRunShellCommand_manyLines(self): | 592 def testRunShellCommand_manyLines(self): |
| 567 cmd = 'ls /some/path' | 593 cmd = 'ls /some/path' |
| 568 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 594 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
| 569 self.assertEquals(['file1', 'file2', 'file3'], | 595 self.assertEquals(['file1', 'file2', 'file3'], |
| (...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), | 1114 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), |
| 1089 'this is a test file\nread with su')): | 1115 'this is a test file\nread with su')): |
| 1090 self.assertEqual( | 1116 self.assertEqual( |
| 1091 'this is a test file\nread with su\n', | 1117 'this is a test file\nread with su\n', |
| 1092 self.device.ReadFile('/this/file/can.be.read.with.su', | 1118 self.device.ReadFile('/this/file/can.be.read.with.su', |
| 1093 as_root=True)) | 1119 as_root=True)) |
| 1094 | 1120 |
| 1095 | 1121 |
| 1096 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1122 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
| 1097 | 1123 |
| 1098 def testWriteFile_withPush(self): | 1124 def testWriteFileWithPush_success(self): |
| 1099 tmp_host = MockTempFile('/tmp/file/on.host') | 1125 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1100 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1126 contents = 'some interesting contents' |
| 1101 with self.assertCalls( | 1127 with self.assertCalls( |
| 1102 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1128 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1103 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1129 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1104 self.device.WriteFile('/path/to/device/file', contents) | 1130 self.device._WriteFileWithPush('/path/to/device/file', contents) |
| 1105 tmp_host.file.write.assert_called_once_with(contents) | 1131 tmp_host.file.write.assert_called_once_with(contents) |
| 1106 | 1132 |
| 1107 def testWriteFile_withPushForced(self): | 1133 def testWriteFileWithPush_rejected(self): |
| 1108 tmp_host = MockTempFile('/tmp/file/on.host') | 1134 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1109 contents = 'tiny contents' | 1135 contents = 'some interesting contents' |
| 1110 with self.assertCalls( | |
| 1111 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
| 1112 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | |
| 1113 self.device.WriteFile('/path/to/device/file', contents, force_push=True) | |
| 1114 tmp_host.file.write.assert_called_once_with(contents) | |
| 1115 | |
| 1116 def testWriteFile_withPushAndSU(self): | |
| 1117 tmp_host = MockTempFile('/tmp/file/on.host') | |
| 1118 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
| 1119 with self.assertCalls( | |
| 1120 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
| 1121 (self.call.device.NeedsSU(), True), | |
| 1122 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
| 1123 MockTempFile('/external/path/tmp/on.device')), | |
| 1124 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), | |
| 1125 self.call.device.RunShellCommand( | |
| 1126 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], | |
| 1127 as_root=True, check_return=True)): | |
| 1128 self.device.WriteFile('/path/to/device/file', contents, as_root=True) | |
| 1129 tmp_host.file.write.assert_called_once_with(contents) | |
| 1130 | |
| 1131 def testWriteFile_withPush_rejected(self): | |
| 1132 tmp_host = MockTempFile('/tmp/file/on.host') | |
| 1133 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
| 1134 with self.assertCalls( | 1136 with self.assertCalls( |
| 1135 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1137 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1136 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), | 1138 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
| 1137 self.CommandError())): | 1139 self.CommandError())): |
| 1138 with self.assertRaises(device_errors.CommandFailedError): | 1140 with self.assertRaises(device_errors.CommandFailedError): |
| 1139 self.device.WriteFile('/path/to/device/file', contents) | 1141 self.device._WriteFileWithPush('/path/to/device/file', contents) |
| 1142 |
| 1143 def testWriteFile_withPush(self): |
| 1144 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1145 with self.assertCalls( |
| 1146 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): |
| 1147 self.device.WriteFile('/path/to/device/file', contents) |
| 1148 |
| 1149 def testWriteFile_withPushForced(self): |
| 1150 contents = 'tiny contents' |
| 1151 with self.assertCalls( |
| 1152 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): |
| 1153 self.device.WriteFile('/path/to/device/file', contents, force_push=True) |
| 1154 |
| 1155 def testWriteFile_withPushAndSU(self): |
| 1156 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1157 with self.assertCalls( |
| 1158 (self.call.device.NeedsSU(), True), |
| 1159 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), |
| 1160 MockTempFile('/sdcard/tmp/on.device')), |
| 1161 self.call.device._WriteFileWithPush('/sdcard/tmp/on.device', contents), |
| 1162 self.call.device.RunShellCommand( |
| 1163 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'], |
| 1164 as_root=True, check_return=True)): |
| 1165 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
| 1140 | 1166 |
| 1141 def testWriteFile_withEcho(self): | 1167 def testWriteFile_withEcho(self): |
| 1142 with self.assertCall(self.call.adb.Shell( | 1168 with self.assertCall(self.call.adb.Shell( |
| 1143 "echo -n the.contents > /test/file/to.write"), ''): | 1169 "echo -n the.contents > /test/file/to.write"), ''): |
| 1144 self.device.WriteFile('/test/file/to.write', 'the.contents') | 1170 self.device.WriteFile('/test/file/to.write', 'the.contents') |
| 1145 | 1171 |
| 1146 def testWriteFile_withEchoAndQuotes(self): | 1172 def testWriteFile_withEchoAndQuotes(self): |
| 1147 with self.assertCall(self.call.adb.Shell( | 1173 with self.assertCall(self.call.adb.Shell( |
| 1148 "echo -n 'the contents' > '/test/file/to write'"), ''): | 1174 "echo -n 'the contents' > '/test/file/to write'"), ''): |
| 1149 self.device.WriteFile('/test/file/to write', 'the contents') | 1175 self.device.WriteFile('/test/file/to write', 'the contents') |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1458 self.assertTrue( | 1484 self.assertTrue( |
| 1459 isinstance(device, device_utils.DeviceUtils) | 1485 isinstance(device, device_utils.DeviceUtils) |
| 1460 and serial == str(device), | 1486 and serial == str(device), |
| 1461 'Expected a DeviceUtils object with serial %s' % serial) | 1487 'Expected a DeviceUtils object with serial %s' % serial) |
| 1462 | 1488 |
| 1463 | 1489 |
| 1464 if __name__ == '__main__': | 1490 if __name__ == '__main__': |
| 1465 logging.getLogger().setLevel(logging.DEBUG) | 1491 logging.getLogger().setLevel(logging.DEBUG) |
| 1466 unittest.main(verbosity=2) | 1492 unittest.main(verbosity=2) |
| 1467 | 1493 |
| OLD | NEW |