| 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 |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 | 608 |
| 609 | 609 |
| 610 class DeviceUtilsUninstallTest(DeviceUtilsTest): | 610 class DeviceUtilsUninstallTest(DeviceUtilsTest): |
| 611 | 611 |
| 612 def testUninstall_callsThrough(self): | 612 def testUninstall_callsThrough(self): |
| 613 with self.assertCalls( | 613 with self.assertCalls( |
| 614 self.call.adb.Uninstall('test.package', True)): | 614 self.call.adb.Uninstall('test.package', True)): |
| 615 self.device.Uninstall('test.package', True) | 615 self.device.Uninstall('test.package', True) |
| 616 | 616 |
| 617 | 617 |
| 618 class DeviceUtilsSuTest(DeviceUtilsTest): |
| 619 def testSu_preM(self): |
| 620 with self.patch_call( |
| 621 self.call.device.build_version_sdk, |
| 622 return_value=constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP_MR1): |
| 623 self.assertEquals('su -c foo', self.device._Su('foo')) |
| 624 |
| 625 def testSu_mAndAbove(self): |
| 626 with self.patch_call( |
| 627 self.call.device.build_version_sdk, |
| 628 return_value=constants.ANDROID_SDK_VERSION_CODES.MARSHMALLOW): |
| 629 self.assertEquals('su 0 foo', self.device._Su('foo')) |
| 630 |
| 631 |
| 618 class DeviceUtilsRunShellCommandTest(DeviceUtilsTest): | 632 class DeviceUtilsRunShellCommandTest(DeviceUtilsTest): |
| 619 | 633 |
| 620 def setUp(self): | 634 def setUp(self): |
| 621 super(DeviceUtilsRunShellCommandTest, self).setUp() | 635 super(DeviceUtilsRunShellCommandTest, self).setUp() |
| 622 self.device.NeedsSU = mock.Mock(return_value=False) | 636 self.device.NeedsSU = mock.Mock(return_value=False) |
| 623 | 637 |
| 624 def testRunShellCommand_commandAsList(self): | 638 def testRunShellCommand_commandAsList(self): |
| 625 with self.assertCall(self.call.adb.Shell('pm list packages'), ''): | 639 with self.assertCall(self.call.adb.Shell('pm list packages'), ''): |
| 626 self.device.RunShellCommand(['pm', 'list', 'packages']) | 640 self.device.RunShellCommand(['pm', 'list', 'packages']) |
| 627 | 641 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 payload = 'hi! ' * 1024 | 674 payload = 'hi! ' * 1024 |
| 661 expected_cmd = "echo '%s'" % payload | 675 expected_cmd = "echo '%s'" % payload |
| 662 with self.assertCalls( | 676 with self.assertCalls( |
| 663 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | 677 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 664 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), | 678 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 665 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), | 679 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), |
| 666 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): | 680 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 667 self.assertEquals([payload], | 681 self.assertEquals([payload], |
| 668 self.device.RunShellCommand(['echo', payload])) | 682 self.device.RunShellCommand(['echo', payload])) |
| 669 | 683 |
| 670 def testRunShellCommand_withHugeCmdAmdSU(self): | 684 def testRunShellCommand_withHugeCmdAndSU(self): |
| 671 payload = 'hi! ' * 1024 | 685 payload = 'hi! ' * 1024 |
| 672 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload | 686 expected_cmd_without_su = """sh -c 'echo '"'"'%s'"'"''""" % payload |
| 687 expected_cmd = 'su -c %s' % expected_cmd_without_su |
| 673 with self.assertCalls( | 688 with self.assertCalls( |
| 674 (self.call.device.NeedsSU(), True), | 689 (self.call.device.NeedsSU(), True), |
| 690 (self.call.device._Su(expected_cmd_without_su), expected_cmd), |
| 675 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | 691 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 676 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), | 692 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')), |
| 677 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), | 693 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd), |
| 678 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): | 694 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')): |
| 679 self.assertEquals( | 695 self.assertEquals( |
| 680 [payload], | 696 [payload], |
| 681 self.device.RunShellCommand(['echo', payload], as_root=True)) | 697 self.device.RunShellCommand(['echo', payload], as_root=True)) |
| 682 | 698 |
| 683 def testRunShellCommand_withSu(self): | 699 def testRunShellCommand_withSu(self): |
| 700 expected_cmd_without_su = "sh -c 'setprop service.adb.root 0'" |
| 701 expected_cmd = 'su -c %s' % expected_cmd_without_su |
| 684 with self.assertCalls( | 702 with self.assertCalls( |
| 685 (self.call.device.NeedsSU(), True), | 703 (self.call.device.NeedsSU(), True), |
| 686 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): | 704 (self.call.device._Su(expected_cmd_without_su), expected_cmd), |
| 705 (self.call.adb.Shell(expected_cmd), '')): |
| 687 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 706 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
| 688 | 707 |
| 689 def testRunShellCommand_manyLines(self): | 708 def testRunShellCommand_manyLines(self): |
| 690 cmd = 'ls /some/path' | 709 cmd = 'ls /some/path' |
| 691 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 710 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
| 692 self.assertEquals(['file1', 'file2', 'file3'], | 711 self.assertEquals(['file1', 'file2', 'file3'], |
| 693 self.device.RunShellCommand(cmd)) | 712 self.device.RunShellCommand(cmd)) |
| 694 | 713 |
| 695 def testRunShellCommand_singleLine_success(self): | 714 def testRunShellCommand_singleLine_success(self): |
| 696 cmd = 'echo $VALUE' | 715 cmd = 'echo $VALUE' |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 (self.call.adb.Shell('kill -9 1234'), ''), | 866 (self.call.adb.Shell('kill -9 1234'), ''), |
| 848 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), | 867 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), |
| 849 (self.call.device.GetPids('some.process'), [])): | 868 (self.call.device.GetPids('some.process'), [])): |
| 850 self.assertEquals( | 869 self.assertEquals( |
| 851 1, self.device.KillAll('some.process', blocking=True)) | 870 1, self.device.KillAll('some.process', blocking=True)) |
| 852 | 871 |
| 853 def testKillAll_root(self): | 872 def testKillAll_root(self): |
| 854 with self.assertCalls( | 873 with self.assertCalls( |
| 855 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), | 874 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), |
| 856 (self.call.device.NeedsSU(), True), | 875 (self.call.device.NeedsSU(), True), |
| 876 (self.call.device._Su("sh -c 'kill -9 1234'"), |
| 877 "su -c sh -c 'kill -9 1234'"), |
| 857 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): | 878 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
| 858 self.assertEquals( | 879 self.assertEquals( |
| 859 1, self.device.KillAll('some.process', as_root=True)) | 880 1, self.device.KillAll('some.process', as_root=True)) |
| 860 | 881 |
| 861 def testKillAll_sigterm(self): | 882 def testKillAll_sigterm(self): |
| 862 with self.assertCalls( | 883 with self.assertCalls( |
| 863 (self.call.device.GetPids('some.process'), | 884 (self.call.device.GetPids('some.process'), |
| 864 {'some.process': ['1234']}), | 885 {'some.process': ['1234']}), |
| 865 (self.call.adb.Shell('kill -15 1234'), '')): | 886 (self.call.adb.Shell('kill -15 1234'), '')): |
| 866 self.assertEquals( | 887 self.assertEquals( |
| (...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1515 with self.assertCall(self.call.adb.Shell( | 1536 with self.assertCall(self.call.adb.Shell( |
| 1516 "echo -n the.contents > /test/file/to.write"), ''): | 1537 "echo -n the.contents > /test/file/to.write"), ''): |
| 1517 self.device.WriteFile('/test/file/to.write', 'the.contents') | 1538 self.device.WriteFile('/test/file/to.write', 'the.contents') |
| 1518 | 1539 |
| 1519 def testWriteFile_withEchoAndQuotes(self): | 1540 def testWriteFile_withEchoAndQuotes(self): |
| 1520 with self.assertCall(self.call.adb.Shell( | 1541 with self.assertCall(self.call.adb.Shell( |
| 1521 "echo -n 'the contents' > '/test/file/to write'"), ''): | 1542 "echo -n 'the contents' > '/test/file/to write'"), ''): |
| 1522 self.device.WriteFile('/test/file/to write', 'the contents') | 1543 self.device.WriteFile('/test/file/to write', 'the contents') |
| 1523 | 1544 |
| 1524 def testWriteFile_withEchoAndSU(self): | 1545 def testWriteFile_withEchoAndSU(self): |
| 1546 expected_cmd_without_su = "sh -c 'echo -n contents > /test/file'" |
| 1547 expected_cmd = 'su -c %s' % expected_cmd_without_su |
| 1525 with self.assertCalls( | 1548 with self.assertCalls( |
| 1526 (self.call.device.NeedsSU(), True), | 1549 (self.call.device.NeedsSU(), True), |
| 1527 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), | 1550 (self.call.device._Su(expected_cmd_without_su), expected_cmd), |
| 1551 (self.call.adb.Shell(expected_cmd), |
| 1528 '')): | 1552 '')): |
| 1529 self.device.WriteFile('/test/file', 'contents', as_root=True) | 1553 self.device.WriteFile('/test/file', 'contents', as_root=True) |
| 1530 | 1554 |
| 1531 | 1555 |
| 1532 class DeviceUtilsLsTest(DeviceUtilsTest): | 1556 class DeviceUtilsLsTest(DeviceUtilsTest): |
| 1533 | 1557 |
| 1534 def testLs_directory(self): | 1558 def testLs_directory(self): |
| 1535 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), | 1559 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), |
| 1536 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), | 1560 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), |
| 1537 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] | 1561 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1911 self.call.device.WriteFile(mock.ANY, mock.ANY), | 1935 self.call.device.WriteFile(mock.ANY, mock.ANY), |
| 1912 (self.call.device.RunShellCommand( | 1936 (self.call.device.RunShellCommand( |
| 1913 ['source', mock_temp_file ], as_root=True)), | 1937 ['source', mock_temp_file ], as_root=True)), |
| 1914 self.call.adb.WaitForDevice()): | 1938 self.call.adb.WaitForDevice()): |
| 1915 self.device.RestartAdbd() | 1939 self.device.RestartAdbd() |
| 1916 | 1940 |
| 1917 | 1941 |
| 1918 if __name__ == '__main__': | 1942 if __name__ == '__main__': |
| 1919 logging.getLogger().setLevel(logging.DEBUG) | 1943 logging.getLogger().setLevel(logging.DEBUG) |
| 1920 unittest.main(verbosity=2) | 1944 unittest.main(verbosity=2) |
| OLD | NEW |