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 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 self.device.RunShellCommand('ls', cwd='/some/test/path') | 617 self.device.RunShellCommand('ls', cwd='/some/test/path') |
605 | 618 |
606 def testNewRunShellImpl_withCwdQuoted(self): | 619 def testNewRunShellImpl_withCwdQuoted(self): |
607 with self.assertCall( | 620 with self.assertCall( |
608 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 621 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
609 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 622 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
610 | 623 |
611 def testRunShellCommand_withSu(self): | 624 def testRunShellCommand_withSu(self): |
612 with self.assertCalls( | 625 with self.assertCalls( |
613 (self.call.device.NeedsSU(), True), | 626 (self.call.device.NeedsSU(), True), |
614 (self.call.adb.Shell('su -c setprop service.adb.root 0'), '')): | 627 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): |
615 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 628 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
616 | 629 |
617 def testRunShellCommand_manyLines(self): | 630 def testRunShellCommand_manyLines(self): |
618 cmd = 'ls /some/path' | 631 cmd = 'ls /some/path' |
619 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 632 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
620 self.assertEquals(['file1', 'file2', 'file3'], | 633 self.assertEquals(['file1', 'file2', 'file3'], |
621 self.device.RunShellCommand(cmd)) | 634 self.device.RunShellCommand(cmd)) |
622 | 635 |
623 def testRunShellCommand_singleLine_success(self): | 636 def testRunShellCommand_singleLine_success(self): |
624 cmd = 'echo $VALUE' | 637 cmd = 'echo $VALUE' |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): | 717 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): |
705 self.assertEquals(1, | 718 self.assertEquals(1, |
706 self.device.KillAll('some.process', blocking=True)) | 719 self.device.KillAll('some.process', blocking=True)) |
707 | 720 |
708 def testKillAll_root(self): | 721 def testKillAll_root(self): |
709 with self.assertCalls( | 722 with self.assertCalls( |
710 (self.call.adb.Shell('ps'), | 723 (self.call.adb.Shell('ps'), |
711 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 724 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
712 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 725 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
713 (self.call.device.NeedsSU(), True), | 726 (self.call.device.NeedsSU(), True), |
714 (self.call.adb.Shell('su -c kill -9 1234'), '')): | 727 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
715 self.assertEquals(1, | 728 self.assertEquals(1, |
716 self.device.KillAll('some.process', as_root=True)) | 729 self.device.KillAll('some.process', as_root=True)) |
717 | 730 |
718 def testKillAll_sigterm(self): | 731 def testKillAll_sigterm(self): |
719 with self.assertCalls( | 732 with self.assertCalls( |
720 (self.call.adb.Shell('ps'), | 733 (self.call.adb.Shell('ps'), |
721 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 734 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
722 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 735 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
723 (self.call.adb.Shell('kill -15 1234'), '')): | 736 (self.call.adb.Shell('kill -15 1234'), '')): |
724 self.assertEquals(1, | 737 self.assertEquals(1, |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 as_root=True)) | 1193 as_root=True)) |
1181 | 1194 |
1182 def testReadFile_asRoot_rejected(self): | 1195 def testReadFile_asRoot_rejected(self): |
1183 self.device.old_interface._privileged_command_runner = None | 1196 self.device.old_interface._privileged_command_runner = None |
1184 self.device.old_interface._protected_file_access_method_initialized = True | 1197 self.device.old_interface._protected_file_access_method_initialized = True |
1185 with self.assertRaises(device_errors.CommandFailedError): | 1198 with self.assertRaises(device_errors.CommandFailedError): |
1186 self.device.ReadFile('/this/file/cannot.be.read.by.user', | 1199 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
1187 as_root=True) | 1200 as_root=True) |
1188 | 1201 |
1189 | 1202 |
1190 class DeviceUtilsWriteFileTest(DeviceUtilsOldImplTest): | 1203 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1191 | 1204 |
1192 def testWriteFile_basic(self): | 1205 def testWriteFile_withPush(self): |
1193 mock_file = mock.MagicMock(spec=file) | 1206 tmp_host = MockTempFile('/tmp/file/on.host') |
1194 mock_file.name = '/tmp/file/to.be.pushed' | 1207 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1195 mock_file.__enter__.return_value = mock_file | 1208 with self.assertCalls( |
1196 with mock.patch('tempfile.NamedTemporaryFile', | 1209 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1197 return_value=mock_file): | 1210 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1198 with self.assertCalls( | 1211 self.device.WriteFile('/path/to/device/file', contents) |
1199 'adb -s 0123456789abcdef push ' | 1212 tmp_host.file.write.assert_called_once_with(contents) |
1200 '/tmp/file/to.be.pushed /test/file/written.to.device', | |
1201 '100 B/s (100 bytes in 1.000s)\r\n'): | |
1202 self.device.WriteFile('/test/file/written.to.device', | |
1203 'new test file contents') | |
1204 mock_file.write.assert_called_once_with('new test file contents') | |
1205 | 1213 |
1206 def testWriteFile_asRoot_withRoot(self): | 1214 def testWriteFile_withPushForced(self): |
1207 self.device.old_interface._external_storage = '/fake/storage/path' | 1215 tmp_host = MockTempFile('/tmp/file/on.host') |
1208 self.device.old_interface._privileged_command_runner = ( | 1216 contents = 'tiny contents' |
1209 self.device.old_interface.RunShellCommand) | 1217 with self.assertCalls( |
1210 self.device.old_interface._protected_file_access_method_initialized = True | 1218 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1219 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1220 self.device.WriteFile('/path/to/device/file', contents, force_push=True) |
| 1221 tmp_host.file.write.assert_called_once_with(contents) |
1211 | 1222 |
1212 mock_file = mock.MagicMock(spec=file) | 1223 def testWriteFile_withPushAndSU(self): |
1213 mock_file.name = '/tmp/file/to.be.pushed' | 1224 tmp_host = MockTempFile('/tmp/file/on.host') |
1214 mock_file.__enter__.return_value = mock_file | 1225 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1215 with mock.patch('tempfile.NamedTemporaryFile', | 1226 with self.assertCalls( |
1216 return_value=mock_file): | 1227 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1217 with self.assertCallsSequence( | 1228 (self.call.device.NeedsSU(), True), |
1218 cmd_ret=[ | 1229 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), |
1219 # Create temporary contents file | 1230 MockTempFile('/external/path/tmp/on.device')), |
1220 (r"adb -s 0123456789abcdef shell " | 1231 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), |
1221 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | 1232 self.call.device.RunShellCommand( |
1222 r"echo \$\?'", | 1233 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], |
1223 '1\r\n'), | 1234 as_root=True, check_return=True)): |
1224 # Create temporary script file | 1235 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
1225 (r"adb -s 0123456789abcdef shell " | 1236 tmp_host.file.write.assert_called_once_with(contents) |
1226 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | |
1227 r"echo \$\?'", | |
1228 '1\r\n'), | |
1229 # Set contents file | |
1230 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1231 r'/fake/storage/path/temp_file-\d+\d+', | |
1232 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1233 # Set script file | |
1234 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1235 r'/fake/storage/path/temp_file-\d+\d+', | |
1236 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1237 # Call script | |
1238 (r"adb -s 0123456789abcdef shell " | |
1239 r"'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1240 # Remove device temporaries | |
1241 (r"adb -s 0123456789abcdef shell " | |
1242 r"'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1243 (r"adb -s 0123456789abcdef shell " | |
1244 r"'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | |
1245 comp=re.match): | |
1246 self.device.WriteFile('/test/file/written.to.device', | |
1247 'new test file contents', as_root=True) | |
1248 | 1237 |
1249 def testWriteFile_asRoot_withSu(self): | 1238 def testWriteFile_withPush_rejected(self): |
1250 self.device.old_interface._external_storage = '/fake/storage/path' | 1239 tmp_host = MockTempFile('/tmp/file/on.host') |
1251 self.device.old_interface._privileged_command_runner = ( | 1240 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1252 self.device.old_interface.RunShellCommandWithSU) | 1241 with self.assertCalls( |
1253 self.device.old_interface._protected_file_access_method_initialized = True | 1242 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1243 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
| 1244 self.CommandError())): |
| 1245 with self.assertRaises(device_errors.CommandFailedError): |
| 1246 self.device.WriteFile('/path/to/device/file', contents) |
1254 | 1247 |
1255 mock_file = mock.MagicMock(spec=file) | 1248 def testWriteFile_withEcho(self): |
1256 mock_file.name = '/tmp/file/to.be.pushed' | 1249 with self.assertCall(self.call.adb.Shell( |
1257 mock_file.__enter__.return_value = mock_file | 1250 "echo -n the.contents > /test/file/to.write"), ''): |
1258 with mock.patch('tempfile.NamedTemporaryFile', | 1251 self.device.WriteFile('/test/file/to.write', 'the.contents') |
1259 return_value=mock_file): | |
1260 with self.assertCallsSequence( | |
1261 cmd_ret=[ | |
1262 # Create temporary contents file | |
1263 (r"adb -s 0123456789abcdef shell " | |
1264 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | |
1265 r"echo \$\?'", | |
1266 '1\r\n'), | |
1267 # Create temporary script file | |
1268 (r"adb -s 0123456789abcdef shell " | |
1269 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | |
1270 r"echo \$\?'", | |
1271 '1\r\n'), | |
1272 # Set contents file | |
1273 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1274 r'/fake/storage/path/temp_file-\d+\d+', | |
1275 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1276 # Set script file | |
1277 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1278 r'/fake/storage/path/temp_file-\d+\d+', | |
1279 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1280 # Call script | |
1281 (r"adb -s 0123456789abcdef shell " | |
1282 r"'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1283 # Remove device temporaries | |
1284 (r"adb -s 0123456789abcdef shell " | |
1285 r"'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1286 (r"adb -s 0123456789abcdef shell " | |
1287 r"'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | |
1288 comp=re.match): | |
1289 self.device.WriteFile('/test/file/written.to.device', | |
1290 'new test file contents', as_root=True) | |
1291 | 1252 |
1292 def testWriteFile_asRoot_rejected(self): | 1253 def testWriteFile_withEchoAndQuotes(self): |
1293 self.device.old_interface._privileged_command_runner = None | 1254 with self.assertCall(self.call.adb.Shell( |
1294 self.device.old_interface._protected_file_access_method_initialized = True | 1255 "echo -n 'the contents' > '/test/file/to write'"), ''): |
1295 with self.assertRaises(device_errors.CommandFailedError): | 1256 self.device.WriteFile('/test/file/to write', 'the contents') |
1296 self.device.WriteFile('/test/file/no.permissions.to.write', | |
1297 'new test file contents', as_root=True) | |
1298 | 1257 |
1299 | 1258 def testWriteFile_withEchoAndSU(self): |
1300 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): | |
1301 | |
1302 def testWriteTextFileTest_basic(self): | |
1303 with self.assertCall( | |
1304 self.call.adb.Shell('echo some.string > /test/file/to.write'), ''): | |
1305 self.device.WriteTextFile('/test/file/to.write', 'some.string') | |
1306 | |
1307 def testWriteTextFileTest_quoted(self): | |
1308 with self.assertCall( | |
1309 self.call.adb.Shell("echo 'some other string' > '/test/file/to write'"), | |
1310 ''): | |
1311 self.device.WriteTextFile('/test/file/to write', 'some other string') | |
1312 | |
1313 def testWriteTextFileTest_withSU(self): | |
1314 with self.assertCalls( | 1259 with self.assertCalls( |
1315 (self.call.device.NeedsSU(), True), | 1260 (self.call.device.NeedsSU(), True), |
1316 (self.call.adb.Shell('su -c echo string > /test/file'), '')): | 1261 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), |
1317 self.device.WriteTextFile('/test/file', 'string', as_root=True) | 1262 '')): |
| 1263 self.device.WriteFile('/test/file', 'contents', as_root=True) |
1318 | 1264 |
1319 | 1265 |
1320 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1266 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
1321 | 1267 |
1322 def testLs_nothing(self): | 1268 def testLs_nothing(self): |
1323 with self.assertCallsSequence([ | 1269 with self.assertCallsSequence([ |
1324 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1270 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
1325 '/this/file/does.not.exist: No such file or directory\r\n'), | 1271 '/this/file/does.not.exist: No such file or directory\r\n'), |
1326 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1272 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
1327 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 1273 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1621 self.device = device_utils.DeviceUtils(None) | 1567 self.device = device_utils.DeviceUtils(None) |
1622 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1568 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1623 self.assertRaises(device_errors.NoDevicesError)): | 1569 self.assertRaises(device_errors.NoDevicesError)): |
1624 str(self.device) | 1570 str(self.device) |
1625 | 1571 |
1626 | 1572 |
1627 if __name__ == '__main__': | 1573 if __name__ == '__main__': |
1628 logging.getLogger().setLevel(logging.DEBUG) | 1574 logging.getLogger().setLevel(logging.DEBUG) |
1629 unittest.main(verbosity=2) | 1575 unittest.main(verbosity=2) |
1630 | 1576 |
OLD | NEW |