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

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

Issue 715853002: Migrate device utils WriteFile to AdbWrapper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed another echo bug 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
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 1061 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 "sh -c '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 """sh -c 'echo -n '"'"'the contents'"'"' >"""
1256 with self.assertRaises(device_errors.CommandFailedError): 1217 """ '"'"'/test/file/to write'"'"''"""), ''):
1257 self.device.WriteFile('/test/file/no.permissions.to.write', 1218 self.device.WriteFile('/test/file/to write', 'the contents')
1258 'new test file contents', as_root=True)
1259 1219
1260 1220 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( 1221 with self.assertCalls(
1276 (self.call.device.NeedsSU(), True), 1222 (self.call.device.NeedsSU(), True),
1277 (self.call.adb.Shell('su -c echo string > /test/file'), '')): 1223 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"),
1278 self.device.WriteTextFile('/test/file', 'string', as_root=True) 1224 '')):
1225 self.device.WriteFile('/test/file', 'contents', as_root=True)
1279 1226
1280 1227
1281 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): 1228 class DeviceUtilsLsTest(DeviceUtilsOldImplTest):
1282 1229
1283 def testLs_nothing(self): 1230 def testLs_nothing(self):
1284 with self.assertCallsSequence([ 1231 with self.assertCallsSequence([
1285 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", 1232 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'",
1286 '/this/file/does.not.exist: No such file or directory\r\n'), 1233 '/this/file/does.not.exist: No such file or directory\r\n'),
1287 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): 1234 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]):
1288 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) 1235 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist'))
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1582 self.device = device_utils.DeviceUtils(None) 1529 self.device = device_utils.DeviceUtils(None)
1583 with self.assertCalls('adb get-serialno', 'unknown'), ( 1530 with self.assertCalls('adb get-serialno', 'unknown'), (
1584 self.assertRaises(device_errors.NoDevicesError)): 1531 self.assertRaises(device_errors.NoDevicesError)):
1585 str(self.device) 1532 str(self.device)
1586 1533
1587 1534
1588 if __name__ == '__main__': 1535 if __name__ == '__main__':
1589 logging.getLogger().setLevel(logging.DEBUG) 1536 logging.getLogger().setLevel(logging.DEBUG)
1590 unittest.main(verbosity=2) 1537 unittest.main(verbosity=2)
1591 1538
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698