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.WriteFile( |
| 633 '/sdcard/temp-123.sh', expected_cmd, as_root=False, force_push=True), |
| 634 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 635 self.assertEquals([payload], |
| 636 self.device.RunShellCommand(['echo', payload])) |
| 637 |
| 638 def testRunShellCommand_withHugeCmdAmdSU(self): |
| 639 payload = 'hi! ' * 1024 |
| 640 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload |
| 641 with self.assertCalls( |
| 642 (self.call.device.NeedsSU(), True), |
| 643 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 644 self.device, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 645 self.call.device.WriteFile( |
| 646 '/sdcard/temp-123.sh', expected_cmd, as_root=False, force_push=True), |
| 647 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 648 self.assertEquals( |
| 649 [payload], |
| 650 self.device.RunShellCommand(['echo', payload], as_root=True)) |
| 651 |
624 def testRunShellCommand_withSu(self): | 652 def testRunShellCommand_withSu(self): |
625 with self.assertCalls( | 653 with self.assertCalls( |
626 (self.call.device.NeedsSU(), True), | 654 (self.call.device.NeedsSU(), True), |
627 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): | 655 (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) | 656 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
629 | 657 |
630 def testRunShellCommand_manyLines(self): | 658 def testRunShellCommand_manyLines(self): |
631 cmd = 'ls /some/path' | 659 cmd = 'ls /some/path' |
632 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 660 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
633 self.assertEquals(['file1', 'file2', 'file3'], | 661 self.assertEquals(['file1', 'file2', 'file3'], |
(...skipping 933 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 |