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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 266 |
267 def setUp(self): | 267 def setUp(self): |
268 self.adb = _AdbWrapperMock('0123456789abcdef') | 268 self.adb = _AdbWrapperMock('0123456789abcdef') |
269 self.device = device_utils.DeviceUtils( | 269 self.device = device_utils.DeviceUtils( |
270 self.adb, default_timeout=10, default_retries=0) | 270 self.adb, default_timeout=10, default_retries=0) |
271 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) | 271 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) |
272 | 272 |
273 def ShellError(self, output=None, exit_code=1): | 273 def ShellError(self, output=None, exit_code=1): |
274 def action(cmd, *args, **kwargs): | 274 def action(cmd, *args, **kwargs): |
275 raise device_errors.AdbCommandFailedError( | 275 raise device_errors.AdbCommandFailedError( |
276 cmd, output, exit_code, str(self.device)) | 276 ['shell', cmd], output, exit_code, str(self.device)) |
277 if output is None: | 277 if output is None: |
278 output = 'Permission denied\n' | 278 output = 'Permission denied\n' |
279 return action | 279 return action |
280 | 280 |
281 def TimeoutError(self, msg=None): | 281 def TimeoutError(self, msg=None): |
282 if msg is None: | 282 if msg is None: |
283 msg = 'Operation timed out' | 283 msg = 'Operation timed out' |
284 return mock.Mock(side_effect=device_errors.CommandTimeoutError( | 284 return mock.Mock(side_effect=device_errors.CommandTimeoutError( |
285 msg, str(self.device))) | 285 msg, str(self.device))) |
286 | 286 |
(...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 with mock.patch('os.path.exists', return_value=True): | 1108 with mock.patch('os.path.exists', return_value=True): |
1109 with self.assertCall( | 1109 with self.assertCall( |
1110 self.call.adb.Pull('/data/app/test.file.does.not.exist', | 1110 self.call.adb.Pull('/data/app/test.file.does.not.exist', |
1111 '/test/file/host/path'), | 1111 '/test/file/host/path'), |
1112 self.CommandError('remote object does not exist')): | 1112 self.CommandError('remote object does not exist')): |
1113 with self.assertRaises(device_errors.CommandFailedError): | 1113 with self.assertRaises(device_errors.CommandFailedError): |
1114 self.device.PullFile('/data/app/test.file.does.not.exist', | 1114 self.device.PullFile('/data/app/test.file.does.not.exist', |
1115 '/test/file/host/path') | 1115 '/test/file/host/path') |
1116 | 1116 |
1117 | 1117 |
1118 class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): | 1118 class DeviceUtilsReadFileTest(DeviceUtilsNewImplTest): |
1119 | 1119 |
1120 def testReadFile_exists(self): | 1120 def testReadFile_exists(self): |
1121 with self.assertCallsSequence([ | 1121 with self.assertCall( |
1122 ("adb -s 0123456789abcdef shell " | 1122 self.call.adb.Shell('cat /read/this/test/file'), |
1123 "'cat \"/read/this/test/file\" 2>/dev/null'", | 1123 'this is a test file\r\n'): |
1124 'this is a test file')]): | 1124 self.assertEqual('this is a test file\n', |
1125 self.assertEqual(['this is a test file'], | |
1126 self.device.ReadFile('/read/this/test/file')) | 1125 self.device.ReadFile('/read/this/test/file')) |
1127 | 1126 |
1128 def testReadFile_doesNotExist(self): | 1127 def testReadFile_doesNotExist(self): |
| 1128 with self.assertCall( |
| 1129 self.call.adb.Shell('cat /this/file/does.not.exist'), |
| 1130 self.ShellError('/system/bin/sh: cat: /this/file/does.not.exist: ' |
| 1131 'No such file or directory')): |
| 1132 with self.assertRaises(device_errors.AdbCommandFailedError): |
| 1133 self.device.ReadFile('/this/file/does.not.exist') |
| 1134 |
| 1135 def testReadFile_withSU(self): |
1129 with self.assertCalls( | 1136 with self.assertCalls( |
1130 "adb -s 0123456789abcdef shell " | 1137 (self.call.device.NeedsSU(), True), |
1131 "'cat \"/this/file/does.not.exist\" 2>/dev/null'", | 1138 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), |
1132 ''): | 1139 'this is a test file\nread with su')): |
1133 self.device.ReadFile('/this/file/does.not.exist') | |
1134 | |
1135 def testReadFile_asRoot_withRoot(self): | |
1136 self.device.old_interface._privileged_command_runner = ( | |
1137 self.device.old_interface.RunShellCommand) | |
1138 self.device.old_interface._protected_file_access_method_initialized = True | |
1139 with self.assertCallsSequence([ | |
1140 ("adb -s 0123456789abcdef shell " | |
1141 "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", | |
1142 'this is a test file\nread by root')]): | |
1143 self.assertEqual( | 1140 self.assertEqual( |
1144 ['this is a test file', 'read by root'], | 1141 'this is a test file\nread with su\n', |
1145 self.device.ReadFile('/this/file/must.be.read.by.root', | |
1146 as_root=True)) | |
1147 | |
1148 def testReadFile_asRoot_withSu(self): | |
1149 self.device.old_interface._privileged_command_runner = ( | |
1150 self.device.old_interface.RunShellCommandWithSU) | |
1151 self.device.old_interface._protected_file_access_method_initialized = True | |
1152 with self.assertCallsSequence([ | |
1153 ("adb -s 0123456789abcdef shell " | |
1154 "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", | |
1155 'this is a test file\nread with su')]): | |
1156 self.assertEqual( | |
1157 ['this is a test file', 'read with su'], | |
1158 self.device.ReadFile('/this/file/can.be.read.with.su', | 1142 self.device.ReadFile('/this/file/can.be.read.with.su', |
1159 as_root=True)) | 1143 as_root=True)) |
1160 | 1144 |
1161 def testReadFile_asRoot_rejected(self): | |
1162 self.device.old_interface._privileged_command_runner = None | |
1163 self.device.old_interface._protected_file_access_method_initialized = True | |
1164 with self.assertRaises(device_errors.CommandFailedError): | |
1165 self.device.ReadFile('/this/file/cannot.be.read.by.user', | |
1166 as_root=True) | |
1167 | |
1168 | 1145 |
1169 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1146 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1170 | 1147 |
1171 def testWriteFile_withPush(self): | 1148 def testWriteFile_withPush(self): |
1172 tmp_host = MockTempFile('/tmp/file/on.host') | 1149 tmp_host = MockTempFile('/tmp/file/on.host') |
1173 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1150 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1174 with self.assertCalls( | 1151 with self.assertCalls( |
1175 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1152 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1176 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1153 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1177 self.device.WriteFile('/path/to/device/file', contents) | 1154 self.device.WriteFile('/path/to/device/file', contents) |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1541 self.assertTrue( | 1518 self.assertTrue( |
1542 isinstance(device, device_utils.DeviceUtils) | 1519 isinstance(device, device_utils.DeviceUtils) |
1543 and serial == str(device), | 1520 and serial == str(device), |
1544 'Expected a DeviceUtils object with serial %s' % serial) | 1521 'Expected a DeviceUtils object with serial %s' % serial) |
1545 | 1522 |
1546 | 1523 |
1547 if __name__ == '__main__': | 1524 if __name__ == '__main__': |
1548 logging.getLogger().setLevel(logging.DEBUG) | 1525 logging.getLogger().setLevel(logging.DEBUG) |
1549 unittest.main(verbosity=2) | 1526 unittest.main(verbosity=2) |
1550 | 1527 |
OLD | NEW |