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...) 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 128 matching lines...) Loading... |
208 def assertNoAdbCalls(self): | 221 def assertNoAdbCalls(self): |
209 return type(self).AndroidCommandsCalls(self, [], str.__eq__) | 222 return type(self).AndroidCommandsCalls(self, [], str.__eq__) |
210 | 223 |
211 def assertCalls(self, cmd, ret, comp=str.__eq__): | 224 def assertCalls(self, cmd, ret, comp=str.__eq__): |
212 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) | 225 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) |
213 | 226 |
214 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): | 227 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): |
215 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) | 228 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) |
216 | 229 |
217 def setUp(self): | 230 def setUp(self): |
| 231 self._get_adb_path_patch = mock.patch('pylib.constants.GetAdbPath', |
| 232 mock.Mock(return_value='adb')) |
| 233 self._get_adb_path_patch.start() |
218 self.device = device_utils.DeviceUtils( | 234 self.device = device_utils.DeviceUtils( |
219 '0123456789abcdef', default_timeout=1, default_retries=0) | 235 '0123456789abcdef', default_timeout=1, default_retries=0) |
220 | 236 |
| 237 def tearDown(self): |
| 238 self._get_adb_path_patch.stop() |
221 | 239 |
222 class DeviceUtilsNewImplTest(mock_calls.TestCase): | 240 class DeviceUtilsNewImplTest(mock_calls.TestCase): |
223 | 241 |
224 def setUp(self): | 242 def setUp(self): |
225 test_serial = '0123456789abcdef' | 243 test_serial = '0123456789abcdef' |
226 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 244 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
227 self.adb.__str__ = mock.Mock(return_value=test_serial) | 245 self.adb.__str__ = mock.Mock(return_value=test_serial) |
228 self.adb.GetDeviceSerial.return_value = test_serial | 246 self.adb.GetDeviceSerial.return_value = test_serial |
229 self.device = device_utils.DeviceUtils( | 247 self.device = device_utils.DeviceUtils( |
230 self.adb, default_timeout=10, default_retries=0) | 248 self.adb, default_timeout=10, default_retries=0) |
(...skipping 259 matching lines...) Loading... |
490 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 508 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
491 ''), | 509 ''), |
492 ("adb -s 0123456789abcdef install /fake/test/app.apk", | 510 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
493 'Success\r\n')]): | 511 'Success\r\n')]): |
494 self.device.Install('/fake/test/app.apk', retries=0) | 512 self.device.Install('/fake/test/app.apk', retries=0) |
495 | 513 |
496 def testInstall_differentPriorInstall(self): | 514 def testInstall_differentPriorInstall(self): |
497 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | 515 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
498 return [(host_path, device_path)] | 516 return [(host_path, device_path)] |
499 | 517 |
500 # Pylint raises a false positive "operator not preceded by a space" | |
501 # warning below. | |
502 # pylint: disable=C0322 | |
503 with mock.patch('os.path.isfile', return_value=True), ( | 518 with mock.patch('os.path.isfile', return_value=True), ( |
504 mock.patch('os.path.exists', return_value=True)), ( | 519 mock.patch('os.path.exists', return_value=True)), ( |
505 mock.patch('pylib.utils.apk_helper.GetPackageName', | 520 mock.patch('pylib.utils.apk_helper.GetPackageName', |
506 return_value='this.is.a.test.package')), ( | 521 return_value='this.is.a.test.package')), ( |
507 mock.patch('pylib.constants.GetOutDirectory', | 522 mock.patch('pylib.constants.GetOutDirectory', |
508 return_value='/fake/test/out')), ( | 523 return_value='/fake/test/out')), ( |
509 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 524 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
510 side_effect=mockGetFilesChanged)): | 525 side_effect=mockGetFilesChanged)): |
511 # pylint: enable=C0322 | |
512 with self.assertCallsSequence([ | 526 with self.assertCallsSequence([ |
513 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 527 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
514 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | 528 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), |
515 # GetFilesChanged is mocked, so its adb calls are omitted. | 529 # GetFilesChanged is mocked, so its adb calls are omitted. |
516 ('adb -s 0123456789abcdef uninstall this.is.a.test.package', | 530 ('adb -s 0123456789abcdef uninstall this.is.a.test.package', |
517 'Success\r\n'), | 531 'Success\r\n'), |
518 ('adb -s 0123456789abcdef install /fake/test/app.apk', | 532 ('adb -s 0123456789abcdef install /fake/test/app.apk', |
519 'Success\r\n')]): | 533 'Success\r\n')]): |
520 self.device.Install('/fake/test/app.apk', retries=0) | 534 self.device.Install('/fake/test/app.apk', retries=0) |
521 | 535 |
522 def testInstall_differentPriorInstall_reinstall(self): | 536 def testInstall_differentPriorInstall_reinstall(self): |
523 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | 537 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
524 return [(host_path, device_path)] | 538 return [(host_path, device_path)] |
525 | 539 |
526 # Pylint raises a false positive "operator not preceded by a space" | |
527 # warning below. | |
528 # pylint: disable=C0322 | |
529 with mock.patch('os.path.isfile', return_value=True), ( | 540 with mock.patch('os.path.isfile', return_value=True), ( |
530 mock.patch('pylib.utils.apk_helper.GetPackageName', | 541 mock.patch('pylib.utils.apk_helper.GetPackageName', |
531 return_value='this.is.a.test.package')), ( | 542 return_value='this.is.a.test.package')), ( |
532 mock.patch('pylib.constants.GetOutDirectory', | 543 mock.patch('pylib.constants.GetOutDirectory', |
533 return_value='/fake/test/out')), ( | 544 return_value='/fake/test/out')), ( |
534 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 545 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
535 side_effect=mockGetFilesChanged)): | 546 side_effect=mockGetFilesChanged)): |
536 # pylint: enable=C0322 | |
537 with self.assertCallsSequence([ | 547 with self.assertCallsSequence([ |
538 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 548 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
539 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | 549 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), |
540 # GetFilesChanged is mocked, so its adb calls are omitted. | 550 # GetFilesChanged is mocked, so its adb calls are omitted. |
541 ('adb -s 0123456789abcdef install -r /fake/test/app.apk', | 551 ('adb -s 0123456789abcdef install -r /fake/test/app.apk', |
542 'Success\r\n')]): | 552 'Success\r\n')]): |
543 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) | 553 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) |
544 | 554 |
545 def testInstall_identicalPriorInstall(self): | 555 def testInstall_identicalPriorInstall(self): |
546 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | 556 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
(...skipping 60 matching lines...) Loading... |
607 self.device.RunShellCommand('ls', cwd='/some/test/path') | 617 self.device.RunShellCommand('ls', cwd='/some/test/path') |
608 | 618 |
609 def testNewRunShellImpl_withCwdQuoted(self): | 619 def testNewRunShellImpl_withCwdQuoted(self): |
610 with self.assertCall( | 620 with self.assertCall( |
611 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): | 621 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): |
612 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | 622 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') |
613 | 623 |
614 def testRunShellCommand_withSu(self): | 624 def testRunShellCommand_withSu(self): |
615 with self.assertCalls( | 625 with self.assertCalls( |
616 (self.call.device.NeedsSU(), True), | 626 (self.call.device.NeedsSU(), True), |
617 (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'"), '')): |
618 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 628 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
619 | 629 |
620 def testRunShellCommand_manyLines(self): | 630 def testRunShellCommand_manyLines(self): |
621 cmd = 'ls /some/path' | 631 cmd = 'ls /some/path' |
622 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'): |
623 self.assertEquals(['file1', 'file2', 'file3'], | 633 self.assertEquals(['file1', 'file2', 'file3'], |
624 self.device.RunShellCommand(cmd)) | 634 self.device.RunShellCommand(cmd)) |
625 | 635 |
626 def testRunShellCommand_singleLine_success(self): | 636 def testRunShellCommand_singleLine_success(self): |
627 cmd = 'echo $VALUE' | 637 cmd = 'echo $VALUE' |
(...skipping 79 matching lines...) Loading... |
707 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): | 717 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): |
708 self.assertEquals(1, | 718 self.assertEquals(1, |
709 self.device.KillAll('some.process', blocking=True)) | 719 self.device.KillAll('some.process', blocking=True)) |
710 | 720 |
711 def testKillAll_root(self): | 721 def testKillAll_root(self): |
712 with self.assertCalls( | 722 with self.assertCalls( |
713 (self.call.adb.Shell('ps'), | 723 (self.call.adb.Shell('ps'), |
714 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 724 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
715 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 725 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
716 (self.call.device.NeedsSU(), True), | 726 (self.call.device.NeedsSU(), True), |
717 (self.call.adb.Shell('su -c kill -9 1234'), '')): | 727 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
718 self.assertEquals(1, | 728 self.assertEquals(1, |
719 self.device.KillAll('some.process', as_root=True)) | 729 self.device.KillAll('some.process', as_root=True)) |
720 | 730 |
721 def testKillAll_sigterm(self): | 731 def testKillAll_sigterm(self): |
722 with self.assertCalls( | 732 with self.assertCalls( |
723 (self.call.adb.Shell('ps'), | 733 (self.call.adb.Shell('ps'), |
724 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 734 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' |
725 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 735 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), |
726 (self.call.adb.Shell('kill -15 1234'), '')): | 736 (self.call.adb.Shell('kill -15 1234'), '')): |
727 self.assertEquals(1, | 737 self.assertEquals(1, |
(...skipping 455 matching lines...) Loading... |
1183 as_root=True)) | 1193 as_root=True)) |
1184 | 1194 |
1185 def testReadFile_asRoot_rejected(self): | 1195 def testReadFile_asRoot_rejected(self): |
1186 self.device.old_interface._privileged_command_runner = None | 1196 self.device.old_interface._privileged_command_runner = None |
1187 self.device.old_interface._protected_file_access_method_initialized = True | 1197 self.device.old_interface._protected_file_access_method_initialized = True |
1188 with self.assertRaises(device_errors.CommandFailedError): | 1198 with self.assertRaises(device_errors.CommandFailedError): |
1189 self.device.ReadFile('/this/file/cannot.be.read.by.user', | 1199 self.device.ReadFile('/this/file/cannot.be.read.by.user', |
1190 as_root=True) | 1200 as_root=True) |
1191 | 1201 |
1192 | 1202 |
1193 class DeviceUtilsWriteFileTest(DeviceUtilsOldImplTest): | 1203 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
1194 | 1204 |
1195 def testWriteFile_basic(self): | 1205 def testWriteFile_withPush(self): |
1196 mock_file = mock.MagicMock(spec=file) | 1206 tmp_host = MockTempFile('/tmp/file/on.host') |
1197 mock_file.name = '/tmp/file/to.be.pushed' | 1207 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1198 mock_file.__enter__.return_value = mock_file | 1208 with self.assertCalls( |
1199 with mock.patch('tempfile.NamedTemporaryFile', | 1209 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1200 return_value=mock_file): | 1210 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
1201 with self.assertCalls( | 1211 self.device.WriteFile('/path/to/device/file', contents) |
1202 'adb -s 0123456789abcdef push ' | 1212 tmp_host.file.write.assert_called_once_with(contents) |
1203 '/tmp/file/to.be.pushed /test/file/written.to.device', | |
1204 '100 B/s (100 bytes in 1.000s)\r\n'): | |
1205 self.device.WriteFile('/test/file/written.to.device', | |
1206 'new test file contents') | |
1207 mock_file.write.assert_called_once_with('new test file contents') | |
1208 | 1213 |
1209 def testWriteFile_asRoot_withRoot(self): | 1214 def testWriteFile_withPushForced(self): |
1210 self.device.old_interface._external_storage = '/fake/storage/path' | 1215 tmp_host = MockTempFile('/tmp/file/on.host') |
1211 self.device.old_interface._privileged_command_runner = ( | 1216 contents = 'tiny contents' |
1212 self.device.old_interface.RunShellCommand) | 1217 with self.assertCalls( |
1213 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) |
1214 | 1222 |
1215 mock_file = mock.MagicMock(spec=file) | 1223 def testWriteFile_withPushAndSU(self): |
1216 mock_file.name = '/tmp/file/to.be.pushed' | 1224 tmp_host = MockTempFile('/tmp/file/on.host') |
1217 mock_file.__enter__.return_value = mock_file | 1225 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1218 with mock.patch('tempfile.NamedTemporaryFile', | 1226 with self.assertCalls( |
1219 return_value=mock_file): | 1227 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
1220 with self.assertCallsSequence( | 1228 (self.call.device.NeedsSU(), True), |
1221 cmd_ret=[ | 1229 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), |
1222 # Create temporary contents file | 1230 MockTempFile('/external/path/tmp/on.device')), |
1223 (r"adb -s 0123456789abcdef shell " | 1231 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), |
1224 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | 1232 self.call.device.RunShellCommand( |
1225 "echo \$\?'", | 1233 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], |
1226 '1\r\n'), | 1234 as_root=True, check_return=True)): |
1227 # Create temporary script file | 1235 self.device.WriteFile('/path/to/device/file', contents, as_root=True) |
1228 (r"adb -s 0123456789abcdef shell " | 1236 tmp_host.file.write.assert_called_once_with(contents) |
1229 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | |
1230 "echo \$\?'", | |
1231 '1\r\n'), | |
1232 # Set contents file | |
1233 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1234 '/fake/storage/path/temp_file-\d+\d+', | |
1235 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1236 # Set script file | |
1237 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1238 '/fake/storage/path/temp_file-\d+\d+', | |
1239 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1240 # Call script | |
1241 (r"adb -s 0123456789abcdef shell " | |
1242 "'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1243 # Remove device temporaries | |
1244 (r"adb -s 0123456789abcdef shell " | |
1245 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1246 (r"adb -s 0123456789abcdef shell " | |
1247 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | |
1248 comp=re.match): | |
1249 self.device.WriteFile('/test/file/written.to.device', | |
1250 'new test file contents', as_root=True) | |
1251 | 1237 |
1252 def testWriteFile_asRoot_withSu(self): | 1238 def testWriteFile_withPush_rejected(self): |
1253 self.device.old_interface._external_storage = '/fake/storage/path' | 1239 tmp_host = MockTempFile('/tmp/file/on.host') |
1254 self.device.old_interface._privileged_command_runner = ( | 1240 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars |
1255 self.device.old_interface.RunShellCommandWithSU) | 1241 with self.assertCalls( |
1256 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) |
1257 | 1247 |
1258 mock_file = mock.MagicMock(spec=file) | 1248 def testWriteFile_withEcho(self): |
1259 mock_file.name = '/tmp/file/to.be.pushed' | 1249 with self.assertCall(self.call.adb.Shell( |
1260 mock_file.__enter__.return_value = mock_file | 1250 "echo -n the.contents > /test/file/to.write"), ''): |
1261 with mock.patch('tempfile.NamedTemporaryFile', | 1251 self.device.WriteFile('/test/file/to.write', 'the.contents') |
1262 return_value=mock_file): | |
1263 with self.assertCallsSequence( | |
1264 cmd_ret=[ | |
1265 # Create temporary contents file | |
1266 (r"adb -s 0123456789abcdef shell " | |
1267 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | |
1268 "echo \$\?'", | |
1269 '1\r\n'), | |
1270 # Create temporary script file | |
1271 (r"adb -s 0123456789abcdef shell " | |
1272 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | |
1273 "echo \$\?'", | |
1274 '1\r\n'), | |
1275 # Set contents file | |
1276 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1277 '/fake/storage/path/temp_file-\d+\d+', | |
1278 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1279 # Set script file | |
1280 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | |
1281 '/fake/storage/path/temp_file-\d+\d+', | |
1282 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1283 # Call script | |
1284 (r"adb -s 0123456789abcdef shell " | |
1285 "'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1286 # Remove device temporaries | |
1287 (r"adb -s 0123456789abcdef shell " | |
1288 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | |
1289 (r"adb -s 0123456789abcdef shell " | |
1290 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | |
1291 comp=re.match): | |
1292 self.device.WriteFile('/test/file/written.to.device', | |
1293 'new test file contents', as_root=True) | |
1294 | 1252 |
1295 def testWriteFile_asRoot_rejected(self): | 1253 def testWriteFile_withEchoAndQuotes(self): |
1296 self.device.old_interface._privileged_command_runner = None | 1254 with self.assertCall(self.call.adb.Shell( |
1297 self.device.old_interface._protected_file_access_method_initialized = True | 1255 "echo -n 'the contents' > '/test/file/to write'"), ''): |
1298 with self.assertRaises(device_errors.CommandFailedError): | 1256 self.device.WriteFile('/test/file/to write', 'the contents') |
1299 self.device.WriteFile('/test/file/no.permissions.to.write', | |
1300 'new test file contents', as_root=True) | |
1301 | 1257 |
1302 | 1258 def testWriteFile_withEchoAndSU(self): |
1303 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): | |
1304 | |
1305 def testWriteTextFileTest_basic(self): | |
1306 with self.assertCall( | |
1307 self.call.adb.Shell('echo some.string > /test/file/to.write'), ''): | |
1308 self.device.WriteTextFile('/test/file/to.write', 'some.string') | |
1309 | |
1310 def testWriteTextFileTest_quoted(self): | |
1311 with self.assertCall( | |
1312 self.call.adb.Shell("echo 'some other string' > '/test/file/to write'"), | |
1313 ''): | |
1314 self.device.WriteTextFile('/test/file/to write', 'some other string') | |
1315 | |
1316 def testWriteTextFileTest_withSU(self): | |
1317 with self.assertCalls( | 1259 with self.assertCalls( |
1318 (self.call.device.NeedsSU(), True), | 1260 (self.call.device.NeedsSU(), True), |
1319 (self.call.adb.Shell('su -c echo string > /test/file'), '')): | 1261 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), |
1320 self.device.WriteTextFile('/test/file', 'string', as_root=True) | 1262 '')): |
| 1263 self.device.WriteFile('/test/file', 'contents', as_root=True) |
1321 | 1264 |
1322 | 1265 |
1323 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1266 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
1324 | 1267 |
1325 def testLs_nothing(self): | 1268 def testLs_nothing(self): |
1326 with self.assertCallsSequence([ | 1269 with self.assertCallsSequence([ |
1327 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1270 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
1328 '/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'), |
1329 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1272 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
1330 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...) Loading... |
1624 self.device = device_utils.DeviceUtils(None) | 1567 self.device = device_utils.DeviceUtils(None) |
1625 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1568 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1626 self.assertRaises(device_errors.NoDevicesError)): | 1569 self.assertRaises(device_errors.NoDevicesError)): |
1627 str(self.device) | 1570 str(self.device) |
1628 | 1571 |
1629 | 1572 |
1630 if __name__ == '__main__': | 1573 if __name__ == '__main__': |
1631 logging.getLogger().setLevel(logging.DEBUG) | 1574 logging.getLogger().setLevel(logging.DEBUG) |
1632 unittest.main(verbosity=2) | 1575 unittest.main(verbosity=2) |
1633 | 1576 |
OLD | NEW |