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