| 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 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 self.device.RunShellCommand('ls', cwd='/some/test/path') | 620 self.device.RunShellCommand('ls', cwd='/some/test/path') |
| 608 | 621 |
| 609 def testNewRunShellImpl_withCwdQuoted(self): | 622 def testNewRunShellImpl_withCwdQuoted(self): |
| 610 with self.assertCall( | 623 with self.assertCall( |
| 611 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 624 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
| 612 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 625 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
| 613 | 626 |
| 614 def testRunShellCommand_withSu(self): | 627 def testRunShellCommand_withSu(self): |
| 615 with self.assertCalls( | 628 with self.assertCalls( |
| 616 (self.call.device.NeedsSU(), True), | 629 (self.call.device.NeedsSU(), True), |
| 617 (self.call.adb.Shell('su -c setprop service.adb.root 0'), '')): | 630 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): |
| 618 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 631 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
| 619 | 632 |
| 620 def testRunShellCommand_manyLines(self): | 633 def testRunShellCommand_manyLines(self): |
| 621 cmd = 'ls /some/path' | 634 cmd = 'ls /some/path' |
| 622 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): | 635 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): |
| 623 self.assertEquals(['file1', 'file2', 'file3'], | 636 self.assertEquals(['file1', 'file2', 'file3'], |
| 624 self.device.RunShellCommand(cmd)) | 637 self.device.RunShellCommand(cmd)) |
| 625 | 638 |
| 626 def testRunShellCommand_singleLine_success(self): | 639 def testRunShellCommand_singleLine_success(self): |
| 627 cmd = 'echo $VALUE' | 640 cmd = 'echo $VALUE' |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): | 720 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): |
| 708 self.assertEquals(1, | 721 self.assertEquals(1, |
| 709 self.device.KillAll('some.process', blocking=True)) | 722 self.device.KillAll('some.process', blocking=True)) |
| 710 | 723 |
| 711 def testKillAll_root(self): | 724 def testKillAll_root(self): |
| 712 with self.assertCalls( | 725 with self.assertCalls( |
| 713 (self.call.adb.Shell('ps'), | 726 (self.call.adb.Shell('ps'), |
| 714 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 727 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
| 715 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 728 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
| 716 (self.call.device.NeedsSU(), True), | 729 (self.call.device.NeedsSU(), True), |
| 717 (self.call.adb.Shell('su -c kill -9 1234'), '')): | 730 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
| 718 self.assertEquals(1, | 731 self.assertEquals(1, |
| 719 self.device.KillAll('some.process', as_root=True)) | 732 self.device.KillAll('some.process', as_root=True)) |
| 720 | 733 |
| 721 def testKillAll_sigterm(self): | 734 def testKillAll_sigterm(self): |
| 722 with self.assertCalls( | 735 with self.assertCalls( |
| 723 (self.call.adb.Shell('ps'), | 736 (self.call.adb.Shell('ps'), |
| 724 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 737 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
| 725 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 738 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
| 726 (self.call.adb.Shell('kill -15 1234'), '')): | 739 (self.call.adb.Shell('kill -15 1234'), '')): |
| 727 self.assertEquals(1, | 740 self.assertEquals(1, |
| (...skipping 413 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 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1156 mock_file.__enter__.return_value = mock_file | 1169 with self.assertCalls( |
| 1157 with mock.patch('tempfile.NamedTemporaryFile', | 1170 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1158 return_value=mock_file): | 1171 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1159 with self.assertCalls( | 1172 self.device.WriteFile('/path/to/device/file', contents) |
| 1160 'adb -s 0123456789abcdef push ' | 1173 tmp_host.file.write.assert_called_once_with(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_withPushForced(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 contents = 'tiny contents' |
| 1170 self.device.old_interface.RunShellCommand) | 1178 with self.assertCalls( |
| 1171 self.device.old_interface._protected_file_access_method_initialized = True | 1179 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1180 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1181 self.device.WriteFile('/path/to/device/file', contents, force_push=True) |
| 1182 tmp_host.file.write.assert_called_once_with(contents) |
| 1172 | 1183 |
| 1173 mock_file = mock.MagicMock(spec=file) | 1184 def testWriteFile_withPushAndSU(self): |
| 1174 mock_file.name = '/tmp/file/to.be.pushed' | 1185 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1175 mock_file.__enter__.return_value = mock_file | 1186 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1176 with mock.patch('tempfile.NamedTemporaryFile', | 1187 with self.assertCalls( |
| 1177 return_value=mock_file): | 1188 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1178 with self.assertCallsSequence( | 1189 (self.call.device.NeedsSU(), True), |
| 1179 cmd_ret=[ | 1190 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), |
| 1180 # Create temporary contents file | 1191 MockTempFile('/external/path/tmp/on.device')), |
| 1181 (r"adb -s 0123456789abcdef shell " | 1192 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), |
| 1182 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | 1193 self.call.device.RunShellCommand( |
| 1183 "echo \$\?'", | 1194 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], |
| 1184 '1\r\n'), | 1195 as_root=True, check_return=True)): |
| 1185 # Create temporary script file | 1196 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
| 1186 (r"adb -s 0123456789abcdef shell " | 1197 tmp_host.file.write.assert_called_once_with(contents) |
| 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 | 1198 |
| 1210 def testWriteFile_asRoot_withSu(self): | 1199 def testWriteFile_withPush_rejected(self): |
| 1211 self.device.old_interface._external_storage = '/fake/storage/path' | 1200 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1212 self.device.old_interface._privileged_command_runner = ( | 1201 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
| 1213 self.device.old_interface.RunShellCommandWithSU) | 1202 with self.assertCalls( |
| 1214 self.device.old_interface._protected_file_access_method_initialized = True | 1203 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1204 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
| 1205 self.CommandError())): |
| 1206 with self.assertRaises(device_errors.CommandFailedError): |
| 1207 self.device.WriteFile('/path/to/device/file', contents) |
| 1215 | 1208 |
| 1216 mock_file = mock.MagicMock(spec=file) | 1209 def testWriteFile_withEcho(self): |
| 1217 mock_file.name = '/tmp/file/to.be.pushed' | 1210 with self.assertCall(self.call.adb.Shell( |
| 1218 mock_file.__enter__.return_value = mock_file | 1211 "echo -n the.contents > /test/file/to.write"), ''): |
| 1219 with mock.patch('tempfile.NamedTemporaryFile', | 1212 self.device.WriteFile('/test/file/to.write', 'the.contents') |
| 1220 return_value=mock_file): | |
| 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 | 1213 |
| 1253 def testWriteFile_asRoot_rejected(self): | 1214 def testWriteFile_withEchoAndQuotes(self): |
| 1254 self.device.old_interface._privileged_command_runner = None | 1215 with self.assertCall(self.call.adb.Shell( |
| 1255 self.device.old_interface._protected_file_access_method_initialized = True | 1216 "echo -n 'the contents' > '/test/file/to write'"), ''): |
| 1256 with self.assertRaises(device_errors.CommandFailedError): | 1217 self.device.WriteFile('/test/file/to write', 'the contents') |
| 1257 self.device.WriteFile('/test/file/no.permissions.to.write', | |
| 1258 'new test file contents', as_root=True) | |
| 1259 | 1218 |
| 1260 | 1219 def testWriteFile_withEchoAndSU(self): |
| 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( | 1220 with self.assertCalls( |
| 1276 (self.call.device.NeedsSU(), True), | 1221 (self.call.device.NeedsSU(), True), |
| 1277 (self.call.adb.Shell('su -c echo string > /test/file'), '')): | 1222 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), |
| 1278 self.device.WriteTextFile('/test/file', 'string', as_root=True) | 1223 '')): |
| 1224 self.device.WriteFile('/test/file', 'contents', as_root=True) |
| 1279 | 1225 |
| 1280 | 1226 |
| 1281 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1227 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
| 1282 | 1228 |
| 1283 def testLs_nothing(self): | 1229 def testLs_nothing(self): |
| 1284 with self.assertCallsSequence([ | 1230 with self.assertCallsSequence([ |
| 1285 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1231 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
| 1286 '/this/file/does.not.exist: No such file or directory\r\n'), | 1232 '/this/file/does.not.exist: No such file or directory\r\n'), |
| 1287 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1233 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
| 1288 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 1234 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) | 1528 self.device = device_utils.DeviceUtils(None) |
| 1583 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1529 with self.assertCalls('adb get-serialno', 'unknown'), ( |
| 1584 self.assertRaises(device_errors.NoDevicesError)): | 1530 self.assertRaises(device_errors.NoDevicesError)): |
| 1585 str(self.device) | 1531 str(self.device) |
| 1586 | 1532 |
| 1587 | 1533 |
| 1588 if __name__ == '__main__': | 1534 if __name__ == '__main__': |
| 1589 logging.getLogger().setLevel(logging.DEBUG) | 1535 logging.getLogger().setLevel(logging.DEBUG) |
| 1590 unittest.main(verbosity=2) | 1536 unittest.main(verbosity=2) |
| 1591 | 1537 |
| OLD | NEW |