blob: dbd80ed6928917435d868d70fe91d423b3d19143 [file] [log] [blame]
Josh Gao49e3c632015-12-09 11:26:11 -08001#!/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 contextlib
21import hashlib
22import os
23import posixpath
24import random
25import re
26import shlex
27import shutil
28import signal
29import socket
30import string
31import subprocess
32import sys
33import tempfile
Josh Gao160bf7e2018-03-19 15:35:11 -070034import threading
Josh Gao2eae66e2016-06-22 18:27:22 -070035import time
Josh Gao49e3c632015-12-09 11:26:11 -080036import unittest
37
Josh Gao18f7a5c2019-01-11 14:42:08 -080038from datetime import datetime
39
Josh Gao49e3c632015-12-09 11:26:11 -080040import adb
41
Josh Gao49e3c632015-12-09 11:26:11 -080042def requires_root(func):
43 def wrapper(self, *args):
44 if self.device.get_prop('ro.debuggable') != '1':
45 raise unittest.SkipTest('requires rootable build')
46
47 was_root = self.device.shell(['id', '-un'])[0].strip() == 'root'
48 if not was_root:
49 self.device.root()
50 self.device.wait()
51
52 try:
53 func(self, *args)
54 finally:
55 if not was_root:
56 self.device.unroot()
57 self.device.wait()
58
59 return wrapper
60
61
62def requires_non_root(func):
63 def wrapper(self, *args):
64 was_root = self.device.shell(['id', '-un'])[0].strip() == 'root'
65 if was_root:
66 self.device.unroot()
67 self.device.wait()
68
69 try:
70 func(self, *args)
71 finally:
72 if was_root:
73 self.device.root()
74 self.device.wait()
75
76 return wrapper
77
78
Josh Gao49e3c632015-12-09 11:26:11 -080079class DeviceTest(unittest.TestCase):
80 def setUp(self):
81 self.device = adb.get_device()
82
83
84class ForwardReverseTest(DeviceTest):
85 def _test_no_rebind(self, description, direction_list, direction,
86 direction_no_rebind, direction_remove_all):
87 msg = direction_list()
88 self.assertEqual('', msg.strip(),
89 description + ' list must be empty to run this test.')
90
91 # Use --no-rebind with no existing binding
92 direction_no_rebind('tcp:5566', 'tcp:6655')
93 msg = direction_list()
94 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
95
96 # Use --no-rebind with existing binding
97 with self.assertRaises(subprocess.CalledProcessError):
98 direction_no_rebind('tcp:5566', 'tcp:6677')
99 msg = direction_list()
100 self.assertFalse(re.search(r'tcp:5566.+tcp:6677', msg))
101 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
102
103 # Use the absence of --no-rebind with existing binding
104 direction('tcp:5566', 'tcp:6677')
105 msg = direction_list()
106 self.assertFalse(re.search(r'tcp:5566.+tcp:6655', msg))
107 self.assertTrue(re.search(r'tcp:5566.+tcp:6677', msg))
108
109 direction_remove_all()
110 msg = direction_list()
111 self.assertEqual('', msg.strip())
112
113 def test_forward_no_rebind(self):
114 self._test_no_rebind('forward', self.device.forward_list,
115 self.device.forward, self.device.forward_no_rebind,
116 self.device.forward_remove_all)
117
118 def test_reverse_no_rebind(self):
119 self._test_no_rebind('reverse', self.device.reverse_list,
120 self.device.reverse, self.device.reverse_no_rebind,
121 self.device.reverse_remove_all)
122
123 def test_forward(self):
124 msg = self.device.forward_list()
125 self.assertEqual('', msg.strip(),
126 'Forwarding list must be empty to run this test.')
127 self.device.forward('tcp:5566', 'tcp:6655')
128 msg = self.device.forward_list()
129 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
130 self.device.forward('tcp:7788', 'tcp:8877')
131 msg = self.device.forward_list()
132 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
133 self.assertTrue(re.search(r'tcp:7788.+tcp:8877', msg))
134 self.device.forward_remove('tcp:5566')
135 msg = self.device.forward_list()
136 self.assertFalse(re.search(r'tcp:5566.+tcp:6655', msg))
137 self.assertTrue(re.search(r'tcp:7788.+tcp:8877', msg))
138 self.device.forward_remove_all()
139 msg = self.device.forward_list()
140 self.assertEqual('', msg.strip())
141
Josh Gao07790752019-09-13 00:12:26 +0800142 def test_forward_old_protocol(self):
143 serialno = subprocess.check_output(self.device.adb_cmd + ['get-serialno']).strip()
144
145 msg = self.device.forward_list()
146 self.assertEqual('', msg.strip(),
147 'Forwarding list must be empty to run this test.')
148
149 s = socket.create_connection(("localhost", 5037))
150 service = b"host-serial:%s:forward:tcp:5566;tcp:6655" % serialno
151 cmd = b"%04x%s" % (len(service), service)
152 s.sendall(cmd)
153
154 msg = self.device.forward_list()
155 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
156
157 self.device.forward_remove_all()
158 msg = self.device.forward_list()
159 self.assertEqual('', msg.strip())
160
David Pursell19d0c232016-04-07 11:25:48 -0700161 def test_forward_tcp_port_0(self):
162 self.assertEqual('', self.device.forward_list().strip(),
163 'Forwarding list must be empty to run this test.')
164
165 try:
166 # If resolving TCP port 0 is supported, `adb forward` will print
167 # the actual port number.
168 port = self.device.forward('tcp:0', 'tcp:8888').strip()
169 if not port:
170 raise unittest.SkipTest('Forwarding tcp:0 is not available.')
171
172 self.assertTrue(re.search(r'tcp:{}.+tcp:8888'.format(port),
173 self.device.forward_list()))
174 finally:
175 self.device.forward_remove_all()
176
Josh Gao49e3c632015-12-09 11:26:11 -0800177 def test_reverse(self):
178 msg = self.device.reverse_list()
179 self.assertEqual('', msg.strip(),
180 'Reverse forwarding list must be empty to run this test.')
181 self.device.reverse('tcp:5566', 'tcp:6655')
182 msg = self.device.reverse_list()
183 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
184 self.device.reverse('tcp:7788', 'tcp:8877')
185 msg = self.device.reverse_list()
186 self.assertTrue(re.search(r'tcp:5566.+tcp:6655', msg))
187 self.assertTrue(re.search(r'tcp:7788.+tcp:8877', msg))
188 self.device.reverse_remove('tcp:5566')
189 msg = self.device.reverse_list()
190 self.assertFalse(re.search(r'tcp:5566.+tcp:6655', msg))
191 self.assertTrue(re.search(r'tcp:7788.+tcp:8877', msg))
192 self.device.reverse_remove_all()
193 msg = self.device.reverse_list()
194 self.assertEqual('', msg.strip())
195
David Pursell19d0c232016-04-07 11:25:48 -0700196 def test_reverse_tcp_port_0(self):
197 self.assertEqual('', self.device.reverse_list().strip(),
198 'Reverse list must be empty to run this test.')
199
200 try:
201 # If resolving TCP port 0 is supported, `adb reverse` will print
202 # the actual port number.
203 port = self.device.reverse('tcp:0', 'tcp:8888').strip()
204 if not port:
205 raise unittest.SkipTest('Reversing tcp:0 is not available.')
206
207 self.assertTrue(re.search(r'tcp:{}.+tcp:8888'.format(port),
208 self.device.reverse_list()))
209 finally:
210 self.device.reverse_remove_all()
211
Josh Gao49e3c632015-12-09 11:26:11 -0800212 def test_forward_reverse_echo(self):
213 """Send data through adb forward and read it back via adb reverse"""
214 forward_port = 12345
215 reverse_port = forward_port + 1
Josh Gao18f74202016-03-03 14:49:02 -0800216 forward_spec = 'tcp:' + str(forward_port)
217 reverse_spec = 'tcp:' + str(reverse_port)
Josh Gao49e3c632015-12-09 11:26:11 -0800218 forward_setup = False
219 reverse_setup = False
220
221 try:
222 # listen on localhost:forward_port, connect to remote:forward_port
223 self.device.forward(forward_spec, forward_spec)
224 forward_setup = True
225 # listen on remote:forward_port, connect to localhost:reverse_port
226 self.device.reverse(forward_spec, reverse_spec)
227 reverse_setup = True
228
229 listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
230 with contextlib.closing(listener):
231 # Use SO_REUSEADDR so that subsequent runs of the test can grab
232 # the port even if it is in TIME_WAIT.
233 listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
234
235 # Listen on localhost:reverse_port before connecting to
236 # localhost:forward_port because that will cause adb to connect
237 # back to localhost:reverse_port.
238 listener.bind(('127.0.0.1', reverse_port))
239 listener.listen(4)
240
241 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
242 with contextlib.closing(client):
243 # Connect to the listener.
244 client.connect(('127.0.0.1', forward_port))
245
246 # Accept the client connection.
247 accepted_connection, addr = listener.accept()
248 with contextlib.closing(accepted_connection) as server:
249 data = 'hello'
250
251 # Send data into the port setup by adb forward.
252 client.sendall(data)
253 # Explicitly close() so that server gets EOF.
254 client.close()
255
256 # Verify that the data came back via adb reverse.
257 self.assertEqual(data, server.makefile().read())
258 finally:
259 if reverse_setup:
260 self.device.reverse_remove(forward_spec)
261 if forward_setup:
262 self.device.forward_remove(forward_spec)
263
264
265class ShellTest(DeviceTest):
266 def _interactive_shell(self, shell_args, input):
267 """Runs an interactive adb shell.
268
269 Args:
270 shell_args: List of string arguments to `adb shell`.
271 input: String input to send to the interactive shell.
272
273 Returns:
274 The remote exit code.
275
276 Raises:
277 unittest.SkipTest: The device doesn't support exit codes.
278 """
David Pursell4b38af42016-04-26 13:25:57 -0700279 if not self.device.has_shell_protocol():
Josh Gao49e3c632015-12-09 11:26:11 -0800280 raise unittest.SkipTest('exit codes are unavailable on this device')
281
282 proc = subprocess.Popen(
283 self.device.adb_cmd + ['shell'] + shell_args,
284 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
285 stderr=subprocess.PIPE)
286 # Closing host-side stdin doesn't trigger a PTY shell to exit so we need
287 # to explicitly add an exit command to close the session from the device
288 # side, plus the necessary newline to complete the interactive command.
289 proc.communicate(input + '; exit\n')
290 return proc.returncode
291
292 def test_cat(self):
293 """Check that we can at least cat a file."""
294 out = self.device.shell(['cat', '/proc/uptime'])[0].strip()
295 elements = out.split()
296 self.assertEqual(len(elements), 2)
297
298 uptime, idle = elements
299 self.assertGreater(float(uptime), 0.0)
300 self.assertGreater(float(idle), 0.0)
301
302 def test_throws_on_failure(self):
303 self.assertRaises(adb.ShellError, self.device.shell, ['false'])
304
305 def test_output_not_stripped(self):
306 out = self.device.shell(['echo', 'foo'])[0]
307 self.assertEqual(out, 'foo' + self.device.linesep)
308
Josh Gao05012022017-06-16 15:34:34 -0700309 def test_shell_command_length(self):
310 # Devices that have shell_v2 should be able to handle long commands.
311 if self.device.has_shell_protocol():
312 rc, out, err = self.device.shell_nocheck(['echo', 'x' * 16384])
313 self.assertEqual(rc, 0)
314 self.assertTrue(out == ('x' * 16384 + '\n'))
315
Josh Gao49e3c632015-12-09 11:26:11 -0800316 def test_shell_nocheck_failure(self):
317 rc, out, _ = self.device.shell_nocheck(['false'])
318 self.assertNotEqual(rc, 0)
319 self.assertEqual(out, '')
320
321 def test_shell_nocheck_output_not_stripped(self):
322 rc, out, _ = self.device.shell_nocheck(['echo', 'foo'])
323 self.assertEqual(rc, 0)
324 self.assertEqual(out, 'foo' + self.device.linesep)
325
326 def test_can_distinguish_tricky_results(self):
327 # If result checking on ADB shell is naively implemented as
328 # `adb shell <cmd>; echo $?`, we would be unable to distinguish the
329 # output from the result for a cmd of `echo -n 1`.
330 rc, out, _ = self.device.shell_nocheck(['echo', '-n', '1'])
331 self.assertEqual(rc, 0)
332 self.assertEqual(out, '1')
333
334 def test_line_endings(self):
335 """Ensure that line ending translation is not happening in the pty.
336
337 Bug: http://b/19735063
338 """
339 output = self.device.shell(['uname'])[0]
340 self.assertEqual(output, 'Linux' + self.device.linesep)
341
342 def test_pty_logic(self):
343 """Tests that a PTY is allocated when it should be.
344
Elliott Hughes02e33782016-10-19 14:47:11 -0700345 PTY allocation behavior should match ssh.
Josh Gao49e3c632015-12-09 11:26:11 -0800346 """
Josh Gao49e3c632015-12-09 11:26:11 -0800347 def check_pty(args):
348 """Checks adb shell PTY allocation.
349
350 Tests |args| for terminal and non-terminal stdin.
351
352 Args:
353 args: -Tt args in a list (e.g. ['-t', '-t']).
354
355 Returns:
356 A tuple (<terminal>, <non-terminal>). True indicates
357 the corresponding shell allocated a remote PTY.
358 """
359 test_cmd = self.device.adb_cmd + ['shell'] + args + ['[ -t 0 ]']
360
361 terminal = subprocess.Popen(
362 test_cmd, stdin=None,
363 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
364 terminal.communicate()
365
366 non_terminal = subprocess.Popen(
367 test_cmd, stdin=subprocess.PIPE,
368 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
369 non_terminal.communicate()
370
371 return (terminal.returncode == 0, non_terminal.returncode == 0)
372
373 # -T: never allocate PTY.
374 self.assertEqual((False, False), check_pty(['-T']))
375
Elliott Hughes02e33782016-10-19 14:47:11 -0700376 # These tests require a new device.
377 if self.device.has_shell_protocol() and os.isatty(sys.stdin.fileno()):
378 # No args: PTY only if stdin is a terminal and shell is interactive,
379 # which is difficult to reliably test from a script.
380 self.assertEqual((False, False), check_pty([]))
Josh Gao49e3c632015-12-09 11:26:11 -0800381
Elliott Hughes02e33782016-10-19 14:47:11 -0700382 # -t: PTY if stdin is a terminal.
383 self.assertEqual((True, False), check_pty(['-t']))
Josh Gao49e3c632015-12-09 11:26:11 -0800384
385 # -t -t: always allocate PTY.
386 self.assertEqual((True, True), check_pty(['-t', '-t']))
387
Elliott Hughes02e33782016-10-19 14:47:11 -0700388 # -tt: always allocate PTY, POSIX style (http://b/32216152).
389 self.assertEqual((True, True), check_pty(['-tt']))
390
391 # -ttt: ssh has weird even/odd behavior with multiple -t flags, but
392 # we follow the man page instead.
393 self.assertEqual((True, True), check_pty(['-ttt']))
394
395 # -ttx: -x and -tt aren't incompatible (though -Tx would be an error).
396 self.assertEqual((True, True), check_pty(['-ttx']))
397
398 # -Ttt: -tt cancels out -T.
399 self.assertEqual((True, True), check_pty(['-Ttt']))
400
401 # -ttT: -T cancels out -tt.
402 self.assertEqual((False, False), check_pty(['-ttT']))
403
Josh Gao49e3c632015-12-09 11:26:11 -0800404 def test_shell_protocol(self):
405 """Tests the shell protocol on the device.
406
407 If the device supports shell protocol, this gives us the ability
408 to separate stdout/stderr and return the exit code directly.
409
410 Bug: http://b/19734861
411 """
David Pursell4b38af42016-04-26 13:25:57 -0700412 if not self.device.has_shell_protocol():
Josh Gao49e3c632015-12-09 11:26:11 -0800413 raise unittest.SkipTest('shell protocol unsupported on this device')
414
415 # Shell protocol should be used by default.
416 result = self.device.shell_nocheck(
417 shlex.split('echo foo; echo bar >&2; exit 17'))
418 self.assertEqual(17, result[0])
419 self.assertEqual('foo' + self.device.linesep, result[1])
420 self.assertEqual('bar' + self.device.linesep, result[2])
421
422 self.assertEqual(17, self._interactive_shell([], 'exit 17'))
423
424 # -x flag should disable shell protocol.
425 result = self.device.shell_nocheck(
426 shlex.split('-x echo foo; echo bar >&2; exit 17'))
427 self.assertEqual(0, result[0])
428 self.assertEqual('foo{0}bar{0}'.format(self.device.linesep), result[1])
429 self.assertEqual('', result[2])
430
431 self.assertEqual(0, self._interactive_shell(['-x'], 'exit 17'))
432
433 def test_non_interactive_sigint(self):
434 """Tests that SIGINT in a non-interactive shell kills the process.
435
436 This requires the shell protocol in order to detect the broken
437 pipe; raw data transfer mode will only see the break once the
438 subprocess tries to read or write.
439
440 Bug: http://b/23825725
441 """
David Pursell4b38af42016-04-26 13:25:57 -0700442 if not self.device.has_shell_protocol():
Josh Gao49e3c632015-12-09 11:26:11 -0800443 raise unittest.SkipTest('shell protocol unsupported on this device')
444
445 # Start a long-running process.
446 sleep_proc = subprocess.Popen(
447 self.device.adb_cmd + shlex.split('shell echo $$; sleep 60'),
448 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
449 stderr=subprocess.STDOUT)
450 remote_pid = sleep_proc.stdout.readline().strip()
451 self.assertIsNone(sleep_proc.returncode, 'subprocess terminated early')
452 proc_query = shlex.split('ps {0} | grep {0}'.format(remote_pid))
453
454 # Verify that the process is running, send signal, verify it stopped.
455 self.device.shell(proc_query)
456 os.kill(sleep_proc.pid, signal.SIGINT)
457 sleep_proc.communicate()
Josh Gao76ffdac2016-10-21 12:40:42 -0700458
459 # It can take some time for the process to receive the signal and die.
460 end_time = time.time() + 3
461 while self.device.shell_nocheck(proc_query)[0] != 1:
462 self.assertFalse(time.time() > end_time,
463 'subprocess failed to terminate in time')
Josh Gao49e3c632015-12-09 11:26:11 -0800464
465 def test_non_interactive_stdin(self):
466 """Tests that non-interactive shells send stdin."""
David Pursell4b38af42016-04-26 13:25:57 -0700467 if not self.device.has_shell_protocol():
Josh Gao49e3c632015-12-09 11:26:11 -0800468 raise unittest.SkipTest('non-interactive stdin unsupported '
469 'on this device')
470
471 # Test both small and large inputs.
472 small_input = 'foo'
473 large_input = '\n'.join(c * 100 for c in (string.ascii_letters +
474 string.digits))
475
476 for input in (small_input, large_input):
477 proc = subprocess.Popen(self.device.adb_cmd + ['shell', 'cat'],
478 stdin=subprocess.PIPE,
479 stdout=subprocess.PIPE,
480 stderr=subprocess.PIPE)
481 stdout, stderr = proc.communicate(input)
482 self.assertEqual(input.splitlines(), stdout.splitlines())
483 self.assertEqual('', stderr)
484
Josh Gao2eae66e2016-06-22 18:27:22 -0700485 def test_sighup(self):
486 """Ensure that SIGHUP gets sent upon non-interactive ctrl-c"""
487 log_path = "/data/local/tmp/adb_signal_test.log"
488
489 # Clear the output file.
490 self.device.shell_nocheck(["echo", ">", log_path])
491
492 script = """
493 trap "echo SIGINT > {path}; exit 0" SIGINT
494 trap "echo SIGHUP > {path}; exit 0" SIGHUP
495 echo Waiting
Josh Gao6a8ce062016-10-21 13:17:32 -0700496 read
Josh Gao2eae66e2016-06-22 18:27:22 -0700497 """.format(path=log_path)
498
499 script = ";".join([x.strip() for x in script.strip().splitlines()])
500
Josh Gao6a8ce062016-10-21 13:17:32 -0700501 process = self.device.shell_popen([script], kill_atexit=False,
502 stdin=subprocess.PIPE,
503 stdout=subprocess.PIPE)
Josh Gao2eae66e2016-06-22 18:27:22 -0700504
505 self.assertEqual("Waiting\n", process.stdout.readline())
506 process.send_signal(signal.SIGINT)
507 process.wait()
508
509 # Waiting for the local adb to finish is insufficient, since it hangs
510 # up immediately.
Josh Gao6a8ce062016-10-21 13:17:32 -0700511 time.sleep(1)
Josh Gao2eae66e2016-06-22 18:27:22 -0700512
513 stdout, _ = self.device.shell(["cat", log_path])
514 self.assertEqual(stdout.strip(), "SIGHUP")
515
Josh Gao160bf7e2018-03-19 15:35:11 -0700516 def test_exit_stress(self):
517 """Hammer `adb shell exit 42` with multiple threads."""
518 thread_count = 48
519 result = dict()
520 def hammer(thread_idx, thread_count, result):
521 success = True
522 for i in range(thread_idx, 240, thread_count):
523 ret = subprocess.call(['adb', 'shell', 'exit {}'.format(i)])
524 if ret != i % 256:
525 success = False
526 break
527 result[thread_idx] = success
528
529 threads = []
530 for i in range(thread_count):
531 thread = threading.Thread(target=hammer, args=(i, thread_count, result))
532 thread.start()
533 threads.append(thread)
534 for thread in threads:
535 thread.join()
536 for i, success in result.iteritems():
537 self.assertTrue(success)
538
Josh Gao49e3c632015-12-09 11:26:11 -0800539
540class ArgumentEscapingTest(DeviceTest):
541 def test_shell_escaping(self):
542 """Make sure that argument escaping is somewhat sane."""
543
544 # http://b/19734868
545 # Note that this actually matches ssh(1)'s behavior --- it's
546 # converted to `sh -c echo hello; echo world` which sh interprets
547 # as `sh -c echo` (with an argument to that shell of "hello"),
548 # and then `echo world` back in the first shell.
549 result = self.device.shell(
550 shlex.split("sh -c 'echo hello; echo world'"))[0]
551 result = result.splitlines()
552 self.assertEqual(['', 'world'], result)
553 # If you really wanted "hello" and "world", here's what you'd do:
554 result = self.device.shell(
555 shlex.split(r'echo hello\;echo world'))[0].splitlines()
556 self.assertEqual(['hello', 'world'], result)
557
558 # http://b/15479704
559 result = self.device.shell(shlex.split("'true && echo t'"))[0].strip()
560 self.assertEqual('t', result)
561 result = self.device.shell(
562 shlex.split("sh -c 'true && echo t'"))[0].strip()
563 self.assertEqual('t', result)
564
565 # http://b/20564385
566 result = self.device.shell(shlex.split('FOO=a BAR=b echo t'))[0].strip()
567 self.assertEqual('t', result)
568 result = self.device.shell(
569 shlex.split(r'echo -n 123\;uname'))[0].strip()
570 self.assertEqual('123Linux', result)
571
572 def test_install_argument_escaping(self):
573 """Make sure that install argument escaping works."""
574 # http://b/20323053, http://b/3090932.
575 for file_suffix in ('-text;ls;1.apk', "-Live Hold'em.apk"):
576 tf = tempfile.NamedTemporaryFile('wb', suffix=file_suffix,
577 delete=False)
578 tf.close()
579
580 # Installing bogus .apks fails if the device supports exit codes.
581 try:
582 output = self.device.install(tf.name)
583 except subprocess.CalledProcessError as e:
584 output = e.output
585
586 self.assertIn(file_suffix, output)
587 os.remove(tf.name)
588
589
590class RootUnrootTest(DeviceTest):
591 def _test_root(self):
592 message = self.device.root()
593 if 'adbd cannot run as root in production builds' in message:
594 return
595 self.device.wait()
596 self.assertEqual('root', self.device.shell(['id', '-un'])[0].strip())
597
598 def _test_unroot(self):
599 self.device.unroot()
600 self.device.wait()
601 self.assertEqual('shell', self.device.shell(['id', '-un'])[0].strip())
602
603 def test_root_unroot(self):
604 """Make sure that adb root and adb unroot work, using id(1)."""
605 if self.device.get_prop('ro.debuggable') != '1':
606 raise unittest.SkipTest('requires rootable build')
607
608 original_user = self.device.shell(['id', '-un'])[0].strip()
609 try:
610 if original_user == 'root':
611 self._test_unroot()
612 self._test_root()
613 elif original_user == 'shell':
614 self._test_root()
615 self._test_unroot()
616 finally:
617 if original_user == 'root':
618 self.device.root()
619 else:
620 self.device.unroot()
621 self.device.wait()
622
623
624class TcpIpTest(DeviceTest):
625 def test_tcpip_failure_raises(self):
626 """adb tcpip requires a port.
627
628 Bug: http://b/22636927
629 """
630 self.assertRaises(
631 subprocess.CalledProcessError, self.device.tcpip, '')
632 self.assertRaises(
633 subprocess.CalledProcessError, self.device.tcpip, 'foo')
634
635
636class SystemPropertiesTest(DeviceTest):
637 def test_get_prop(self):
638 self.assertEqual(self.device.get_prop('init.svc.adbd'), 'running')
639
640 @requires_root
641 def test_set_prop(self):
642 prop_name = 'foo.bar'
643 self.device.shell(['setprop', prop_name, '""'])
644
645 self.device.set_prop(prop_name, 'qux')
646 self.assertEqual(
647 self.device.shell(['getprop', prop_name])[0].strip(), 'qux')
648
649
650def compute_md5(string):
651 hsh = hashlib.md5()
652 hsh.update(string)
653 return hsh.hexdigest()
654
655
656def get_md5_prog(device):
657 """Older platforms (pre-L) had the name md5 rather than md5sum."""
658 try:
659 device.shell(['md5sum', '/proc/uptime'])
660 return 'md5sum'
661 except adb.ShellError:
662 return 'md5'
663
664
665class HostFile(object):
666 def __init__(self, handle, checksum):
667 self.handle = handle
668 self.checksum = checksum
669 self.full_path = handle.name
670 self.base_name = os.path.basename(self.full_path)
671
672
673class DeviceFile(object):
674 def __init__(self, checksum, full_path):
675 self.checksum = checksum
676 self.full_path = full_path
677 self.base_name = posixpath.basename(self.full_path)
678
679
680def make_random_host_files(in_dir, num_files):
681 min_size = 1 * (1 << 10)
682 max_size = 16 * (1 << 10)
683
684 files = []
685 for _ in xrange(num_files):
686 file_handle = tempfile.NamedTemporaryFile(dir=in_dir, delete=False)
687
688 size = random.randrange(min_size, max_size, 1024)
689 rand_str = os.urandom(size)
690 file_handle.write(rand_str)
691 file_handle.flush()
692 file_handle.close()
693
694 md5 = compute_md5(rand_str)
695 files.append(HostFile(file_handle, md5))
696 return files
697
698
699def make_random_device_files(device, in_dir, num_files, prefix='device_tmpfile'):
700 min_size = 1 * (1 << 10)
701 max_size = 16 * (1 << 10)
702
703 files = []
704 for file_num in xrange(num_files):
705 size = random.randrange(min_size, max_size, 1024)
706
707 base_name = prefix + str(file_num)
708 full_path = posixpath.join(in_dir, base_name)
709
710 device.shell(['dd', 'if=/dev/urandom', 'of={}'.format(full_path),
711 'bs={}'.format(size), 'count=1'])
712 dev_md5, _ = device.shell([get_md5_prog(device), full_path])[0].split()
713
714 files.append(DeviceFile(dev_md5, full_path))
715 return files
716
717
718class FileOperationsTest(DeviceTest):
719 SCRATCH_DIR = '/data/local/tmp'
720 DEVICE_TEMP_FILE = SCRATCH_DIR + '/adb_test_file'
721 DEVICE_TEMP_DIR = SCRATCH_DIR + '/adb_test_dir'
722
723 def _verify_remote(self, checksum, remote_path):
724 dev_md5, _ = self.device.shell([get_md5_prog(self.device),
725 remote_path])[0].split()
726 self.assertEqual(checksum, dev_md5)
727
728 def _verify_local(self, checksum, local_path):
729 with open(local_path, 'rb') as host_file:
730 host_md5 = compute_md5(host_file.read())
731 self.assertEqual(host_md5, checksum)
732
733 def test_push(self):
734 """Push a randomly generated file to specified device."""
735 kbytes = 512
736 tmp = tempfile.NamedTemporaryFile(mode='wb', delete=False)
737 rand_str = os.urandom(1024 * kbytes)
738 tmp.write(rand_str)
739 tmp.close()
740
741 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
742 self.device.push(local=tmp.name, remote=self.DEVICE_TEMP_FILE)
743
744 self._verify_remote(compute_md5(rand_str), self.DEVICE_TEMP_FILE)
745 self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE])
746
747 os.remove(tmp.name)
748
749 def test_push_dir(self):
750 """Push a randomly generated directory of files to the device."""
751 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
752 self.device.shell(['mkdir', self.DEVICE_TEMP_DIR])
753
754 try:
755 host_dir = tempfile.mkdtemp()
756
757 # Make sure the temp directory isn't setuid, or else adb will complain.
758 os.chmod(host_dir, 0o700)
759
760 # Create 32 random files.
761 temp_files = make_random_host_files(in_dir=host_dir, num_files=32)
762 self.device.push(host_dir, self.DEVICE_TEMP_DIR)
763
764 for temp_file in temp_files:
765 remote_path = posixpath.join(self.DEVICE_TEMP_DIR,
766 os.path.basename(host_dir),
767 temp_file.base_name)
768 self._verify_remote(temp_file.checksum, remote_path)
769 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
770 finally:
771 if host_dir is not None:
772 shutil.rmtree(host_dir)
773
Josh Gao281aab72018-10-22 13:00:05 -0700774 def disabled_test_push_empty(self):
Josh Gao290ccaf2018-09-21 13:40:16 -0700775 """Push an empty directory to the device."""
Josh Gao49e3c632015-12-09 11:26:11 -0800776 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
777 self.device.shell(['mkdir', self.DEVICE_TEMP_DIR])
778
779 try:
780 host_dir = tempfile.mkdtemp()
781
782 # Make sure the temp directory isn't setuid, or else adb will complain.
783 os.chmod(host_dir, 0o700)
784
785 # Create an empty directory.
Josh Gaoc117edb2018-08-08 14:25:41 -0700786 empty_dir_path = os.path.join(host_dir, 'empty')
787 os.mkdir(empty_dir_path);
Josh Gao49e3c632015-12-09 11:26:11 -0800788
Josh Gaoc117edb2018-08-08 14:25:41 -0700789 self.device.push(empty_dir_path, self.DEVICE_TEMP_DIR)
Josh Gao49e3c632015-12-09 11:26:11 -0800790
Josh Gao290ccaf2018-09-21 13:40:16 -0700791 remote_path = os.path.join(self.DEVICE_TEMP_DIR, "empty")
792 test_empty_cmd = ["[", "-d", remote_path, "]"]
Josh Gao49e3c632015-12-09 11:26:11 -0800793 rc, _, _ = self.device.shell_nocheck(test_empty_cmd)
Josh Gao290ccaf2018-09-21 13:40:16 -0700794
Josh Gao49e3c632015-12-09 11:26:11 -0800795 self.assertEqual(rc, 0)
796 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
797 finally:
798 if host_dir is not None:
799 shutil.rmtree(host_dir)
800
Josh Gao1deea102016-09-14 16:13:50 -0700801 @unittest.skipIf(sys.platform == "win32", "symlinks require elevated privileges on windows")
802 def test_push_symlink(self):
803 """Push a symlink.
804
805 Bug: http://b/31491920
806 """
807 try:
808 host_dir = tempfile.mkdtemp()
809
810 # Make sure the temp directory isn't setuid, or else adb will
811 # complain.
812 os.chmod(host_dir, 0o700)
813
814 with open(os.path.join(host_dir, 'foo'), 'w') as f:
815 f.write('foo')
816
817 symlink_path = os.path.join(host_dir, 'symlink')
818 os.symlink('foo', symlink_path)
819
820 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
821 self.device.shell(['mkdir', self.DEVICE_TEMP_DIR])
822 self.device.push(symlink_path, self.DEVICE_TEMP_DIR)
823 rc, out, _ = self.device.shell_nocheck(
824 ['cat', posixpath.join(self.DEVICE_TEMP_DIR, 'symlink')])
825 self.assertEqual(0, rc)
826 self.assertEqual(out.strip(), 'foo')
827 finally:
828 if host_dir is not None:
829 shutil.rmtree(host_dir)
830
Josh Gao49e3c632015-12-09 11:26:11 -0800831 def test_multiple_push(self):
832 """Push multiple files to the device in one adb push command.
833
834 Bug: http://b/25324823
835 """
836
837 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
838 self.device.shell(['mkdir', self.DEVICE_TEMP_DIR])
839
840 try:
841 host_dir = tempfile.mkdtemp()
842
843 # Create some random files and a subdirectory containing more files.
844 temp_files = make_random_host_files(in_dir=host_dir, num_files=4)
845
Josh Gao18f74202016-03-03 14:49:02 -0800846 subdir = os.path.join(host_dir, 'subdir')
Josh Gao49e3c632015-12-09 11:26:11 -0800847 os.mkdir(subdir)
848 subdir_temp_files = make_random_host_files(in_dir=subdir,
849 num_files=4)
850
851 paths = map(lambda temp_file: temp_file.full_path, temp_files)
852 paths.append(subdir)
853 self.device._simple_call(['push'] + paths + [self.DEVICE_TEMP_DIR])
854
855 for temp_file in temp_files:
856 remote_path = posixpath.join(self.DEVICE_TEMP_DIR,
857 temp_file.base_name)
858 self._verify_remote(temp_file.checksum, remote_path)
859
860 for subdir_temp_file in subdir_temp_files:
861 remote_path = posixpath.join(self.DEVICE_TEMP_DIR,
862 # BROKEN: http://b/25394682
Josh Gao18f74202016-03-03 14:49:02 -0800863 # 'subdir';
Josh Gao49e3c632015-12-09 11:26:11 -0800864 temp_file.base_name)
865 self._verify_remote(temp_file.checksum, remote_path)
866
867
868 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
869 finally:
870 if host_dir is not None:
871 shutil.rmtree(host_dir)
872
Josh Gaoa53abe72016-02-19 15:55:55 -0800873 @requires_non_root
874 def test_push_error_reporting(self):
875 """Make sure that errors that occur while pushing a file get reported
876
877 Bug: http://b/26816782
878 """
879 with tempfile.NamedTemporaryFile() as tmp_file:
880 tmp_file.write('\0' * 1024 * 1024)
881 tmp_file.flush()
882 try:
883 self.device.push(local=tmp_file.name, remote='/system/')
Josh Gao18f74202016-03-03 14:49:02 -0800884 self.fail('push should not have succeeded')
Josh Gaoa53abe72016-02-19 15:55:55 -0800885 except subprocess.CalledProcessError as e:
886 output = e.output
887
Josh Gaocb6497a2016-11-18 15:31:11 -0800888 self.assertTrue('Permission denied' in output or
889 'Read-only file system' in output)
Josh Gao49e3c632015-12-09 11:26:11 -0800890
Josh Gaof9671172018-06-28 18:43:19 -0700891 @requires_non_root
892 def test_push_directory_creation(self):
893 """Regression test for directory creation.
894
895 Bug: http://b/110953234
896 """
897 with tempfile.NamedTemporaryFile() as tmp_file:
898 tmp_file.write('\0' * 1024 * 1024)
899 tmp_file.flush()
900 remote_path = self.DEVICE_TEMP_DIR + '/test_push_directory_creation'
901 self.device.shell(['rm', '-rf', remote_path])
902
903 remote_path += '/filename'
904 self.device.push(local=tmp_file.name, remote=remote_path)
905
Josh Gao49e3c632015-12-09 11:26:11 -0800906 def _test_pull(self, remote_file, checksum):
907 tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False)
908 tmp_write.close()
909 self.device.pull(remote=remote_file, local=tmp_write.name)
910 with open(tmp_write.name, 'rb') as tmp_read:
911 host_contents = tmp_read.read()
912 host_md5 = compute_md5(host_contents)
913 self.assertEqual(checksum, host_md5)
914 os.remove(tmp_write.name)
915
916 @requires_non_root
917 def test_pull_error_reporting(self):
918 self.device.shell(['touch', self.DEVICE_TEMP_FILE])
919 self.device.shell(['chmod', 'a-rwx', self.DEVICE_TEMP_FILE])
920
921 try:
922 output = self.device.pull(remote=self.DEVICE_TEMP_FILE, local='x')
923 except subprocess.CalledProcessError as e:
924 output = e.output
925
926 self.assertIn('Permission denied', output)
927
928 self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE])
929
930 def test_pull(self):
931 """Pull a randomly generated file from specified device."""
932 kbytes = 512
933 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
934 cmd = ['dd', 'if=/dev/urandom',
935 'of={}'.format(self.DEVICE_TEMP_FILE), 'bs=1024',
936 'count={}'.format(kbytes)]
937 self.device.shell(cmd)
938 dev_md5, _ = self.device.shell(
939 [get_md5_prog(self.device), self.DEVICE_TEMP_FILE])[0].split()
940 self._test_pull(self.DEVICE_TEMP_FILE, dev_md5)
941 self.device.shell_nocheck(['rm', self.DEVICE_TEMP_FILE])
942
943 def test_pull_dir(self):
944 """Pull a randomly generated directory of files from the device."""
945 try:
946 host_dir = tempfile.mkdtemp()
947
948 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
949 self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
950
951 # Populate device directory with random files.
952 temp_files = make_random_device_files(
953 self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
954
955 self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir)
956
957 for temp_file in temp_files:
Josh Gao38752792015-12-09 14:20:23 -0800958 host_path = os.path.join(
959 host_dir, posixpath.basename(self.DEVICE_TEMP_DIR),
960 temp_file.base_name)
961 self._verify_local(temp_file.checksum, host_path)
Josh Gao49e3c632015-12-09 11:26:11 -0800962
963 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
964 finally:
965 if host_dir is not None:
966 shutil.rmtree(host_dir)
967
Josh Gao49726bc2016-02-26 13:26:55 -0800968 def test_pull_dir_symlink(self):
969 """Pull a directory into a symlink to a directory.
970
971 Bug: http://b/27362811
972 """
Josh Gao18f74202016-03-03 14:49:02 -0800973 if os.name != 'posix':
Josh Gao49726bc2016-02-26 13:26:55 -0800974 raise unittest.SkipTest('requires POSIX')
975
976 try:
977 host_dir = tempfile.mkdtemp()
Josh Gao18f74202016-03-03 14:49:02 -0800978 real_dir = os.path.join(host_dir, 'dir')
979 symlink = os.path.join(host_dir, 'symlink')
Josh Gao49726bc2016-02-26 13:26:55 -0800980 os.mkdir(real_dir)
981 os.symlink(real_dir, symlink)
982
983 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
984 self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
985
986 # Populate device directory with random files.
987 temp_files = make_random_device_files(
988 self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
989
990 self.device.pull(remote=self.DEVICE_TEMP_DIR, local=symlink)
991
992 for temp_file in temp_files:
993 host_path = os.path.join(
994 real_dir, posixpath.basename(self.DEVICE_TEMP_DIR),
995 temp_file.base_name)
996 self._verify_local(temp_file.checksum, host_path)
997
998 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
999 finally:
1000 if host_dir is not None:
1001 shutil.rmtree(host_dir)
1002
1003 def test_pull_dir_symlink_collision(self):
1004 """Pull a directory into a colliding symlink to directory."""
Josh Gao18f74202016-03-03 14:49:02 -08001005 if os.name != 'posix':
Josh Gao49726bc2016-02-26 13:26:55 -08001006 raise unittest.SkipTest('requires POSIX')
1007
1008 try:
1009 host_dir = tempfile.mkdtemp()
Josh Gao18f74202016-03-03 14:49:02 -08001010 real_dir = os.path.join(host_dir, 'real')
Josh Gao49726bc2016-02-26 13:26:55 -08001011 tmp_dirname = os.path.basename(self.DEVICE_TEMP_DIR)
1012 symlink = os.path.join(host_dir, tmp_dirname)
1013 os.mkdir(real_dir)
1014 os.symlink(real_dir, symlink)
1015
1016 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1017 self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
1018
1019 # Populate device directory with random files.
1020 temp_files = make_random_device_files(
1021 self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
1022
1023 self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir)
1024
1025 for temp_file in temp_files:
1026 host_path = os.path.join(real_dir, temp_file.base_name)
1027 self._verify_local(temp_file.checksum, host_path)
1028
1029 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1030 finally:
1031 if host_dir is not None:
1032 shutil.rmtree(host_dir)
1033
Josh Gaoa842b382016-03-02 16:00:02 -08001034 def test_pull_dir_nonexistent(self):
1035 """Pull a directory of files from the device to a nonexistent path."""
1036 try:
1037 host_dir = tempfile.mkdtemp()
1038 dest_dir = os.path.join(host_dir, 'dest')
1039
1040 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1041 self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
1042
1043 # Populate device directory with random files.
1044 temp_files = make_random_device_files(
1045 self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
1046
1047 self.device.pull(remote=self.DEVICE_TEMP_DIR, local=dest_dir)
1048
1049 for temp_file in temp_files:
1050 host_path = os.path.join(dest_dir, temp_file.base_name)
1051 self._verify_local(temp_file.checksum, host_path)
1052
1053 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1054 finally:
1055 if host_dir is not None:
1056 shutil.rmtree(host_dir)
1057
Josh Gaofcd0ffb2018-08-08 14:26:03 -07001058 # selinux prevents adbd from accessing symlinks on /data/local/tmp.
1059 def disabled_test_pull_symlink_dir(self):
Josh Gaod9a2fd62015-12-09 14:03:30 -08001060 """Pull a symlink to a directory of symlinks to files."""
1061 try:
1062 host_dir = tempfile.mkdtemp()
1063
1064 remote_dir = posixpath.join(self.DEVICE_TEMP_DIR, 'contents')
1065 remote_links = posixpath.join(self.DEVICE_TEMP_DIR, 'links')
1066 remote_symlink = posixpath.join(self.DEVICE_TEMP_DIR, 'symlink')
1067
1068 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1069 self.device.shell(['mkdir', '-p', remote_dir, remote_links])
1070 self.device.shell(['ln', '-s', remote_links, remote_symlink])
1071
1072 # Populate device directory with random files.
1073 temp_files = make_random_device_files(
1074 self.device, in_dir=remote_dir, num_files=32)
1075
1076 for temp_file in temp_files:
1077 self.device.shell(
1078 ['ln', '-s', '../contents/{}'.format(temp_file.base_name),
1079 posixpath.join(remote_links, temp_file.base_name)])
1080
1081 self.device.pull(remote=remote_symlink, local=host_dir)
1082
1083 for temp_file in temp_files:
1084 host_path = os.path.join(
1085 host_dir, 'symlink', temp_file.base_name)
1086 self._verify_local(temp_file.checksum, host_path)
1087
1088 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1089 finally:
1090 if host_dir is not None:
1091 shutil.rmtree(host_dir)
1092
Josh Gao49e3c632015-12-09 11:26:11 -08001093 def test_pull_empty(self):
1094 """Pull a directory containing an empty directory from the device."""
1095 try:
1096 host_dir = tempfile.mkdtemp()
1097
1098 remote_empty_path = posixpath.join(self.DEVICE_TEMP_DIR, 'empty')
1099 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1100 self.device.shell(['mkdir', '-p', remote_empty_path])
1101
1102 self.device.pull(remote=remote_empty_path, local=host_dir)
1103 self.assertTrue(os.path.isdir(os.path.join(host_dir, 'empty')))
1104 finally:
1105 if host_dir is not None:
1106 shutil.rmtree(host_dir)
1107
1108 def test_multiple_pull(self):
1109 """Pull a randomly generated directory of files from the device."""
1110
1111 try:
1112 host_dir = tempfile.mkdtemp()
1113
Josh Gao18f74202016-03-03 14:49:02 -08001114 subdir = posixpath.join(self.DEVICE_TEMP_DIR, 'subdir')
Josh Gao49e3c632015-12-09 11:26:11 -08001115 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1116 self.device.shell(['mkdir', '-p', subdir])
1117
1118 # Create some random files and a subdirectory containing more files.
1119 temp_files = make_random_device_files(
1120 self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=4)
1121
1122 subdir_temp_files = make_random_device_files(
1123 self.device, in_dir=subdir, num_files=4, prefix='subdir_')
1124
1125 paths = map(lambda temp_file: temp_file.full_path, temp_files)
1126 paths.append(subdir)
1127 self.device._simple_call(['pull'] + paths + [host_dir])
1128
1129 for temp_file in temp_files:
1130 local_path = os.path.join(host_dir, temp_file.base_name)
1131 self._verify_local(temp_file.checksum, local_path)
1132
1133 for subdir_temp_file in subdir_temp_files:
1134 local_path = os.path.join(host_dir,
Josh Gao18f74202016-03-03 14:49:02 -08001135 'subdir',
Josh Gao49e3c632015-12-09 11:26:11 -08001136 subdir_temp_file.base_name)
1137 self._verify_local(subdir_temp_file.checksum, local_path)
1138
1139 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1140 finally:
1141 if host_dir is not None:
1142 shutil.rmtree(host_dir)
1143
Dan Albert8449e062017-05-18 22:56:48 -07001144 def verify_sync(self, device, temp_files, device_dir):
1145 """Verifies that a list of temp files was synced to the device."""
1146 # Confirm that every file on the device mirrors that on the host.
1147 for temp_file in temp_files:
1148 device_full_path = posixpath.join(
1149 device_dir, temp_file.base_name)
1150 dev_md5, _ = device.shell(
1151 [get_md5_prog(self.device), device_full_path])[0].split()
1152 self.assertEqual(temp_file.checksum, dev_md5)
1153
Josh Gao49e3c632015-12-09 11:26:11 -08001154 def test_sync(self):
Dan Albert8449e062017-05-18 22:56:48 -07001155 """Sync a host directory to the data partition."""
Josh Gao49e3c632015-12-09 11:26:11 -08001156
1157 try:
1158 base_dir = tempfile.mkdtemp()
1159
1160 # Create mirror device directory hierarchy within base_dir.
1161 full_dir_path = base_dir + self.DEVICE_TEMP_DIR
1162 os.makedirs(full_dir_path)
1163
1164 # Create 32 random files within the host mirror.
Dan Albert8449e062017-05-18 22:56:48 -07001165 temp_files = make_random_host_files(
1166 in_dir=full_dir_path, num_files=32)
Josh Gao49e3c632015-12-09 11:26:11 -08001167
Dan Albert8449e062017-05-18 22:56:48 -07001168 # Clean up any stale files on the device.
Dan Albertb29a57b2017-05-18 13:52:45 -07001169 device = adb.get_device() # pylint: disable=no-member
Josh Gao49e3c632015-12-09 11:26:11 -08001170 device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1171
Dan Albertb29a57b2017-05-18 13:52:45 -07001172 old_product_out = os.environ.get('ANDROID_PRODUCT_OUT')
1173 os.environ['ANDROID_PRODUCT_OUT'] = base_dir
Josh Gao49e3c632015-12-09 11:26:11 -08001174 device.sync('data')
Dan Albertb29a57b2017-05-18 13:52:45 -07001175 if old_product_out is None:
1176 del os.environ['ANDROID_PRODUCT_OUT']
1177 else:
1178 os.environ['ANDROID_PRODUCT_OUT'] = old_product_out
Josh Gao49e3c632015-12-09 11:26:11 -08001179
Dan Albert8449e062017-05-18 22:56:48 -07001180 self.verify_sync(device, temp_files, self.DEVICE_TEMP_DIR)
Josh Gao49e3c632015-12-09 11:26:11 -08001181
Dan Albert8449e062017-05-18 22:56:48 -07001182 #self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
Josh Gao49e3c632015-12-09 11:26:11 -08001183 finally:
1184 if base_dir is not None:
1185 shutil.rmtree(base_dir)
1186
Dan Albert8449e062017-05-18 22:56:48 -07001187 def test_push_sync(self):
1188 """Sync a host directory to a specific path."""
1189
1190 try:
1191 temp_dir = tempfile.mkdtemp()
1192 temp_files = make_random_host_files(in_dir=temp_dir, num_files=32)
1193
1194 device_dir = posixpath.join(self.DEVICE_TEMP_DIR, 'sync_src_dst')
1195
1196 # Clean up any stale files on the device.
1197 device = adb.get_device() # pylint: disable=no-member
1198 device.shell(['rm', '-rf', device_dir])
1199
1200 device.push(temp_dir, device_dir, sync=True)
1201
1202 self.verify_sync(device, temp_files, device_dir)
1203
1204 self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
1205 finally:
1206 if temp_dir is not None:
1207 shutil.rmtree(temp_dir)
1208
Josh Gao49e3c632015-12-09 11:26:11 -08001209 def test_unicode_paths(self):
1210 """Ensure that we can support non-ASCII paths, even on Windows."""
1211 name = u'로보카 폴리'
1212
1213 self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*'])
1214 remote_path = u'/data/local/tmp/adb-test-{}'.format(name)
1215
1216 ## push.
1217 tf = tempfile.NamedTemporaryFile('wb', suffix=name, delete=False)
1218 tf.close()
1219 self.device.push(tf.name, remote_path)
1220 os.remove(tf.name)
1221 self.assertFalse(os.path.exists(tf.name))
1222
1223 # Verify that the device ended up with the expected UTF-8 path
1224 output = self.device.shell(
1225 ['ls', '/data/local/tmp/adb-test-*'])[0].strip()
Josh Gao4f8504e2018-03-19 16:09:05 -07001226 self.assertEqual(remote_path, output)
Josh Gao49e3c632015-12-09 11:26:11 -08001227
1228 # pull.
1229 self.device.pull(remote_path, tf.name)
1230 self.assertTrue(os.path.exists(tf.name))
1231 os.remove(tf.name)
1232 self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*'])
1233
1234
Yabin Cui3cf1b362017-03-10 16:01:01 -08001235class DeviceOfflineTest(DeviceTest):
1236 def _get_device_state(self, serialno):
1237 output = subprocess.check_output(self.device.adb_cmd + ['devices'])
1238 for line in output.split('\n'):
1239 m = re.match('(\S+)\s+(\S+)', line)
1240 if m and m.group(1) == serialno:
1241 return m.group(2)
1242 return None
1243
Josh Gao6e0ed552017-09-13 14:51:23 -07001244 def disabled_test_killed_when_pushing_a_large_file(self):
Yabin Cui3cf1b362017-03-10 16:01:01 -08001245 """
1246 While running adb push with a large file, kill adb server.
1247 Occasionally the device becomes offline. Because the device is still
1248 reading data without realizing that the adb server has been restarted.
1249 Test if we can bring the device online automatically now.
1250 http://b/32952319
1251 """
1252 serialno = subprocess.check_output(self.device.adb_cmd + ['get-serialno']).strip()
1253 # 1. Push a large file
1254 file_path = 'tmp_large_file'
1255 try:
1256 fh = open(file_path, 'w')
1257 fh.write('\0' * (100 * 1024 * 1024))
1258 fh.close()
1259 subproc = subprocess.Popen(self.device.adb_cmd + ['push', file_path, '/data/local/tmp'])
1260 time.sleep(0.1)
1261 # 2. Kill the adb server
1262 subprocess.check_call(self.device.adb_cmd + ['kill-server'])
1263 subproc.terminate()
1264 finally:
1265 try:
1266 os.unlink(file_path)
1267 except:
1268 pass
1269 # 3. See if the device still exist.
1270 # Sleep to wait for the adb server exit.
1271 time.sleep(0.5)
1272 # 4. The device should be online
1273 self.assertEqual(self._get_device_state(serialno), 'device')
1274
Josh Gao6e0ed552017-09-13 14:51:23 -07001275 def disabled_test_killed_when_pulling_a_large_file(self):
Yabin Cui3cf1b362017-03-10 16:01:01 -08001276 """
1277 While running adb pull with a large file, kill adb server.
1278 Occasionally the device can't be connected. Because the device is trying to
1279 send a message larger than what is expected by the adb server.
1280 Test if we can bring the device online automatically now.
1281 """
1282 serialno = subprocess.check_output(self.device.adb_cmd + ['get-serialno']).strip()
1283 file_path = 'tmp_large_file'
1284 try:
1285 # 1. Create a large file on device.
1286 self.device.shell(['dd', 'if=/dev/zero', 'of=/data/local/tmp/tmp_large_file',
1287 'bs=1000000', 'count=100'])
1288 # 2. Pull the large file on host.
1289 subproc = subprocess.Popen(self.device.adb_cmd +
1290 ['pull','/data/local/tmp/tmp_large_file', file_path])
1291 time.sleep(0.1)
1292 # 3. Kill the adb server
1293 subprocess.check_call(self.device.adb_cmd + ['kill-server'])
1294 subproc.terminate()
1295 finally:
1296 try:
1297 os.unlink(file_path)
1298 except:
1299 pass
1300 # 4. See if the device still exist.
1301 # Sleep to wait for the adb server exit.
1302 time.sleep(0.5)
1303 self.assertEqual(self._get_device_state(serialno), 'device')
1304
1305
Josh Gao3734cf02017-05-02 15:01:09 -07001306 def test_packet_size_regression(self):
1307 """Test for http://b/37783561
1308
1309 Receiving packets of a length divisible by 512 but not 1024 resulted in
1310 the adb client waiting indefinitely for more input.
1311 """
1312 # The values that trigger things are 507 (512 - 5 bytes from shell protocol) + 1024*n
1313 # Probe some surrounding values as well, for the hell of it.
Josh Gaoc7f2d192018-04-10 14:35:06 -07001314 for base in [512] + range(1024, 1024 * 16, 1024):
1315 for offset in [-6, -5, -4]:
1316 length = base + offset
1317 cmd = ['dd', 'if=/dev/zero', 'bs={}'.format(length), 'count=1', '2>/dev/null;'
1318 'echo', 'foo']
1319 rc, stdout, _ = self.device.shell_nocheck(cmd)
Josh Gao3734cf02017-05-02 15:01:09 -07001320
Josh Gaoc7f2d192018-04-10 14:35:06 -07001321 self.assertEqual(0, rc)
Josh Gao3734cf02017-05-02 15:01:09 -07001322
Josh Gaoc7f2d192018-04-10 14:35:06 -07001323 # Output should be '\0' * length, followed by "foo\n"
1324 self.assertEqual(length, len(stdout) - 4)
1325 self.assertEqual(stdout, "\0" * length + "foo\n")
Josh Gao3734cf02017-05-02 15:01:09 -07001326
Josh Gao9fae8762018-08-22 15:13:18 -07001327 def test_zero_packet(self):
1328 """Test for http://b/113070258
1329
1330 Make sure that we don't blow up when sending USB transfers that line up
1331 exactly with the USB packet size.
1332 """
1333
1334 local_port = int(self.device.forward("tcp:0", "tcp:12345"))
1335 try:
1336 for size in [512, 1024]:
1337 def listener():
1338 cmd = ["echo foo | nc -l -p 12345; echo done"]
1339 rc, stdout, stderr = self.device.shell_nocheck(cmd)
1340
1341 thread = threading.Thread(target=listener)
1342 thread.start()
1343
1344 # Wait a bit to let the shell command start.
1345 time.sleep(0.25)
1346
1347 sock = socket.create_connection(("localhost", local_port))
1348 with contextlib.closing(sock):
1349 bytesWritten = sock.send("a" * size)
1350 self.assertEqual(size, bytesWritten)
1351 readBytes = sock.recv(4096)
1352 self.assertEqual("foo\n", readBytes)
1353
1354 thread.join()
1355 finally:
1356 self.device.forward_remove("tcp:{}".format(local_port))
1357
Josh Gao3734cf02017-05-02 15:01:09 -07001358
Josh Gao18f7a5c2019-01-11 14:42:08 -08001359class SocketTest(DeviceTest):
1360 def test_socket_flush(self):
1361 """Test that we handle socket closure properly.
1362
1363 If we're done writing to a socket, closing before the other end has
1364 closed will send a TCP_RST if we have incoming data queued up, which
1365 may result in data that we've written being discarded.
1366
1367 Bug: http://b/74616284
1368 """
1369 s = socket.create_connection(("localhost", 5037))
1370
1371 def adb_length_prefixed(string):
1372 encoded = string.encode("utf8")
1373 result = b"%04x%s" % (len(encoded), encoded)
1374 return result
1375
1376 if "ANDROID_SERIAL" in os.environ:
1377 transport_string = "host:transport:" + os.environ["ANDROID_SERIAL"]
1378 else:
1379 transport_string = "host:transport-any"
1380
1381 s.sendall(adb_length_prefixed(transport_string))
1382 response = s.recv(4)
1383 self.assertEquals(b"OKAY", response)
1384
1385 shell_string = "shell:sleep 0.5; dd if=/dev/zero bs=1m count=1 status=none; echo foo"
1386 s.sendall(adb_length_prefixed(shell_string))
1387
1388 response = s.recv(4)
1389 self.assertEquals(b"OKAY", response)
1390
1391 # Spawn a thread that dumps garbage into the socket until failure.
1392 def spam():
1393 buf = b"\0" * 16384
1394 try:
1395 while True:
1396 s.sendall(buf)
1397 except Exception as ex:
1398 print(ex)
1399
1400 thread = threading.Thread(target=spam)
1401 thread.start()
1402
1403 time.sleep(1)
1404
1405 received = b""
1406 while True:
1407 read = s.recv(512)
1408 if len(read) == 0:
1409 break
1410 received += read
1411
1412 self.assertEquals(1024 * 1024 + len("foo\n"), len(received))
1413 thread.join()
1414
1415
Spencer Low35a47db2018-08-11 00:16:16 -07001416if sys.platform == "win32":
1417 # From https://stackoverflow.com/a/38749458
1418 import os
1419 import contextlib
1420 import msvcrt
1421 import ctypes
1422 from ctypes import wintypes
1423
1424 kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
1425
1426 GENERIC_READ = 0x80000000
1427 GENERIC_WRITE = 0x40000000
1428 FILE_SHARE_READ = 1
1429 FILE_SHARE_WRITE = 2
1430 CONSOLE_TEXTMODE_BUFFER = 1
1431 INVALID_HANDLE_VALUE = wintypes.HANDLE(-1).value
1432 STD_OUTPUT_HANDLE = wintypes.DWORD(-11)
1433 STD_ERROR_HANDLE = wintypes.DWORD(-12)
1434
1435 def _check_zero(result, func, args):
1436 if not result:
1437 raise ctypes.WinError(ctypes.get_last_error())
1438 return args
1439
1440 def _check_invalid(result, func, args):
1441 if result == INVALID_HANDLE_VALUE:
1442 raise ctypes.WinError(ctypes.get_last_error())
1443 return args
1444
1445 if not hasattr(wintypes, 'LPDWORD'): # Python 2
1446 wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)
1447 wintypes.PSMALL_RECT = ctypes.POINTER(wintypes.SMALL_RECT)
1448
1449 class COORD(ctypes.Structure):
1450 _fields_ = (('X', wintypes.SHORT),
1451 ('Y', wintypes.SHORT))
1452
1453 class CONSOLE_SCREEN_BUFFER_INFOEX(ctypes.Structure):
1454 _fields_ = (('cbSize', wintypes.ULONG),
1455 ('dwSize', COORD),
1456 ('dwCursorPosition', COORD),
1457 ('wAttributes', wintypes.WORD),
1458 ('srWindow', wintypes.SMALL_RECT),
1459 ('dwMaximumWindowSize', COORD),
1460 ('wPopupAttributes', wintypes.WORD),
1461 ('bFullscreenSupported', wintypes.BOOL),
1462 ('ColorTable', wintypes.DWORD * 16))
1463 def __init__(self, *args, **kwds):
1464 super(CONSOLE_SCREEN_BUFFER_INFOEX, self).__init__(
1465 *args, **kwds)
1466 self.cbSize = ctypes.sizeof(self)
1467
1468 PCONSOLE_SCREEN_BUFFER_INFOEX = ctypes.POINTER(
1469 CONSOLE_SCREEN_BUFFER_INFOEX)
1470 LPSECURITY_ATTRIBUTES = wintypes.LPVOID
1471
1472 kernel32.GetStdHandle.errcheck = _check_invalid
1473 kernel32.GetStdHandle.restype = wintypes.HANDLE
1474 kernel32.GetStdHandle.argtypes = (
1475 wintypes.DWORD,) # _In_ nStdHandle
1476
1477 kernel32.CreateConsoleScreenBuffer.errcheck = _check_invalid
1478 kernel32.CreateConsoleScreenBuffer.restype = wintypes.HANDLE
1479 kernel32.CreateConsoleScreenBuffer.argtypes = (
1480 wintypes.DWORD, # _In_ dwDesiredAccess
1481 wintypes.DWORD, # _In_ dwShareMode
1482 LPSECURITY_ATTRIBUTES, # _In_opt_ lpSecurityAttributes
1483 wintypes.DWORD, # _In_ dwFlags
1484 wintypes.LPVOID) # _Reserved_ lpScreenBufferData
1485
1486 kernel32.GetConsoleScreenBufferInfoEx.errcheck = _check_zero
1487 kernel32.GetConsoleScreenBufferInfoEx.argtypes = (
1488 wintypes.HANDLE, # _In_ hConsoleOutput
1489 PCONSOLE_SCREEN_BUFFER_INFOEX) # _Out_ lpConsoleScreenBufferInfo
1490
1491 kernel32.SetConsoleScreenBufferInfoEx.errcheck = _check_zero
1492 kernel32.SetConsoleScreenBufferInfoEx.argtypes = (
1493 wintypes.HANDLE, # _In_ hConsoleOutput
1494 PCONSOLE_SCREEN_BUFFER_INFOEX) # _In_ lpConsoleScreenBufferInfo
1495
1496 kernel32.SetConsoleWindowInfo.errcheck = _check_zero
1497 kernel32.SetConsoleWindowInfo.argtypes = (
1498 wintypes.HANDLE, # _In_ hConsoleOutput
1499 wintypes.BOOL, # _In_ bAbsolute
1500 wintypes.PSMALL_RECT) # _In_ lpConsoleWindow
1501
1502 kernel32.FillConsoleOutputCharacterW.errcheck = _check_zero
1503 kernel32.FillConsoleOutputCharacterW.argtypes = (
1504 wintypes.HANDLE, # _In_ hConsoleOutput
1505 wintypes.WCHAR, # _In_ cCharacter
1506 wintypes.DWORD, # _In_ nLength
1507 COORD, # _In_ dwWriteCoord
1508 wintypes.LPDWORD) # _Out_ lpNumberOfCharsWritten
1509
1510 kernel32.ReadConsoleOutputCharacterW.errcheck = _check_zero
1511 kernel32.ReadConsoleOutputCharacterW.argtypes = (
1512 wintypes.HANDLE, # _In_ hConsoleOutput
1513 wintypes.LPWSTR, # _Out_ lpCharacter
1514 wintypes.DWORD, # _In_ nLength
1515 COORD, # _In_ dwReadCoord
1516 wintypes.LPDWORD) # _Out_ lpNumberOfCharsRead
1517
1518 @contextlib.contextmanager
1519 def allocate_console():
1520 allocated = kernel32.AllocConsole()
1521 try:
1522 yield allocated
1523 finally:
1524 if allocated:
1525 kernel32.FreeConsole()
1526
1527 @contextlib.contextmanager
1528 def console_screen(ncols=None, nrows=None):
1529 info = CONSOLE_SCREEN_BUFFER_INFOEX()
1530 new_info = CONSOLE_SCREEN_BUFFER_INFOEX()
1531 nwritten = (wintypes.DWORD * 1)()
1532 hStdOut = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
1533 kernel32.GetConsoleScreenBufferInfoEx(
1534 hStdOut, ctypes.byref(info))
1535 if ncols is None:
1536 ncols = info.dwSize.X
1537 if nrows is None:
1538 nrows = info.dwSize.Y
1539 elif nrows > 9999:
1540 raise ValueError('nrows must be 9999 or less')
1541 fd_screen = None
1542 hScreen = kernel32.CreateConsoleScreenBuffer(
1543 GENERIC_READ | GENERIC_WRITE,
1544 FILE_SHARE_READ | FILE_SHARE_WRITE,
1545 None, CONSOLE_TEXTMODE_BUFFER, None)
1546 try:
1547 fd_screen = msvcrt.open_osfhandle(
1548 hScreen, os.O_RDWR | os.O_BINARY)
1549 kernel32.GetConsoleScreenBufferInfoEx(
1550 hScreen, ctypes.byref(new_info))
1551 new_info.dwSize = COORD(ncols, nrows)
1552 new_info.srWindow = wintypes.SMALL_RECT(
1553 Left=0, Top=0, Right=(ncols - 1),
1554 Bottom=(info.srWindow.Bottom - info.srWindow.Top))
1555 kernel32.SetConsoleScreenBufferInfoEx(
1556 hScreen, ctypes.byref(new_info))
1557 kernel32.SetConsoleWindowInfo(hScreen, True,
1558 ctypes.byref(new_info.srWindow))
1559 kernel32.FillConsoleOutputCharacterW(
1560 hScreen, u'\0', ncols * nrows, COORD(0,0), nwritten)
1561 kernel32.SetConsoleActiveScreenBuffer(hScreen)
1562 try:
1563 yield fd_screen
1564 finally:
1565 kernel32.SetConsoleScreenBufferInfoEx(
1566 hStdOut, ctypes.byref(info))
1567 kernel32.SetConsoleWindowInfo(hStdOut, True,
1568 ctypes.byref(info.srWindow))
1569 kernel32.SetConsoleActiveScreenBuffer(hStdOut)
1570 finally:
1571 if fd_screen is not None:
1572 os.close(fd_screen)
1573 else:
1574 kernel32.CloseHandle(hScreen)
1575
1576 def read_screen(fd):
1577 hScreen = msvcrt.get_osfhandle(fd)
1578 csbi = CONSOLE_SCREEN_BUFFER_INFOEX()
1579 kernel32.GetConsoleScreenBufferInfoEx(
1580 hScreen, ctypes.byref(csbi))
1581 ncols = csbi.dwSize.X
1582 pos = csbi.dwCursorPosition
1583 length = ncols * pos.Y + pos.X + 1
1584 buf = (ctypes.c_wchar * length)()
1585 n = (wintypes.DWORD * 1)()
1586 kernel32.ReadConsoleOutputCharacterW(
1587 hScreen, buf, length, COORD(0,0), n)
1588 lines = [buf[i:i+ncols].rstrip(u'\0')
1589 for i in range(0, n[0], ncols)]
1590 return u'\n'.join(lines)
1591
1592@unittest.skipUnless(sys.platform == "win32", "requires Windows")
1593class WindowsConsoleTest(DeviceTest):
1594 def test_unicode_output(self):
1595 """Test Unicode command line parameters and Unicode console window output.
1596
1597 Bug: https://issuetracker.google.com/issues/111972753
1598 """
1599 # If we don't have a console window, allocate one. This isn't necessary if we're already
1600 # being run from a console window, which is typical.
1601 with allocate_console() as allocated_console:
1602 # Create a temporary console buffer and switch to it. We could also pass a parameter of
1603 # ncols=len(unicode_string), but it causes the window to flash as it is resized and
1604 # likely unnecessary given the typical console window size.
1605 with console_screen(nrows=1000) as screen:
1606 unicode_string = u'로보카 폴리'
1607 # Run adb and allow it to detect that stdout is a console, not a pipe, by using
1608 # device.shell_popen() which does not use a pipe, unlike device.shell().
1609 process = self.device.shell_popen(['echo', '"' + unicode_string + '"'])
1610 process.wait()
1611 # Read what was written by adb to the temporary console buffer.
1612 console_output = read_screen(screen)
1613 self.assertEqual(unicode_string, console_output)
1614
1615
Josh Gao49e3c632015-12-09 11:26:11 -08001616def main():
1617 random.seed(0)
1618 if len(adb.get_devices()) > 0:
1619 suite = unittest.TestLoader().loadTestsFromName(__name__)
1620 unittest.TextTestRunner(verbosity=3).run(suite)
1621 else:
1622 print('Test suite must be run with attached devices')
1623
1624
1625if __name__ == '__main__':
1626 main()