Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2015 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """Tests for the adb program itself. |
| 18 | |
| 19 | This differs from things in test_device.py in that there is no API for these |
| 20 | things. Most of these tests involve specific error messages or the help text. |
| 21 | """ |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 22 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 23 | import contextlib |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 24 | import os |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 25 | import random |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 26 | import select |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 27 | import socket |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 28 | import string |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 29 | import struct |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 30 | import subprocess |
Josh Gao | 6d7b965 | 2018-08-21 14:25:05 -0700 | [diff] [blame] | 31 | import sys |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 32 | import threading |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 33 | import time |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 34 | import unittest |
Josh Gao | be29491 | 2018-10-16 11:00:39 -0700 | [diff] [blame] | 35 | import warnings |
Joshua Duong | 5f63d11 | 2020-03-31 08:39:24 -0700 | [diff] [blame] | 36 | from importlib import util |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 37 | |
Josh Gao | e9fdf4a | 2020-02-27 14:29:38 -0800 | [diff] [blame] | 38 | def find_open_port(): |
| 39 | # Find an open port. |
| 40 | with socket.socket() as s: |
| 41 | s.bind(("localhost", 0)) |
| 42 | return s.getsockname()[1] |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 43 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 44 | @contextlib.contextmanager |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 45 | def fake_adbd(protocol=socket.AF_INET, port=0): |
| 46 | """Creates a fake ADB daemon that just replies with a CNXN packet.""" |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 47 | |
| 48 | serversock = socket.socket(protocol, socket.SOCK_STREAM) |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 49 | serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 50 | if protocol == socket.AF_INET: |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 51 | serversock.bind(("127.0.0.1", port)) |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 52 | else: |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 53 | serversock.bind(("::1", port)) |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 54 | serversock.listen(1) |
| 55 | |
| 56 | # A pipe that is used to signal the thread that it should terminate. |
Josh Gao | 6afbf79 | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 57 | readsock, writesock = socket.socketpair() |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 58 | |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 59 | def _adb_packet(command: bytes, arg0: int, arg1: int, data: bytes) -> bytes: |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 60 | bin_command = struct.unpack("I", command)[0] |
| 61 | buf = struct.pack("IIIIII", bin_command, arg0, arg1, len(data), 0, |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 62 | bin_command ^ 0xffffffff) |
| 63 | buf += data |
| 64 | return buf |
| 65 | |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 66 | def _handle(sock): |
| 67 | with contextlib.closing(sock) as serversock: |
Josh Gao | 6afbf79 | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 68 | rlist = [readsock, serversock] |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 69 | cnxn_sent = {} |
| 70 | while True: |
| 71 | read_ready, _, _ = select.select(rlist, [], []) |
| 72 | for ready in read_ready: |
Josh Gao | 6afbf79 | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 73 | if ready == readsock: |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 74 | # Closure pipe |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 75 | for f in rlist: |
Josh Gao | 6afbf79 | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 76 | f.close() |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 77 | return |
| 78 | elif ready == serversock: |
| 79 | # Server socket |
| 80 | conn, _ = ready.accept() |
| 81 | rlist.append(conn) |
| 82 | else: |
| 83 | # Client socket |
| 84 | data = ready.recv(1024) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 85 | if not data or data.startswith(b"OPEN"): |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 86 | if ready in cnxn_sent: |
| 87 | del cnxn_sent[ready] |
| 88 | ready.shutdown(socket.SHUT_RDWR) |
| 89 | ready.close() |
| 90 | rlist.remove(ready) |
| 91 | continue |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 92 | if ready in cnxn_sent: |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 93 | continue |
| 94 | cnxn_sent[ready] = True |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 95 | ready.sendall(_adb_packet(b"CNXN", 0x01000001, 1024 * 1024, |
| 96 | b"device::ro.product.name=fakeadb")) |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 97 | |
| 98 | port = serversock.getsockname()[1] |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 99 | server_thread = threading.Thread(target=_handle, args=(serversock,)) |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 100 | server_thread.start() |
| 101 | |
| 102 | try: |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 103 | yield port, writesock |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 104 | finally: |
Josh Gao | 6afbf79 | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 105 | writesock.close() |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 106 | server_thread.join() |
| 107 | |
| 108 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 109 | @contextlib.contextmanager |
| 110 | def adb_connect(unittest, serial): |
| 111 | """Context manager for an ADB connection. |
| 112 | |
| 113 | This automatically disconnects when done with the connection. |
| 114 | """ |
| 115 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 116 | output = subprocess.check_output(["adb", "connect", serial]) |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 117 | unittest.assertEqual(output.strip(), |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 118 | "connected to {}".format(serial).encode("utf8")) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 119 | |
| 120 | try: |
| 121 | yield |
| 122 | finally: |
| 123 | # Perform best-effort disconnection. Discard the output. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 124 | subprocess.Popen(["adb", "disconnect", serial], |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 125 | stdout=subprocess.PIPE, |
| 126 | stderr=subprocess.PIPE).communicate() |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 127 | |
| 128 | |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 129 | @contextlib.contextmanager |
| 130 | def adb_server(): |
| 131 | """Context manager for an ADB server. |
| 132 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 133 | This creates an ADB server and returns the port it's listening on. |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 134 | """ |
| 135 | |
Josh Gao | e9fdf4a | 2020-02-27 14:29:38 -0800 | [diff] [blame] | 136 | port = find_open_port() |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 137 | read_pipe, write_pipe = os.pipe() |
Josh Gao | 6d7b965 | 2018-08-21 14:25:05 -0700 | [diff] [blame] | 138 | |
| 139 | if sys.platform == "win32": |
| 140 | import msvcrt |
| 141 | write_handle = msvcrt.get_osfhandle(write_pipe) |
| 142 | os.set_handle_inheritable(write_handle, True) |
| 143 | reply_fd = str(write_handle) |
| 144 | else: |
| 145 | os.set_inheritable(write_pipe, True) |
| 146 | reply_fd = str(write_pipe) |
| 147 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 148 | proc = subprocess.Popen(["adb", "-L", "tcp:localhost:{}".format(port), |
| 149 | "fork-server", "server", |
Josh Gao | 6d7b965 | 2018-08-21 14:25:05 -0700 | [diff] [blame] | 150 | "--reply-fd", reply_fd], close_fds=False) |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 151 | try: |
| 152 | os.close(write_pipe) |
| 153 | greeting = os.read(read_pipe, 1024) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 154 | assert greeting == b"OK\n", repr(greeting) |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 155 | yield port |
| 156 | finally: |
| 157 | proc.terminate() |
| 158 | proc.wait() |
| 159 | |
| 160 | |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 161 | class CommandlineTest(unittest.TestCase): |
| 162 | """Tests for the ADB commandline.""" |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 163 | |
| 164 | def test_help(self): |
| 165 | """Make sure we get _something_ out of help.""" |
| 166 | out = subprocess.check_output( |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 167 | ["adb", "help"], stderr=subprocess.STDOUT) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 168 | self.assertGreater(len(out), 0) |
| 169 | |
| 170 | def test_version(self): |
| 171 | """Get a version number out of the output of adb.""" |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 172 | lines = subprocess.check_output(["adb", "version"]).splitlines() |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 173 | version_line = lines[0] |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 174 | self.assertRegex( |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 175 | version_line, rb"^Android Debug Bridge version \d+\.\d+\.\d+$") |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 176 | if len(lines) == 2: |
| 177 | # Newer versions of ADB have a second line of output for the |
| 178 | # version that includes a specific revision (git SHA). |
| 179 | revision_line = lines[1] |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 180 | self.assertRegex( |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 181 | revision_line, rb"^Revision [0-9a-f]{12}-android$") |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 182 | |
| 183 | def test_tcpip_error_messages(self): |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 184 | """Make sure 'adb tcpip' parsing is sane.""" |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 185 | proc = subprocess.Popen(["adb", "tcpip"], |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 186 | stdout=subprocess.PIPE, |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 187 | stderr=subprocess.STDOUT) |
| 188 | out, _ = proc.communicate() |
| 189 | self.assertEqual(1, proc.returncode) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 190 | self.assertIn(b"requires an argument", out) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 191 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 192 | proc = subprocess.Popen(["adb", "tcpip", "foo"], |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 193 | stdout=subprocess.PIPE, |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 194 | stderr=subprocess.STDOUT) |
| 195 | out, _ = proc.communicate() |
| 196 | self.assertEqual(1, proc.returncode) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 197 | self.assertIn(b"invalid port", out) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 198 | |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 199 | |
| 200 | class ServerTest(unittest.TestCase): |
| 201 | """Tests for the ADB server.""" |
| 202 | |
| 203 | @staticmethod |
| 204 | def _read_pipe_and_set_event(pipe, event): |
| 205 | """Reads a pipe until it is closed, then sets the event.""" |
| 206 | pipe.read() |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 207 | event.set() |
| 208 | |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 209 | def test_handle_inheritance(self): |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 210 | """Test that launch_server() does not inherit handles. |
| 211 | |
| 212 | launch_server() should not let the adb server inherit |
| 213 | stdin/stdout/stderr handles, which can cause callers of adb.exe to hang. |
| 214 | This test also runs fine on unix even though the impetus is an issue |
| 215 | unique to Windows. |
| 216 | """ |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 217 | # This test takes 5 seconds to run on Windows: if there is no adb server |
| 218 | # running on the the port used below, adb kill-server tries to make a |
| 219 | # TCP connection to a closed port and that takes 1 second on Windows; |
| 220 | # adb start-server does the same TCP connection which takes another |
| 221 | # second, and it waits 3 seconds after starting the server. |
| 222 | |
| 223 | # Start adb client with redirected stdin/stdout/stderr to check if it |
| 224 | # passes those redirections to the adb server that it starts. To do |
| 225 | # this, run an instance of the adb server on a non-default port so we |
| 226 | # don't conflict with a pre-existing adb server that may already be |
| 227 | # setup with adb TCP/emulator connections. If there is a pre-existing |
| 228 | # adb server, this also tests whether multiple instances of the adb |
| 229 | # server conflict on adb.log. |
| 230 | |
Josh Gao | e9fdf4a | 2020-02-27 14:29:38 -0800 | [diff] [blame] | 231 | port = find_open_port() |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 232 | |
| 233 | try: |
Josh Gao | be29491 | 2018-10-16 11:00:39 -0700 | [diff] [blame] | 234 | # We get warnings for unclosed files for the subprocess's pipes, |
| 235 | # and it's somewhat cumbersome to close them, so just ignore this. |
| 236 | warnings.simplefilter("ignore", ResourceWarning) |
| 237 | |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 238 | # Run the adb client and have it start the adb server. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 239 | proc = subprocess.Popen(["adb", "-P", str(port), "start-server"], |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 240 | stdin=subprocess.PIPE, |
| 241 | stdout=subprocess.PIPE, |
| 242 | stderr=subprocess.PIPE) |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 243 | |
| 244 | # Start threads that set events when stdout/stderr are closed. |
| 245 | stdout_event = threading.Event() |
| 246 | stdout_thread = threading.Thread( |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 247 | target=ServerTest._read_pipe_and_set_event, |
| 248 | args=(proc.stdout, stdout_event)) |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 249 | stdout_thread.start() |
| 250 | |
| 251 | stderr_event = threading.Event() |
| 252 | stderr_thread = threading.Thread( |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 253 | target=ServerTest._read_pipe_and_set_event, |
| 254 | args=(proc.stderr, stderr_event)) |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 255 | stderr_thread.start() |
| 256 | |
| 257 | # Wait for the adb client to finish. Once that has occurred, if |
| 258 | # stdin/stderr/stdout are still open, it must be open in the adb |
| 259 | # server. |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 260 | proc.wait() |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 261 | |
| 262 | # Try to write to stdin which we expect is closed. If it isn't |
| 263 | # closed, we should get an IOError. If we don't get an IOError, |
| 264 | # stdin must still be open in the adb server. The adb client is |
| 265 | # probably letting the adb server inherit stdin which would be |
| 266 | # wrong. |
| 267 | with self.assertRaises(IOError): |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 268 | proc.stdin.write(b"x") |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 269 | proc.stdin.flush() |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 270 | |
| 271 | # Wait a few seconds for stdout/stderr to be closed (in the success |
| 272 | # case, this won't wait at all). If there is a timeout, that means |
| 273 | # stdout/stderr were not closed and and they must be open in the adb |
| 274 | # server, suggesting that the adb client is letting the adb server |
| 275 | # inherit stdout/stderr which would be wrong. |
| 276 | self.assertTrue(stdout_event.wait(5), "adb stdout not closed") |
| 277 | self.assertTrue(stderr_event.wait(5), "adb stderr not closed") |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 278 | stdout_thread.join() |
| 279 | stderr_thread.join() |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 280 | finally: |
| 281 | # If we started a server, kill it. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 282 | subprocess.check_output(["adb", "-P", str(port), "kill-server"], |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 283 | stderr=subprocess.STDOUT) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 284 | |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 285 | @unittest.skipUnless( |
| 286 | os.name == "posix", |
| 287 | "adb doesn't yet support IPv6 on Windows", |
| 288 | ) |
Elliott Hughes | 0bab026 | 2021-03-03 16:23:05 +0000 | [diff] [blame] | 289 | def test_starts_on_ipv6_localhost(self): |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 290 | """ |
| 291 | Tests that the server can start up on ::1 and that it's accessible |
| 292 | """ |
Josh Gao | e9fdf4a | 2020-02-27 14:29:38 -0800 | [diff] [blame] | 293 | |
| 294 | server_port = find_open_port() |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 295 | try: |
| 296 | subprocess.check_output( |
| 297 | ["adb", "-L", "tcp:[::1]:{}".format(server_port), "server"], |
| 298 | stderr=subprocess.STDOUT, |
| 299 | ) |
| 300 | with fake_adbd() as (port, _): |
| 301 | with adb_connect(self, serial="localhost:{}".format(port)): |
| 302 | pass |
| 303 | finally: |
| 304 | # If we started a server, kill it. |
| 305 | subprocess.check_output( |
| 306 | ["adb", "-P", str(server_port), "kill-server"], |
| 307 | stderr=subprocess.STDOUT, |
| 308 | ) |
| 309 | |
| 310 | |
| 311 | |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 312 | |
| 313 | class EmulatorTest(unittest.TestCase): |
| 314 | """Tests for the emulator connection.""" |
| 315 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 316 | def _reset_socket_on_close(self, sock): |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 317 | """Use SO_LINGER to cause TCP RST segment to be sent on socket close.""" |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 318 | # The linger structure is two shorts on Windows, but two ints on Unix. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 319 | linger_format = "hh" if os.name == "nt" else "ii" |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 320 | l_onoff = 1 |
| 321 | l_linger = 0 |
| 322 | |
| 323 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
| 324 | struct.pack(linger_format, l_onoff, l_linger)) |
| 325 | # Verify that we set the linger structure properly by retrieving it. |
| 326 | linger = sock.getsockopt(socket.SOL_SOCKET, socket.SO_LINGER, 16) |
| 327 | self.assertEqual((l_onoff, l_linger), |
| 328 | struct.unpack_from(linger_format, linger)) |
| 329 | |
| 330 | def test_emu_kill(self): |
| 331 | """Ensure that adb emu kill works. |
| 332 | |
| 333 | Bug: https://code.google.com/p/android/issues/detail?id=21021 |
| 334 | """ |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 335 | with contextlib.closing( |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 336 | socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as listener: |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 337 | # Use SO_REUSEADDR so subsequent runs of the test can grab the port |
| 338 | # even if it is in TIME_WAIT. |
| 339 | listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 340 | listener.bind(("127.0.0.1", 0)) |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 341 | listener.listen(4) |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 342 | port = listener.getsockname()[1] |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 343 | |
| 344 | # Now that listening has started, start adb emu kill, telling it to |
| 345 | # connect to our mock emulator. |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 346 | proc = subprocess.Popen( |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 347 | ["adb", "-s", "emulator-" + str(port), "emu", "kill"], |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 348 | stderr=subprocess.STDOUT) |
| 349 | |
| 350 | accepted_connection, addr = listener.accept() |
| 351 | with contextlib.closing(accepted_connection) as conn: |
| 352 | # If WSAECONNABORTED (10053) is raised by any socket calls, |
| 353 | # then adb probably isn't reading the data that we sent it. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 354 | conn.sendall(("Android Console: type 'help' for a list " |
| 355 | "of commands\r\n").encode("utf8")) |
| 356 | conn.sendall(b"OK\r\n") |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 357 | |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 358 | with contextlib.closing(conn.makefile()) as connf: |
| 359 | line = connf.readline() |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 360 | if line.startswith("auth"): |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 361 | # Ignore the first auth line. |
| 362 | line = connf.readline() |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 363 | self.assertEqual("kill\n", line) |
| 364 | self.assertEqual("quit\n", connf.readline()) |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 365 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 366 | conn.sendall(b"OK: killing emulator, bye bye\r\n") |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 367 | |
| 368 | # Use SO_LINGER to send TCP RST segment to test whether adb |
| 369 | # ignores WSAECONNRESET on Windows. This happens with the |
| 370 | # real emulator because it just calls exit() without closing |
| 371 | # the socket or calling shutdown(SD_SEND). At process |
| 372 | # termination, Windows sends a TCP RST segment for every |
| 373 | # open socket that shutdown(SD_SEND) wasn't used on. |
| 374 | self._reset_socket_on_close(conn) |
| 375 | |
| 376 | # Wait for adb to finish, so we can check return code. |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 377 | proc.communicate() |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 378 | |
| 379 | # If this fails, adb probably isn't ignoring WSAECONNRESET when |
| 380 | # reading the response from the adb emu kill command (on Windows). |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 381 | self.assertEqual(0, proc.returncode) |
| 382 | |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 383 | def test_emulator_connect(self): |
| 384 | """Ensure that the emulator can connect. |
| 385 | |
| 386 | Bug: http://b/78991667 |
| 387 | """ |
| 388 | with adb_server() as server_port: |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 389 | with fake_adbd() as (port, _): |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 390 | serial = "emulator-{}".format(port - 1) |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 391 | # Ensure that the emulator is not there. |
| 392 | try: |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 393 | subprocess.check_output(["adb", "-P", str(server_port), |
| 394 | "-s", serial, "get-state"], |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 395 | stderr=subprocess.STDOUT) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 396 | self.fail("Device should not be available") |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 397 | except subprocess.CalledProcessError as err: |
| 398 | self.assertEqual( |
| 399 | err.output.strip(), |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 400 | "error: device '{}' not found".format(serial).encode("utf8")) |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 401 | |
| 402 | # Let the ADB server know that the emulator has started. |
| 403 | with contextlib.closing( |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 404 | socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 405 | sock.connect(("localhost", server_port)) |
| 406 | command = "host:emulator:{}".format(port).encode("utf8") |
| 407 | sock.sendall(b"%04x%s" % (len(command), command)) |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 408 | |
| 409 | # Ensure the emulator is there. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 410 | subprocess.check_call(["adb", "-P", str(server_port), |
| 411 | "-s", serial, "wait-for-device"]) |
| 412 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 413 | "-s", serial, "get-state"]) |
| 414 | self.assertEqual(output.strip(), b"device") |
Luis Hector Chavez | a452f0e | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 415 | |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 416 | |
| 417 | class ConnectionTest(unittest.TestCase): |
| 418 | """Tests for adb connect.""" |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 419 | |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 420 | def test_connect_ipv4_ipv6(self): |
| 421 | """Ensure that `adb connect localhost:1234` will try both IPv4 and IPv6. |
| 422 | |
| 423 | Bug: http://b/30313466 |
| 424 | """ |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 425 | for protocol in (socket.AF_INET, socket.AF_INET6): |
| 426 | try: |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 427 | with fake_adbd(protocol=protocol) as (port, _): |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 428 | serial = "localhost:{}".format(port) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 429 | with adb_connect(self, serial): |
| 430 | pass |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 431 | except socket.error: |
| 432 | print("IPv6 not available, skipping") |
| 433 | continue |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 434 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 435 | def test_already_connected(self): |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 436 | """Ensure that an already-connected device stays connected.""" |
| 437 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 438 | with fake_adbd() as (port, _): |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 439 | serial = "localhost:{}".format(port) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 440 | with adb_connect(self, serial): |
| 441 | # b/31250450: this always returns 0 but probably shouldn't. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 442 | output = subprocess.check_output(["adb", "connect", serial]) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 443 | self.assertEqual( |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 444 | output.strip(), |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 445 | "already connected to {}".format(serial).encode("utf8")) |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 446 | |
Julien Desprez | 0e8a0a0 | 2019-02-20 09:42:49 -0800 | [diff] [blame] | 447 | @unittest.skip("Currently failing b/123247844") |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 448 | def test_reconnect(self): |
| 449 | """Ensure that a disconnected device reconnects.""" |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 450 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 451 | with fake_adbd() as (port, _): |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 452 | serial = "localhost:{}".format(port) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 453 | with adb_connect(self, serial): |
Josh Gao | 2645b0a | 2019-02-07 17:53:29 -0800 | [diff] [blame] | 454 | # Wait a bit to give adb some time to connect. |
| 455 | time.sleep(0.25) |
| 456 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 457 | output = subprocess.check_output(["adb", "-s", serial, |
| 458 | "get-state"]) |
| 459 | self.assertEqual(output.strip(), b"device") |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 460 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 461 | # This will fail. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 462 | proc = subprocess.Popen(["adb", "-s", serial, "shell", "true"], |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 463 | stdout=subprocess.PIPE, |
| 464 | stderr=subprocess.STDOUT) |
| 465 | output, _ = proc.communicate() |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 466 | self.assertEqual(output.strip(), b"error: closed") |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 467 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 468 | subprocess.check_call(["adb", "-s", serial, "wait-for-device"]) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 469 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 470 | output = subprocess.check_output(["adb", "-s", serial, |
| 471 | "get-state"]) |
| 472 | self.assertEqual(output.strip(), b"device") |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 473 | |
| 474 | # Once we explicitly kick a device, it won't attempt to |
| 475 | # reconnect. |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 476 | output = subprocess.check_output(["adb", "disconnect", serial]) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 477 | self.assertEqual( |
Josh Gao | b1df00e | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 478 | output.strip(), |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 479 | "disconnected {}".format(serial).encode("utf8")) |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 480 | try: |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 481 | subprocess.check_output(["adb", "-s", serial, "get-state"], |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 482 | stderr=subprocess.STDOUT) |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 483 | self.fail("Device should not be available") |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 484 | except subprocess.CalledProcessError as err: |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 485 | self.assertEqual( |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 486 | err.output.strip(), |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 487 | "error: device '{}' not found".format(serial).encode("utf8")) |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 488 | |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 489 | |
Josh Gao | 13cb8c0 | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 490 | class DisconnectionTest(unittest.TestCase): |
| 491 | """Tests for adb disconnect.""" |
| 492 | |
| 493 | def test_disconnect(self): |
| 494 | """Ensure that `adb disconnect` takes effect immediately.""" |
| 495 | |
| 496 | def _devices(port): |
| 497 | output = subprocess.check_output(["adb", "-P", str(port), "devices"]) |
| 498 | return [x.split("\t") for x in output.decode("utf8").strip().splitlines()[1:]] |
| 499 | |
| 500 | with adb_server() as server_port: |
| 501 | with fake_adbd() as (port, sock): |
| 502 | device_name = "localhost:{}".format(port) |
| 503 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 504 | "connect", device_name]) |
| 505 | self.assertEqual(output.strip(), |
| 506 | "connected to {}".format(device_name).encode("utf8")) |
| 507 | |
| 508 | |
| 509 | self.assertEqual(_devices(server_port), [[device_name, "device"]]) |
| 510 | |
| 511 | # Send a deliberately malformed packet to make the device go offline. |
| 512 | packet = struct.pack("IIIIII", 0, 0, 0, 0, 0, 0) |
| 513 | sock.sendall(packet) |
| 514 | |
| 515 | # Wait a bit. |
| 516 | time.sleep(0.1) |
| 517 | |
| 518 | self.assertEqual(_devices(server_port), [[device_name, "offline"]]) |
| 519 | |
| 520 | # Disconnect the device. |
| 521 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 522 | "disconnect", device_name]) |
| 523 | |
| 524 | # Wait a bit. |
| 525 | time.sleep(0.1) |
| 526 | |
| 527 | self.assertEqual(_devices(server_port), []) |
| 528 | |
| 529 | |
Spencer Low | ebd8d32 | 2018-08-31 19:49:46 -0700 | [diff] [blame] | 530 | @unittest.skipUnless(sys.platform == "win32", "requires Windows") |
| 531 | class PowerTest(unittest.TestCase): |
| 532 | def test_resume_usb_kick(self): |
| 533 | """Resuming from sleep/hibernate should kick USB devices.""" |
| 534 | try: |
| 535 | usb_serial = subprocess.check_output(["adb", "-d", "get-serialno"]).strip() |
| 536 | except subprocess.CalledProcessError: |
| 537 | # If there are multiple USB devices, we don't have a way to check whether the selected |
| 538 | # device is USB. |
| 539 | raise unittest.SkipTest('requires single USB device') |
| 540 | |
| 541 | try: |
| 542 | serial = subprocess.check_output(["adb", "get-serialno"]).strip() |
| 543 | except subprocess.CalledProcessError: |
| 544 | # Did you forget to select a device with $ANDROID_SERIAL? |
| 545 | raise unittest.SkipTest('requires $ANDROID_SERIAL set to a USB device') |
| 546 | |
| 547 | # Test only works with USB devices because adb _power_notification_thread does not kick |
| 548 | # non-USB devices on resume event. |
| 549 | if serial != usb_serial: |
| 550 | raise unittest.SkipTest('requires USB device') |
| 551 | |
| 552 | # Run an adb shell command in the background that takes a while to complete. |
| 553 | proc = subprocess.Popen(['adb', 'shell', 'sleep', '5']) |
| 554 | |
| 555 | # Wait for startup of adb server's _power_notification_thread. |
| 556 | time.sleep(0.1) |
| 557 | |
| 558 | # Simulate resuming from sleep/hibernation by sending Windows message. |
| 559 | import ctypes |
| 560 | from ctypes import wintypes |
| 561 | HWND_BROADCAST = 0xffff |
| 562 | WM_POWERBROADCAST = 0x218 |
| 563 | PBT_APMRESUMEAUTOMATIC = 0x12 |
| 564 | |
| 565 | PostMessageW = ctypes.windll.user32.PostMessageW |
| 566 | PostMessageW.restype = wintypes.BOOL |
| 567 | PostMessageW.argtypes = (wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM) |
| 568 | result = PostMessageW(HWND_BROADCAST, WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC, 0) |
| 569 | if not result: |
| 570 | raise ctypes.WinError() |
| 571 | |
| 572 | # Wait for connection to adb shell to be broken by _power_notification_thread detecting the |
| 573 | # Windows message. |
| 574 | start = time.time() |
| 575 | proc.wait() |
| 576 | end = time.time() |
| 577 | |
| 578 | # If the power event was detected, the adb shell command should be broken very quickly. |
| 579 | self.assertLess(end - start, 2) |
| 580 | |
Joshua Duong | e22e161 | 2020-03-31 10:58:50 -0700 | [diff] [blame] | 581 | """Use 'adb mdns check' to see if mdns discovery is available.""" |
| 582 | def is_adb_mdns_available(): |
| 583 | with adb_server() as server_port: |
| 584 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 585 | "mdns", "check"]).strip() |
| 586 | return output.startswith(b"mdns daemon version") |
| 587 | |
Joshua Duong | 5f63d11 | 2020-03-31 08:39:24 -0700 | [diff] [blame] | 588 | """Check if we have zeroconf python library installed""" |
| 589 | def is_zeroconf_installed(): |
| 590 | zeroconf_spec = util.find_spec("zeroconf") |
| 591 | return zeroconf_spec is not None |
| 592 | |
| 593 | @contextlib.contextmanager |
| 594 | def zeroconf_context(ipversion): |
| 595 | from zeroconf import Zeroconf |
| 596 | """Context manager for a zeroconf instance |
| 597 | |
| 598 | This creates a zeroconf instance and returns it. |
| 599 | """ |
| 600 | |
| 601 | try: |
| 602 | zeroconf = Zeroconf(ip_version=ipversion) |
| 603 | yield zeroconf |
| 604 | finally: |
| 605 | zeroconf.close() |
| 606 | |
| 607 | @contextlib.contextmanager |
| 608 | def zeroconf_register_service(zeroconf_ctx, info): |
| 609 | """Context manager for a zeroconf service |
| 610 | |
| 611 | Registers a service and unregisters it on cleanup. Returns the ServiceInfo |
| 612 | supplied. |
| 613 | """ |
| 614 | |
| 615 | try: |
| 616 | zeroconf_ctx.register_service(info) |
| 617 | yield info |
| 618 | finally: |
| 619 | zeroconf_ctx.unregister_service(info) |
| 620 | |
Joshua Duong | da53707 | 2020-06-29 16:27:03 -0700 | [diff] [blame] | 621 | @contextlib.contextmanager |
| 622 | def zeroconf_register_services(zeroconf_ctx, infos): |
| 623 | """Context manager for multiple zeroconf services |
| 624 | |
| 625 | Registers all services given and unregisters all on cleanup. Returns the ServiceInfo |
| 626 | list supplied. |
| 627 | """ |
| 628 | |
| 629 | try: |
| 630 | for info in infos: |
| 631 | zeroconf_ctx.register_service(info) |
| 632 | yield infos |
| 633 | finally: |
| 634 | for info in infos: |
| 635 | zeroconf_ctx.unregister_service(info) |
| 636 | |
Joshua Duong | 5f63d11 | 2020-03-31 08:39:24 -0700 | [diff] [blame] | 637 | """Should match the service names listed in adb_mdns.h""" |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 638 | class MdnsTest: |
Joshua Duong | e22e161 | 2020-03-31 10:58:50 -0700 | [diff] [blame] | 639 | """Tests for adb mdns.""" |
Joshua Duong | da53707 | 2020-06-29 16:27:03 -0700 | [diff] [blame] | 640 | @staticmethod |
| 641 | def _mdns_services(port): |
| 642 | output = subprocess.check_output(["adb", "-P", str(port), "mdns", "services"]) |
| 643 | return [x.split("\t") for x in output.decode("utf8").strip().splitlines()[1:]] |
| 644 | |
| 645 | @staticmethod |
| 646 | def _devices(port): |
| 647 | output = subprocess.check_output(["adb", "-P", str(port), "devices"]) |
| 648 | return [x.split("\t") for x in output.decode("utf8").strip().splitlines()[1:]] |
| 649 | |
Joshua Duong | 5f63d11 | 2020-03-31 08:39:24 -0700 | [diff] [blame] | 650 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 651 | class Base(unittest.TestCase): |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 652 | @contextlib.contextmanager |
| 653 | def _adb_mdns_connect(self, server_port, mdns_instance, serial, should_connect): |
| 654 | """Context manager for an ADB connection. |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 655 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 656 | This automatically disconnects when done with the connection. |
| 657 | """ |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 658 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 659 | output = subprocess.check_output(["adb", "-P", str(server_port), "connect", mdns_instance]) |
| 660 | if should_connect: |
| 661 | self.assertEqual(output.strip(), "connected to {}".format(serial).encode("utf8")) |
| 662 | else: |
| 663 | self.assertTrue(output.startswith("failed to resolve host: '{}'" |
| 664 | .format(mdns_instance).encode("utf8"))) |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 665 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 666 | try: |
| 667 | yield |
| 668 | finally: |
| 669 | # Perform best-effort disconnection. Discard the output. |
| 670 | subprocess.Popen(["adb", "disconnect", serial], |
| 671 | stdout=subprocess.PIPE, |
| 672 | stderr=subprocess.PIPE).communicate() |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 673 | |
| 674 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 675 | @unittest.skipIf(not is_zeroconf_installed(), "zeroconf library not installed") |
| 676 | def test_mdns_services_register_unregister(self): |
| 677 | """Ensure that `adb mdns services` correctly adds and removes a service |
| 678 | """ |
| 679 | from zeroconf import IPVersion, ServiceInfo |
Joshua Duong | 5f63d11 | 2020-03-31 08:39:24 -0700 | [diff] [blame] | 680 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 681 | with adb_server() as server_port: |
| 682 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 683 | "mdns", "services"]).strip() |
| 684 | self.assertTrue(output.startswith(b"List of discovered mdns services")) |
Joshua Duong | 5f63d11 | 2020-03-31 08:39:24 -0700 | [diff] [blame] | 685 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 686 | """TODO(joshuaduong): Add ipv6 tests once we have it working in adb""" |
| 687 | """Register/Unregister a service""" |
| 688 | with zeroconf_context(IPVersion.V4Only) as zc: |
| 689 | serv_instance = "my_fake_test_service" |
| 690 | serv_type = "_" + self.service_name + "._tcp." |
| 691 | serv_ipaddr = socket.inet_aton("1.2.3.4") |
| 692 | serv_port = 12345 |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 693 | service_info = ServiceInfo( |
| 694 | serv_type + "local.", |
| 695 | name=serv_instance + "." + serv_type + "local.", |
| 696 | addresses=[serv_ipaddr], |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 697 | port=serv_port) |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 698 | with zeroconf_register_service(zc, service_info) as info: |
| 699 | """Give adb some time to register the service""" |
| 700 | time.sleep(1) |
| 701 | self.assertTrue(any((serv_instance in line and serv_type in line) |
| 702 | for line in MdnsTest._mdns_services(server_port))) |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 703 | |
| 704 | """Give adb some time to unregister the service""" |
| 705 | time.sleep(1) |
| 706 | self.assertFalse(any((serv_instance in line and serv_type in line) |
| 707 | for line in MdnsTest._mdns_services(server_port))) |
Spencer Low | ebd8d32 | 2018-08-31 19:49:46 -0700 | [diff] [blame] | 708 | |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 709 | @unittest.skipIf(not is_zeroconf_installed(), "zeroconf library not installed") |
Joshua Duong | da53707 | 2020-06-29 16:27:03 -0700 | [diff] [blame] | 710 | def test_mdns_services_register_unregister_multiple(self): |
| 711 | """Ensure that `adb mdns services` correctly adds and removes multiple services |
| 712 | """ |
| 713 | from zeroconf import IPVersion, ServiceInfo |
| 714 | |
| 715 | with adb_server() as server_port: |
| 716 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 717 | "mdns", "services"]).strip() |
| 718 | self.assertTrue(output.startswith(b"List of discovered mdns services")) |
| 719 | |
| 720 | """TODO(joshuaduong): Add ipv6 tests once we have it working in adb""" |
| 721 | """Register/Unregister a service""" |
| 722 | with zeroconf_context(IPVersion.V4Only) as zc: |
| 723 | srvs = { |
| 724 | 'mdns_name': ["testservice0", "testservice1", "testservice2"], |
| 725 | 'mdns_type': "_" + self.service_name + "._tcp.", |
| 726 | 'ipaddr': [ |
| 727 | socket.inet_aton("192.168.0.1"), |
| 728 | socket.inet_aton("10.0.0.255"), |
| 729 | socket.inet_aton("172.16.1.100")], |
| 730 | 'port': [10000, 20000, 65535]} |
| 731 | srv_infos = [] |
| 732 | for i in range(len(srvs['mdns_name'])): |
| 733 | srv_infos.append(ServiceInfo( |
| 734 | srvs['mdns_type'] + "local.", |
| 735 | name=srvs['mdns_name'][i] + "." + srvs['mdns_type'] + "local.", |
| 736 | addresses=[srvs['ipaddr'][i]], |
| 737 | port=srvs['port'][i])) |
| 738 | |
| 739 | """ Register all devices, then unregister""" |
| 740 | with zeroconf_register_services(zc, srv_infos) as infos: |
| 741 | """Give adb some time to register the service""" |
| 742 | time.sleep(1) |
| 743 | for i in range(len(srvs['mdns_name'])): |
| 744 | self.assertTrue(any((srvs['mdns_name'][i] in line and srvs['mdns_type'] in line) |
| 745 | for line in MdnsTest._mdns_services(server_port))) |
| 746 | |
| 747 | """Give adb some time to unregister the service""" |
| 748 | time.sleep(1) |
| 749 | for i in range(len(srvs['mdns_name'])): |
| 750 | self.assertFalse(any((srvs['mdns_name'][i] in line and srvs['mdns_type'] in line) |
| 751 | for line in MdnsTest._mdns_services(server_port))) |
| 752 | |
| 753 | @unittest.skipIf(not is_zeroconf_installed(), "zeroconf library not installed") |
Josh Gao | 5ece8e4 | 2020-06-02 13:14:45 -0700 | [diff] [blame] | 754 | def test_mdns_connect(self): |
| 755 | """Ensure that `adb connect` by mdns instance name works (for non-pairing services) |
| 756 | """ |
| 757 | from zeroconf import IPVersion, ServiceInfo |
| 758 | |
| 759 | with adb_server() as server_port: |
| 760 | with zeroconf_context(IPVersion.V4Only) as zc: |
| 761 | serv_instance = "fakeadbd-" + ''.join( |
| 762 | random.choice(string.ascii_letters) for i in range(4)) |
| 763 | serv_type = "_" + self.service_name + "._tcp." |
| 764 | serv_ipaddr = socket.inet_aton("127.0.0.1") |
| 765 | should_connect = self.service_name != "adb-tls-pairing" |
| 766 | with fake_adbd() as (port, _): |
| 767 | service_info = ServiceInfo( |
| 768 | serv_type + "local.", |
| 769 | name=serv_instance + "." + serv_type + "local.", |
| 770 | addresses=[serv_ipaddr], |
| 771 | port=port) |
| 772 | with zeroconf_register_service(zc, service_info) as info: |
| 773 | """Give adb some time to register the service""" |
| 774 | time.sleep(1) |
| 775 | self.assertTrue(any((serv_instance in line and serv_type in line) |
| 776 | for line in MdnsTest._mdns_services(server_port))) |
| 777 | full_name = '.'.join([serv_instance, serv_type]) |
| 778 | with self._adb_mdns_connect(server_port, serv_instance, full_name, |
| 779 | should_connect): |
| 780 | if should_connect: |
| 781 | self.assertEqual(MdnsTest._devices(server_port), |
| 782 | [[full_name, "device"]]) |
| 783 | |
| 784 | """Give adb some time to unregister the service""" |
| 785 | time.sleep(1) |
| 786 | self.assertFalse(any((serv_instance in line and serv_type in line) |
| 787 | for line in MdnsTest._mdns_services(server_port))) |
| 788 | |
| 789 | |
| 790 | @unittest.skipIf(not is_adb_mdns_available(), "mdns feature not available") |
| 791 | class MdnsTestAdb(MdnsTest.Base): |
| 792 | service_name = "adb" |
| 793 | |
| 794 | |
| 795 | @unittest.skipIf(not is_adb_mdns_available(), "mdns feature not available") |
| 796 | class MdnsTestAdbTlsConnect(MdnsTest.Base): |
| 797 | service_name = "adb-tls-connect" |
| 798 | |
| 799 | |
| 800 | @unittest.skipIf(not is_adb_mdns_available(), "mdns feature not available") |
| 801 | class MdnsTestAdbTlsPairing(MdnsTest.Base): |
| 802 | service_name = "adb-tls-pairing" |
| 803 | |
| 804 | |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 805 | def main(): |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 806 | """Main entrypoint.""" |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 807 | random.seed(0) |
Luis Hector Chavez | 947b213 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 808 | unittest.main(verbosity=3) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 809 | |
| 810 | |
Josh Gao | 4db70c5 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 811 | if __name__ == "__main__": |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 812 | main() |