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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 ' Skin: WVGA800\n'): | 83 ' Skin: WVGA800\n'): |
83 self.assertEquals(['my_android5.0'], | 84 self.assertEquals(['my_android5.0'], |
84 device_utils.GetAVDs()) | 85 device_utils.GetAVDs()) |
85 | 86 |
86 | 87 |
87 class MockTempFile(object): | 88 class MockTempFile(object): |
88 | 89 |
89 def __init__(self, name='/tmp/some/file'): | 90 def __init__(self, name='/tmp/some/file'): |
90 self.file = mock.MagicMock(spec=file) | 91 self.file = mock.MagicMock(spec=file) |
91 self.file.name = name | 92 self.file.name = name |
| 93 self.file.name_quoted = cmd_helper.SingleQuote(name) |
92 | 94 |
93 def __enter__(self): | 95 def __enter__(self): |
94 return self.file | 96 return self.file |
95 | 97 |
96 def __exit__(self, exc_type, exc_val, exc_tb): | 98 def __exit__(self, exc_type, exc_val, exc_tb): |
97 pass | 99 pass |
98 | 100 |
99 | 101 |
100 class _PatchedFunction(object): | 102 class _PatchedFunction(object): |
101 def __init__(self, patched=None, mocked=None): | 103 def __init__(self, patched=None, mocked=None): |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 | 615 |
614 def testNewRunShellImpl_withCwd(self): | 616 def testNewRunShellImpl_withCwd(self): |
615 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): | 617 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): |
616 self.device.RunShellCommand('ls', cwd='/some/test/path') | 618 self.device.RunShellCommand('ls', cwd='/some/test/path') |
617 | 619 |
618 def testNewRunShellImpl_withCwdQuoted(self): | 620 def testNewRunShellImpl_withCwdQuoted(self): |
619 with self.assertCall( | 621 with self.assertCall( |
620 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 622 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
621 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 623 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
622 | 624 |
| 625 def testRunShellCommand_withHugeCmd(self): |
| 626 payload = 'hi! ' * 1024 |
| 627 expected_cmd = "echo '%s'" % payload |
| 628 with self.assertCalls( |
| 629 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 630 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 631 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), |
| 632 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 633 self.assertEquals([payload], |
| 634 self.device.RunShellCommand(['echo', payload])) |
| 635 |
| 636 def testRunShellCommand_withHugeCmdAmdSU(self): |
| 637 payload = 'hi! ' * 1024 |
| 638 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload |
| 639 with self.assertCalls( |
| 640 (self.call.device.NeedsSU(), True), |
| 641 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 642 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 643 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), |
| 644 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 645 self.assertEquals( |
| 646 [payload], |
| 647 self.device.RunShellCommand(['echo', payload], as_root=True)) |
| 648 |
623 def testRunShellCommand_withSu(self): | 649 def testRunShellCommand_withSu(self): |
624 with self.assertCalls( | 650 with self.assertCalls( |
625 (self.call.device.NeedsSU(), True), | 651 (self.call.device.NeedsSU(), True), |
626 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): | 652 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): |
627 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 653 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
628 | 654 |
629 def testRunShellCommand_manyLines(self): | 655 def testRunShellCommand_manyLines(self): |
630 cmd = 'ls /some/path' | 656 cmd = 'ls /some/path' |
631 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 657 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
632 self.assertEquals(['file1', 'file2', 'file3'], | 658 self.assertEquals(['file1', 'file2', 'file3'], |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 def testReadFile_asRoot_rejected(self): | 1187 def testReadFile_asRoot_rejected(self): |
1162 self.device.old_interface._privileged_command_runner = None | 1188 self.device.old_interface._privileged_command_runner = None |
1163 self.device.old_interface._protected_file_access_method_initialized = True | 1189 self.device.old_interface._protected_file_access_method_initialized = True |
1164 with self.assertRaises(device_errors.CommandFailedError): | 1190 with self.assertRaises(device_errors.CommandFailedError): |
1165 self.device.ReadFile('/this/file/cannot.be.read.by.user', | 1191 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
1166 as_root=True) | 1192 as_root=True) |
1167 | 1193 |
1168 | 1194 |
1169 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1195 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1170 | 1196 |
1171 def testWriteFile_withPush(self): | 1197 def testWriteFileWithPush_success(self): |
1172 tmp_host = MockTempFile('/tmp/file/on.host') | 1198 tmp_host = MockTempFile('/tmp/file/on.host') |
1173 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1199 contents = 'some interesting contents' |
1174 with self.assertCalls( | 1200 with self.assertCalls( |
1175 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1201 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1176 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1202 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1177 self.device.WriteFile('/path/to/device/file', contents) | 1203 self.device._WriteFileWithPush('/path/to/device/file', contents) |
1178 tmp_host.file.write.assert_called_once_with(contents) | 1204 tmp_host.file.write.assert_called_once_with(contents) |
1179 | 1205 |
1180 def testWriteFile_withPushForced(self): | 1206 def testWriteFileWithPush_rejected(self): |
1181 tmp_host = MockTempFile('/tmp/file/on.host') | 1207 tmp_host = MockTempFile('/tmp/file/on.host') |
1182 contents = 'tiny contents' | 1208 contents = 'some interesting contents' |
1183 with self.assertCalls( | |
1184 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
1185 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | |
1186 self.device.WriteFile('/path/to/device/file', contents, force_push=True) | |
1187 tmp_host.file.write.assert_called_once_with(contents) | |
1188 | |
1189 def testWriteFile_withPushAndSU(self): | |
1190 tmp_host = MockTempFile('/tmp/file/on.host') | |
1191 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
1192 with self.assertCalls( | |
1193 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | |
1194 (self.call.device.NeedsSU(), True), | |
1195 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
1196 MockTempFile('/external/path/tmp/on.device')), | |
1197 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), | |
1198 self.call.device.RunShellCommand( | |
1199 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], | |
1200 as_root=True, check_return=True)): | |
1201 self.device.WriteFile('/path/to/device/file', contents, as_root=True) | |
1202 tmp_host.file.write.assert_called_once_with(contents) | |
1203 | |
1204 def testWriteFile_withPush_rejected(self): | |
1205 tmp_host = MockTempFile('/tmp/file/on.host') | |
1206 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
1207 with self.assertCalls( | 1209 with self.assertCalls( |
1208 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1210 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1209 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), | 1211 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
1210 self.CommandError())): | 1212 self.CommandError())): |
1211 with self.assertRaises(device_errors.CommandFailedError): | 1213 with self.assertRaises(device_errors.CommandFailedError): |
1212 self.device.WriteFile('/path/to/device/file', contents) | 1214 self.device._WriteFileWithPush('/path/to/device/file', contents) |
| 1215 |
| 1216 def testWriteFile_withPush(self): |
| 1217 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1218 with self.assertCalls( |
| 1219 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): |
| 1220 self.device.WriteFile('/path/to/device/file', contents) |
| 1221 |
| 1222 def testWriteFile_withPushForced(self): |
| 1223 contents = 'tiny contents' |
| 1224 with self.assertCalls( |
| 1225 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): |
| 1226 self.device.WriteFile('/path/to/device/file', contents, force_push=True) |
| 1227 |
| 1228 def testWriteFile_withPushAndSU(self): |
| 1229 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1230 with self.assertCalls( |
| 1231 (self.call.device.NeedsSU(), True), |
| 1232 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), |
| 1233 MockTempFile('/sdcard/tmp/on.device')), |
| 1234 self.call.device._WriteFileWithPush('/sdcard/tmp/on.device', contents), |
| 1235 self.call.device.RunShellCommand( |
| 1236 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'], |
| 1237 as_root=True, check_return=True)): |
| 1238 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
1213 | 1239 |
1214 def testWriteFile_withEcho(self): | 1240 def testWriteFile_withEcho(self): |
1215 with self.assertCall(self.call.adb.Shell( | 1241 with self.assertCall(self.call.adb.Shell( |
1216 "echo -n the.contents > /test/file/to.write"), ''): | 1242 "echo -n the.contents > /test/file/to.write"), ''): |
1217 self.device.WriteFile('/test/file/to.write', 'the.contents') | 1243 self.device.WriteFile('/test/file/to.write', 'the.contents') |
1218 | 1244 |
1219 def testWriteFile_withEchoAndQuotes(self): | 1245 def testWriteFile_withEchoAndQuotes(self): |
1220 with self.assertCall(self.call.adb.Shell( | 1246 with self.assertCall(self.call.adb.Shell( |
1221 "echo -n 'the contents' > '/test/file/to write'"), ''): | 1247 "echo -n 'the contents' > '/test/file/to write'"), ''): |
1222 self.device.WriteFile('/test/file/to write', 'the contents') | 1248 self.device.WriteFile('/test/file/to write', 'the contents') |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1541 self.assertTrue( | 1567 self.assertTrue( |
1542 isinstance(device, device_utils.DeviceUtils) | 1568 isinstance(device, device_utils.DeviceUtils) |
1543 and serial == str(device), | 1569 and serial == str(device), |
1544 'Expected a DeviceUtils object with serial %s' % serial) | 1570 'Expected a DeviceUtils object with serial %s' % serial) |
1545 | 1571 |
1546 | 1572 |
1547 if __name__ == '__main__': | 1573 if __name__ == '__main__': |
1548 logging.getLogger().setLevel(logging.DEBUG) | 1574 logging.getLogger().setLevel(logging.DEBUG) |
1549 unittest.main(verbosity=2) | 1575 unittest.main(verbosity=2) |
1550 | 1576 |
OLD | NEW |