blob: 4452eed386a1f7461331cddf6e2349cfd1cfec6e [file] [log] [blame]
Dan Albert8e1fdd72015-07-24 17:08:33 -07001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2015 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18from __future__ import print_function
19
20import hashlib
21import os
22import posixpath
23import random
24import shlex
25import shutil
David Pursell544e7952015-09-14 15:36:26 -070026import signal
Dan Albert8e1fdd72015-07-24 17:08:33 -070027import subprocess
28import tempfile
29import unittest
30
31import mock
32
33import adb
34
35
Dan Alberte2b4a5f2015-07-28 14:53:03 -070036def requires_root(func):
37 def wrapper(self, *args):
38 if self.device.get_prop('ro.debuggable') != '1':
39 raise unittest.SkipTest('requires rootable build')
40
David Pursell606835a2015-09-08 17:17:02 -070041 was_root = self.device.shell(['id', '-un'])[0].strip() == 'root'
Dan Alberte2b4a5f2015-07-28 14:53:03 -070042 if not was_root:
43 self.device.root()
44 self.device.wait()
45
46 try:
47 func(self, *args)
48 finally:
49 if not was_root:
50 self.device.unroot()
51 self.device.wait()
52
53 return wrapper
54
55
Dan Albert8e1fdd72015-07-24 17:08:33 -070056class GetDeviceTest(unittest.TestCase):
57 def setUp(self):
58 self.android_serial = os.getenv('ANDROID_SERIAL')
Spencer Low3e7feda2015-07-30 01:19:52 -070059 if 'ANDROID_SERIAL' in os.environ:
60 del os.environ['ANDROID_SERIAL']
Dan Albert8e1fdd72015-07-24 17:08:33 -070061
62 def tearDown(self):
Spencer Low3e7feda2015-07-30 01:19:52 -070063 if self.android_serial is not None:
64 os.environ['ANDROID_SERIAL'] = self.android_serial
65 else:
66 if 'ANDROID_SERIAL' in os.environ:
67 del os.environ['ANDROID_SERIAL']
Dan Albert8e1fdd72015-07-24 17:08:33 -070068
69 @mock.patch('adb.device.get_devices')
70 def test_explicit(self, mock_get_devices):
71 mock_get_devices.return_value = ['foo', 'bar']
72 device = adb.get_device('foo')
73 self.assertEqual(device.serial, 'foo')
74
75 @mock.patch('adb.device.get_devices')
76 def test_from_env(self, mock_get_devices):
77 mock_get_devices.return_value = ['foo', 'bar']
78 os.environ['ANDROID_SERIAL'] = 'foo'
79 device = adb.get_device()
80 self.assertEqual(device.serial, 'foo')
81
82 @mock.patch('adb.device.get_devices')
83 def test_arg_beats_env(self, mock_get_devices):
84 mock_get_devices.return_value = ['foo', 'bar']
85 os.environ['ANDROID_SERIAL'] = 'bar'
86 device = adb.get_device('foo')
87 self.assertEqual(device.serial, 'foo')
88
89 @mock.patch('adb.device.get_devices')
90 def test_no_such_device(self, mock_get_devices):
91 mock_get_devices.return_value = ['foo', 'bar']
92 self.assertRaises(adb.DeviceNotFoundError, adb.get_device, ['baz'])
93
94 os.environ['ANDROID_SERIAL'] = 'baz'
95 self.assertRaises(adb.DeviceNotFoundError, adb.get_device)
96
97 @mock.patch('adb.device.get_devices')
98 def test_unique_device(self, mock_get_devices):
99 mock_get_devices.return_value = ['foo']
100 device = adb.get_device()
101 self.assertEqual(device.serial, 'foo')
102
103 @mock.patch('adb.device.get_devices')
104 def test_no_unique_device(self, mock_get_devices):
105 mock_get_devices.return_value = ['foo', 'bar']
106 self.assertRaises(adb.NoUniqueDeviceError, adb.get_device)
107
108
109class DeviceTest(unittest.TestCase):
110 def setUp(self):
111 self.device = adb.get_device()
112
113
114class ShellTest(DeviceTest):
115 def test_cat(self):
116 """Check that we can at least cat a file."""
David Pursell606835a2015-09-08 17:17:02 -0700117 out = self.device.shell(['cat', '/proc/uptime'])[0].strip()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700118 elements = out.split()
119 self.assertEqual(len(elements), 2)
120
121 uptime, idle = elements
122 self.assertGreater(float(uptime), 0.0)
123 self.assertGreater(float(idle), 0.0)
124
125 def test_throws_on_failure(self):
David Pursell606835a2015-09-08 17:17:02 -0700126 self.assertRaises(adb.ShellError, self.device.shell, ['false'])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700127
128 def test_output_not_stripped(self):
David Pursell606835a2015-09-08 17:17:02 -0700129 out = self.device.shell(['echo', 'foo'])[0]
Dan Albert8e1fdd72015-07-24 17:08:33 -0700130 self.assertEqual(out, 'foo' + self.device.linesep)
131
132 def test_shell_nocheck_failure(self):
David Pursell606835a2015-09-08 17:17:02 -0700133 rc, out, _ = self.device.shell_nocheck(['false'])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700134 self.assertNotEqual(rc, 0)
135 self.assertEqual(out, '')
136
137 def test_shell_nocheck_output_not_stripped(self):
David Pursell606835a2015-09-08 17:17:02 -0700138 rc, out, _ = self.device.shell_nocheck(['echo', 'foo'])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700139 self.assertEqual(rc, 0)
140 self.assertEqual(out, 'foo' + self.device.linesep)
141
142 def test_can_distinguish_tricky_results(self):
143 # If result checking on ADB shell is naively implemented as
144 # `adb shell <cmd>; echo $?`, we would be unable to distinguish the
145 # output from the result for a cmd of `echo -n 1`.
David Pursell606835a2015-09-08 17:17:02 -0700146 rc, out, _ = self.device.shell_nocheck(['echo', '-n', '1'])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700147 self.assertEqual(rc, 0)
148 self.assertEqual(out, '1')
149
150 def test_line_endings(self):
151 """Ensure that line ending translation is not happening in the pty.
152
153 Bug: http://b/19735063
154 """
David Pursell606835a2015-09-08 17:17:02 -0700155 output = self.device.shell(['uname'])[0]
Dan Albert8e1fdd72015-07-24 17:08:33 -0700156 self.assertEqual(output, 'Linux' + self.device.linesep)
157
David Purselld4093f12015-08-10 12:52:16 -0700158 def test_pty_logic(self):
159 """Verify PTY logic for shells.
160
161 Interactive shells should use a PTY, non-interactive should not.
162
163 Bug: http://b/21215503
164 """
165 proc = subprocess.Popen(
166 self.device.adb_cmd + ['shell'], stdin=subprocess.PIPE,
167 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
168 # [ -t 0 ] is used (rather than `tty`) to provide portability. This
169 # gives an exit code of 0 iff stdin is connected to a terminal.
170 #
171 # Closing host-side stdin doesn't currently trigger the interactive
172 # shell to exit so we need to explicitly add an exit command to
173 # close the session from the device side, and append \n to complete
174 # the interactive command.
175 result = proc.communicate('[ -t 0 ]; echo x$?; exit 0\n')[0]
176 partition = result.rpartition('x')
177 self.assertEqual(partition[1], 'x')
178 self.assertEqual(int(partition[2]), 0)
179
180 exit_code = self.device.shell_nocheck(['[ -t 0 ]'])[0]
181 self.assertEqual(exit_code, 1)
182
David Pursell606835a2015-09-08 17:17:02 -0700183 def test_shell_protocol(self):
184 """Tests the shell protocol on the device.
185
186 If the device supports shell protocol, this gives us the ability
187 to separate stdout/stderr and return the exit code directly.
188
189 Bug: http://b/19734861
190 """
191 if self.device.SHELL_PROTOCOL_FEATURE not in self.device.features:
192 raise unittest.SkipTest('shell protocol unsupported on this device')
193 result = self.device.shell_nocheck(
194 shlex.split('echo foo; echo bar >&2; exit 17'))
195
196 self.assertEqual(17, result[0])
197 self.assertEqual('foo' + self.device.linesep, result[1])
198 self.assertEqual('bar' + self.device.linesep, result[2])
199
David Pursell544e7952015-09-14 15:36:26 -0700200 def test_non_interactive_sigint(self):
201 """Tests that SIGINT in a non-interactive shell kills the process.
202
203 This requires the shell protocol in order to detect the broken
204 pipe; raw data transfer mode will only see the break once the
205 subprocess tries to read or write.
206
207 Bug: http://b/23825725
208 """
209 if self.device.SHELL_PROTOCOL_FEATURE not in self.device.features:
210 raise unittest.SkipTest('shell protocol unsupported on this device')
211
212 # Start a long-running process.
213 sleep_proc = subprocess.Popen(
214 self.device.adb_cmd + shlex.split('shell echo $$; sleep 60'),
215 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
216 stderr=subprocess.STDOUT)
217 remote_pid = sleep_proc.stdout.readline().strip()
218 self.assertIsNone(sleep_proc.returncode, 'subprocess terminated early')
219 proc_query = shlex.split('ps {0} | grep {0}'.format(remote_pid))
220
221 # Verify that the process is running, send signal, verify it stopped.
222 self.device.shell(proc_query)
223 os.kill(sleep_proc.pid, signal.SIGINT)
224 sleep_proc.communicate()
225 self.assertEqual(1, self.device.shell_nocheck(proc_query)[0],
226 'subprocess failed to terminate')
227
Dan Albert8e1fdd72015-07-24 17:08:33 -0700228
229class ArgumentEscapingTest(DeviceTest):
230 def test_shell_escaping(self):
231 """Make sure that argument escaping is somewhat sane."""
232
233 # http://b/19734868
234 # Note that this actually matches ssh(1)'s behavior --- it's
235 # converted to `sh -c echo hello; echo world` which sh interprets
236 # as `sh -c echo` (with an argument to that shell of "hello"),
237 # and then `echo world` back in the first shell.
238 result = self.device.shell(
David Pursell606835a2015-09-08 17:17:02 -0700239 shlex.split("sh -c 'echo hello; echo world'"))[0]
Dan Albert8e1fdd72015-07-24 17:08:33 -0700240 result = result.splitlines()
241 self.assertEqual(['', 'world'], result)
242 # If you really wanted "hello" and "world", here's what you'd do:
243 result = self.device.shell(
David Pursell606835a2015-09-08 17:17:02 -0700244 shlex.split(r'echo hello\;echo world'))[0].splitlines()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700245 self.assertEqual(['hello', 'world'], result)
246
247 # http://b/15479704
David Pursell606835a2015-09-08 17:17:02 -0700248 result = self.device.shell(shlex.split("'true && echo t'"))[0].strip()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700249 self.assertEqual('t', result)
250 result = self.device.shell(
David Pursell606835a2015-09-08 17:17:02 -0700251 shlex.split("sh -c 'true && echo t'"))[0].strip()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700252 self.assertEqual('t', result)
253
254 # http://b/20564385
David Pursell606835a2015-09-08 17:17:02 -0700255 result = self.device.shell(shlex.split('FOO=a BAR=b echo t'))[0].strip()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700256 self.assertEqual('t', result)
David Pursell606835a2015-09-08 17:17:02 -0700257 result = self.device.shell(
258 shlex.split(r'echo -n 123\;uname'))[0].strip()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700259 self.assertEqual('123Linux', result)
260
261 def test_install_argument_escaping(self):
262 """Make sure that install argument escaping works."""
263 # http://b/20323053
Spencer Lowd8cce182015-08-28 01:07:30 -0700264 tf = tempfile.NamedTemporaryFile('wb', suffix='-text;ls;1.apk',
265 delete=False)
266 tf.close()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700267 self.assertIn("-text;ls;1.apk", self.device.install(tf.name))
Spencer Lowd8cce182015-08-28 01:07:30 -0700268 os.remove(tf.name)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700269
270 # http://b/3090932
Spencer Lowd8cce182015-08-28 01:07:30 -0700271 tf = tempfile.NamedTemporaryFile('wb', suffix="-Live Hold'em.apk",
272 delete=False)
273 tf.close()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700274 self.assertIn("-Live Hold'em.apk", self.device.install(tf.name))
Spencer Lowd8cce182015-08-28 01:07:30 -0700275 os.remove(tf.name)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700276
277
278class RootUnrootTest(DeviceTest):
279 def _test_root(self):
280 message = self.device.root()
281 if 'adbd cannot run as root in production builds' in message:
282 return
283 self.device.wait()
David Pursell606835a2015-09-08 17:17:02 -0700284 self.assertEqual('root', self.device.shell(['id', '-un'])[0].strip())
Dan Albert8e1fdd72015-07-24 17:08:33 -0700285
286 def _test_unroot(self):
287 self.device.unroot()
288 self.device.wait()
David Pursell606835a2015-09-08 17:17:02 -0700289 self.assertEqual('shell', self.device.shell(['id', '-un'])[0].strip())
Dan Albert8e1fdd72015-07-24 17:08:33 -0700290
291 def test_root_unroot(self):
292 """Make sure that adb root and adb unroot work, using id(1)."""
Dan Alberte2b4a5f2015-07-28 14:53:03 -0700293 if self.device.get_prop('ro.debuggable') != '1':
294 raise unittest.SkipTest('requires rootable build')
295
David Pursell606835a2015-09-08 17:17:02 -0700296 original_user = self.device.shell(['id', '-un'])[0].strip()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700297 try:
298 if original_user == 'root':
299 self._test_unroot()
300 self._test_root()
301 elif original_user == 'shell':
302 self._test_root()
303 self._test_unroot()
304 finally:
305 if original_user == 'root':
306 self.device.root()
307 else:
308 self.device.unroot()
309 self.device.wait()
310
311
312class TcpIpTest(DeviceTest):
313 def test_tcpip_failure_raises(self):
314 """adb tcpip requires a port.
315
316 Bug: http://b/22636927
317 """
318 self.assertRaises(
319 subprocess.CalledProcessError, self.device.tcpip, '')
320 self.assertRaises(
321 subprocess.CalledProcessError, self.device.tcpip, 'foo')
322
323
Dan Alberte2b4a5f2015-07-28 14:53:03 -0700324class SystemPropertiesTest(DeviceTest):
325 def test_get_prop(self):
326 self.assertEqual(self.device.get_prop('init.svc.adbd'), 'running')
327
328 @requires_root
329 def test_set_prop(self):
330 prop_name = 'foo.bar'
331 self.device.shell(['setprop', prop_name, '""'])
332
333 self.device.set_prop(prop_name, 'qux')
334 self.assertEqual(
David Pursell606835a2015-09-08 17:17:02 -0700335 self.device.shell(['getprop', prop_name])[0].strip(), 'qux')
Dan Alberte2b4a5f2015-07-28 14:53:03 -0700336
337
Dan Albert8e1fdd72015-07-24 17:08:33 -0700338def compute_md5(string):
339 hsh = hashlib.md5()
340 hsh.update(string)
341 return hsh.hexdigest()
342
343
344def get_md5_prog(device):
345 """Older platforms (pre-L) had the name md5 rather than md5sum."""
346 try:
347 device.shell(['md5sum', '/proc/uptime'])
348 return 'md5sum'
349 except subprocess.CalledProcessError:
350 return 'md5'
351
352
353class HostFile(object):
354 def __init__(self, handle, checksum):
355 self.handle = handle
356 self.checksum = checksum
357 self.full_path = handle.name
358 self.base_name = os.path.basename(self.full_path)
359
360
361class DeviceFile(object):
362 def __init__(self, checksum, full_path):
363 self.checksum = checksum
364 self.full_path = full_path
365 self.base_name = posixpath.basename(self.full_path)
366
367
368def make_random_host_files(in_dir, num_files):
369 min_size = 1 * (1 << 10)
370 max_size = 16 * (1 << 10)
371
372 files = []
373 for _ in xrange(num_files):
374 file_handle = tempfile.NamedTemporaryFile(dir=in_dir, delete=False)
375
376 size = random.randrange(min_size, max_size, 1024)
377 rand_str = os.urandom(size)
378 file_handle.write(rand_str)
379 file_handle.flush()
380 file_handle.close()
381
382 md5 = compute_md5(rand_str)
383 files.append(HostFile(file_handle, md5))
384 return files
385
386
387def make_random_device_files(device, in_dir, num_files):
388 min_size = 1 * (1 << 10)
389 max_size = 16 * (1 << 10)
390
391 files = []
392 for file_num in xrange(num_files):
393 size = random.randrange(min_size, max_size, 1024)
394
395 base_name = 'device_tmpfile' + str(file_num)
Spencer Low3e7feda2015-07-30 01:19:52 -0700396 full_path = posixpath.join(in_dir, base_name)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700397
398 device.shell(['dd', 'if=/dev/urandom', 'of={}'.format(full_path),
399 'bs={}'.format(size), 'count=1'])
David Pursell606835a2015-09-08 17:17:02 -0700400 dev_md5, _ = device.shell([get_md5_prog(device), full_path])[0].split()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700401
402 files.append(DeviceFile(dev_md5, full_path))
403 return files
404
405
406class FileOperationsTest(DeviceTest):
407 SCRATCH_DIR = '/data/local/tmp'
408 DEVICE_TEMP_FILE = SCRATCH_DIR + '/adb_test_file'
409 DEVICE_TEMP_DIR = SCRATCH_DIR + '/adb_test_dir'
410
411 def _test_push(self, local_file, checksum):
412 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700413 self.device.push(local=local_file, remote=self.DEVICE_TEMP_FILE)
414 dev_md5, _ = self.device.shell([get_md5_prog(self.device),
David Pursell606835a2015-09-08 17:17:02 -0700415 self.DEVICE_TEMP_FILE])[0].split()
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700416 self.assertEqual(checksum, dev_md5)
417 self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700418
419 def test_push(self):
420 """Push a randomly generated file to specified device."""
421 kbytes = 512
422 tmp = tempfile.NamedTemporaryFile(mode='wb', delete=False)
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700423 rand_str = os.urandom(1024 * kbytes)
424 tmp.write(rand_str)
425 tmp.close()
426 self._test_push(tmp.name, compute_md5(rand_str))
427 os.remove(tmp.name)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700428
429 # TODO: write push directory test.
430
431 def _test_pull(self, remote_file, checksum):
432 tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False)
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700433 tmp_write.close()
434 self.device.pull(remote=remote_file, local=tmp_write.name)
435 with open(tmp_write.name, 'rb') as tmp_read:
436 host_contents = tmp_read.read()
437 host_md5 = compute_md5(host_contents)
438 self.assertEqual(checksum, host_md5)
439 os.remove(tmp_write.name)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700440
441 def test_pull(self):
442 """Pull a randomly generated file from specified device."""
443 kbytes = 512
444 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700445 cmd = ['dd', 'if=/dev/urandom',
446 'of={}'.format(self.DEVICE_TEMP_FILE), 'bs=1024',
447 'count={}'.format(kbytes)]
448 self.device.shell(cmd)
449 dev_md5, _ = self.device.shell(
David Pursell606835a2015-09-08 17:17:02 -0700450 [get_md5_prog(self.device), self.DEVICE_TEMP_FILE])[0].split()
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700451 self._test_pull(self.DEVICE_TEMP_FILE, dev_md5)
452 self.device.shell_nocheck(['rm', self.DEVICE_TEMP_FILE])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700453
454 def test_pull_dir(self):
455 """Pull a randomly generated directory of files from the device."""
456 host_dir = tempfile.mkdtemp()
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700457 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
458 self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700459
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700460 # Populate device directory with random files.
461 temp_files = make_random_device_files(
462 self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700463
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700464 self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700465
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700466 for temp_file in temp_files:
467 host_path = os.path.join(host_dir, temp_file.base_name)
468 with open(host_path, 'rb') as host_file:
469 host_md5 = compute_md5(host_file.read())
470 self.assertEqual(host_md5, temp_file.checksum)
471
472 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
473 if host_dir is not None:
474 shutil.rmtree(host_dir)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700475
476 def test_sync(self):
477 """Sync a randomly generated directory of files to specified device."""
478 base_dir = tempfile.mkdtemp()
Dan Albert8e1fdd72015-07-24 17:08:33 -0700479
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700480 # Create mirror device directory hierarchy within base_dir.
481 full_dir_path = base_dir + self.DEVICE_TEMP_DIR
482 os.makedirs(full_dir_path)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700483
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700484 # Create 32 random files within the host mirror.
485 temp_files = make_random_host_files(in_dir=full_dir_path, num_files=32)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700486
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700487 # Clean up any trash on the device.
488 device = adb.get_device(product=base_dir)
489 device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700490
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700491 device.sync('data')
492
493 # Confirm that every file on the device mirrors that on the host.
494 for temp_file in temp_files:
495 device_full_path = posixpath.join(self.DEVICE_TEMP_DIR,
496 temp_file.base_name)
497 dev_md5, _ = device.shell(
David Pursell606835a2015-09-08 17:17:02 -0700498 [get_md5_prog(self.device), device_full_path])[0].split()
Elliott Hughes3841a9f2015-08-03 13:58:49 -0700499 self.assertEqual(temp_file.checksum, dev_md5)
500
501 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
502 shutil.rmtree(base_dir + self.DEVICE_TEMP_DIR)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700503
Dan Albert8e1fdd72015-07-24 17:08:33 -0700504 def test_unicode_paths(self):
505 """Ensure that we can support non-ASCII paths, even on Windows."""
Spencer Lowde4505f2015-08-27 20:58:29 -0700506 name = u'로보카 폴리'
Dan Albert8e1fdd72015-07-24 17:08:33 -0700507
508 ## push.
Spencer Lowde4505f2015-08-27 20:58:29 -0700509 tf = tempfile.NamedTemporaryFile('wb', suffix=name, delete=False)
510 tf.close()
511 self.device.push(tf.name, u'/data/local/tmp/adb-test-{}'.format(name))
512 os.remove(tf.name)
Dan Albert8e1fdd72015-07-24 17:08:33 -0700513 self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*'])
514
515 # pull.
Spencer Lowde4505f2015-08-27 20:58:29 -0700516 cmd = ['touch', u'"/data/local/tmp/adb-test-{}"'.format(name)]
Dan Albert8e1fdd72015-07-24 17:08:33 -0700517 self.device.shell(cmd)
518
Spencer Lowde4505f2015-08-27 20:58:29 -0700519 tf = tempfile.NamedTemporaryFile('wb', suffix=name, delete=False)
520 tf.close()
521 self.device.pull(u'/data/local/tmp/adb-test-{}'.format(name), tf.name)
522 os.remove(tf.name)
523 self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*'])
Dan Albert8e1fdd72015-07-24 17:08:33 -0700524
525
526def main():
527 random.seed(0)
528 if len(adb.get_devices()) > 0:
529 suite = unittest.TestLoader().loadTestsFromName(__name__)
530 unittest.TextTestRunner(verbosity=3).run(suite)
531 else:
532 print('Test suite must be run with attached devices')
533
534
535if __name__ == '__main__':
536 main()