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 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 with mock.patch('os.path.exists', return_value=True): | 1058 with mock.patch('os.path.exists', return_value=True): |
1059 with self.assertCall( | 1059 with self.assertCall( |
1060 self.call.adb.Pull('/data/app/test.file.does.not.exist', | 1060 self.call.adb.Pull('/data/app/test.file.does.not.exist', |
1061 '/test/file/host/path'), | 1061 '/test/file/host/path'), |
1062 self.CommandError('remote object does not exist')): | 1062 self.CommandError('remote object does not exist')): |
1063 with self.assertRaises(device_errors.CommandFailedError): | 1063 with self.assertRaises(device_errors.CommandFailedError): |
1064 self.device.PullFile('/data/app/test.file.does.not.exist', | 1064 self.device.PullFile('/data/app/test.file.does.not.exist', |
1065 '/test/file/host/path') | 1065 '/test/file/host/path') |
1066 | 1066 |
1067 | 1067 |
1068 class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): | 1068 class DeviceUtilsReadFileTest(DeviceUtilsNewImplTest): |
1069 | 1069 |
1070 def testReadFile_exists(self): | 1070 def testReadFile_exists(self): |
1071 with self.assertCallsSequence([ | 1071 with self.assertCall( |
1072 ("adb -s 0123456789abcdef shell " | 1072 self.call.adb.Shell('cat /read/this/test/file'), |
1073 "'cat \"/read/this/test/file\" 2>/dev/null'", | 1073 'this is a test file\r\n'): |
1074 'this is a test file')]): | 1074 self.assertEqual('this is a test file\n', |
1075 self.assertEqual(['this is a test file'], | |
1076 self.device.ReadFile('/read/this/test/file')) | 1075 self.device.ReadFile('/read/this/test/file')) |
1077 | 1076 |
1078 def testReadFile_doesNotExist(self): | 1077 def testReadFile_doesNotExist(self): |
| 1078 with self.assertCall( |
| 1079 self.call.adb.Shell('cat /this/file/does.not.exist'), |
| 1080 self.ShellError('/system/bin/sh: cat: /this/file/does.not.exist: ' |
| 1081 'No such file or directory')): |
| 1082 with self.assertRaises(device_errors.AdbCommandFailedError): |
| 1083 self.device.ReadFile('/this/file/does.not.exist') |
| 1084 |
| 1085 def testReadFile_withSU(self): |
1079 with self.assertCalls( | 1086 with self.assertCalls( |
1080 "adb -s 0123456789abcdef shell " | 1087 (self.call.device.NeedsSU(), True), |
1081 "'cat \"/this/file/does.not.exist\" 2>/dev/null'", | 1088 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), |
1082 ''): | 1089 'this is a test file\nread with su')): |
1083 self.device.ReadFile('/this/file/does.not.exist') | |
1084 | |
1085 def testReadFile_asRoot_withRoot(self): | |
1086 self.device.old_interface._privileged_command_runner = ( | |
1087 self.device.old_interface.RunShellCommand) | |
1088 self.device.old_interface._protected_file_access_method_initialized = True | |
1089 with self.assertCallsSequence([ | |
1090 ("adb -s 0123456789abcdef shell " | |
1091 "'cat \"/this/file/must.be.read.by.root\" 2> /dev/null'", | |
1092 'this is a test file\nread by root')]): | |
1093 self.assertEqual( | 1090 self.assertEqual( |
1094 ['this is a test file', 'read by root'], | 1091 'this is a test file\nread with su\n', |
1095 self.device.ReadFile('/this/file/must.be.read.by.root', | |
1096 as_root=True)) | |
1097 | |
1098 def testReadFile_asRoot_withSu(self): | |
1099 self.device.old_interface._privileged_command_runner = ( | |
1100 self.device.old_interface.RunShellCommandWithSU) | |
1101 self.device.old_interface._protected_file_access_method_initialized = True | |
1102 with self.assertCallsSequence([ | |
1103 ("adb -s 0123456789abcdef shell " | |
1104 "'su -c cat \"/this/file/can.be.read.with.su\" 2> /dev/null'", | |
1105 'this is a test file\nread with su')]): | |
1106 self.assertEqual( | |
1107 ['this is a test file', 'read with su'], | |
1108 self.device.ReadFile('/this/file/can.be.read.with.su', | 1092 self.device.ReadFile('/this/file/can.be.read.with.su', |
1109 as_root=True)) | 1093 as_root=True)) |
1110 | 1094 |
1111 def testReadFile_asRoot_rejected(self): | |
1112 self.device.old_interface._privileged_command_runner = None | |
1113 self.device.old_interface._protected_file_access_method_initialized = True | |
1114 with self.assertRaises(device_errors.CommandFailedError): | |
1115 self.device.ReadFile('/this/file/cannot.be.read.by.user', | |
1116 as_root=True) | |
1117 | |
1118 | 1095 |
1119 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): | 1096 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1120 | 1097 |
1121 def testWriteFile_withPush(self): | 1098 def testWriteFile_withPush(self): |
1122 tmp_host = MockTempFile('/tmp/file/on.host') | 1099 tmp_host = MockTempFile('/tmp/file/on.host') |
1123 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars | 1100 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1124 with self.assertCalls( | 1101 with self.assertCalls( |
1125 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1102 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1126 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1103 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1127 self.device.WriteFile('/path/to/device/file', contents) | 1104 self.device.WriteFile('/path/to/device/file', contents) |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1481 self.assertTrue( | 1458 self.assertTrue( |
1482 isinstance(device, device_utils.DeviceUtils) | 1459 isinstance(device, device_utils.DeviceUtils) |
1483 and serial == str(device), | 1460 and serial == str(device), |
1484 'Expected a DeviceUtils object with serial %s' % serial) | 1461 'Expected a DeviceUtils object with serial %s' % serial) |
1485 | 1462 |
1486 | 1463 |
1487 if __name__ == '__main__': | 1464 if __name__ == '__main__': |
1488 logging.getLogger().setLevel(logging.DEBUG) | 1465 logging.getLogger().setLevel(logging.DEBUG) |
1489 unittest.main(verbosity=2) | 1466 unittest.main(verbosity=2) |
1490 | 1467 |
OLD | NEW |