Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(492)

Side by Side Diff: build/android/pylib/device/device_utils_test.py

Issue 725553002: Revert of Migrate device utils WriteFile to AdbWrapper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
83 class _PatchedFunction(object): 70 class _PatchedFunction(object):
84 def __init__(self, patched=None, mocked=None): 71 def __init__(self, patched=None, mocked=None):
85 self.patched = patched 72 self.patched = patched
86 self.mocked = mocked 73 self.mocked = mocked
87 74
88 75
89 class MockFileSystem(object): 76 class MockFileSystem(object):
90 77
91 @staticmethod 78 @staticmethod
92 def osStatResult( 79 def osStatResult(
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 self.device.RunShellCommand('ls', cwd='/some/test/path') 607 self.device.RunShellCommand('ls', cwd='/some/test/path')
621 608
622 def testNewRunShellImpl_withCwdQuoted(self): 609 def testNewRunShellImpl_withCwdQuoted(self):
623 with self.assertCall( 610 with self.assertCall(
624 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): 611 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''):
625 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') 612 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces')
626 613
627 def testRunShellCommand_withSu(self): 614 def testRunShellCommand_withSu(self):
628 with self.assertCalls( 615 with self.assertCalls(
629 (self.call.device.NeedsSU(), True), 616 (self.call.device.NeedsSU(), True),
630 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): 617 (self.call.adb.Shell('su -c setprop service.adb.root 0'), '')):
631 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) 618 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True)
632 619
633 def testRunShellCommand_manyLines(self): 620 def testRunShellCommand_manyLines(self):
634 cmd = 'ls /some/path' 621 cmd = 'ls /some/path'
635 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): 622 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'):
636 self.assertEquals(['file1', 'file2', 'file3'], 623 self.assertEquals(['file1', 'file2', 'file3'],
637 self.device.RunShellCommand(cmd)) 624 self.device.RunShellCommand(cmd))
638 625
639 def testRunShellCommand_singleLine_success(self): 626 def testRunShellCommand_singleLine_success(self):
640 cmd = 'echo $VALUE' 627 cmd = 'echo $VALUE'
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): 707 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')):
721 self.assertEquals(1, 708 self.assertEquals(1,
722 self.device.KillAll('some.process', blocking=True)) 709 self.device.KillAll('some.process', blocking=True))
723 710
724 def testKillAll_root(self): 711 def testKillAll_root(self):
725 with self.assertCalls( 712 with self.assertCalls(
726 (self.call.adb.Shell('ps'), 713 (self.call.adb.Shell('ps'),
727 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' 714 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
728 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), 715 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
729 (self.call.device.NeedsSU(), True), 716 (self.call.device.NeedsSU(), True),
730 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): 717 (self.call.adb.Shell('su -c kill -9 1234'), '')):
731 self.assertEquals(1, 718 self.assertEquals(1,
732 self.device.KillAll('some.process', as_root=True)) 719 self.device.KillAll('some.process', as_root=True))
733 720
734 def testKillAll_sigterm(self): 721 def testKillAll_sigterm(self):
735 with self.assertCalls( 722 with self.assertCalls(
736 (self.call.adb.Shell('ps'), 723 (self.call.adb.Shell('ps'),
737 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' 724 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'
738 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), 725 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'),
739 (self.call.adb.Shell('kill -15 1234'), '')): 726 (self.call.adb.Shell('kill -15 1234'), '')):
740 self.assertEquals(1, 727 self.assertEquals(1,
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 as_root=True)) 1141 as_root=True))
1155 1142
1156 def testReadFile_asRoot_rejected(self): 1143 def testReadFile_asRoot_rejected(self):
1157 self.device.old_interface._privileged_command_runner = None 1144 self.device.old_interface._privileged_command_runner = None
1158 self.device.old_interface._protected_file_access_method_initialized = True 1145 self.device.old_interface._protected_file_access_method_initialized = True
1159 with self.assertRaises(device_errors.CommandFailedError): 1146 with self.assertRaises(device_errors.CommandFailedError):
1160 self.device.ReadFile('/this/file/cannot.be.read.by.user', 1147 self.device.ReadFile('/this/file/cannot.be.read.by.user',
1161 as_root=True) 1148 as_root=True)
1162 1149
1163 1150
1164 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): 1151 class DeviceUtilsWriteFileTest(DeviceUtilsOldImplTest):
1165 1152
1166 def testWriteFile_withPush(self): 1153 def testWriteFile_basic(self):
1167 tmp_host = MockTempFile('/tmp/file/on.host') 1154 mock_file = mock.MagicMock(spec=file)
1168 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars 1155 mock_file.name = '/tmp/file/to.be.pushed'
1169 with self.assertCalls( 1156 mock_file.__enter__.return_value = mock_file
1170 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1157 with mock.patch('tempfile.NamedTemporaryFile',
1171 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): 1158 return_value=mock_file):
1172 self.device.WriteFile('/path/to/device/file', contents) 1159 with self.assertCalls(
1173 tmp_host.file.write.assert_called_once_with(contents) 1160 'adb -s 0123456789abcdef push '
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')
1174 1166
1175 def testWriteFile_withPushForced(self): 1167 def testWriteFile_asRoot_withRoot(self):
1176 tmp_host = MockTempFile('/tmp/file/on.host') 1168 self.device.old_interface._external_storage = '/fake/storage/path'
1177 contents = 'tiny contents' 1169 self.device.old_interface._privileged_command_runner = (
1178 with self.assertCalls( 1170 self.device.old_interface.RunShellCommand)
1179 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1171 self.device.old_interface._protected_file_access_method_initialized = True
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)
1183 1172
1184 def testWriteFile_withPushAndSU(self): 1173 mock_file = mock.MagicMock(spec=file)
1185 tmp_host = MockTempFile('/tmp/file/on.host') 1174 mock_file.name = '/tmp/file/to.be.pushed'
1186 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars 1175 mock_file.__enter__.return_value = mock_file
1187 with self.assertCalls( 1176 with mock.patch('tempfile.NamedTemporaryFile',
1188 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1177 return_value=mock_file):
1189 (self.call.device.NeedsSU(), True), 1178 with self.assertCallsSequence(
1190 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), 1179 cmd_ret=[
1191 MockTempFile('/external/path/tmp/on.device')), 1180 # Create temporary contents file
1192 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), 1181 (r"adb -s 0123456789abcdef shell "
1193 self.call.device.RunShellCommand( 1182 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; "
1194 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'], 1183 "echo \$\?'",
1195 as_root=True, check_return=True)): 1184 '1\r\n'),
1196 self.device.WriteFile('/path/to/device/file', contents, as_root=True) 1185 # Create temporary script file
1197 tmp_host.file.write.assert_called_once_with(contents) 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)
1198 1209
1199 def testWriteFile_withPush_rejected(self): 1210 def testWriteFile_asRoot_withSu(self):
1200 tmp_host = MockTempFile('/tmp/file/on.host') 1211 self.device.old_interface._external_storage = '/fake/storage/path'
1201 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars 1212 self.device.old_interface._privileged_command_runner = (
1202 with self.assertCalls( 1213 self.device.old_interface.RunShellCommandWithSU)
1203 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1214 self.device.old_interface._protected_file_access_method_initialized = True
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)
1208 1215
1209 def testWriteFile_withEcho(self): 1216 mock_file = mock.MagicMock(spec=file)
1210 with self.assertCall(self.call.adb.Shell( 1217 mock_file.name = '/tmp/file/to.be.pushed'
1211 "echo -n the.contents > /test/file/to.write"), ''): 1218 mock_file.__enter__.return_value = mock_file
1212 self.device.WriteFile('/test/file/to.write', 'the.contents') 1219 with mock.patch('tempfile.NamedTemporaryFile',
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)
1213 1252
1214 def testWriteFile_withEchoAndQuotes(self): 1253 def testWriteFile_asRoot_rejected(self):
1215 with self.assertCall(self.call.adb.Shell( 1254 self.device.old_interface._privileged_command_runner = None
1216 "echo -n 'the contents' > '/test/file/to write'"), ''): 1255 self.device.old_interface._protected_file_access_method_initialized = True
1217 self.device.WriteFile('/test/file/to write', 'the contents') 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)
1218 1259
1219 def testWriteFile_withEchoAndSU(self): 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):
1220 with self.assertCalls( 1275 with self.assertCalls(
1221 (self.call.device.NeedsSU(), True), 1276 (self.call.device.NeedsSU(), True),
1222 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), 1277 (self.call.adb.Shell('su -c echo string > /test/file'), '')):
1223 '')): 1278 self.device.WriteTextFile('/test/file', 'string', as_root=True)
1224 self.device.WriteFile('/test/file', 'contents', as_root=True)
1225 1279
1226 1280
1227 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): 1281 class DeviceUtilsLsTest(DeviceUtilsOldImplTest):
1228 1282
1229 def testLs_nothing(self): 1283 def testLs_nothing(self):
1230 with self.assertCallsSequence([ 1284 with self.assertCallsSequence([
1231 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", 1285 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'",
1232 '/this/file/does.not.exist: No such file or directory\r\n'), 1286 '/this/file/does.not.exist: No such file or directory\r\n'),
1233 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): 1287 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]):
1234 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) 1288 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist'))
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1528 self.device = device_utils.DeviceUtils(None) 1582 self.device = device_utils.DeviceUtils(None)
1529 with self.assertCalls('adb get-serialno', 'unknown'), ( 1583 with self.assertCalls('adb get-serialno', 'unknown'), (
1530 self.assertRaises(device_errors.NoDevicesError)): 1584 self.assertRaises(device_errors.NoDevicesError)):
1531 str(self.device) 1585 str(self.device)
1532 1586
1533 1587
1534 if __name__ == '__main__': 1588 if __name__ == '__main__':
1535 logging.getLogger().setLevel(logging.DEBUG) 1589 logging.getLogger().setLevel(logging.DEBUG)
1536 unittest.main(verbosity=2) 1590 unittest.main(verbosity=2)
1537 1591
OLDNEW
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | tools/telemetry/telemetry/core/backends/chrome/android_browser_backend.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698