| 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 815 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1102 with mock.patch('os.path.exists', return_value=True): | 1102 with mock.patch('os.path.exists', return_value=True): |
| 1103 with self.assertCall( | 1103 with self.assertCall( |
| 1104 self.call.adb.Pull('/data/app/test.file.does.not.exist', | 1104 self.call.adb.Pull('/data/app/test.file.does.not.exist', |
| 1105 '/test/file/host/path'), | 1105 '/test/file/host/path'), |
| 1106 self.CommandError('remote object does not exist')): | 1106 self.CommandError('remote object does not exist')): |
| 1107 with self.assertRaises(device_errors.CommandFailedError): | 1107 with self.assertRaises(device_errors.CommandFailedError): |
| 1108 self.device.PullFile('/data/app/test.file.does.not.exist', | 1108 self.device.PullFile('/data/app/test.file.does.not.exist', |
| 1109 '/test/file/host/path') | 1109 '/test/file/host/path') |
| 1110 | 1110 |
| 1111 | 1111 |
| 1112 class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): | 1112 class DeviceUtilsReadFileTest(DeviceUtilsNewImplTest): |
| 1113 | 1113 |
| 1114 def testReadFile_exists(self): | 1114 def testReadFile_exists(self): |
| 1115 with self.assertCallsSequence([ | 1115 with self.assertCall( |
| 1116 ("adb -s 0123456789abcdef shell " | 1116 self.call.adb.Shell('cat /read/this/test/file'), |
| 1117 "'cat \"/read/this/test/file\" 2>/dev/null'", | 1117 'this is a test file\r\n'): |
| 1118 'this is a test file')]): | 1118 self.assertEqual('this is a test file\n', |
| 1119 self.assertEqual(['this is a test file'], | |
| 1120 self.device.ReadFile('/read/this/test/file')) | 1119 self.device.ReadFile('/read/this/test/file')) |
| 1121 | 1120 |
| 1122 def testReadFile_doesNotExist(self): | 1121 def testReadFile_doesNotExist(self): |
| 1122 with self.assertCall( |
| 1123 self.call.adb.Shell('cat /this/file/does.not.exist'), |
| 1124 self.ShellError('/system/bin/sh: cat: /this/file/does.not.exist: ' |
| 1125 'No such file or directory')): |
| 1126 with self.assertRaises(device_errors.AdbCommandFailedError): |
| 1127 self.device.ReadFile('/this/file/does.not.exist') |
| 1128 |
| 1129 def testReadFile_withSU(self): |
| 1123 with self.assertCalls( | 1130 with self.assertCalls( |
| 1124 "adb -s 0123456789abcdef shell " | 1131 (self.call.device.NeedsSU(), True), |
| 1125 "'cat \"/this/file/does.not.exist\" 2>/dev/null'", | 1132 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), |
| 1126 ''): | 1133 'this is a test file\nread with su')): |
| 1127 self.device.ReadFile('/this/file/does.not.exist') | |
| 1128 | |
| 1129 def testReadFile_asRoot_withRoot(self): | |
| 1130 self.device.old_interface._privileged_command_runner = ( | |
| 1131 self.device.old_interface.RunShellCommand) | |
| 1132 self.device.old_interface._protected_file_access_method_initialized = True | |
| 1133 with self.assertCallsSequence([ | |
| 1134 ("adb -s 0123456789abcdef shell " | |
| 1135 "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", | |
| 1136 'this is a test file\nread by root')]): | |
| 1137 self.assertEqual( | 1134 self.assertEqual( |
| 1138 ['this is a test file', 'read by root'], | 1135 'this is a test file\nread with su\n', |
| 1139 self.device.ReadFile('/this/file/must.be.read.by.root', | |
| 1140 as_root=True)) | |
| 1141 | |
| 1142 def testReadFile_asRoot_withSu(self): | |
| 1143 self.device.old_interface._privileged_command_runner = ( | |
| 1144 self.device.old_interface.RunShellCommandWithSU) | |
| 1145 self.device.old_interface._protected_file_access_method_initialized = True | |
| 1146 with self.assertCallsSequence([ | |
| 1147 ("adb -s 0123456789abcdef shell " | |
| 1148 "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", | |
| 1149 'this is a test file\nread with su')]): | |
| 1150 self.assertEqual( | |
| 1151 ['this is a test file', 'read with su'], | |
| 1152 self.device.ReadFile('/this/file/can.be.read.with.su', | 1136 self.device.ReadFile('/this/file/can.be.read.with.su', |
| 1153 as_root=True)) | 1137 as_root=True)) |
| 1154 | 1138 |
| 1155 def testReadFile_asRoot_rejected(self): | |
| 1156 self.device.old_interface._privileged_command_runner = None | |
| 1157 self.device.old_interface._protected_file_access_method_initialized = True | |
| 1158 with self.assertRaises(device_errors.CommandFailedError): | |
| 1159 self.device.ReadFile('/this/file/cannot.be.read.by.user', | |
| 1160 as_root=True) | |
| 1161 | |
| 1162 | 1139 |
| 1163 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1140 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
| 1164 | 1141 |
| 1165 def testWriteFile_withPush(self): | 1142 def testWriteFile_withPush(self): |
| 1166 tmp_host = MockTempFile('/tmp/file/on.host') | 1143 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1167 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1144 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1168 with self.assertCalls( | 1145 with self.assertCalls( |
| 1169 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1146 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1170 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1147 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1171 self.device.WriteFile('/path/to/device/file', contents) | 1148 self.device.WriteFile('/path/to/device/file', contents) |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1535 self.assertTrue( | 1512 self.assertTrue( |
| 1536 isinstance(device, device_utils.DeviceUtils) | 1513 isinstance(device, device_utils.DeviceUtils) |
| 1537 and serial == str(device), | 1514 and serial == str(device), |
| 1538 'Expected a DeviceUtils object with serial %s' % serial) | 1515 'Expected a DeviceUtils object with serial %s' % serial) |
| 1539 | 1516 |
| 1540 | 1517 |
| 1541 if __name__ == '__main__': | 1518 if __name__ == '__main__': |
| 1542 logging.getLogger().setLevel(logging.DEBUG) | 1519 logging.getLogger().setLevel(logging.DEBUG) |
| 1543 unittest.main(verbosity=2) | 1520 unittest.main(verbosity=2) |
| 1544 | 1521 |
| OLD | NEW |