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

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

Issue 751063002: Allow RunShellCommand to work even with very large commands (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes w.r.t. latest comments Created 6 years 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
11 # pylint: disable=W0212 11 # pylint: disable=W0212
12 # pylint: disable=W0613 12 # pylint: disable=W0613
13 13
14 import collections 14 import collections
15 import datetime 15 import datetime
16 import logging 16 import logging
17 import os 17 import os
18 import re 18 import re
19 import signal 19 import signal
20 import sys 20 import sys
21 import unittest 21 import unittest
22 22
23 from pylib import android_commands 23 from pylib import android_commands
24 from pylib import cmd_helper
24 from pylib import constants 25 from pylib import constants
25 from pylib.device import adb_wrapper 26 from pylib.device import adb_wrapper
26 from pylib.device import device_errors 27 from pylib.device import device_errors
27 from pylib.device import device_utils 28 from pylib.device import device_utils
28 from pylib.device import intent 29 from pylib.device import intent
29 from pylib.utils import mock_calls 30 from pylib.utils import mock_calls
30 31
31 # RunCommand from third_party/android_testrunner/run_command.py is mocked 32 # RunCommand from third_party/android_testrunner/run_command.py is mocked
32 # below, so its path needs to be in sys.path. 33 # below, so its path needs to be in sys.path.
33 sys.path.append(os.path.join( 34 sys.path.append(os.path.join(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 def testInitWithNone(self): 66 def testInitWithNone(self):
66 d = device_utils.DeviceUtils(None) 67 d = device_utils.DeviceUtils(None)
67 self.assertIsNone(d.old_interface.GetDevice()) 68 self.assertIsNone(d.old_interface.GetDevice())
68 69
69 70
70 class MockTempFile(object): 71 class MockTempFile(object):
71 72
72 def __init__(self, name='/tmp/some/file'): 73 def __init__(self, name='/tmp/some/file'):
73 self.file = mock.MagicMock(spec=file) 74 self.file = mock.MagicMock(spec=file)
74 self.file.name = name 75 self.file.name = name
76 self.file.name_quoted = cmd_helper.SingleQuote(name)
75 77
76 def __enter__(self): 78 def __enter__(self):
77 return self.file 79 return self.file
78 80
79 def __exit__(self, exc_type, exc_val, exc_tb): 81 def __exit__(self, exc_type, exc_val, exc_tb):
80 pass 82 pass
81 83
82 84
83 class _PatchedFunction(object): 85 class _PatchedFunction(object):
84 def __init__(self, patched=None, mocked=None): 86 def __init__(self, patched=None, mocked=None):
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 616
615 def testNewRunShellImpl_withCwd(self): 617 def testNewRunShellImpl_withCwd(self):
616 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''): 618 with self.assertCall(self.call.adb.Shell('cd /some/test/path && ls'), ''):
617 self.device.RunShellCommand('ls', cwd='/some/test/path') 619 self.device.RunShellCommand('ls', cwd='/some/test/path')
618 620
619 def testNewRunShellImpl_withCwdQuoted(self): 621 def testNewRunShellImpl_withCwdQuoted(self):
620 with self.assertCall( 622 with self.assertCall(
621 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''): 623 self.call.adb.Shell("cd '/some test/path with/spaces' && ls"), ''):
622 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') 624 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces')
623 625
626 def testRunShellCommand_withHugeCmd(self):
627 payload = 'hi! ' * 1024
628 expected_cmd = "echo '%s'" % payload
629 with self.assertCalls(
630 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(
631 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')),
632 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd),
633 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')):
634 self.assertEquals([payload],
635 self.device.RunShellCommand(['echo', payload]))
636
637 def testRunShellCommand_withHugeCmdAmdSU(self):
638 payload = 'hi! ' * 1024
639 expected_cmd = """su -c sh -c 'echo '"'"'%s'"'"''""" % payload
640 with self.assertCalls(
641 (self.call.device.NeedsSU(), True),
642 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(
643 self.adb, suffix='.sh'), MockTempFile('/sdcard/temp-123.sh')),
644 self.call.device._WriteFileWithPush('/sdcard/temp-123.sh', expected_cmd),
645 (self.call.adb.Shell('sh /sdcard/temp-123.sh'), payload + '\n')):
646 self.assertEquals(
647 [payload],
648 self.device.RunShellCommand(['echo', payload], as_root=True))
649
624 def testRunShellCommand_withSu(self): 650 def testRunShellCommand_withSu(self):
625 with self.assertCalls( 651 with self.assertCalls(
626 (self.call.device.NeedsSU(), True), 652 (self.call.device.NeedsSU(), True),
627 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')): 653 (self.call.adb.Shell("su -c sh -c 'setprop service.adb.root 0'"), '')):
628 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) 654 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True)
629 655
630 def testRunShellCommand_manyLines(self): 656 def testRunShellCommand_manyLines(self):
631 cmd = 'ls /some/path' 657 cmd = 'ls /some/path'
632 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'): 658 with self.assertCall(self.call.adb.Shell(cmd), 'file1\nfile2\nfile3\n'):
633 self.assertEquals(['file1', 'file2', 'file3'], 659 self.assertEquals(['file1', 'file2', 'file3'],
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 def testReadFile_asRoot_rejected(self): 1197 def testReadFile_asRoot_rejected(self):
1172 self.device.old_interface._privileged_command_runner = None 1198 self.device.old_interface._privileged_command_runner = None
1173 self.device.old_interface._protected_file_access_method_initialized = True 1199 self.device.old_interface._protected_file_access_method_initialized = True
1174 with self.assertRaises(device_errors.CommandFailedError): 1200 with self.assertRaises(device_errors.CommandFailedError):
1175 self.device.ReadFile('/this/file/cannot.be.read.by.user', 1201 self.device.ReadFile('/this/file/cannot.be.read.by.user',
1176 as_root=True) 1202 as_root=True)
1177 1203
1178 1204
1179 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): 1205 class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest):
1180 1206
1181 def testWriteFile_withPush(self): 1207 def testWriteFileWithPush_success(self):
1182 tmp_host = MockTempFile('/tmp/file/on.host') 1208 tmp_host = MockTempFile('/tmp/file/on.host')
1183 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars 1209 contents = 'some interesting contents'
1184 with self.assertCalls( 1210 with self.assertCalls(
1185 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1211 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1186 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): 1212 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')):
1187 self.device.WriteFile('/path/to/device/file', contents) 1213 self.device._WriteFileWithPush('/path/to/device/file', contents)
1188 tmp_host.file.write.assert_called_once_with(contents) 1214 tmp_host.file.write.assert_called_once_with(contents)
1189 1215
1190 def testWriteFile_withPushForced(self): 1216 def testWriteFileWithPush_rejected(self):
1191 tmp_host = MockTempFile('/tmp/file/on.host') 1217 tmp_host = MockTempFile('/tmp/file/on.host')
1192 contents = 'tiny contents' 1218 contents = 'some interesting contents'
1193 with self.assertCalls(
1194 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1195 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')):
1196 self.device.WriteFile('/path/to/device/file', contents, force_push=True)
1197 tmp_host.file.write.assert_called_once_with(contents)
1198
1199 def testWriteFile_withPushAndSU(self):
1200 tmp_host = MockTempFile('/tmp/file/on.host')
1201 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1202 with self.assertCalls(
1203 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1204 (self.call.device.NeedsSU(), True),
1205 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device),
1206 MockTempFile('/external/path/tmp/on.device')),
1207 self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'),
1208 self.call.device.RunShellCommand(
1209 ['cp', '/external/path/tmp/on.device', '/path/to/device/file'],
1210 as_root=True, check_return=True)):
1211 self.device.WriteFile('/path/to/device/file', contents, as_root=True)
1212 tmp_host.file.write.assert_called_once_with(contents)
1213
1214 def testWriteFile_withPush_rejected(self):
1215 tmp_host = MockTempFile('/tmp/file/on.host')
1216 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1217 with self.assertCalls( 1219 with self.assertCalls(
1218 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), 1220 (mock.call.tempfile.NamedTemporaryFile(), tmp_host),
1219 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), 1221 (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'),
1220 self.CommandError())): 1222 self.CommandError())):
1221 with self.assertRaises(device_errors.CommandFailedError): 1223 with self.assertRaises(device_errors.CommandFailedError):
1222 self.device.WriteFile('/path/to/device/file', contents) 1224 self.device._WriteFileWithPush('/path/to/device/file', contents)
1225
1226 def testWriteFile_withPush(self):
1227 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1228 with self.assertCalls(
1229 self.call.device._WriteFileWithPush('/path/to/device/file', contents)):
1230 self.device.WriteFile('/path/to/device/file', contents)
1231
1232 def testWriteFile_withPushForced(self):
1233 contents = 'tiny contents'
1234 with self.assertCalls(
1235 self.call.device._WriteFileWithPush('/path/to/device/file', contents)):
1236 self.device.WriteFile('/path/to/device/file', contents, force_push=True)
1237
1238 def testWriteFile_withPushAndSU(self):
1239 contents = 'some large contents ' * 26 # 20 * 26 = 520 chars
1240 with self.assertCalls(
1241 (self.call.device.NeedsSU(), True),
1242 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb),
1243 MockTempFile('/sdcard/tmp/on.device')),
1244 self.call.device._WriteFileWithPush('/sdcard/tmp/on.device', contents),
1245 self.call.device.RunShellCommand(
1246 ['cp', '/sdcard/tmp/on.device', '/path/to/device/file'],
1247 as_root=True, check_return=True)):
1248 self.device.WriteFile('/path/to/device/file', contents, as_root=True)
1223 1249
1224 def testWriteFile_withEcho(self): 1250 def testWriteFile_withEcho(self):
1225 with self.assertCall(self.call.adb.Shell( 1251 with self.assertCall(self.call.adb.Shell(
1226 "echo -n the.contents > /test/file/to.write"), ''): 1252 "echo -n the.contents > /test/file/to.write"), ''):
1227 self.device.WriteFile('/test/file/to.write', 'the.contents') 1253 self.device.WriteFile('/test/file/to.write', 'the.contents')
1228 1254
1229 def testWriteFile_withEchoAndQuotes(self): 1255 def testWriteFile_withEchoAndQuotes(self):
1230 with self.assertCall(self.call.adb.Shell( 1256 with self.assertCall(self.call.adb.Shell(
1231 "echo -n 'the contents' > '/test/file/to write'"), ''): 1257 "echo -n 'the contents' > '/test/file/to write'"), ''):
1232 self.device.WriteFile('/test/file/to write', 'the contents') 1258 self.device.WriteFile('/test/file/to write', 'the contents')
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1543 self.device = device_utils.DeviceUtils(None) 1569 self.device = device_utils.DeviceUtils(None)
1544 with self.assertCalls('adb get-serialno', 'unknown'), ( 1570 with self.assertCalls('adb get-serialno', 'unknown'), (
1545 self.assertRaises(device_errors.NoDevicesError)): 1571 self.assertRaises(device_errors.NoDevicesError)):
1546 str(self.device) 1572 str(self.device)
1547 1573
1548 1574
1549 if __name__ == '__main__': 1575 if __name__ == '__main__':
1550 logging.getLogger().setLevel(logging.DEBUG) 1576 logging.getLogger().setLevel(logging.DEBUG)
1551 unittest.main(verbosity=2) 1577 unittest.main(verbosity=2)
1552 1578
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698