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 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1039 self.call.adb.Pull('/data/app/test.file.does.not.exist', | 1039 self.call.adb.Pull('/data/app/test.file.does.not.exist', |
1040 '/test/file/host/path'), | 1040 '/test/file/host/path'), |
1041 self.CommandError('remote object does not exist')): | 1041 self.CommandError('remote object does not exist')): |
1042 with self.assertRaises(device_errors.CommandFailedError): | 1042 with self.assertRaises(device_errors.CommandFailedError): |
1043 self.device.PullFile('/data/app/test.file.does.not.exist', | 1043 self.device.PullFile('/data/app/test.file.does.not.exist', |
1044 '/test/file/host/path') | 1044 '/test/file/host/path') |
1045 | 1045 |
1046 | 1046 |
1047 class DeviceUtilsReadFileTest(DeviceUtilsTest): | 1047 class DeviceUtilsReadFileTest(DeviceUtilsTest): |
1048 | 1048 |
| 1049 def testReadFileWithPull_success(self): |
| 1050 tmp_host_dir = '/tmp/dir/on.host/' |
| 1051 tmp_host = MockTempFile('/tmp/dir/on.host/tmp_ReadFileWithPull') |
| 1052 tmp_host.file.read.return_value = 'some interesting contents' |
| 1053 with self.assertCalls( |
| 1054 (mock.call.tempfile.mkdtemp(), tmp_host_dir), |
| 1055 (self.call.adb.Pull('/path/to/device/file', mock.ANY)), |
| 1056 (mock.call.__builtin__.open(mock.ANY, 'r'), tmp_host), |
| 1057 (mock.call.os.path.exists(tmp_host_dir), True), |
| 1058 (mock.call.shutil.rmtree(tmp_host_dir), None)): |
| 1059 self.assertEquals('some interesting contents', |
| 1060 self.device._ReadFileWithPull('/path/to/device/file')) |
| 1061 tmp_host.file.read.assert_called_once_with() |
| 1062 |
| 1063 def testReadFileWithPull_rejected(self): |
| 1064 tmp_host_dir = '/tmp/dir/on.host/' |
| 1065 with self.assertCalls( |
| 1066 (mock.call.tempfile.mkdtemp(), tmp_host_dir), |
| 1067 (self.call.adb.Pull('/path/to/device/file', mock.ANY), |
| 1068 self.CommandError()), |
| 1069 (mock.call.os.path.exists(tmp_host_dir), True), |
| 1070 (mock.call.shutil.rmtree(tmp_host_dir), None)): |
| 1071 with self.assertRaises(device_errors.CommandFailedError): |
| 1072 self.device._ReadFileWithPull('/path/to/device/file') |
| 1073 |
1049 def testReadFile_exists(self): | 1074 def testReadFile_exists(self): |
1050 with self.assertCall( | 1075 with self.assertCalls( |
1051 self.call.adb.Shell('cat /read/this/test/file'), | 1076 (self.call.device.RunShellCommand( |
1052 'this is a test file\r\n'): | 1077 ['ls', '-l', '/read/this/test/file'], |
| 1078 as_root=False, check_return=True), |
| 1079 ['-rw-rw---- root foo 256 1970-01-01 00:00 file']), |
| 1080 (self.call.device.RunShellCommand( |
| 1081 ['cat', '/read/this/test/file'], as_root=False, check_return=True), |
| 1082 ['this is a test file'])): |
1053 self.assertEqual('this is a test file\n', | 1083 self.assertEqual('this is a test file\n', |
1054 self.device.ReadFile('/read/this/test/file')) | 1084 self.device.ReadFile('/read/this/test/file')) |
1055 | 1085 |
1056 def testReadFile_doesNotExist(self): | 1086 def testReadFile_doesNotExist(self): |
1057 with self.assertCall( | 1087 with self.assertCall( |
1058 self.call.adb.Shell('cat /this/file/does.not.exist'), | 1088 self.call.device.RunShellCommand( |
1059 self.ShellError('/system/bin/sh: cat: /this/file/does.not.exist: ' | 1089 ['ls', '-l', '/this/file/does.not.exist'], |
1060 'No such file or directory')): | 1090 as_root=False, check_return=True), |
1061 with self.assertRaises(device_errors.AdbCommandFailedError): | 1091 self.CommandError('File does not exist')): |
| 1092 with self.assertRaises(device_errors.CommandFailedError): |
1062 self.device.ReadFile('/this/file/does.not.exist') | 1093 self.device.ReadFile('/this/file/does.not.exist') |
1063 | 1094 |
1064 def testReadFile_withSU(self): | 1095 def testReadFile_withSU(self): |
1065 with self.assertCalls( | 1096 with self.assertCalls( |
1066 (self.call.device.NeedsSU(), True), | 1097 (self.call.device.RunShellCommand( |
1067 (self.call.adb.Shell("su -c sh -c 'cat /this/file/can.be.read.with.su'"), | 1098 ['ls', '-l', '/this/file/can.be.read.with.su'], |
1068 'this is a test file\nread with su')): | 1099 as_root=True, check_return=True), |
| 1100 ['-rw------- root root 256 1970-01-01 00:00 can.be.read.with.su']), |
| 1101 (self.call.device.RunShellCommand( |
| 1102 ['cat', '/this/file/can.be.read.with.su'], |
| 1103 as_root=True, check_return=True), |
| 1104 ['this is a test file', 'read with su'])): |
1069 self.assertEqual( | 1105 self.assertEqual( |
1070 'this is a test file\nread with su\n', | 1106 'this is a test file\nread with su\n', |
1071 self.device.ReadFile('/this/file/can.be.read.with.su', | 1107 self.device.ReadFile('/this/file/can.be.read.with.su', |
1072 as_root=True)) | 1108 as_root=True)) |
1073 | 1109 |
| 1110 def testReadFile_withPull(self): |
| 1111 contents = 'a' * 123456 |
| 1112 with self.assertCalls( |
| 1113 (self.call.device.RunShellCommand( |
| 1114 ['ls', '-l', '/read/this/big/test/file'], |
| 1115 as_root=False, check_return=True), |
| 1116 ['-rw-rw---- root foo 123456 1970-01-01 00:00 file']), |
| 1117 (self.call.device._ReadFileWithPull('/read/this/big/test/file'), |
| 1118 contents)): |
| 1119 self.assertEqual( |
| 1120 contents, self.device.ReadFile('/read/this/big/test/file')) |
| 1121 |
| 1122 def testReadFile_withPullAndSU(self): |
| 1123 contents = 'b' * 123456 |
| 1124 with self.assertCalls( |
| 1125 (self.call.device.RunShellCommand( |
| 1126 ['ls', '-l', '/this/big/file/can.be.read.with.su'], |
| 1127 as_root=True, check_return=True), |
| 1128 ['-rw------- root root 123456 1970-01-01 00:00 can.be.read.with.su']), |
| 1129 (self.call.device.NeedsSU(), True), |
| 1130 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), |
| 1131 MockTempFile('/sdcard/tmp/on.device')), |
| 1132 self.call.device.RunShellCommand( |
| 1133 ['cp', '/this/big/file/can.be.read.with.su', |
| 1134 '/sdcard/tmp/on.device'], |
| 1135 as_root=True, check_return=True), |
| 1136 (self.call.device._ReadFileWithPull('/sdcard/tmp/on.device'), |
| 1137 contents)): |
| 1138 self.assertEqual( |
| 1139 contents, |
| 1140 self.device.ReadFile('/this/big/file/can.be.read.with.su', |
| 1141 as_root=True)) |
| 1142 |
1074 | 1143 |
1075 class DeviceUtilsWriteFileTest(DeviceUtilsTest): | 1144 class DeviceUtilsWriteFileTest(DeviceUtilsTest): |
1076 | 1145 |
1077 def testWriteFileWithPush_success(self): | 1146 def testWriteFileWithPush_success(self): |
1078 tmp_host = MockTempFile('/tmp/file/on.host') | 1147 tmp_host = MockTempFile('/tmp/file/on.host') |
1079 contents = 'some interesting contents' | 1148 contents = 'some interesting contents' |
1080 with self.assertCalls( | 1149 with self.assertCalls( |
1081 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1150 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1082 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1151 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1083 self.device._WriteFileWithPush('/path/to/device/file', contents) | 1152 self.device._WriteFileWithPush('/path/to/device/file', contents) |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1495 with self.assertCall( | 1564 with self.assertCall( |
1496 mock.call.pylib.device.adb_wrapper.AdbWrapper.GetDevices(), []): | 1565 mock.call.pylib.device.adb_wrapper.AdbWrapper.GetDevices(), []): |
1497 with self.assertRaises(device_errors.NoDevicesError): | 1566 with self.assertRaises(device_errors.NoDevicesError): |
1498 device_utils.DeviceUtils.parallel() | 1567 device_utils.DeviceUtils.parallel() |
1499 | 1568 |
1500 | 1569 |
1501 if __name__ == '__main__': | 1570 if __name__ == '__main__': |
1502 logging.getLogger().setLevel(logging.DEBUG) | 1571 logging.getLogger().setLevel(logging.DEBUG) |
1503 unittest.main(verbosity=2) | 1572 unittest.main(verbosity=2) |
1504 | 1573 |
OLD | NEW |