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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 246 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
247 self.adb.__str__ = mock.Mock(return_value=test_serial) | 247 self.adb.__str__ = mock.Mock(return_value=test_serial) |
248 self.adb.GetDeviceSerial.return_value = test_serial | 248 self.adb.GetDeviceSerial.return_value = test_serial |
249 self.device = device_utils.DeviceUtils( | 249 self.device = device_utils.DeviceUtils( |
250 self.adb, default_timeout=10, default_retries=0) | 250 self.adb, default_timeout=10, default_retries=0) |
251 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) | 251 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) |
252 | 252 |
253 def ShellError(self, output=None, exit_code=1): | 253 def ShellError(self, output=None, exit_code=1): |
254 def action(cmd, *args, **kwargs): | 254 def action(cmd, *args, **kwargs): |
255 raise device_errors.AdbCommandFailedError( | 255 raise device_errors.AdbCommandFailedError( |
256 cmd, output, exit_code, str(self.device)) | 256 ['shell', cmd], output, exit_code, str(self.device)) |
257 if output is None: | 257 if output is None: |
258 output = 'Permission denied\n' | 258 output = 'Permission denied\n' |
259 return action | 259 return action |
260 | 260 |
261 def TimeoutError(self, msg=None): | 261 def TimeoutError(self, msg=None): |
262 if msg is None: | 262 if msg is None: |
263 msg = 'Operation timed out' | 263 msg = 'Operation timed out' |
264 return mock.Mock(side_effect=device_errors.CommandTimeoutError( | 264 return mock.Mock(side_effect=device_errors.CommandTimeoutError( |
265 msg, str(self.device))) | 265 msg, str(self.device))) |
266 | 266 |
(...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 with mock.patch('os.path.exists', return_value=True): | 1116 with mock.patch('os.path.exists', return_value=True): |
1117 with self.assertCalls( | 1117 with self.assertCalls( |
1118 'adb -s 0123456789abcdef shell ' | 1118 'adb -s 0123456789abcdef shell ' |
1119 'ls /data/app/test.file.does.not.exist', | 1119 'ls /data/app/test.file.does.not.exist', |
1120 '/data/app/test.file.does.not.exist: No such file or directory\r\n'): | 1120 '/data/app/test.file.does.not.exist: No such file or directory\r\n'): |
1121 with self.assertRaises(device_errors.CommandFailedError): | 1121 with self.assertRaises(device_errors.CommandFailedError): |
1122 self.device.PullFile('/data/app/test.file.does.not.exist', | 1122 self.device.PullFile('/data/app/test.file.does.not.exist', |
1123 '/test/file/host/path') | 1123 '/test/file/host/path') |
1124 | 1124 |
1125 | 1125 |
1126 class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): | 1126 class DeviceUtilsReadFileTest(DeviceUtilsNewImplTest): |
1127 | 1127 |
1128 def testReadFile_exists(self): | 1128 def testReadFile_exists(self): |
1129 with self.assertCallsSequence([ | 1129 with self.assertCall( |
1130 ("adb -s 0123456789abcdef shell " | 1130 self.call.adb.Shell('cat /read/this/test/file'), |
1131 "'cat \"/read/this/test/file\" 2>/dev/null'", | 1131 'this is a test file\r\n'): |
1132 'this is a test file')]): | 1132 self.assertEqual('this is a test file\n', |
1133 self.assertEqual(['this is a test file'], | |
1134 self.device.ReadFile('/read/this/test/file')) | 1133 self.device.ReadFile('/read/this/test/file')) |
1135 | 1134 |
1136 def testReadFile_doesNotExist(self): | 1135 def testReadFile_doesNotExist(self): |
| 1136 with self.assertCall( |
| 1137 self.call.adb.Shell('cat /this/file/does.not.exist'), |
| 1138 self.ShellError('/system/bin/sh: cat: /this/file/does.not.exist: ' |
| 1139 'No such file or directory')): |
| 1140 with self.assertRaises(device_errors.AdbCommandFailedError): |
| 1141 self.device.ReadFile('/this/file/does.not.exist') |
| 1142 |
| 1143 def testReadFile_withSU(self): |
1137 with self.assertCalls( | 1144 with self.assertCalls( |
1138 "adb -s 0123456789abcdef shell " | 1145 (self.call.device.NeedsSU(), True), |
1139 "'cat \"/this/file/does.not.exist\" 2>/dev/null'", | 1146 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), |
1140 ''): | 1147 'this is a test file\nread with su')): |
1141 self.device.ReadFile('/this/file/does.not.exist') | |
1142 | |
1143 def testReadFile_asRoot_withRoot(self): | |
1144 self.device.old_interface._privileged_command_runner = ( | |
1145 self.device.old_interface.RunShellCommand) | |
1146 self.device.old_interface._protected_file_access_method_initialized = True | |
1147 with self.assertCallsSequence([ | |
1148 ("adb -s 0123456789abcdef shell " | |
1149 "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", | |
1150 'this is a test file\nread by root')]): | |
1151 self.assertEqual( | 1148 self.assertEqual( |
1152 ['this is a test file', 'read by root'], | 1149 'this is a test file\nread with su\n', |
1153 self.device.ReadFile('/this/file/must.be.read.by.root', | |
1154 as_root=True)) | |
1155 | |
1156 def testReadFile_asRoot_withSu(self): | |
1157 self.device.old_interface._privileged_command_runner = ( | |
1158 self.device.old_interface.RunShellCommandWithSU) | |
1159 self.device.old_interface._protected_file_access_method_initialized = True | |
1160 with self.assertCallsSequence([ | |
1161 ("adb -s 0123456789abcdef shell " | |
1162 "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", | |
1163 'this is a test file\nread with su')]): | |
1164 self.assertEqual( | |
1165 ['this is a test file', 'read with su'], | |
1166 self.device.ReadFile('/this/file/can.be.read.with.su', | 1150 self.device.ReadFile('/this/file/can.be.read.with.su', |
1167 as_root=True)) | 1151 as_root=True)) |
1168 | 1152 |
1169 def testReadFile_asRoot_rejected(self): | |
1170 self.device.old_interface._privileged_command_runner = None | |
1171 self.device.old_interface._protected_file_access_method_initialized = True | |
1172 with self.assertRaises(device_errors.CommandFailedError): | |
1173 self.device.ReadFile('/this/file/cannot.be.read.by.user', | |
1174 as_root=True) | |
1175 | |
1176 | 1153 |
1177 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1154 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1178 | 1155 |
1179 def testWriteFile_withPush(self): | 1156 def testWriteFile_withPush(self): |
1180 tmp_host = MockTempFile('/tmp/file/on.host') | 1157 tmp_host = MockTempFile('/tmp/file/on.host') |
1181 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1158 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1182 with self.assertCalls( | 1159 with self.assertCalls( |
1183 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1160 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1184 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1161 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1185 self.device.WriteFile('/path/to/device/file', contents) | 1162 self.device.WriteFile('/path/to/device/file', contents) |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 def testStr_returnsSerial(self): | 1508 def testStr_returnsSerial(self): |
1532 with self.assertCalls( | 1509 with self.assertCalls( |
1533 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): | 1510 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): |
1534 self.assertEqual('0123456789abcdef', str(self.device)) | 1511 self.assertEqual('0123456789abcdef', str(self.device)) |
1535 | 1512 |
1536 | 1513 |
1537 if __name__ == '__main__': | 1514 if __name__ == '__main__': |
1538 logging.getLogger().setLevel(logging.DEBUG) | 1515 logging.getLogger().setLevel(logging.DEBUG) |
1539 unittest.main(verbosity=2) | 1516 unittest.main(verbosity=2) |
1540 | 1517 |
OLD | NEW |