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