| 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 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 with mock.patch('os.path.exists', return_value=True): | 1042 with mock.patch('os.path.exists', return_value=True): |
| 1043 with self.assertCall( | 1043 with self.assertCall( |
| 1044 self.call.adb.Pull('/data/app/test.file.does.not.exist', | 1044 self.call.adb.Pull('/data/app/test.file.does.not.exist', |
| 1045 '/test/file/host/path'), | 1045 '/test/file/host/path'), |
| 1046 self.CommandError('remote object does not exist')): | 1046 self.CommandError('remote object does not exist')): |
| 1047 with self.assertRaises(device_errors.CommandFailedError): | 1047 with self.assertRaises(device_errors.CommandFailedError): |
| 1048 self.device.PullFile('/data/app/test.file.does.not.exist', | 1048 self.device.PullFile('/data/app/test.file.does.not.exist', |
| 1049 '/test/file/host/path') | 1049 '/test/file/host/path') |
| 1050 | 1050 |
| 1051 | 1051 |
| 1052 class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): | 1052 class DeviceUtilsReadFileTest(DeviceUtilsNewImplTest): |
| 1053 | 1053 |
| 1054 def testReadFile_exists(self): | 1054 def testReadFile_exists(self): |
| 1055 with self.assertCallsSequence([ | 1055 with self.assertCall( |
| 1056 ("adb -s 0123456789abcdef shell " | 1056 self.call.adb.Shell('cat /read/this/test/file'), |
| 1057 "'cat \"/read/this/test/file\" 2>/dev/null'", | 1057 'this is a test file\r\n'): |
| 1058 'this is a test file')]): | 1058 self.assertEqual('this is a test file\n', |
| 1059 self.assertEqual(['this is a test file'], | |
| 1060 self.device.ReadFile('/read/this/test/file')) | 1059 self.device.ReadFile('/read/this/test/file')) |
| 1061 | 1060 |
| 1062 def testReadFile_doesNotExist(self): | 1061 def testReadFile_doesNotExist(self): |
| 1062 with self.assertCall( |
| 1063 self.call.adb.Shell('cat /this/file/does.not.exist'), |
| 1064 self.ShellError('/system/bin/sh: cat: /this/file/does.not.exist: ' |
| 1065 'No such file or directory')): |
| 1066 with self.assertRaises(device_errors.AdbCommandFailedError): |
| 1067 self.device.ReadFile('/this/file/does.not.exist') |
| 1068 |
| 1069 def testReadFile_withSU(self): |
| 1063 with self.assertCalls( | 1070 with self.assertCalls( |
| 1064 "adb -s 0123456789abcdef shell " | 1071 (self.call.device.NeedsSU(), True), |
| 1065 "'cat \"/this/file/does.not.exist\" 2>/dev/null'", | 1072 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), |
| 1066 ''): | 1073 'this is a test file\nread with su')): |
| 1067 self.device.ReadFile('/this/file/does.not.exist') | |
| 1068 | |
| 1069 def testReadFile_asRoot_withRoot(self): | |
| 1070 self.device.old_interface._privileged_command_runner = ( | |
| 1071 self.device.old_interface.RunShellCommand) | |
| 1072 self.device.old_interface._protected_file_access_method_initialized = True | |
| 1073 with self.assertCallsSequence([ | |
| 1074 ("adb -s 0123456789abcdef shell " | |
| 1075 "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", | |
| 1076 'this is a test file\nread by root')]): | |
| 1077 self.assertEqual( | 1074 self.assertEqual( |
| 1078 ['this is a test file', 'read by root'], | 1075 'this is a test file\nread with su\n', |
| 1079 self.device.ReadFile('/this/file/must.be.read.by.root', | |
| 1080 as_root=True)) | |
| 1081 | |
| 1082 def testReadFile_asRoot_withSu(self): | |
| 1083 self.device.old_interface._privileged_command_runner = ( | |
| 1084 self.device.old_interface.RunShellCommandWithSU) | |
| 1085 self.device.old_interface._protected_file_access_method_initialized = True | |
| 1086 with self.assertCallsSequence([ | |
| 1087 ("adb -s 0123456789abcdef shell " | |
| 1088 "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", | |
| 1089 'this is a test file\nread with su')]): | |
| 1090 self.assertEqual( | |
| 1091 ['this is a test file', 'read with su'], | |
| 1092 self.device.ReadFile('/this/file/can.be.read.with.su', | 1076 self.device.ReadFile('/this/file/can.be.read.with.su', |
| 1093 as_root=True)) | 1077 as_root=True)) |
| 1094 | 1078 |
| 1095 def testReadFile_asRoot_rejected(self): | |
| 1096 self.device.old_interface._privileged_command_runner = None | |
| 1097 self.device.old_interface._protected_file_access_method_initialized = True | |
| 1098 with self.assertRaises(device_errors.CommandFailedError): | |
| 1099 self.device.ReadFile('/this/file/cannot.be.read.by.user', | |
| 1100 as_root=True) | |
| 1101 | |
| 1102 | 1079 |
| 1103 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1080 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
| 1104 | 1081 |
| 1105 def testWriteFile_withPush(self): | 1082 def testWriteFile_withPush(self): |
| 1106 tmp_host = MockTempFile('/tmp/file/on.host') | 1083 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1107 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1084 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1108 with self.assertCalls( | 1085 with self.assertCalls( |
| 1109 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1086 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1110 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1087 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1111 self.device.WriteFile('/path/to/device/file', contents) | 1088 self.device.WriteFile('/path/to/device/file', contents) |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1465 self.assertTrue( | 1442 self.assertTrue( |
| 1466 isinstance(device, device_utils.DeviceUtils) | 1443 isinstance(device, device_utils.DeviceUtils) |
| 1467 and serial == str(device), | 1444 and serial == str(device), |
| 1468 'Expected a DeviceUtils object with serial %s' % serial) | 1445 'Expected a DeviceUtils object with serial %s' % serial) |
| 1469 | 1446 |
| 1470 | 1447 |
| 1471 if __name__ == '__main__': | 1448 if __name__ == '__main__': |
| 1472 logging.getLogger().setLevel(logging.DEBUG) | 1449 logging.getLogger().setLevel(logging.DEBUG) |
| 1473 unittest.main(verbosity=2) | 1450 unittest.main(verbosity=2) |
| 1474 | 1451 |
| OLD | NEW |