Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | """ |
| 22 | from __future__ import print_function |
| 23 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 24 | import binascii |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 25 | import contextlib |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 26 | import os |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 27 | import random |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 28 | import select |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 29 | import socket |
| 30 | import struct |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 31 | import subprocess |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 32 | import threading |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 33 | import unittest |
| 34 | |
| 35 | import adb |
| 36 | |
| 37 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 38 | @contextlib.contextmanager |
| 39 | def fake_adb_server(protocol=socket.AF_INET, port=0): |
| 40 | """Creates a fake ADB server that just replies with a CNXN packet.""" |
| 41 | |
| 42 | serversock = socket.socket(protocol, socket.SOCK_STREAM) |
| 43 | if protocol == socket.AF_INET: |
| 44 | serversock.bind(('127.0.0.1', port)) |
| 45 | else: |
| 46 | serversock.bind(('::1', port)) |
| 47 | serversock.listen(1) |
| 48 | |
| 49 | # A pipe that is used to signal the thread that it should terminate. |
| 50 | readpipe, writepipe = os.pipe() |
| 51 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 52 | def _adb_packet(command, arg0, arg1, data): |
| 53 | bin_command = struct.unpack('I', command)[0] |
| 54 | buf = struct.pack('IIIIII', bin_command, arg0, arg1, len(data), 0, |
| 55 | bin_command ^ 0xffffffff) |
| 56 | buf += data |
| 57 | return buf |
| 58 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 59 | def _handle(): |
| 60 | rlist = [readpipe, serversock] |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 61 | cnxn_sent = {} |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 62 | while True: |
| 63 | ready, _, _ = select.select(rlist, [], []) |
| 64 | for r in ready: |
| 65 | if r == readpipe: |
| 66 | # Closure pipe |
| 67 | os.close(r) |
| 68 | serversock.shutdown(socket.SHUT_RDWR) |
| 69 | serversock.close() |
| 70 | return |
| 71 | elif r == serversock: |
| 72 | # Server socket |
| 73 | conn, _ = r.accept() |
| 74 | rlist.append(conn) |
| 75 | else: |
| 76 | # Client socket |
| 77 | data = r.recv(1024) |
| 78 | if not data: |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 79 | if r in cnxn_sent: |
| 80 | del cnxn_sent[r] |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 81 | rlist.remove(r) |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 82 | continue |
| 83 | if r in cnxn_sent: |
| 84 | continue |
| 85 | cnxn_sent[r] = True |
| 86 | r.sendall(_adb_packet('CNXN', 0x01000001, 1024 * 1024, |
| 87 | 'device::ro.product.name=fakeadb')) |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 88 | |
| 89 | port = serversock.getsockname()[1] |
| 90 | server_thread = threading.Thread(target=_handle) |
| 91 | server_thread.start() |
| 92 | |
| 93 | try: |
| 94 | yield port |
| 95 | finally: |
| 96 | os.close(writepipe) |
| 97 | server_thread.join() |
| 98 | |
| 99 | |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 100 | class NonApiTest(unittest.TestCase): |
| 101 | """Tests for ADB that aren't a part of the AndroidDevice API.""" |
| 102 | |
| 103 | def test_help(self): |
| 104 | """Make sure we get _something_ out of help.""" |
| 105 | out = subprocess.check_output( |
| 106 | ['adb', 'help'], stderr=subprocess.STDOUT) |
| 107 | self.assertGreater(len(out), 0) |
| 108 | |
| 109 | def test_version(self): |
| 110 | """Get a version number out of the output of adb.""" |
| 111 | lines = subprocess.check_output(['adb', 'version']).splitlines() |
| 112 | version_line = lines[0] |
| 113 | self.assertRegexpMatches( |
| 114 | version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$') |
| 115 | if len(lines) == 2: |
| 116 | # Newer versions of ADB have a second line of output for the |
| 117 | # version that includes a specific revision (git SHA). |
| 118 | revision_line = lines[1] |
| 119 | self.assertRegexpMatches( |
| 120 | revision_line, r'^Revision [0-9a-f]{12}-android$') |
| 121 | |
| 122 | def test_tcpip_error_messages(self): |
| 123 | p = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE, |
| 124 | stderr=subprocess.STDOUT) |
| 125 | out, _ = p.communicate() |
| 126 | self.assertEqual(1, p.returncode) |
Elliott Hughes | fa085be | 2017-08-23 15:42:28 -0700 | [diff] [blame] | 127 | self.assertIn('requires an argument', out) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 128 | |
| 129 | p = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE, |
| 130 | stderr=subprocess.STDOUT) |
| 131 | out, _ = p.communicate() |
| 132 | self.assertEqual(1, p.returncode) |
Elliott Hughes | fa085be | 2017-08-23 15:42:28 -0700 | [diff] [blame] | 133 | self.assertIn('invalid port', out) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 134 | |
Spencer Low | 9a99924 | 2015-09-16 20:45:53 -0700 | [diff] [blame] | 135 | # Helper method that reads a pipe until it is closed, then sets the event. |
| 136 | def _read_pipe_and_set_event(self, pipe, event): |
| 137 | x = pipe.read() |
| 138 | event.set() |
| 139 | |
| 140 | # Test that launch_server() does not let the adb server inherit |
| 141 | # stdin/stdout/stderr handles which can cause callers of adb.exe to hang. |
| 142 | # This test also runs fine on unix even though the impetus is an issue |
| 143 | # unique to Windows. |
| 144 | def test_handle_inheritance(self): |
| 145 | # This test takes 5 seconds to run on Windows: if there is no adb server |
| 146 | # running on the the port used below, adb kill-server tries to make a |
| 147 | # TCP connection to a closed port and that takes 1 second on Windows; |
| 148 | # adb start-server does the same TCP connection which takes another |
| 149 | # second, and it waits 3 seconds after starting the server. |
| 150 | |
| 151 | # Start adb client with redirected stdin/stdout/stderr to check if it |
| 152 | # passes those redirections to the adb server that it starts. To do |
| 153 | # this, run an instance of the adb server on a non-default port so we |
| 154 | # don't conflict with a pre-existing adb server that may already be |
| 155 | # setup with adb TCP/emulator connections. If there is a pre-existing |
| 156 | # adb server, this also tests whether multiple instances of the adb |
| 157 | # server conflict on adb.log. |
| 158 | |
| 159 | port = 5038 |
| 160 | # Kill any existing server on this non-default port. |
| 161 | subprocess.check_output(['adb', '-P', str(port), 'kill-server'], |
| 162 | stderr=subprocess.STDOUT) |
| 163 | |
| 164 | try: |
| 165 | # Run the adb client and have it start the adb server. |
| 166 | p = subprocess.Popen(['adb', '-P', str(port), 'start-server'], |
| 167 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 168 | stderr=subprocess.PIPE) |
| 169 | |
| 170 | # Start threads that set events when stdout/stderr are closed. |
| 171 | stdout_event = threading.Event() |
| 172 | stdout_thread = threading.Thread( |
| 173 | target=self._read_pipe_and_set_event, |
| 174 | args=(p.stdout, stdout_event)) |
| 175 | stdout_thread.daemon = True |
| 176 | stdout_thread.start() |
| 177 | |
| 178 | stderr_event = threading.Event() |
| 179 | stderr_thread = threading.Thread( |
| 180 | target=self._read_pipe_and_set_event, |
| 181 | args=(p.stderr, stderr_event)) |
| 182 | stderr_thread.daemon = True |
| 183 | stderr_thread.start() |
| 184 | |
| 185 | # Wait for the adb client to finish. Once that has occurred, if |
| 186 | # stdin/stderr/stdout are still open, it must be open in the adb |
| 187 | # server. |
| 188 | p.wait() |
| 189 | |
| 190 | # Try to write to stdin which we expect is closed. If it isn't |
| 191 | # closed, we should get an IOError. If we don't get an IOError, |
| 192 | # stdin must still be open in the adb server. The adb client is |
| 193 | # probably letting the adb server inherit stdin which would be |
| 194 | # wrong. |
| 195 | with self.assertRaises(IOError): |
| 196 | p.stdin.write('x') |
| 197 | |
| 198 | # Wait a few seconds for stdout/stderr to be closed (in the success |
| 199 | # case, this won't wait at all). If there is a timeout, that means |
| 200 | # stdout/stderr were not closed and and they must be open in the adb |
| 201 | # server, suggesting that the adb client is letting the adb server |
| 202 | # inherit stdout/stderr which would be wrong. |
| 203 | self.assertTrue(stdout_event.wait(5), "adb stdout not closed") |
| 204 | self.assertTrue(stderr_event.wait(5), "adb stderr not closed") |
| 205 | finally: |
| 206 | # If we started a server, kill it. |
| 207 | subprocess.check_output(['adb', '-P', str(port), 'kill-server'], |
| 208 | stderr=subprocess.STDOUT) |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 209 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 210 | # Use SO_LINGER to cause TCP RST segment to be sent on socket close. |
| 211 | def _reset_socket_on_close(self, sock): |
| 212 | # The linger structure is two shorts on Windows, but two ints on Unix. |
| 213 | linger_format = 'hh' if os.name == 'nt' else 'ii' |
| 214 | l_onoff = 1 |
| 215 | l_linger = 0 |
| 216 | |
| 217 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
| 218 | struct.pack(linger_format, l_onoff, l_linger)) |
| 219 | # Verify that we set the linger structure properly by retrieving it. |
| 220 | linger = sock.getsockopt(socket.SOL_SOCKET, socket.SO_LINGER, 16) |
| 221 | self.assertEqual((l_onoff, l_linger), |
| 222 | struct.unpack_from(linger_format, linger)) |
| 223 | |
| 224 | def test_emu_kill(self): |
| 225 | """Ensure that adb emu kill works. |
| 226 | |
| 227 | Bug: https://code.google.com/p/android/issues/detail?id=21021 |
| 228 | """ |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 229 | with contextlib.closing( |
| 230 | socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as listener: |
| 231 | # Use SO_REUSEADDR so subsequent runs of the test can grab the port |
| 232 | # even if it is in TIME_WAIT. |
| 233 | listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 234 | listener.bind(('127.0.0.1', 0)) |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 235 | listener.listen(4) |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 236 | port = listener.getsockname()[1] |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 237 | |
| 238 | # Now that listening has started, start adb emu kill, telling it to |
| 239 | # connect to our mock emulator. |
| 240 | p = subprocess.Popen( |
| 241 | ['adb', '-s', 'emulator-' + str(port), 'emu', 'kill'], |
| 242 | stderr=subprocess.STDOUT) |
| 243 | |
| 244 | accepted_connection, addr = listener.accept() |
| 245 | with contextlib.closing(accepted_connection) as conn: |
| 246 | # If WSAECONNABORTED (10053) is raised by any socket calls, |
| 247 | # then adb probably isn't reading the data that we sent it. |
| 248 | conn.sendall('Android Console: type \'help\' for a list ' + |
| 249 | 'of commands\r\n') |
| 250 | conn.sendall('OK\r\n') |
| 251 | |
| 252 | with contextlib.closing(conn.makefile()) as f: |
| 253 | self.assertEqual('kill\n', f.readline()) |
| 254 | self.assertEqual('quit\n', f.readline()) |
| 255 | |
| 256 | conn.sendall('OK: killing emulator, bye bye\r\n') |
| 257 | |
| 258 | # Use SO_LINGER to send TCP RST segment to test whether adb |
| 259 | # ignores WSAECONNRESET on Windows. This happens with the |
| 260 | # real emulator because it just calls exit() without closing |
| 261 | # the socket or calling shutdown(SD_SEND). At process |
| 262 | # termination, Windows sends a TCP RST segment for every |
| 263 | # open socket that shutdown(SD_SEND) wasn't used on. |
| 264 | self._reset_socket_on_close(conn) |
| 265 | |
| 266 | # Wait for adb to finish, so we can check return code. |
| 267 | p.communicate() |
| 268 | |
| 269 | # If this fails, adb probably isn't ignoring WSAECONNRESET when |
| 270 | # reading the response from the adb emu kill command (on Windows). |
| 271 | self.assertEqual(0, p.returncode) |
| 272 | |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 273 | def test_connect_ipv4_ipv6(self): |
| 274 | """Ensure that `adb connect localhost:1234` will try both IPv4 and IPv6. |
| 275 | |
| 276 | Bug: http://b/30313466 |
| 277 | """ |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 278 | for protocol in (socket.AF_INET, socket.AF_INET6): |
| 279 | try: |
| 280 | with fake_adb_server(protocol=protocol) as port: |
| 281 | output = subprocess.check_output( |
| 282 | ['adb', 'connect', 'localhost:{}'.format(port)]) |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 283 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 284 | self.assertEqual( |
| 285 | output.strip(), 'connected to localhost:{}'.format(port)) |
| 286 | except socket.error: |
| 287 | print("IPv6 not available, skipping") |
| 288 | continue |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 289 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 290 | def test_already_connected(self): |
| 291 | with fake_adb_server() as port: |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 292 | output = subprocess.check_output( |
| 293 | ['adb', 'connect', 'localhost:{}'.format(port)]) |
| 294 | |
| 295 | self.assertEqual( |
| 296 | output.strip(), 'connected to localhost:{}'.format(port)) |
Josh Gao | 50bde8d | 2016-09-01 14:54:18 -0700 | [diff] [blame] | 297 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 298 | # b/31250450: this always returns 0 but probably shouldn't. |
| 299 | output = subprocess.check_output( |
| 300 | ['adb', 'connect', 'localhost:{}'.format(port)]) |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 301 | |
Luis Hector Chavez | bf8a722 | 2018-04-17 19:25:33 -0700 | [diff] [blame] | 302 | self.assertEqual( |
| 303 | output.strip(), 'already connected to localhost:{}'.format(port)) |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 304 | |
Dan Albert | a4169f9 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 305 | def main(): |
| 306 | random.seed(0) |
| 307 | if len(adb.get_devices()) > 0: |
| 308 | suite = unittest.TestLoader().loadTestsFromName(__name__) |
| 309 | unittest.TextTestRunner(verbosity=3).run(suite) |
| 310 | else: |
| 311 | print('Test suite must be run with attached devices') |
| 312 | |
| 313 | |
| 314 | if __name__ == '__main__': |
| 315 | main() |