Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Dan Albert | 8e1fdd7 | 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 | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 22 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 23 | import contextlib |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 24 | import os |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 25 | import random |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 26 | import select |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 27 | import socket |
| 28 | import struct |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 29 | import subprocess |
Josh Gao | 92cd59f | 2018-08-21 14:25:05 -0700 | [diff] [blame^] | 30 | import sys |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 31 | import threading |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 32 | import time |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 33 | import unittest |
| 34 | |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 35 | |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 36 | @contextlib.contextmanager |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 37 | def fake_adbd(protocol=socket.AF_INET, port=0): |
| 38 | """Creates a fake ADB daemon that just replies with a CNXN packet.""" |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 39 | |
| 40 | serversock = socket.socket(protocol, socket.SOCK_STREAM) |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 41 | serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 42 | if protocol == socket.AF_INET: |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 43 | serversock.bind(("127.0.0.1", port)) |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 44 | else: |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 45 | serversock.bind(("::1", port)) |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 46 | serversock.listen(1) |
| 47 | |
| 48 | # A pipe that is used to signal the thread that it should terminate. |
Josh Gao | 676f375d | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 49 | readsock, writesock = socket.socketpair() |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 50 | |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 51 | def _adb_packet(command: bytes, arg0: int, arg1: int, data: bytes) -> bytes: |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 52 | bin_command = struct.unpack("I", command)[0] |
| 53 | buf = struct.pack("IIIIII", bin_command, arg0, arg1, len(data), 0, |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 54 | bin_command ^ 0xffffffff) |
| 55 | buf += data |
| 56 | return buf |
| 57 | |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 58 | def _handle(sock): |
| 59 | with contextlib.closing(sock) as serversock: |
Josh Gao | 676f375d | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 60 | rlist = [readsock, serversock] |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 61 | cnxn_sent = {} |
| 62 | while True: |
| 63 | read_ready, _, _ = select.select(rlist, [], []) |
| 64 | for ready in read_ready: |
Josh Gao | 676f375d | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 65 | if ready == readsock: |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 66 | # Closure pipe |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 67 | for f in rlist: |
Josh Gao | 676f375d | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 68 | f.close() |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 69 | return |
| 70 | elif ready == serversock: |
| 71 | # Server socket |
| 72 | conn, _ = ready.accept() |
| 73 | rlist.append(conn) |
| 74 | else: |
| 75 | # Client socket |
| 76 | data = ready.recv(1024) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 77 | if not data or data.startswith(b"OPEN"): |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 78 | if ready in cnxn_sent: |
| 79 | del cnxn_sent[ready] |
| 80 | ready.shutdown(socket.SHUT_RDWR) |
| 81 | ready.close() |
| 82 | rlist.remove(ready) |
| 83 | continue |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 84 | if ready in cnxn_sent: |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 85 | continue |
| 86 | cnxn_sent[ready] = True |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 87 | ready.sendall(_adb_packet(b"CNXN", 0x01000001, 1024 * 1024, |
| 88 | b"device::ro.product.name=fakeadb")) |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 89 | |
| 90 | port = serversock.getsockname()[1] |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 91 | server_thread = threading.Thread(target=_handle, args=(serversock,)) |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 92 | server_thread.start() |
| 93 | |
| 94 | try: |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 95 | yield port, writesock |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 96 | finally: |
Josh Gao | 676f375d | 2018-08-07 16:07:25 -0700 | [diff] [blame] | 97 | writesock.close() |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 98 | server_thread.join() |
| 99 | |
| 100 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 101 | @contextlib.contextmanager |
| 102 | def adb_connect(unittest, serial): |
| 103 | """Context manager for an ADB connection. |
| 104 | |
| 105 | This automatically disconnects when done with the connection. |
| 106 | """ |
| 107 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 108 | output = subprocess.check_output(["adb", "connect", serial]) |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 109 | unittest.assertEqual(output.strip(), |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 110 | "connected to {}".format(serial).encode("utf8")) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 111 | |
| 112 | try: |
| 113 | yield |
| 114 | finally: |
| 115 | # Perform best-effort disconnection. Discard the output. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 116 | subprocess.Popen(["adb", "disconnect", serial], |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 117 | stdout=subprocess.PIPE, |
| 118 | stderr=subprocess.PIPE).communicate() |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 119 | |
| 120 | |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 121 | @contextlib.contextmanager |
| 122 | def adb_server(): |
| 123 | """Context manager for an ADB server. |
| 124 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 125 | This creates an ADB server and returns the port it's listening on. |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 126 | """ |
| 127 | |
| 128 | port = 5038 |
| 129 | # Kill any existing server on this non-default port. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 130 | subprocess.check_output(["adb", "-P", str(port), "kill-server"], |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 131 | stderr=subprocess.STDOUT) |
| 132 | read_pipe, write_pipe = os.pipe() |
Josh Gao | 92cd59f | 2018-08-21 14:25:05 -0700 | [diff] [blame^] | 133 | |
| 134 | if sys.platform == "win32": |
| 135 | import msvcrt |
| 136 | write_handle = msvcrt.get_osfhandle(write_pipe) |
| 137 | os.set_handle_inheritable(write_handle, True) |
| 138 | reply_fd = str(write_handle) |
| 139 | else: |
| 140 | os.set_inheritable(write_pipe, True) |
| 141 | reply_fd = str(write_pipe) |
| 142 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 143 | proc = subprocess.Popen(["adb", "-L", "tcp:localhost:{}".format(port), |
| 144 | "fork-server", "server", |
Josh Gao | 92cd59f | 2018-08-21 14:25:05 -0700 | [diff] [blame^] | 145 | "--reply-fd", reply_fd], close_fds=False) |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 146 | try: |
| 147 | os.close(write_pipe) |
| 148 | greeting = os.read(read_pipe, 1024) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 149 | assert greeting == b"OK\n", repr(greeting) |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 150 | yield port |
| 151 | finally: |
| 152 | proc.terminate() |
| 153 | proc.wait() |
| 154 | |
| 155 | |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 156 | class CommandlineTest(unittest.TestCase): |
| 157 | """Tests for the ADB commandline.""" |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 158 | |
| 159 | def test_help(self): |
| 160 | """Make sure we get _something_ out of help.""" |
| 161 | out = subprocess.check_output( |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 162 | ["adb", "help"], stderr=subprocess.STDOUT) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 163 | self.assertGreater(len(out), 0) |
| 164 | |
| 165 | def test_version(self): |
| 166 | """Get a version number out of the output of adb.""" |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 167 | lines = subprocess.check_output(["adb", "version"]).splitlines() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 168 | version_line = lines[0] |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 169 | self.assertRegex( |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 170 | version_line, rb"^Android Debug Bridge version \d+\.\d+\.\d+$") |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 171 | if len(lines) == 2: |
| 172 | # Newer versions of ADB have a second line of output for the |
| 173 | # version that includes a specific revision (git SHA). |
| 174 | revision_line = lines[1] |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 175 | self.assertRegex( |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 176 | revision_line, rb"^Revision [0-9a-f]{12}-android$") |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 177 | |
| 178 | def test_tcpip_error_messages(self): |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 179 | """Make sure 'adb tcpip' parsing is sane.""" |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 180 | proc = subprocess.Popen(["adb", "tcpip"], |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 181 | stdout=subprocess.PIPE, |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 182 | stderr=subprocess.STDOUT) |
| 183 | out, _ = proc.communicate() |
| 184 | self.assertEqual(1, proc.returncode) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 185 | self.assertIn(b"requires an argument", out) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 186 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 187 | proc = subprocess.Popen(["adb", "tcpip", "foo"], |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 188 | stdout=subprocess.PIPE, |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 189 | stderr=subprocess.STDOUT) |
| 190 | out, _ = proc.communicate() |
| 191 | self.assertEqual(1, proc.returncode) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 192 | self.assertIn(b"invalid port", out) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 193 | |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 194 | |
| 195 | class ServerTest(unittest.TestCase): |
| 196 | """Tests for the ADB server.""" |
| 197 | |
| 198 | @staticmethod |
| 199 | def _read_pipe_and_set_event(pipe, event): |
| 200 | """Reads a pipe until it is closed, then sets the event.""" |
| 201 | pipe.read() |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 202 | event.set() |
| 203 | |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 204 | def test_handle_inheritance(self): |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 205 | """Test that launch_server() does not inherit handles. |
| 206 | |
| 207 | launch_server() should not let the adb server inherit |
| 208 | stdin/stdout/stderr handles, which can cause callers of adb.exe to hang. |
| 209 | This test also runs fine on unix even though the impetus is an issue |
| 210 | unique to Windows. |
| 211 | """ |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 212 | # This test takes 5 seconds to run on Windows: if there is no adb server |
| 213 | # running on the the port used below, adb kill-server tries to make a |
| 214 | # TCP connection to a closed port and that takes 1 second on Windows; |
| 215 | # adb start-server does the same TCP connection which takes another |
| 216 | # second, and it waits 3 seconds after starting the server. |
| 217 | |
| 218 | # Start adb client with redirected stdin/stdout/stderr to check if it |
| 219 | # passes those redirections to the adb server that it starts. To do |
| 220 | # this, run an instance of the adb server on a non-default port so we |
| 221 | # don't conflict with a pre-existing adb server that may already be |
| 222 | # setup with adb TCP/emulator connections. If there is a pre-existing |
| 223 | # adb server, this also tests whether multiple instances of the adb |
| 224 | # server conflict on adb.log. |
| 225 | |
| 226 | port = 5038 |
| 227 | # Kill any existing server on this non-default port. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 228 | subprocess.check_output(["adb", "-P", str(port), "kill-server"], |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 229 | stderr=subprocess.STDOUT) |
| 230 | |
| 231 | try: |
| 232 | # Run the adb client and have it start the adb server. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 233 | proc = subprocess.Popen(["adb", "-P", str(port), "start-server"], |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 234 | stdin=subprocess.PIPE, |
| 235 | stdout=subprocess.PIPE, |
| 236 | stderr=subprocess.PIPE) |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 237 | |
| 238 | # Start threads that set events when stdout/stderr are closed. |
| 239 | stdout_event = threading.Event() |
| 240 | stdout_thread = threading.Thread( |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 241 | target=ServerTest._read_pipe_and_set_event, |
| 242 | args=(proc.stdout, stdout_event)) |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 243 | stdout_thread.start() |
| 244 | |
| 245 | stderr_event = threading.Event() |
| 246 | stderr_thread = threading.Thread( |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 247 | target=ServerTest._read_pipe_and_set_event, |
| 248 | args=(proc.stderr, stderr_event)) |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 249 | stderr_thread.start() |
| 250 | |
| 251 | # Wait for the adb client to finish. Once that has occurred, if |
| 252 | # stdin/stderr/stdout are still open, it must be open in the adb |
| 253 | # server. |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 254 | proc.wait() |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 255 | |
| 256 | # Try to write to stdin which we expect is closed. If it isn't |
| 257 | # closed, we should get an IOError. If we don't get an IOError, |
| 258 | # stdin must still be open in the adb server. The adb client is |
| 259 | # probably letting the adb server inherit stdin which would be |
| 260 | # wrong. |
| 261 | with self.assertRaises(IOError): |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 262 | proc.stdin.write(b"x") |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 263 | proc.stdin.flush() |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 264 | |
| 265 | # Wait a few seconds for stdout/stderr to be closed (in the success |
| 266 | # case, this won't wait at all). If there is a timeout, that means |
| 267 | # stdout/stderr were not closed and and they must be open in the adb |
| 268 | # server, suggesting that the adb client is letting the adb server |
| 269 | # inherit stdout/stderr which would be wrong. |
| 270 | self.assertTrue(stdout_event.wait(5), "adb stdout not closed") |
| 271 | self.assertTrue(stderr_event.wait(5), "adb stderr not closed") |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 272 | stdout_thread.join() |
| 273 | stderr_thread.join() |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 274 | finally: |
| 275 | # If we started a server, kill it. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 276 | subprocess.check_output(["adb", "-P", str(port), "kill-server"], |
Spencer Low | 1ce0608 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 277 | stderr=subprocess.STDOUT) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 278 | |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 279 | |
| 280 | class EmulatorTest(unittest.TestCase): |
| 281 | """Tests for the emulator connection.""" |
| 282 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 283 | def _reset_socket_on_close(self, sock): |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 284 | """Use SO_LINGER to cause TCP RST segment to be sent on socket close.""" |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 285 | # The linger structure is two shorts on Windows, but two ints on Unix. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 286 | linger_format = "hh" if os.name == "nt" else "ii" |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 287 | l_onoff = 1 |
| 288 | l_linger = 0 |
| 289 | |
| 290 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
| 291 | struct.pack(linger_format, l_onoff, l_linger)) |
| 292 | # Verify that we set the linger structure properly by retrieving it. |
| 293 | linger = sock.getsockopt(socket.SOL_SOCKET, socket.SO_LINGER, 16) |
| 294 | self.assertEqual((l_onoff, l_linger), |
| 295 | struct.unpack_from(linger_format, linger)) |
| 296 | |
| 297 | def test_emu_kill(self): |
| 298 | """Ensure that adb emu kill works. |
| 299 | |
| 300 | Bug: https://code.google.com/p/android/issues/detail?id=21021 |
| 301 | """ |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 302 | with contextlib.closing( |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 303 | socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as listener: |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 304 | # Use SO_REUSEADDR so subsequent runs of the test can grab the port |
| 305 | # even if it is in TIME_WAIT. |
| 306 | listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 307 | listener.bind(("127.0.0.1", 0)) |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 308 | listener.listen(4) |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 309 | port = listener.getsockname()[1] |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 310 | |
| 311 | # Now that listening has started, start adb emu kill, telling it to |
| 312 | # connect to our mock emulator. |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 313 | proc = subprocess.Popen( |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 314 | ["adb", "-s", "emulator-" + str(port), "emu", "kill"], |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 315 | stderr=subprocess.STDOUT) |
| 316 | |
| 317 | accepted_connection, addr = listener.accept() |
| 318 | with contextlib.closing(accepted_connection) as conn: |
| 319 | # If WSAECONNABORTED (10053) is raised by any socket calls, |
| 320 | # then adb probably isn't reading the data that we sent it. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 321 | conn.sendall(("Android Console: type 'help' for a list " |
| 322 | "of commands\r\n").encode("utf8")) |
| 323 | conn.sendall(b"OK\r\n") |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 324 | |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 325 | with contextlib.closing(conn.makefile()) as connf: |
| 326 | line = connf.readline() |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 327 | if line.startswith("auth"): |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 328 | # Ignore the first auth line. |
| 329 | line = connf.readline() |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 330 | self.assertEqual("kill\n", line) |
| 331 | self.assertEqual("quit\n", connf.readline()) |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 332 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 333 | conn.sendall(b"OK: killing emulator, bye bye\r\n") |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 334 | |
| 335 | # Use SO_LINGER to send TCP RST segment to test whether adb |
| 336 | # ignores WSAECONNRESET on Windows. This happens with the |
| 337 | # real emulator because it just calls exit() without closing |
| 338 | # the socket or calling shutdown(SD_SEND). At process |
| 339 | # termination, Windows sends a TCP RST segment for every |
| 340 | # open socket that shutdown(SD_SEND) wasn't used on. |
| 341 | self._reset_socket_on_close(conn) |
| 342 | |
| 343 | # Wait for adb to finish, so we can check return code. |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 344 | proc.communicate() |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 345 | |
| 346 | # If this fails, adb probably isn't ignoring WSAECONNRESET when |
| 347 | # reading the response from the adb emu kill command (on Windows). |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 348 | self.assertEqual(0, proc.returncode) |
| 349 | |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 350 | def test_emulator_connect(self): |
| 351 | """Ensure that the emulator can connect. |
| 352 | |
| 353 | Bug: http://b/78991667 |
| 354 | """ |
| 355 | with adb_server() as server_port: |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 356 | with fake_adbd() as (port, _): |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 357 | serial = "emulator-{}".format(port - 1) |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 358 | # Ensure that the emulator is not there. |
| 359 | try: |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 360 | subprocess.check_output(["adb", "-P", str(server_port), |
| 361 | "-s", serial, "get-state"], |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 362 | stderr=subprocess.STDOUT) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 363 | self.fail("Device should not be available") |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 364 | except subprocess.CalledProcessError as err: |
| 365 | self.assertEqual( |
| 366 | err.output.strip(), |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 367 | "error: device '{}' not found".format(serial).encode("utf8")) |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 368 | |
| 369 | # Let the ADB server know that the emulator has started. |
| 370 | with contextlib.closing( |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 371 | socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 372 | sock.connect(("localhost", server_port)) |
| 373 | command = "host:emulator:{}".format(port).encode("utf8") |
| 374 | sock.sendall(b"%04x%s" % (len(command), command)) |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 375 | |
| 376 | # Ensure the emulator is there. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 377 | subprocess.check_call(["adb", "-P", str(server_port), |
| 378 | "-s", serial, "wait-for-device"]) |
| 379 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 380 | "-s", serial, "get-state"]) |
| 381 | self.assertEqual(output.strip(), b"device") |
Luis Hector Chavez | 3255974 | 2018-05-02 10:47:01 -0700 | [diff] [blame] | 382 | |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 383 | |
| 384 | class ConnectionTest(unittest.TestCase): |
| 385 | """Tests for adb connect.""" |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 386 | |
Josh Gao | 78cc20f | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 387 | def test_connect_ipv4_ipv6(self): |
| 388 | """Ensure that `adb connect localhost:1234` will try both IPv4 and IPv6. |
| 389 | |
| 390 | Bug: http://b/30313466 |
| 391 | """ |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 392 | for protocol in (socket.AF_INET, socket.AF_INET6): |
| 393 | try: |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 394 | with fake_adbd(protocol=protocol) as (port, _): |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 395 | serial = "localhost:{}".format(port) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 396 | with adb_connect(self, serial): |
| 397 | pass |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 398 | except socket.error: |
| 399 | print("IPv6 not available, skipping") |
| 400 | continue |
Josh Gao | 78cc20f | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 401 | |
Luis Hector Chavez | 8b67c52 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 402 | def test_already_connected(self): |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 403 | """Ensure that an already-connected device stays connected.""" |
| 404 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 405 | with fake_adbd() as (port, _): |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 406 | serial = "localhost:{}".format(port) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 407 | with adb_connect(self, serial): |
| 408 | # b/31250450: this always returns 0 but probably shouldn't. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 409 | output = subprocess.check_output(["adb", "connect", serial]) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 410 | self.assertEqual( |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 411 | output.strip(), |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 412 | "already connected to {}".format(serial).encode("utf8")) |
Josh Gao | 78cc20f | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 413 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 414 | def test_reconnect(self): |
| 415 | """Ensure that a disconnected device reconnects.""" |
Josh Gao | 78cc20f | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 416 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 417 | with fake_adbd() as (port, _): |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 418 | serial = "localhost:{}".format(port) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 419 | with adb_connect(self, serial): |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 420 | output = subprocess.check_output(["adb", "-s", serial, |
| 421 | "get-state"]) |
| 422 | self.assertEqual(output.strip(), b"device") |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 423 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 424 | # This will fail. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 425 | proc = subprocess.Popen(["adb", "-s", serial, "shell", "true"], |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 426 | stdout=subprocess.PIPE, |
| 427 | stderr=subprocess.STDOUT) |
| 428 | output, _ = proc.communicate() |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 429 | self.assertEqual(output.strip(), b"error: closed") |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 430 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 431 | subprocess.check_call(["adb", "-s", serial, "wait-for-device"]) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 432 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 433 | output = subprocess.check_output(["adb", "-s", serial, |
| 434 | "get-state"]) |
| 435 | self.assertEqual(output.strip(), b"device") |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 436 | |
| 437 | # Once we explicitly kick a device, it won't attempt to |
| 438 | # reconnect. |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 439 | output = subprocess.check_output(["adb", "disconnect", serial]) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 440 | self.assertEqual( |
Josh Gao | 9b6522b | 2018-08-07 14:31:17 -0700 | [diff] [blame] | 441 | output.strip(), |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 442 | "disconnected {}".format(serial).encode("utf8")) |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 443 | try: |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 444 | subprocess.check_output(["adb", "-s", serial, "get-state"], |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 445 | stderr=subprocess.STDOUT) |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 446 | self.fail("Device should not be available") |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 447 | except subprocess.CalledProcessError as err: |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 448 | self.assertEqual( |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 449 | err.output.strip(), |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 450 | "error: device '{}' not found".format(serial).encode("utf8")) |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 451 | |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 452 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 453 | class DisconnectionTest(unittest.TestCase): |
| 454 | """Tests for adb disconnect.""" |
| 455 | |
| 456 | def test_disconnect(self): |
| 457 | """Ensure that `adb disconnect` takes effect immediately.""" |
| 458 | |
| 459 | def _devices(port): |
| 460 | output = subprocess.check_output(["adb", "-P", str(port), "devices"]) |
| 461 | return [x.split("\t") for x in output.decode("utf8").strip().splitlines()[1:]] |
| 462 | |
| 463 | with adb_server() as server_port: |
| 464 | with fake_adbd() as (port, sock): |
| 465 | device_name = "localhost:{}".format(port) |
| 466 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 467 | "connect", device_name]) |
| 468 | self.assertEqual(output.strip(), |
| 469 | "connected to {}".format(device_name).encode("utf8")) |
| 470 | |
| 471 | |
| 472 | self.assertEqual(_devices(server_port), [[device_name, "device"]]) |
| 473 | |
| 474 | # Send a deliberately malformed packet to make the device go offline. |
| 475 | packet = struct.pack("IIIIII", 0, 0, 0, 0, 0, 0) |
| 476 | sock.sendall(packet) |
| 477 | |
| 478 | # Wait a bit. |
| 479 | time.sleep(0.1) |
| 480 | |
| 481 | self.assertEqual(_devices(server_port), [[device_name, "offline"]]) |
| 482 | |
| 483 | # Disconnect the device. |
| 484 | output = subprocess.check_output(["adb", "-P", str(server_port), |
| 485 | "disconnect", device_name]) |
| 486 | |
| 487 | # Wait a bit. |
| 488 | time.sleep(0.1) |
| 489 | |
| 490 | self.assertEqual(_devices(server_port), []) |
| 491 | |
| 492 | |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 493 | def main(): |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 494 | """Main entrypoint.""" |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 495 | random.seed(0) |
Luis Hector Chavez | fbee0a9 | 2018-05-02 09:10:29 -0700 | [diff] [blame] | 496 | unittest.main(verbosity=3) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 497 | |
| 498 | |
Josh Gao | b361073 | 2018-08-08 13:08:08 -0700 | [diff] [blame] | 499 | if __name__ == "__main__": |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 500 | main() |