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 | |
25 from pylib import constants | 24 from pylib import constants |
26 from pylib.device import adb_wrapper | 25 from pylib.device import adb_wrapper |
27 from pylib.device import device_errors | 26 from pylib.device import device_errors |
28 from pylib.device import device_utils | 27 from pylib.device import device_utils |
29 from pylib.device import intent | 28 from pylib.device import intent |
30 from pylib.utils import mock_calls | 29 from pylib.utils import mock_calls |
31 | 30 |
32 # RunCommand from third_party/android_testrunner/run_command.py is mocked | 31 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
33 # below, so its path needs to be in sys.path. | 32 # below, so its path needs to be in sys.path. |
34 sys.path.append(os.path.join( | 33 sys.path.append(os.path.join( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 device_utils.DeviceUtils(None) | 67 device_utils.DeviceUtils(None) |
69 with self.assertRaises(ValueError): | 68 with self.assertRaises(ValueError): |
70 device_utils.DeviceUtils('') | 69 device_utils.DeviceUtils('') |
71 | 70 |
72 | 71 |
73 class MockTempFile(object): | 72 class MockTempFile(object): |
74 | 73 |
75 def __init__(self, name='/tmp/some/file'): | 74 def __init__(self, name='/tmp/some/file'): |
76 self.file = mock.MagicMock(spec=file) | 75 self.file = mock.MagicMock(spec=file) |
77 self.file.name = name | 76 self.file.name = name |
78 self.file.name_quoted = cmd_helper.SingleQuote(name) | |
79 | 77 |
80 def __enter__(self): | 78 def __enter__(self): |
81 return self.file | 79 return self.file |
82 | 80 |
83 def __exit__(self, exc_type, exc_val, exc_tb): | 81 def __exit__(self, exc_type, exc_val, exc_tb): |
84 pass | 82 pass |
85 | 83 |
86 | 84 |
87 class _PatchedFunction(object): | 85 class _PatchedFunction(object): |
88 def __init__(self, patched=None, mocked=None): | 86 def __init__(self, patched=None, mocked=None): |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 | 612 |
615 def testNewRunShellImpl_withCwd(self): | 613 def testNewRunShellImpl_withCwd(self): |
616 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): | 614 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): |
617 self.device.RunShellCommand('ls', cwd='/some/test/path') | 615 self.device.RunShellCommand('ls', cwd='/some/test/path') |
618 | 616 |
619 def testNewRunShellImpl_withCwdQuoted(self): | 617 def testNewRunShellImpl_withCwdQuoted(self): |
620 with self.assertCall( | 618 with self.assertCall( |
621 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 619 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
622 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 620 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
623 | 621 |
624 def testRunShellCommand_withHugeCmd(self): | |
625 payload = 'hi! ' * 1024 | |
626 expected_cmd = "echo '%s'" % payload | |
627 with self.assertCalls( | |
628 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | |
629 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), | |
630 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), | |
631 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): | |
632 self.assertEquals([payload], | |
633 self.device.RunShellCommand(['echo', payload])) | |
634 | |
635 def testRunShellCommand_withHugeCmdAmdSU(self): | |
636 payload = 'hi! ' * 1024 | |
637 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload | |
638 with self.assertCalls( | |
639 (self.call.device.NeedsSU(), True), | |
640 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | |
641 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), | |
642 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), | |
643 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): | |
644 self.assertEquals( | |
645 [payload], | |
646 self.device.RunShellCommand(['echo', payload], as_root=True)) | |
647 | |
648 def testRunShellCommand_withSu(self): | 622 def testRunShellCommand_withSu(self): |
649 with self.assertCalls( | 623 with self.assertCalls( |
650 (self.call.device.NeedsSU(), True), | 624 (self.call.device.NeedsSU(), True), |
651 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): | 625 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): |
652 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 626 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
653 | 627 |
654 def testRunShellCommand_manyLines(self): | 628 def testRunShellCommand_manyLines(self): |
655 cmd = 'ls /some/path' | 629 cmd = 'ls /some/path' |
656 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 630 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
657 self.assertEquals(['file1', 'file2', 'file3'], | 631 self.assertEquals(['file1', 'file2', 'file3'], |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1190 def testReadFile_asRoot_rejected(self): | 1164 def testReadFile_asRoot_rejected(self): |
1191 self.device.old_interface._privileged_command_runner = None | 1165 self.device.old_interface._privileged_command_runner = None |
1192 self.device.old_interface._protected_file_access_method_initialized = True | 1166 self.device.old_interface._protected_file_access_method_initialized = True |
1193 with self.assertRaises(device_errors.CommandFailedError): | 1167 with self.assertRaises(device_errors.CommandFailedError): |
1194 self.device.ReadFile('/this/file/cannot.be.read.by.user', | 1168 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
1195 as_root=True) | 1169 as_root=True) |
1196 | 1170 |
1197 | 1171 |
1198 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1172 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1199 | 1173 |
1200 def testWriteFileWithPush_success(self): | 1174 def testWriteFile_withPush(self): |
1201 tmp_host = MockTempFile('/tmp/file/on.host') | 1175 tmp_host = MockTempFile('/tmp/file/on.host') |
1202 contents = 'some interesting contents' | 1176 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1203 with self.assertCalls( | 1177 with self.assertCalls( |
1204 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1178 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1205 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1179 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1206 self.device._WriteFileWithPush('/path/to/device/file', contents) | 1180 self.device.WriteFile('/path/to/device/file', contents) |
1207 tmp_host.file.write.assert_called_once_with(contents) | 1181 tmp_host.file.write.assert_called_once_with(contents) |
1208 | 1182 |
1209 def testWriteFileWithPush_rejected(self): | 1183 def testWriteFile_withPushForced(self): |
1210 tmp_host = MockTempFile('/tmp/file/on.host') | 1184 tmp_host = MockTempFile('/tmp/file/on.host') |
1211 contents = 'some interesting contents' | 1185 contents = 'tiny contents' |
| 1186 with self.assertCalls( |
| 1187 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1188 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1189 self.device.WriteFile('/path/to/device/file', contents, force_push=True) |
| 1190 tmp_host.file.write.assert_called_once_with(contents) |
| 1191 |
| 1192 def testWriteFile_withPushAndSU(self): |
| 1193 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1194 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1195 with self.assertCalls( |
| 1196 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1197 (self.call.device.NeedsSU(), True), |
| 1198 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), |
| 1199 MockTempFile('/external/path/tmp/on.device')), |
| 1200 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), |
| 1201 self.call.device.RunShellCommand( |
| 1202 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], |
| 1203 as_root=True, check_return=True)): |
| 1204 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
| 1205 tmp_host.file.write.assert_called_once_with(contents) |
| 1206 |
| 1207 def testWriteFile_withPush_rejected(self): |
| 1208 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1209 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1212 with self.assertCalls( | 1210 with self.assertCalls( |
1213 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1211 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1214 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), | 1212 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
1215 self.CommandError())): | 1213 self.CommandError())): |
1216 with self.assertRaises(device_errors.CommandFailedError): | 1214 with self.assertRaises(device_errors.CommandFailedError): |
1217 self.device._WriteFileWithPush('/path/to/device/file', contents) | 1215 self.device.WriteFile('/path/to/device/file', contents) |
1218 | |
1219 def testWriteFile_withPush(self): | |
1220 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
1221 with self.assertCalls( | |
1222 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): | |
1223 self.device.WriteFile('/path/to/device/file', contents) | |
1224 | |
1225 def testWriteFile_withPushForced(self): | |
1226 contents = 'tiny contents' | |
1227 with self.assertCalls( | |
1228 self.call.device._WriteFileWithPush('/path/to/device/file', contents)): | |
1229 self.device.WriteFile('/path/to/device/file', contents, force_push=True) | |
1230 | |
1231 def testWriteFile_withPushAndSU(self): | |
1232 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | |
1233 with self.assertCalls( | |
1234 (self.call.device.NeedsSU(), True), | |
1235 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | |
1236 MockTempFile('/sdcard/tmp/on.device')), | |
1237 self.call.device._WriteFileWithPush('/sdcard/tmp/on.device', contents), | |
1238 self.call.device.RunShellCommand( | |
1239 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'], | |
1240 as_root=True, check_return=True)): | |
1241 self.device.WriteFile('/path/to/device/file', contents, as_root=True) | |
1242 | 1216 |
1243 def testWriteFile_withEcho(self): | 1217 def testWriteFile_withEcho(self): |
1244 with self.assertCall(self.call.adb.Shell( | 1218 with self.assertCall(self.call.adb.Shell( |
1245 "echo -n the.contents > /test/file/to.write"), ''): | 1219 "echo -n the.contents > /test/file/to.write"), ''): |
1246 self.device.WriteFile('/test/file/to.write', 'the.contents') | 1220 self.device.WriteFile('/test/file/to.write', 'the.contents') |
1247 | 1221 |
1248 def testWriteFile_withEchoAndQuotes(self): | 1222 def testWriteFile_withEchoAndQuotes(self): |
1249 with self.assertCall(self.call.adb.Shell( | 1223 with self.assertCall(self.call.adb.Shell( |
1250 "echo -n 'the contents' > '/test/file/to write'"), ''): | 1224 "echo -n 'the contents' > '/test/file/to write'"), ''): |
1251 self.device.WriteFile('/test/file/to write', 'the contents') | 1225 self.device.WriteFile('/test/file/to write', 'the contents') |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1555 def testStr_returnsSerial(self): | 1529 def testStr_returnsSerial(self): |
1556 with self.assertCalls( | 1530 with self.assertCalls( |
1557 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): | 1531 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): |
1558 self.assertEqual('0123456789abcdef', str(self.device)) | 1532 self.assertEqual('0123456789abcdef', str(self.device)) |
1559 | 1533 |
1560 | 1534 |
1561 if __name__ == '__main__': | 1535 if __name__ == '__main__': |
1562 logging.getLogger().setLevel(logging.DEBUG) | 1536 logging.getLogger().setLevel(logging.DEBUG) |
1563 unittest.main(verbosity=2) | 1537 unittest.main(verbosity=2) |
1564 | 1538 |
OLD | NEW |