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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 serial = '0fedcba987654321' | 60 serial = '0fedcba987654321' |
61 a = android_commands.AndroidCommands(device=serial) | 61 a = android_commands.AndroidCommands(device=serial) |
62 d = device_utils.DeviceUtils(a) | 62 d = device_utils.DeviceUtils(a) |
63 self.assertEqual(serial, d.old_interface.GetDevice()) | 63 self.assertEqual(serial, d.old_interface.GetDevice()) |
64 | 64 |
65 def testInitWithNone(self): | 65 def testInitWithNone(self): |
66 d = device_utils.DeviceUtils(None) | 66 d = device_utils.DeviceUtils(None) |
67 self.assertIsNone(d.old_interface.GetDevice()) | 67 self.assertIsNone(d.old_interface.GetDevice()) |
68 | 68 |
69 | 69 |
70 class MockTempFile(object): | |
71 | |
72 def __init__(self, name='/tmp/some/file'): | |
73 self.file = mock.MagicMock(spec=file) | |
74 self.file.name = name | |
75 | |
76 def __enter__(self): | |
77 return self.file | |
78 | |
79 def __exit__(self, exc_type, exc_val, exc_tb): | |
80 pass | |
81 | |
82 | |
70 class _PatchedFunction(object): | 83 class _PatchedFunction(object): |
71 def __init__(self, patched=None, mocked=None): | 84 def __init__(self, patched=None, mocked=None): |
72 self.patched = patched | 85 self.patched = patched |
73 self.mocked = mocked | 86 self.mocked = mocked |
74 | 87 |
75 | 88 |
76 class MockFileSystem(object): | 89 class MockFileSystem(object): |
77 | 90 |
78 @staticmethod | 91 @staticmethod |
79 def osStatResult( | 92 def osStatResult( |
(...skipping 1061 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1141 as_root=True)) | 1154 as_root=True)) |
1142 | 1155 |
1143 def testReadFile_asRoot_rejected(self): | 1156 def testReadFile_asRoot_rejected(self): |
1144 self.device.old_interface._privileged_command_runner = None | 1157 self.device.old_interface._privileged_command_runner = None |
1145 self.device.old_interface._protected_file_access_method_initialized = True | 1158 self.device.old_interface._protected_file_access_method_initialized = True |
1146 with self.assertRaises(device_errors.CommandFailedError): | 1159 with self.assertRaises(device_errors.CommandFailedError): |
1147 self.device.ReadFile('/this/file/cannot.be.read.by.user', | 1160 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
1148 as_root=True) | 1161 as_root=True) |
1149 | 1162 |
1150 | 1163 |
1151 class DeviceUtilsWriteFileTest(DeviceUtilsOldImplTest): | 1164 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1152 | 1165 |
1153 def testWriteFile_basic(self): | 1166 def testWriteFile_withPush(self): |
1154 mock_file = mock.MagicMock(spec=file) | 1167 tmp_host = MockTempFile('/tmp/file/on.host') |
1155 mock_file.name = '/tmp/file/to.be.pushed' | 1168 with self.assertCalls( |
1156 mock_file.__enter__.return_value = mock_file | 1169 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1157 with mock.patch('tempfile.NamedTemporaryFile', | 1170 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1158 return_value=mock_file): | 1171 self.device.WriteFile('/path/to/device/file', 'new file contents', |
1159 with self.assertCalls( | 1172 force_push=True) |
1160 'adb -s 0123456789abcdef push ' | 1173 tmp_host.file.write.assert_called_once_with('new file contents') |
1161 '/tmp/file/to.be.pushed /test/file/written.to.device', | |
1162 '100 B/s (100 bytes in 1.000s)\r\n'): | |
1163 self.device.WriteFile('/test/file/written.to.device', | |
1164 'new test file contents') | |
1165 mock_file.write.assert_called_once_with('new test file contents') | |
1166 | 1174 |
1167 def testWriteFile_asRoot_withRoot(self): | 1175 def testWriteFile_withPushAndSU(self): |
1168 self.device.old_interface._external_storage = '/fake/storage/path' | 1176 tmp_host = MockTempFile('/tmp/file/on.host') |
1169 self.device.old_interface._privileged_command_runner = ( | 1177 with self.assertCalls( |
1170 self.device.old_interface.RunShellCommand) | 1178 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1171 self.device.old_interface._protected_file_access_method_initialized = True | 1179 (self.call.device.NeedsSU(), True), |
1180 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), | |
1181 MockTempFile('/external/path/tmp/on.device')), | |
1182 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), | |
1183 self.call.device.RunShellCommand( | |
1184 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], | |
1185 as_root=True, check_return=True)): | |
1186 self.device.WriteFile('/path/to/device/file', 'new file contents', | |
1187 as_root=True, force_push=True) | |
1188 tmp_host.file.write.assert_called_once_with('new file contents') | |
1172 | 1189 |
1173 mock_file = mock.MagicMock(spec=file) | 1190 def testWriteFile_withPush_rejected(self): |
1174 mock_file.name = '/tmp/file/to.be.pushed' | 1191 tmp_host = MockTempFile('/tmp/file/on.host') |
1175 mock_file.__enter__.return_value = mock_file | 1192 with self.assertCalls( |
1176 with mock.patch('tempfile.NamedTemporaryFile', | 1193 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1177 return_value=mock_file): | 1194 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
1178 with self.assertCallsSequence( | 1195 self.CommandError())): |
1179 cmd_ret=[ | 1196 with self.assertRaises(device_errors.CommandFailedError): |
1180 # Create temporary contents file | 1197 self.device.WriteFile('/path/to/device/file', 'new file contents', |
1181 (r"adb -s 0123456789abcdef shell " | 1198 force_push=True) |
1182 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | 1199 tmp_host.file.write.assert_called_once_with('new file contents') |
1183 "echo \$\?'", | |
1184 '1\r\n'), | |
1185 # Create temporary script file | |
1186 (r"adb -s 0123456789abcdef shell " | |
1187 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | |
1188 "echo \$\?'", | |
1189 '1\r\n'), | |
1190 # Set contents file | |
1191 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1192 '/fake/storage/path/temp_file-\d+\d+', | |
1193 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1194 # Set script file | |
1195 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1196 '/fake/storage/path/temp_file-\d+\d+', | |
1197 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1198 # Call script | |
1199 (r"adb -s 0123456789abcdef shell " | |
1200 "'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1201 # Remove device temporaries | |
1202 (r"adb -s 0123456789abcdef shell " | |
1203 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1204 (r"adb -s 0123456789abcdef shell " | |
1205 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | |
1206 comp=re.match): | |
1207 self.device.WriteFile('/test/file/written.to.device', | |
1208 'new test file contents', as_root=True) | |
1209 | 1200 |
1210 def testWriteFile_asRoot_withSu(self): | 1201 def testWriteFile_withEcho(self): |
1211 self.device.old_interface._external_storage = '/fake/storage/path' | 1202 with self.assertCall(self.call.adb.Shell( |
1212 self.device.old_interface._privileged_command_runner = ( | 1203 "sh -c 'echo -n the.contents' > /test/file/to.write"), ''): |
1213 self.device.old_interface.RunShellCommandWithSU) | 1204 self.device.WriteFile('/test/file/to.write', 'the.contents') |
1214 self.device.old_interface._protected_file_access_method_initialized = True | |
1215 | 1205 |
1216 mock_file = mock.MagicMock(spec=file) | 1206 def testWriteFile_withEchoAndQuotes(self): |
1217 mock_file.name = '/tmp/file/to.be.pushed' | 1207 with self.assertCall(self.call.adb.Shell( |
1218 mock_file.__enter__.return_value = mock_file | 1208 """sh -c 'echo -n '"'"'the contents'"'"'' > '/test/file/to write'"""), |
perezju
2014/11/12 01:31:24
And imagine trying to get those quotes right witho
jbudorick
2014/11/12 01:42:38
Ha, no kidding.
Sami
2014/11/12 14:14:14
Should we add a couple of cases where the contents
perezju
2014/11/12 17:37:03
Yeah, these are now being tested in pylib.cmd_help
| |
1219 with mock.patch('tempfile.NamedTemporaryFile', | 1209 ''): |
1220 return_value=mock_file): | 1210 self.device.WriteFile('/test/file/to write', 'the contents') |
1221 with self.assertCallsSequence( | |
1222 cmd_ret=[ | |
1223 # Create temporary contents file | |
1224 (r"adb -s 0123456789abcdef shell " | |
1225 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | |
1226 "echo \$\?'", | |
1227 '1\r\n'), | |
1228 # Create temporary script file | |
1229 (r"adb -s 0123456789abcdef shell " | |
1230 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | |
1231 "echo \$\?'", | |
1232 '1\r\n'), | |
1233 # Set contents file | |
1234 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1235 '/fake/storage/path/temp_file-\d+\d+', | |
1236 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1237 # Set script file | |
1238 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1239 '/fake/storage/path/temp_file-\d+\d+', | |
1240 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1241 # Call script | |
1242 (r"adb -s 0123456789abcdef shell " | |
1243 "'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1244 # Remove device temporaries | |
1245 (r"adb -s 0123456789abcdef shell " | |
1246 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1247 (r"adb -s 0123456789abcdef shell " | |
1248 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | |
1249 comp=re.match): | |
1250 self.device.WriteFile('/test/file/written.to.device', | |
1251 'new test file contents', as_root=True) | |
1252 | 1211 |
1253 def testWriteFile_asRoot_rejected(self): | 1212 def testWriteFile_withEchoAndSU(self): |
1254 self.device.old_interface._privileged_command_runner = None | |
1255 self.device.old_interface._protected_file_access_method_initialized = True | |
1256 with self.assertRaises(device_errors.CommandFailedError): | |
1257 self.device.WriteFile('/test/file/no.permissions.to.write', | |
1258 'new test file contents', as_root=True) | |
1259 | |
1260 | |
1261 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): | |
1262 | |
1263 def testWriteTextFileTest_basic(self): | |
1264 with self.assertCall( | |
1265 self.call.adb.Shell('echo some.string > /test/file/to.write'), ''): | |
1266 self.device.WriteTextFile('/test/file/to.write', 'some.string') | |
1267 | |
1268 def testWriteTextFileTest_quoted(self): | |
1269 with self.assertCall( | |
1270 self.call.adb.Shell("echo 'some other string' > '/test/file/to write'"), | |
1271 ''): | |
1272 self.device.WriteTextFile('/test/file/to write', 'some other string') | |
1273 | |
1274 def testWriteTextFileTest_withSU(self): | |
1275 with self.assertCalls( | 1213 with self.assertCalls( |
1276 (self.call.device.NeedsSU(), True), | 1214 (self.call.device.NeedsSU(), True), |
1277 (self.call.adb.Shell('su -c echo string > /test/file'), '')): | 1215 (self.call.adb.Shell("su -c sh -c 'echo -n contents' > /test/file"), |
1278 self.device.WriteTextFile('/test/file', 'string', as_root=True) | 1216 '')): |
1217 self.device.WriteFile('/test/file', 'contents', as_root=True) | |
1279 | 1218 |
1280 | 1219 |
1281 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1220 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
1282 | 1221 |
1283 def testLs_nothing(self): | 1222 def testLs_nothing(self): |
1284 with self.assertCallsSequence([ | 1223 with self.assertCallsSequence([ |
1285 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1224 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
1286 '/this/file/does.not.exist: No such file or directory\r\n'), | 1225 '/this/file/does.not.exist: No such file or directory\r\n'), |
1287 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1226 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
1288 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 1227 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1582 self.device = device_utils.DeviceUtils(None) | 1521 self.device = device_utils.DeviceUtils(None) |
1583 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1522 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1584 self.assertRaises(device_errors.NoDevicesError)): | 1523 self.assertRaises(device_errors.NoDevicesError)): |
1585 str(self.device) | 1524 str(self.device) |
1586 | 1525 |
1587 | 1526 |
1588 if __name__ == '__main__': | 1527 if __name__ == '__main__': |
1589 logging.getLogger().setLevel(logging.DEBUG) | 1528 logging.getLogger().setLevel(logging.DEBUG) |
1590 unittest.main(verbosity=2) | 1529 unittest.main(verbosity=2) |
1591 | 1530 |
OLD | NEW |