OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
7 """ | 7 """ |
8 | 8 |
9 # pylint: disable=C0321 | 9 # pylint: disable=C0321 |
10 # pylint: disable=W0212 | 10 # pylint: disable=W0212 |
11 # pylint: disable=W0613 | 11 # pylint: disable=W0613 |
12 | 12 |
| 13 import collections |
13 import os | 14 import os |
| 15 import re |
14 import signal | 16 import signal |
15 import sys | 17 import sys |
16 import unittest | 18 import unittest |
17 | 19 |
18 from pylib import android_commands | 20 from pylib import android_commands |
19 from pylib import constants | 21 from pylib import constants |
20 from pylib.device import adb_wrapper | 22 from pylib.device import adb_wrapper |
21 from pylib.device import device_errors | 23 from pylib.device import device_errors |
22 from pylib.device import device_utils | 24 from pylib.device import device_utils |
23 from pylib.device import intent | 25 from pylib.device import intent |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 59 |
58 def testInitWithNone(self): | 60 def testInitWithNone(self): |
59 d = device_utils.DeviceUtils(None) | 61 d = device_utils.DeviceUtils(None) |
60 self.assertIsNone(d.old_interface.GetDevice()) | 62 self.assertIsNone(d.old_interface.GetDevice()) |
61 | 63 |
62 | 64 |
63 class DeviceUtilsOldImplTest(unittest.TestCase): | 65 class DeviceUtilsOldImplTest(unittest.TestCase): |
64 | 66 |
65 class AndroidCommandsCalls(object): | 67 class AndroidCommandsCalls(object): |
66 | 68 |
67 def __init__(self, test_case, cmd_ret): | 69 def __init__(self, test_case, cmd_ret, comp): |
68 self._cmds = cmd_ret | 70 self._cmds = cmd_ret |
| 71 self._comp = comp |
69 self._test_case = test_case | 72 self._test_case = test_case |
70 self._total_received = 0 | 73 self._total_received = 0 |
71 | 74 |
72 def __enter__(self): | 75 def __enter__(self): |
73 atr_run_command.RunCommand = mock.Mock() | 76 atr_run_command.RunCommand = mock.Mock() |
74 atr_run_command.RunCommand.side_effect = lambda c, **kw: self._ret(c) | 77 atr_run_command.RunCommand.side_effect = lambda c, **kw: self._ret(c) |
75 | 78 |
76 def _ret(self, actual_cmd): | 79 def _ret(self, actual_cmd): |
77 on_failure_fmt = ('\n' | 80 if sys.exc_info()[0] is None: |
78 ' received command: %s\n' | 81 on_failure_fmt = ('\n' |
79 ' expected command: %s') | 82 ' received command: %s\n' |
80 self._test_case.assertGreater( | 83 ' expected command: %s') |
81 len(self._cmds), self._total_received, | 84 self._test_case.assertGreater( |
82 msg=on_failure_fmt % (actual_cmd, None)) | 85 len(self._cmds), self._total_received, |
83 expected_cmd, ret = self._cmds[self._total_received] | 86 msg=on_failure_fmt % (actual_cmd, None)) |
84 self._total_received += 1 | 87 expected_cmd, ret = self._cmds[self._total_received] |
85 self._test_case.assertEqual( | 88 self._total_received += 1 |
86 actual_cmd, expected_cmd, | 89 self._test_case.assertTrue( |
87 msg=on_failure_fmt % (actual_cmd, expected_cmd)) | 90 self._comp(expected_cmd, actual_cmd), |
88 return ret | 91 msg=on_failure_fmt % (actual_cmd, expected_cmd)) |
| 92 return ret |
| 93 return '' |
89 | 94 |
90 def __exit__(self, exc_type, _exc_val, exc_trace): | 95 def __exit__(self, exc_type, _exc_val, exc_trace): |
91 if exc_type is None: | 96 if exc_type is None: |
92 on_failure = "adb commands don't match.\nExpected:%s\nActual:%s" % ( | 97 on_failure = "adb commands don't match.\nExpected:%s\nActual:%s" % ( |
93 ''.join('\n %s' % c for c, _ in self._cmds), | 98 ''.join('\n %s' % c for c, _ in self._cmds), |
94 ''.join('\n %s' % a[0] | 99 ''.join('\n %s' % a[0] |
95 for _, a, kw in atr_run_command.RunCommand.mock_calls)) | 100 for _, a, kw in atr_run_command.RunCommand.mock_calls)) |
96 self._test_case.assertEqual( | 101 self._test_case.assertEqual( |
97 len(self._cmds), len(atr_run_command.RunCommand.mock_calls), | 102 len(self._cmds), len(atr_run_command.RunCommand.mock_calls), |
98 msg=on_failure) | 103 msg=on_failure) |
99 for (expected_cmd, _r), (_n, actual_args, actual_kwargs) in zip( | 104 for (expected_cmd, _r), (_n, actual_args, actual_kwargs) in zip( |
100 self._cmds, atr_run_command.RunCommand.mock_calls): | 105 self._cmds, atr_run_command.RunCommand.mock_calls): |
101 self._test_case.assertEqual(1, len(actual_args), msg=on_failure) | 106 self._test_case.assertEqual(1, len(actual_args), msg=on_failure) |
102 self._test_case.assertEqual(expected_cmd, actual_args[0], | 107 self._test_case.assertTrue(self._comp(expected_cmd, actual_args[0]), |
103 msg=on_failure) | 108 msg=on_failure) |
104 self._test_case.assertTrue('timeout_time' in actual_kwargs, | 109 self._test_case.assertTrue('timeout_time' in actual_kwargs, |
105 msg=on_failure) | 110 msg=on_failure) |
106 self._test_case.assertTrue('retry_count' in actual_kwargs, | 111 self._test_case.assertTrue('retry_count' in actual_kwargs, |
107 msg=on_failure) | 112 msg=on_failure) |
108 | 113 |
109 def assertOldImplCalls(self, cmd, ret): | 114 def assertNoAdbCalls(self): |
110 return type(self).AndroidCommandsCalls(self, [(cmd, ret)]) | 115 return type(self).AndroidCommandsCalls(self, [], str.__eq__) |
111 | 116 |
112 def assertOldImplCallsSequence(self, cmd_ret): | 117 def assertOldImplCalls(self, cmd, ret, comp=str.__eq__): |
113 return type(self).AndroidCommandsCalls(self, cmd_ret) | 118 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) |
| 119 |
| 120 def assertOldImplCallsSequence(self, cmd_ret, comp=str.__eq__): |
| 121 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) |
114 | 122 |
115 def setUp(self): | 123 def setUp(self): |
116 self.device = device_utils.DeviceUtils( | 124 self.device = device_utils.DeviceUtils( |
117 '0123456789abcdef', default_timeout=1, default_retries=0) | 125 '0123456789abcdef', default_timeout=1, default_retries=0) |
118 | 126 |
119 def testIsOnline_true(self): | 127 def testIsOnline_true(self): |
120 with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', | 128 with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', |
121 'device\r\n'): | 129 'device\r\n'): |
122 self.assertTrue(self.device.IsOnline()) | 130 self.assertTrue(self.device.IsOnline()) |
123 | 131 |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 ("adb -s 0123456789abcdef shell 'pm clear this.package.exists'", | 718 ("adb -s 0123456789abcdef shell 'pm clear this.package.exists'", |
711 'Success\r\n')]): | 719 'Success\r\n')]): |
712 self.device.ClearApplicationState('this.package.exists') | 720 self.device.ClearApplicationState('this.package.exists') |
713 | 721 |
714 def testSendKeyEvent(self): | 722 def testSendKeyEvent(self): |
715 with self.assertOldImplCalls( | 723 with self.assertOldImplCalls( |
716 "adb -s 0123456789abcdef shell 'input keyevent 66'", | 724 "adb -s 0123456789abcdef shell 'input keyevent 66'", |
717 ''): | 725 ''): |
718 self.device.SendKeyEvent(66) | 726 self.device.SendKeyEvent(66) |
719 | 727 |
| 728 def testPushChangedFiles_noHostPath(self): |
| 729 with mock.patch('os.path.exists', return_value=False): |
| 730 with self.assertRaises(device_errors.CommandFailedError): |
| 731 self.device.PushChangedFiles('/test/host/path', '/test/device/path') |
| 732 |
| 733 def testPushChangedFiles_file_noChange(self): |
| 734 self.device.old_interface._push_if_needed_cache = {} |
| 735 |
| 736 host_file_path = '/test/host/path' |
| 737 device_file_path = '/test/device/path' |
| 738 |
| 739 mock_file_info = { |
| 740 '/test/host/path': { |
| 741 'os.path.exists': True, |
| 742 'os.path.isdir': False, |
| 743 'os.path.getsize': 100, |
| 744 }, |
| 745 } |
| 746 |
| 747 os_path_exists = mock.Mock() |
| 748 os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] |
| 749 |
| 750 os_path_isdir = mock.Mock() |
| 751 os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] |
| 752 |
| 753 os_path_getsize = mock.Mock() |
| 754 os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] |
| 755 |
| 756 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) |
| 757 |
| 758 with mock.patch('os.path.exists', new=os_path_exists), ( |
| 759 mock.patch('os.path.isdir', new=os_path_isdir)), ( |
| 760 mock.patch('os.path.getsize', new=os_path_getsize)): |
| 761 # GetFilesChanged is mocked, so its adb calls are omitted. |
| 762 with self.assertNoAdbCalls(): |
| 763 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 764 |
| 765 @staticmethod |
| 766 def createMockOSStatResult( |
| 767 st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, |
| 768 st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): |
| 769 MockOSStatResult = collections.namedtuple('MockOSStatResult', [ |
| 770 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', |
| 771 'st_size', 'st_atime', 'st_mtime', 'st_ctime']) |
| 772 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, |
| 773 st_size, st_atime, st_mtime, st_ctime) |
| 774 |
| 775 def testPushChangedFiles_file_changed(self): |
| 776 self.device.old_interface._push_if_needed_cache = {} |
| 777 |
| 778 host_file_path = '/test/host/path' |
| 779 device_file_path = '/test/device/path' |
| 780 |
| 781 mock_file_info = { |
| 782 '/test/host/path': { |
| 783 'os.path.exists': True, |
| 784 'os.path.isdir': False, |
| 785 'os.path.getsize': 100, |
| 786 'os.stat': self.createMockOSStatResult(st_mtime=1000000000) |
| 787 }, |
| 788 } |
| 789 |
| 790 os_path_exists = mock.Mock() |
| 791 os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] |
| 792 |
| 793 os_path_isdir = mock.Mock() |
| 794 os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] |
| 795 |
| 796 os_path_getsize = mock.Mock() |
| 797 os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] |
| 798 |
| 799 os_stat = mock.Mock() |
| 800 os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] |
| 801 |
| 802 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 803 return_value=[('/test/host/path', '/test/device/path')]) |
| 804 |
| 805 with mock.patch('os.path.exists', new=os_path_exists), ( |
| 806 mock.patch('os.path.isdir', new=os_path_isdir)), ( |
| 807 mock.patch('os.path.getsize', new=os_path_getsize)), ( |
| 808 mock.patch('os.stat', new=os_stat)): |
| 809 with self.assertOldImplCalls('adb -s 0123456789abcdef push ' |
| 810 '/test/host/path /test/device/path', '100 B/s (100 B in 1.000s)\r\n'): |
| 811 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 812 |
| 813 def testPushChangedFiles_directory_nothingChanged(self): |
| 814 self.device.old_interface._push_if_needed_cache = {} |
| 815 |
| 816 host_file_path = '/test/host/path' |
| 817 device_file_path = '/test/device/path' |
| 818 |
| 819 mock_file_info = { |
| 820 '/test/host/path': { |
| 821 'os.path.exists': True, |
| 822 'os.path.isdir': True, |
| 823 'os.path.getsize': 256, |
| 824 'os.stat': self.createMockOSStatResult(st_mtime=1000000000) |
| 825 }, |
| 826 '/test/host/path/file1': { |
| 827 'os.path.exists': True, |
| 828 'os.path.isdir': False, |
| 829 'os.path.getsize': 251, |
| 830 'os.stat': self.createMockOSStatResult(st_mtime=1000000001) |
| 831 }, |
| 832 '/test/host/path/file2': { |
| 833 'os.path.exists': True, |
| 834 'os.path.isdir': False, |
| 835 'os.path.getsize': 252, |
| 836 'os.stat': self.createMockOSStatResult(st_mtime=1000000002) |
| 837 }, |
| 838 } |
| 839 |
| 840 os_path_exists = mock.Mock() |
| 841 os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] |
| 842 |
| 843 os_path_isdir = mock.Mock() |
| 844 os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] |
| 845 |
| 846 os_path_getsize = mock.Mock() |
| 847 os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] |
| 848 |
| 849 os_stat = mock.Mock() |
| 850 os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] |
| 851 |
| 852 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) |
| 853 |
| 854 with mock.patch('os.path.exists', new=os_path_exists), ( |
| 855 mock.patch('os.path.isdir', new=os_path_isdir)), ( |
| 856 mock.patch('os.path.getsize', new=os_path_getsize)), ( |
| 857 mock.patch('os.stat', new=os_stat)): |
| 858 with self.assertOldImplCallsSequence([ |
| 859 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 860 '')]): |
| 861 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 862 |
| 863 def testPushChangedFiles_directory_somethingChanged(self): |
| 864 self.device.old_interface._push_if_needed_cache = {} |
| 865 |
| 866 host_file_path = '/test/host/path' |
| 867 device_file_path = '/test/device/path' |
| 868 |
| 869 mock_file_info = { |
| 870 '/test/host/path': { |
| 871 'os.path.exists': True, |
| 872 'os.path.isdir': True, |
| 873 'os.path.getsize': 256, |
| 874 'os.stat': self.createMockOSStatResult(st_mtime=1000000000), |
| 875 'os.walk': [('/test/host/path', [], ['file1', 'file2'])] |
| 876 }, |
| 877 '/test/host/path/file1': { |
| 878 'os.path.exists': True, |
| 879 'os.path.isdir': False, |
| 880 'os.path.getsize': 256, |
| 881 'os.stat': self.createMockOSStatResult(st_mtime=1000000001) |
| 882 }, |
| 883 '/test/host/path/file2': { |
| 884 'os.path.exists': True, |
| 885 'os.path.isdir': False, |
| 886 'os.path.getsize': 256, |
| 887 'os.stat': self.createMockOSStatResult(st_mtime=1000000002) |
| 888 }, |
| 889 } |
| 890 |
| 891 os_path_exists = mock.Mock() |
| 892 os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] |
| 893 |
| 894 os_path_isdir = mock.Mock() |
| 895 os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] |
| 896 |
| 897 os_path_getsize = mock.Mock() |
| 898 os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] |
| 899 |
| 900 os_stat = mock.Mock() |
| 901 os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] |
| 902 |
| 903 os_walk = mock.Mock() |
| 904 os_walk.side_effect = lambda f: mock_file_info[f]['os.walk'] |
| 905 |
| 906 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 907 return_value=[('/test/host/path/file1', '/test/device/path/file1')]) |
| 908 |
| 909 with mock.patch('os.path.exists', new=os_path_exists), ( |
| 910 mock.patch('os.path.isdir', new=os_path_isdir)), ( |
| 911 mock.patch('os.path.getsize', new=os_path_getsize)), ( |
| 912 mock.patch('os.stat', new=os_stat)), ( |
| 913 mock.patch('os.walk', new=os_walk)): |
| 914 with self.assertOldImplCallsSequence([ |
| 915 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 916 ''), |
| 917 ('adb -s 0123456789abcdef push ' |
| 918 '/test/host/path/file1 /test/device/path/file1', |
| 919 '256 B/s (256 B in 1.000s)\r\n')]): |
| 920 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 921 |
| 922 def testPushChangedFiles_directory_everythingChanged(self): |
| 923 self.device.old_interface._push_if_needed_cache = {} |
| 924 |
| 925 host_file_path = '/test/host/path' |
| 926 device_file_path = '/test/device/path' |
| 927 |
| 928 mock_file_info = { |
| 929 '/test/host/path': { |
| 930 'os.path.exists': True, |
| 931 'os.path.isdir': True, |
| 932 'os.path.getsize': 256, |
| 933 'os.stat': self.createMockOSStatResult(st_mtime=1000000000) |
| 934 }, |
| 935 '/test/host/path/file1': { |
| 936 'os.path.exists': True, |
| 937 'os.path.isdir': False, |
| 938 'os.path.getsize': 256, |
| 939 'os.stat': self.createMockOSStatResult(st_mtime=1000000001) |
| 940 }, |
| 941 '/test/host/path/file2': { |
| 942 'os.path.exists': True, |
| 943 'os.path.isdir': False, |
| 944 'os.path.getsize': 256, |
| 945 'os.stat': self.createMockOSStatResult(st_mtime=1000000002) |
| 946 }, |
| 947 } |
| 948 |
| 949 os_path_exists = mock.Mock() |
| 950 os_path_exists.side_effect = lambda f: mock_file_info[f]['os.path.exists'] |
| 951 |
| 952 os_path_isdir = mock.Mock() |
| 953 os_path_isdir.side_effect = lambda f: mock_file_info[f]['os.path.isdir'] |
| 954 |
| 955 os_path_getsize = mock.Mock() |
| 956 os_path_getsize.side_effect = lambda f: mock_file_info[f]['os.path.getsize'] |
| 957 |
| 958 os_stat = mock.Mock() |
| 959 os_stat.side_effect = lambda f: mock_file_info[f]['os.stat'] |
| 960 |
| 961 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 962 return_value=[('/test/host/path/file1', '/test/device/path/file1'), |
| 963 ('/test/host/path/file2', '/test/device/path/file2')]) |
| 964 |
| 965 with mock.patch('os.path.exists', new=os_path_exists), ( |
| 966 mock.patch('os.path.isdir', new=os_path_isdir)), ( |
| 967 mock.patch('os.path.getsize', new=os_path_getsize)), ( |
| 968 mock.patch('os.stat', new=os_stat)): |
| 969 with self.assertOldImplCallsSequence([ |
| 970 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 971 ''), |
| 972 ('adb -s 0123456789abcdef push /test/host/path /test/device/path', |
| 973 '768 B/s (768 B in 1.000s)\r\n')]): |
| 974 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 975 |
| 976 def testFileExists_usingTest_fileExists(self): |
| 977 with self.assertOldImplCalls( |
| 978 "adb -s 0123456789abcdef shell " |
| 979 "'test -e \"/data/app/test.file.exists\"; echo $?'", |
| 980 '0\r\n'): |
| 981 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) |
| 982 |
| 983 def testFileExists_usingTest_fileDoesntExist(self): |
| 984 with self.assertOldImplCalls( |
| 985 "adb -s 0123456789abcdef shell " |
| 986 "'test -e \"/data/app/test.file.does.not.exist\"; echo $?'", |
| 987 '1\r\n'): |
| 988 self.assertFalse(self.device.FileExists( |
| 989 '/data/app/test.file.does.not.exist')) |
| 990 |
| 991 def testFileExists_usingLs_fileExists(self): |
| 992 with self.assertOldImplCallsSequence([ |
| 993 ("adb -s 0123456789abcdef shell " |
| 994 "'test -e \"/data/app/test.file.exists\"; echo $?'", |
| 995 'test: not found\r\n'), |
| 996 ("adb -s 0123456789abcdef shell " |
| 997 "'ls \"/data/app/test.file.exists\" >/dev/null 2>&1; echo $?'", |
| 998 '0\r\n')]): |
| 999 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) |
| 1000 |
| 1001 def testFileExists_usingLs_fileDoesntExist(self): |
| 1002 with self.assertOldImplCallsSequence([ |
| 1003 ("adb -s 0123456789abcdef shell " |
| 1004 "'test -e \"/data/app/test.file.does.not.exist\"; echo $?'", |
| 1005 'test: not found\r\n'), |
| 1006 ("adb -s 0123456789abcdef shell " |
| 1007 "'ls \"/data/app/test.file.does.not.exist\" " |
| 1008 ">/dev/null 2>&1; echo $?'", |
| 1009 '1\r\n')]): |
| 1010 self.assertFalse(self.device.FileExists( |
| 1011 '/data/app/test.file.does.not.exist')) |
| 1012 |
| 1013 def testPullFile_existsOnDevice(self): |
| 1014 with mock.patch('os.path.exists', return_value=True): |
| 1015 with self.assertOldImplCallsSequence([ |
| 1016 ('adb -s 0123456789abcdef shell ' |
| 1017 'ls /data/app/test.file.exists', |
| 1018 '/data/app/test.file.exists'), |
| 1019 ('adb -s 0123456789abcdef pull ' |
| 1020 '/data/app/test.file.exists /test/file/host/path', |
| 1021 '100 B/s (100 bytes in 1.000s)\r\n')]): |
| 1022 self.device.PullFile('/data/app/test.file.exists', |
| 1023 '/test/file/host/path') |
| 1024 |
| 1025 def testPullFile_doesntExistOnDevice(self): |
| 1026 with mock.patch('os.path.exists', return_value=True): |
| 1027 with self.assertOldImplCalls( |
| 1028 'adb -s 0123456789abcdef shell ' |
| 1029 'ls /data/app/test.file.does.not.exist', |
| 1030 '/data/app/test.file.does.not.exist: No such file or directory\r\n'): |
| 1031 with self.assertRaises(device_errors.CommandFailedError): |
| 1032 self.device.PullFile('/data/app/test.file.does.not.exist', |
| 1033 '/test/file/host/path') |
| 1034 |
| 1035 def testReadFile_exists(self): |
| 1036 with self.assertOldImplCallsSequence([ |
| 1037 ("adb -s 0123456789abcdef shell " |
| 1038 "'test -e \"/read/this/test/file\"; echo $?'", |
| 1039 '0\r\n'), |
| 1040 ("adb -s 0123456789abcdef shell " |
| 1041 "'cat \"/read/this/test/file\" 2>/dev/null'", |
| 1042 'this is a test file')]): |
| 1043 self.assertEqual(['this is a test file'], |
| 1044 self.device.ReadFile('/read/this/test/file')) |
| 1045 |
| 1046 def testReadFile_doesNotExist(self): |
| 1047 with self.assertOldImplCalls( |
| 1048 "adb -s 0123456789abcdef shell " |
| 1049 "'test -e \"/this/file/does.not.exist\"; echo $?'", |
| 1050 '1\r\n'): |
| 1051 with self.assertRaises(device_errors.CommandFailedError): |
| 1052 self.device.ReadFile('/this/file/does.not.exist') |
| 1053 |
| 1054 def testReadFile_asRoot_withRoot(self): |
| 1055 self.device.old_interface._privileged_command_runner = ( |
| 1056 self.device.old_interface.RunShellCommand) |
| 1057 self.device.old_interface._protected_file_access_method_initialized = True |
| 1058 with self.assertOldImplCallsSequence([ |
| 1059 ("adb -s 0123456789abcdef shell " |
| 1060 "'test -e \"/this/file/must.be.read.by.root\"; echo $?'", |
| 1061 '0\r\n'), |
| 1062 ("adb -s 0123456789abcdef shell " |
| 1063 "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", |
| 1064 'this is a test file\nread by root')]): |
| 1065 self.assertEqual( |
| 1066 ['this is a test file', 'read by root'], |
| 1067 self.device.ReadFile('/this/file/must.be.read.by.root', |
| 1068 as_root=True)) |
| 1069 |
| 1070 def testReadFile_asRoot_withSu(self): |
| 1071 self.device.old_interface._privileged_command_runner = ( |
| 1072 self.device.old_interface.RunShellCommandWithSU) |
| 1073 self.device.old_interface._protected_file_access_method_initialized = True |
| 1074 with self.assertOldImplCallsSequence([ |
| 1075 ("adb -s 0123456789abcdef shell " |
| 1076 "'test -e \"/this/file/can.be.read.with.su\"; echo $?'", |
| 1077 '0\r\n'), |
| 1078 ("adb -s 0123456789abcdef shell " |
| 1079 "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", |
| 1080 'this is a test file\nread with su')]): |
| 1081 self.assertEqual( |
| 1082 ['this is a test file', 'read with su'], |
| 1083 self.device.ReadFile('/this/file/can.be.read.with.su', |
| 1084 as_root=True)) |
| 1085 |
| 1086 def testReadFile_asRoot_rejected(self): |
| 1087 self.device.old_interface._privileged_command_runner = None |
| 1088 self.device.old_interface._protected_file_access_method_initialized = True |
| 1089 with self.assertOldImplCalls( |
| 1090 "adb -s 0123456789abcdef shell " |
| 1091 "'test -e \"/this/file/cannot.be.read.by.user\"; echo $?'", |
| 1092 '0\r\n'): |
| 1093 with self.assertRaises(device_errors.CommandFailedError): |
| 1094 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
| 1095 as_root=True) |
| 1096 |
| 1097 def testWriteFile_basic(self): |
| 1098 mock_file = mock.MagicMock(spec=file) |
| 1099 mock_file.name = '/tmp/file/to.be.pushed' |
| 1100 mock_file.__enter__.return_value = mock_file |
| 1101 with mock.patch('tempfile.NamedTemporaryFile', |
| 1102 return_value=mock_file): |
| 1103 with self.assertOldImplCalls( |
| 1104 'adb -s 0123456789abcdef push ' |
| 1105 '/tmp/file/to.be.pushed /test/file/written.to.device', |
| 1106 '100 B/s (100 bytes in 1.000s)\r\n'): |
| 1107 self.device.WriteFile('/test/file/written.to.device', |
| 1108 'new test file contents') |
| 1109 mock_file.write.assert_called_once_with('new test file contents') |
| 1110 |
| 1111 def testWriteFile_asRoot_withRoot(self): |
| 1112 self.device.old_interface._external_storage = '/fake/storage/path' |
| 1113 self.device.old_interface._privileged_command_runner = ( |
| 1114 self.device.old_interface.RunShellCommand) |
| 1115 self.device.old_interface._protected_file_access_method_initialized = True |
| 1116 |
| 1117 mock_file = mock.MagicMock(spec=file) |
| 1118 mock_file.name = '/tmp/file/to.be.pushed' |
| 1119 mock_file.__enter__.return_value = mock_file |
| 1120 with mock.patch('tempfile.NamedTemporaryFile', |
| 1121 return_value=mock_file): |
| 1122 with self.assertOldImplCallsSequence( |
| 1123 cmd_ret=[ |
| 1124 # Create temporary contents file |
| 1125 (r"adb -s 0123456789abcdef shell " |
| 1126 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " |
| 1127 "echo \$\?'", |
| 1128 '1\r\n'), |
| 1129 # Create temporary script file |
| 1130 (r"adb -s 0123456789abcdef shell " |
| 1131 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " |
| 1132 "echo \$\?'", |
| 1133 '1\r\n'), |
| 1134 # Set contents file |
| 1135 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
| 1136 '/fake/storage/path/temp_file-\d+\d+', |
| 1137 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1138 # Set script file |
| 1139 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
| 1140 '/fake/storage/path/temp_file-\d+\d+', |
| 1141 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1142 # Call script |
| 1143 (r"adb -s 0123456789abcdef shell " |
| 1144 "'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
| 1145 # Remove device temporaries |
| 1146 (r"adb -s 0123456789abcdef shell " |
| 1147 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
| 1148 (r"adb -s 0123456789abcdef shell " |
| 1149 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], |
| 1150 comp=re.match): |
| 1151 self.device.WriteFile('/test/file/written.to.device', |
| 1152 'new test file contents', as_root=True) |
| 1153 |
| 1154 def testWriteFile_asRoot_withSu(self): |
| 1155 self.device.old_interface._external_storage = '/fake/storage/path' |
| 1156 self.device.old_interface._privileged_command_runner = ( |
| 1157 self.device.old_interface.RunShellCommandWithSU) |
| 1158 self.device.old_interface._protected_file_access_method_initialized = True |
| 1159 |
| 1160 mock_file = mock.MagicMock(spec=file) |
| 1161 mock_file.name = '/tmp/file/to.be.pushed' |
| 1162 mock_file.__enter__.return_value = mock_file |
| 1163 with mock.patch('tempfile.NamedTemporaryFile', |
| 1164 return_value=mock_file): |
| 1165 with self.assertOldImplCallsSequence( |
| 1166 cmd_ret=[ |
| 1167 # Create temporary contents file |
| 1168 (r"adb -s 0123456789abcdef shell " |
| 1169 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " |
| 1170 "echo \$\?'", |
| 1171 '1\r\n'), |
| 1172 # Create temporary script file |
| 1173 (r"adb -s 0123456789abcdef shell " |
| 1174 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " |
| 1175 "echo \$\?'", |
| 1176 '1\r\n'), |
| 1177 # Set contents file |
| 1178 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
| 1179 '/fake/storage/path/temp_file-\d+\d+', |
| 1180 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1181 # Set script file |
| 1182 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
| 1183 '/fake/storage/path/temp_file-\d+\d+', |
| 1184 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1185 # Call script |
| 1186 (r"adb -s 0123456789abcdef shell " |
| 1187 "'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
| 1188 # Remove device temporaries |
| 1189 (r"adb -s 0123456789abcdef shell " |
| 1190 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
| 1191 (r"adb -s 0123456789abcdef shell " |
| 1192 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], |
| 1193 comp=re.match): |
| 1194 self.device.WriteFile('/test/file/written.to.device', |
| 1195 'new test file contents', as_root=True) |
| 1196 |
| 1197 def testWriteFile_asRoot_rejected(self): |
| 1198 self.device.old_interface._privileged_command_runner = None |
| 1199 self.device.old_interface._protected_file_access_method_initialized = True |
| 1200 with self.assertRaises(device_errors.CommandFailedError): |
| 1201 self.device.WriteFile('/test/file/no.permissions.to.write', |
| 1202 'new test file contents', as_root=True) |
| 1203 |
720 | 1204 |
721 if __name__ == '__main__': | 1205 if __name__ == '__main__': |
722 unittest.main(verbosity=2) | 1206 unittest.main(verbosity=2) |
723 | 1207 |
OLD | NEW |