Loading adb/Android.bp +2 −5 Original line number Diff line number Diff line Loading @@ -455,17 +455,14 @@ python_test_host { srcs: [ "test_adb.py", ], libs: [ "adb_py", ], test_config: "adb_integration_test_adb.xml", test_suites: ["general-tests"], version: { py2: { enabled: true, enabled: false, }, py3: { enabled: false, enabled: true, }, }, } Loading adb/client/auth.cpp +1 −0 Original line number Diff line number Diff line Loading @@ -465,6 +465,7 @@ void send_auth_response(const char* token, size_t token_size, atransport* t) { if (key == nullptr) { // No more private keys to try, send the public key. t->SetConnectionState(kCsUnauthorized); t->SetConnectionEstablished(true); send_auth_publickey(t); return; } Loading adb/test_adb.py 100644 → 100755 +115 −112 Original line number Diff line number Diff line #!/usr/bin/env python #!/usr/bin/env python3 # # Copyright (C) 2015 The Android Open Source Project # Loading @@ -19,9 +19,7 @@ This differs from things in test_device.py in that there is no API for these things. Most of these tests involve specific error messages or the help text. """ from __future__ import print_function import binascii import contextlib import os import random Loading @@ -32,8 +30,6 @@ import subprocess import threading import unittest import adb @contextlib.contextmanager def fake_adbd(protocol=socket.AF_INET, port=0): Loading @@ -42,32 +38,32 @@ def fake_adbd(protocol=socket.AF_INET, port=0): serversock = socket.socket(protocol, socket.SOCK_STREAM) serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if protocol == socket.AF_INET: serversock.bind(('127.0.0.1', port)) serversock.bind(("127.0.0.1", port)) else: serversock.bind(('::1', port)) serversock.bind(("::1", port)) serversock.listen(1) # A pipe that is used to signal the thread that it should terminate. readpipe, writepipe = os.pipe() readsock, writesock = socket.socketpair() def _adb_packet(command, arg0, arg1, data): bin_command = struct.unpack('I', command)[0] buf = struct.pack('IIIIII', bin_command, arg0, arg1, len(data), 0, def _adb_packet(command: bytes, arg0: int, arg1: int, data: bytes) -> bytes: bin_command = struct.unpack("I", command)[0] buf = struct.pack("IIIIII", bin_command, arg0, arg1, len(data), 0, bin_command ^ 0xffffffff) buf += data return buf def _handle(): rlist = [readpipe, serversock] def _handle(sock): with contextlib.closing(sock) as serversock: rlist = [readsock, serversock] cnxn_sent = {} while True: read_ready, _, _ = select.select(rlist, [], []) for ready in read_ready: if ready == readpipe: if ready == readsock: # Closure pipe os.close(ready) serversock.shutdown(socket.SHUT_RDWR) serversock.close() for f in rlist: f.close() return elif ready == serversock: # Server socket Loading @@ -76,7 +72,7 @@ def fake_adbd(protocol=socket.AF_INET, port=0): else: # Client socket data = ready.recv(1024) if not data or data.startswith('OPEN'): if not data or data.startswith(b"OPEN"): if ready in cnxn_sent: del cnxn_sent[ready] ready.shutdown(socket.SHUT_RDWR) Loading @@ -86,17 +82,17 @@ def fake_adbd(protocol=socket.AF_INET, port=0): if ready in cnxn_sent: continue cnxn_sent[ready] = True ready.sendall(_adb_packet('CNXN', 0x01000001, 1024 * 1024, 'device::ro.product.name=fakeadb')) ready.sendall(_adb_packet(b"CNXN", 0x01000001, 1024 * 1024, b"device::ro.product.name=fakeadb")) port = serversock.getsockname()[1] server_thread = threading.Thread(target=_handle) server_thread = threading.Thread(target=_handle, args=(serversock,)) server_thread.start() try: yield port finally: os.close(writepipe) writesock.close() server_thread.join() Loading @@ -107,14 +103,15 @@ def adb_connect(unittest, serial): This automatically disconnects when done with the connection. """ output = subprocess.check_output(['adb', 'connect', serial]) unittest.assertEqual(output.strip(), 'connected to {}'.format(serial)) output = subprocess.check_output(["adb", "connect", serial]) unittest.assertEqual(output.strip(), "connected to {}".format(serial).encode("utf8")) try: yield finally: # Perform best-effort disconnection. Discard the output. subprocess.Popen(['adb', 'disconnect', serial], subprocess.Popen(["adb", "disconnect", serial], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() Loading @@ -123,21 +120,22 @@ def adb_connect(unittest, serial): def adb_server(): """Context manager for an ADB server. This creates an ADB server and returns the port it's listening on. This creates an ADB server and returns the port it"s listening on. """ port = 5038 # Kill any existing server on this non-default port. subprocess.check_output(['adb', '-P', str(port), 'kill-server'], subprocess.check_output(["adb", "-P", str(port), "kill-server"], stderr=subprocess.STDOUT) read_pipe, write_pipe = os.pipe() proc = subprocess.Popen(['adb', '-L', 'tcp:localhost:{}'.format(port), 'fork-server', 'server', '--reply-fd', str(write_pipe)]) os.set_inheritable(write_pipe, True) proc = subprocess.Popen(["adb", "-L", "tcp:localhost:{}".format(port), "fork-server", "server", "--reply-fd", str(write_pipe)], close_fds=False) try: os.close(write_pipe) greeting = os.read(read_pipe, 1024) assert greeting == 'OK\n', repr(greeting) assert greeting == b"OK\n", repr(greeting) yield port finally: proc.terminate() Loading @@ -150,35 +148,37 @@ class CommandlineTest(unittest.TestCase): def test_help(self): """Make sure we get _something_ out of help.""" out = subprocess.check_output( ['adb', 'help'], stderr=subprocess.STDOUT) ["adb", "help"], stderr=subprocess.STDOUT) self.assertGreater(len(out), 0) def test_version(self): """Get a version number out of the output of adb.""" lines = subprocess.check_output(['adb', 'version']).splitlines() lines = subprocess.check_output(["adb", "version"]).splitlines() version_line = lines[0] self.assertRegexpMatches( version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$') self.assertRegex( version_line, rb"^Android Debug Bridge version \d+\.\d+\.\d+$") if len(lines) == 2: # Newer versions of ADB have a second line of output for the # version that includes a specific revision (git SHA). revision_line = lines[1] self.assertRegexpMatches( revision_line, r'^Revision [0-9a-f]{12}-android$') self.assertRegex( revision_line, rb"^Revision [0-9a-f]{12}-android$") def test_tcpip_error_messages(self): """Make sure 'adb tcpip' parsing is sane.""" proc = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE, proc = subprocess.Popen(["adb", "tcpip"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, _ = proc.communicate() self.assertEqual(1, proc.returncode) self.assertIn('requires an argument', out) self.assertIn(b"requires an argument", out) proc = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE, proc = subprocess.Popen(["adb", "tcpip", "foo"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, _ = proc.communicate() self.assertEqual(1, proc.returncode) self.assertIn('invalid port', out) self.assertIn(b"invalid port", out) class ServerTest(unittest.TestCase): Loading Loading @@ -214,12 +214,12 @@ class ServerTest(unittest.TestCase): port = 5038 # Kill any existing server on this non-default port. subprocess.check_output(['adb', '-P', str(port), 'kill-server'], subprocess.check_output(["adb", "-P", str(port), "kill-server"], stderr=subprocess.STDOUT) try: # Run the adb client and have it start the adb server. proc = subprocess.Popen(['adb', '-P', str(port), 'start-server'], proc = subprocess.Popen(["adb", "-P", str(port), "start-server"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) Loading @@ -229,14 +229,12 @@ class ServerTest(unittest.TestCase): stdout_thread = threading.Thread( target=ServerTest._read_pipe_and_set_event, args=(proc.stdout, stdout_event)) stdout_thread.daemon = True stdout_thread.start() stderr_event = threading.Event() stderr_thread = threading.Thread( target=ServerTest._read_pipe_and_set_event, args=(proc.stderr, stderr_event)) stderr_thread.daemon = True stderr_thread.start() # Wait for the adb client to finish. Once that has occurred, if Loading @@ -250,7 +248,8 @@ class ServerTest(unittest.TestCase): # probably letting the adb server inherit stdin which would be # wrong. with self.assertRaises(IOError): proc.stdin.write('x') proc.stdin.write(b"x") proc.stdin.flush() # Wait a few seconds for stdout/stderr to be closed (in the success # case, this won't wait at all). If there is a timeout, that means Loading @@ -259,9 +258,11 @@ class ServerTest(unittest.TestCase): # inherit stdout/stderr which would be wrong. self.assertTrue(stdout_event.wait(5), "adb stdout not closed") self.assertTrue(stderr_event.wait(5), "adb stderr not closed") stdout_thread.join() stderr_thread.join() finally: # If we started a server, kill it. subprocess.check_output(['adb', '-P', str(port), 'kill-server'], subprocess.check_output(["adb", "-P", str(port), "kill-server"], stderr=subprocess.STDOUT) Loading @@ -271,7 +272,7 @@ class EmulatorTest(unittest.TestCase): def _reset_socket_on_close(self, sock): """Use SO_LINGER to cause TCP RST segment to be sent on socket close.""" # The linger structure is two shorts on Windows, but two ints on Unix. linger_format = 'hh' if os.name == 'nt' else 'ii' linger_format = "hh" if os.name == "nt" else "ii" l_onoff = 1 l_linger = 0 Loading @@ -292,33 +293,33 @@ class EmulatorTest(unittest.TestCase): # Use SO_REUSEADDR so subsequent runs of the test can grab the port # even if it is in TIME_WAIT. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('127.0.0.1', 0)) listener.bind(("127.0.0.1", 0)) listener.listen(4) port = listener.getsockname()[1] # Now that listening has started, start adb emu kill, telling it to # connect to our mock emulator. proc = subprocess.Popen( ['adb', '-s', 'emulator-' + str(port), 'emu', 'kill'], ["adb", "-s", "emulator-" + str(port), "emu", "kill"], stderr=subprocess.STDOUT) accepted_connection, addr = listener.accept() with contextlib.closing(accepted_connection) as conn: # If WSAECONNABORTED (10053) is raised by any socket calls, # then adb probably isn't reading the data that we sent it. conn.sendall('Android Console: type \'help\' for a list ' + 'of commands\r\n') conn.sendall('OK\r\n') conn.sendall(("Android Console: type 'help' for a list " "of commands\r\n").encode("utf8")) conn.sendall(b"OK\r\n") with contextlib.closing(conn.makefile()) as connf: line = connf.readline() if line.startswith('auth'): if line.startswith("auth"): # Ignore the first auth line. line = connf.readline() self.assertEqual('kill\n', line) self.assertEqual('quit\n', connf.readline()) self.assertEqual("kill\n", line) self.assertEqual("quit\n", connf.readline()) conn.sendall('OK: killing emulator, bye bye\r\n') conn.sendall(b"OK: killing emulator, bye bye\r\n") # Use SO_LINGER to send TCP RST segment to test whether adb # ignores WSAECONNRESET on Windows. This happens with the Loading @@ -342,31 +343,31 @@ class EmulatorTest(unittest.TestCase): """ with adb_server() as server_port: with fake_adbd() as port: serial = 'emulator-{}'.format(port - 1) serial = "emulator-{}".format(port - 1) # Ensure that the emulator is not there. try: subprocess.check_output(['adb', '-P', str(server_port), '-s', serial, 'get-state'], subprocess.check_output(["adb", "-P", str(server_port), "-s", serial, "get-state"], stderr=subprocess.STDOUT) self.fail('Device should not be available') self.fail("Device should not be available") except subprocess.CalledProcessError as err: self.assertEqual( err.output.strip(), 'error: device \'{}\' not found'.format(serial)) "error: device '{}' not found".format(serial).encode("utf8")) # Let the ADB server know that the emulator has started. with contextlib.closing( socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: sock.connect(('localhost', server_port)) command = 'host:emulator:{}'.format(port) sock.sendall('%04x%s' % (len(command), command)) sock.connect(("localhost", server_port)) command = "host:emulator:{}".format(port).encode("utf8") sock.sendall(b"%04x%s" % (len(command), command)) # Ensure the emulator is there. subprocess.check_call(['adb', '-P', str(server_port), '-s', serial, 'wait-for-device']) output = subprocess.check_output(['adb', '-P', str(server_port), '-s', serial, 'get-state']) self.assertEqual(output.strip(), 'device') subprocess.check_call(["adb", "-P", str(server_port), "-s", serial, "wait-for-device"]) output = subprocess.check_output(["adb", "-P", str(server_port), "-s", serial, "get-state"]) self.assertEqual(output.strip(), b"device") class ConnectionTest(unittest.TestCase): Loading @@ -380,7 +381,7 @@ class ConnectionTest(unittest.TestCase): for protocol in (socket.AF_INET, socket.AF_INET6): try: with fake_adbd(protocol=protocol) as port: serial = 'localhost:{}'.format(port) serial = "localhost:{}".format(port) with adb_connect(self, serial): pass except socket.error: Loading @@ -391,49 +392,51 @@ class ConnectionTest(unittest.TestCase): """Ensure that an already-connected device stays connected.""" with fake_adbd() as port: serial = 'localhost:{}'.format(port) serial = "localhost:{}".format(port) with adb_connect(self, serial): # b/31250450: this always returns 0 but probably shouldn't. output = subprocess.check_output(['adb', 'connect', serial]) output = subprocess.check_output(["adb", "connect", serial]) self.assertEqual( output.strip(), 'already connected to {}'.format(serial)) output.strip(), "already connected to {}".format(serial).encode("utf8")) def test_reconnect(self): """Ensure that a disconnected device reconnects.""" with fake_adbd() as port: serial = 'localhost:{}'.format(port) serial = "localhost:{}".format(port) with adb_connect(self, serial): output = subprocess.check_output(['adb', '-s', serial, 'get-state']) self.assertEqual(output.strip(), 'device') output = subprocess.check_output(["adb", "-s", serial, "get-state"]) self.assertEqual(output.strip(), b"device") # This will fail. proc = subprocess.Popen(['adb', '-s', serial, 'shell', 'true'], proc = subprocess.Popen(["adb", "-s", serial, "shell", "true"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = proc.communicate() self.assertEqual(output.strip(), 'error: closed') self.assertEqual(output.strip(), b"error: closed") subprocess.check_call(['adb', '-s', serial, 'wait-for-device']) subprocess.check_call(["adb", "-s", serial, "wait-for-device"]) output = subprocess.check_output(['adb', '-s', serial, 'get-state']) self.assertEqual(output.strip(), 'device') output = subprocess.check_output(["adb", "-s", serial, "get-state"]) self.assertEqual(output.strip(), b"device") # Once we explicitly kick a device, it won't attempt to # reconnect. output = subprocess.check_output(['adb', 'disconnect', serial]) output = subprocess.check_output(["adb", "disconnect", serial]) self.assertEqual( output.strip(), 'disconnected {}'.format(serial)) output.strip(), "disconnected {}".format(serial).encode("utf8")) try: subprocess.check_output(['adb', '-s', serial, 'get-state'], subprocess.check_output(["adb", "-s", serial, "get-state"], stderr=subprocess.STDOUT) self.fail('Device should not be available') self.fail("Device should not be available") except subprocess.CalledProcessError as err: self.assertEqual( err.output.strip(), 'error: device \'{}\' not found'.format(serial)) "error: device '{}' not found".format(serial).encode("utf8")) def main(): Loading @@ -442,5 +445,5 @@ def main(): unittest.main(verbosity=3) if __name__ == '__main__': if __name__ == "__main__": main() adb/test_device.py +5 −3 Original line number Diff line number Diff line Loading @@ -762,9 +762,10 @@ class FileOperationsTest(DeviceTest): os.chmod(host_dir, 0o700) # Create an empty directory. os.mkdir(os.path.join(host_dir, 'empty')) empty_dir_path = os.path.join(host_dir, 'empty') os.mkdir(empty_dir_path); self.device.push(host_dir, self.DEVICE_TEMP_DIR) self.device.push(empty_dir_path, self.DEVICE_TEMP_DIR) test_empty_cmd = ['[', '-d', os.path.join(self.DEVICE_TEMP_DIR, 'empty')] Loading Loading @@ -1032,7 +1033,8 @@ class FileOperationsTest(DeviceTest): if host_dir is not None: shutil.rmtree(host_dir) def test_pull_symlink_dir(self): # selinux prevents adbd from accessing symlinks on /data/local/tmp. def disabled_test_pull_symlink_dir(self): """Pull a symlink to a directory of symlinks to files.""" try: host_dir = tempfile.mkdtemp() Loading adb/transport.cpp +20 −7 Original line number Diff line number Diff line Loading @@ -1190,14 +1190,15 @@ void close_usb_devices() { } #endif // ADB_HOST int register_socket_transport(unique_fd s, std::string serial, int port, int local, atransport::ReconnectCallback reconnect) { bool register_socket_transport(unique_fd s, std::string serial, int port, int local, atransport::ReconnectCallback reconnect, int* error) { atransport* t = new atransport(std::move(reconnect), kCsOffline); D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port); if (init_socket_transport(t, std::move(s), port, local) < 0) { delete t; return -1; if (error) *error = errno; return false; } std::unique_lock<std::recursive_mutex> lock(transport_lock); Loading @@ -1206,7 +1207,8 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc VLOG(TRANSPORT) << "socket transport " << transport->serial << " is already in pending_list and fails to register"; delete t; return -EALREADY; if (error) *error = EALREADY; return false; } } Loading @@ -1215,7 +1217,8 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc VLOG(TRANSPORT) << "socket transport " << transport->serial << " is already in transport_list and fails to register"; delete t; return -EALREADY; if (error) *error = EALREADY; return false; } } Loading @@ -1229,10 +1232,20 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc if (local == 1) { // Do not wait for emulator transports. return 0; return true; } return waitable->WaitForConnection(std::chrono::seconds(10)) ? 0 : -1; if (!waitable->WaitForConnection(std::chrono::seconds(10))) { if (error) *error = ETIMEDOUT; return false; } if (t->GetConnectionState() == kCsUnauthorized) { if (error) *error = EPERM; return false; } return true; } #if ADB_HOST Loading Loading
adb/Android.bp +2 −5 Original line number Diff line number Diff line Loading @@ -455,17 +455,14 @@ python_test_host { srcs: [ "test_adb.py", ], libs: [ "adb_py", ], test_config: "adb_integration_test_adb.xml", test_suites: ["general-tests"], version: { py2: { enabled: true, enabled: false, }, py3: { enabled: false, enabled: true, }, }, } Loading
adb/client/auth.cpp +1 −0 Original line number Diff line number Diff line Loading @@ -465,6 +465,7 @@ void send_auth_response(const char* token, size_t token_size, atransport* t) { if (key == nullptr) { // No more private keys to try, send the public key. t->SetConnectionState(kCsUnauthorized); t->SetConnectionEstablished(true); send_auth_publickey(t); return; } Loading
adb/test_adb.py 100644 → 100755 +115 −112 Original line number Diff line number Diff line #!/usr/bin/env python #!/usr/bin/env python3 # # Copyright (C) 2015 The Android Open Source Project # Loading @@ -19,9 +19,7 @@ This differs from things in test_device.py in that there is no API for these things. Most of these tests involve specific error messages or the help text. """ from __future__ import print_function import binascii import contextlib import os import random Loading @@ -32,8 +30,6 @@ import subprocess import threading import unittest import adb @contextlib.contextmanager def fake_adbd(protocol=socket.AF_INET, port=0): Loading @@ -42,32 +38,32 @@ def fake_adbd(protocol=socket.AF_INET, port=0): serversock = socket.socket(protocol, socket.SOCK_STREAM) serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if protocol == socket.AF_INET: serversock.bind(('127.0.0.1', port)) serversock.bind(("127.0.0.1", port)) else: serversock.bind(('::1', port)) serversock.bind(("::1", port)) serversock.listen(1) # A pipe that is used to signal the thread that it should terminate. readpipe, writepipe = os.pipe() readsock, writesock = socket.socketpair() def _adb_packet(command, arg0, arg1, data): bin_command = struct.unpack('I', command)[0] buf = struct.pack('IIIIII', bin_command, arg0, arg1, len(data), 0, def _adb_packet(command: bytes, arg0: int, arg1: int, data: bytes) -> bytes: bin_command = struct.unpack("I", command)[0] buf = struct.pack("IIIIII", bin_command, arg0, arg1, len(data), 0, bin_command ^ 0xffffffff) buf += data return buf def _handle(): rlist = [readpipe, serversock] def _handle(sock): with contextlib.closing(sock) as serversock: rlist = [readsock, serversock] cnxn_sent = {} while True: read_ready, _, _ = select.select(rlist, [], []) for ready in read_ready: if ready == readpipe: if ready == readsock: # Closure pipe os.close(ready) serversock.shutdown(socket.SHUT_RDWR) serversock.close() for f in rlist: f.close() return elif ready == serversock: # Server socket Loading @@ -76,7 +72,7 @@ def fake_adbd(protocol=socket.AF_INET, port=0): else: # Client socket data = ready.recv(1024) if not data or data.startswith('OPEN'): if not data or data.startswith(b"OPEN"): if ready in cnxn_sent: del cnxn_sent[ready] ready.shutdown(socket.SHUT_RDWR) Loading @@ -86,17 +82,17 @@ def fake_adbd(protocol=socket.AF_INET, port=0): if ready in cnxn_sent: continue cnxn_sent[ready] = True ready.sendall(_adb_packet('CNXN', 0x01000001, 1024 * 1024, 'device::ro.product.name=fakeadb')) ready.sendall(_adb_packet(b"CNXN", 0x01000001, 1024 * 1024, b"device::ro.product.name=fakeadb")) port = serversock.getsockname()[1] server_thread = threading.Thread(target=_handle) server_thread = threading.Thread(target=_handle, args=(serversock,)) server_thread.start() try: yield port finally: os.close(writepipe) writesock.close() server_thread.join() Loading @@ -107,14 +103,15 @@ def adb_connect(unittest, serial): This automatically disconnects when done with the connection. """ output = subprocess.check_output(['adb', 'connect', serial]) unittest.assertEqual(output.strip(), 'connected to {}'.format(serial)) output = subprocess.check_output(["adb", "connect", serial]) unittest.assertEqual(output.strip(), "connected to {}".format(serial).encode("utf8")) try: yield finally: # Perform best-effort disconnection. Discard the output. subprocess.Popen(['adb', 'disconnect', serial], subprocess.Popen(["adb", "disconnect", serial], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() Loading @@ -123,21 +120,22 @@ def adb_connect(unittest, serial): def adb_server(): """Context manager for an ADB server. This creates an ADB server and returns the port it's listening on. This creates an ADB server and returns the port it"s listening on. """ port = 5038 # Kill any existing server on this non-default port. subprocess.check_output(['adb', '-P', str(port), 'kill-server'], subprocess.check_output(["adb", "-P", str(port), "kill-server"], stderr=subprocess.STDOUT) read_pipe, write_pipe = os.pipe() proc = subprocess.Popen(['adb', '-L', 'tcp:localhost:{}'.format(port), 'fork-server', 'server', '--reply-fd', str(write_pipe)]) os.set_inheritable(write_pipe, True) proc = subprocess.Popen(["adb", "-L", "tcp:localhost:{}".format(port), "fork-server", "server", "--reply-fd", str(write_pipe)], close_fds=False) try: os.close(write_pipe) greeting = os.read(read_pipe, 1024) assert greeting == 'OK\n', repr(greeting) assert greeting == b"OK\n", repr(greeting) yield port finally: proc.terminate() Loading @@ -150,35 +148,37 @@ class CommandlineTest(unittest.TestCase): def test_help(self): """Make sure we get _something_ out of help.""" out = subprocess.check_output( ['adb', 'help'], stderr=subprocess.STDOUT) ["adb", "help"], stderr=subprocess.STDOUT) self.assertGreater(len(out), 0) def test_version(self): """Get a version number out of the output of adb.""" lines = subprocess.check_output(['adb', 'version']).splitlines() lines = subprocess.check_output(["adb", "version"]).splitlines() version_line = lines[0] self.assertRegexpMatches( version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$') self.assertRegex( version_line, rb"^Android Debug Bridge version \d+\.\d+\.\d+$") if len(lines) == 2: # Newer versions of ADB have a second line of output for the # version that includes a specific revision (git SHA). revision_line = lines[1] self.assertRegexpMatches( revision_line, r'^Revision [0-9a-f]{12}-android$') self.assertRegex( revision_line, rb"^Revision [0-9a-f]{12}-android$") def test_tcpip_error_messages(self): """Make sure 'adb tcpip' parsing is sane.""" proc = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE, proc = subprocess.Popen(["adb", "tcpip"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, _ = proc.communicate() self.assertEqual(1, proc.returncode) self.assertIn('requires an argument', out) self.assertIn(b"requires an argument", out) proc = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE, proc = subprocess.Popen(["adb", "tcpip", "foo"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, _ = proc.communicate() self.assertEqual(1, proc.returncode) self.assertIn('invalid port', out) self.assertIn(b"invalid port", out) class ServerTest(unittest.TestCase): Loading Loading @@ -214,12 +214,12 @@ class ServerTest(unittest.TestCase): port = 5038 # Kill any existing server on this non-default port. subprocess.check_output(['adb', '-P', str(port), 'kill-server'], subprocess.check_output(["adb", "-P", str(port), "kill-server"], stderr=subprocess.STDOUT) try: # Run the adb client and have it start the adb server. proc = subprocess.Popen(['adb', '-P', str(port), 'start-server'], proc = subprocess.Popen(["adb", "-P", str(port), "start-server"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) Loading @@ -229,14 +229,12 @@ class ServerTest(unittest.TestCase): stdout_thread = threading.Thread( target=ServerTest._read_pipe_and_set_event, args=(proc.stdout, stdout_event)) stdout_thread.daemon = True stdout_thread.start() stderr_event = threading.Event() stderr_thread = threading.Thread( target=ServerTest._read_pipe_and_set_event, args=(proc.stderr, stderr_event)) stderr_thread.daemon = True stderr_thread.start() # Wait for the adb client to finish. Once that has occurred, if Loading @@ -250,7 +248,8 @@ class ServerTest(unittest.TestCase): # probably letting the adb server inherit stdin which would be # wrong. with self.assertRaises(IOError): proc.stdin.write('x') proc.stdin.write(b"x") proc.stdin.flush() # Wait a few seconds for stdout/stderr to be closed (in the success # case, this won't wait at all). If there is a timeout, that means Loading @@ -259,9 +258,11 @@ class ServerTest(unittest.TestCase): # inherit stdout/stderr which would be wrong. self.assertTrue(stdout_event.wait(5), "adb stdout not closed") self.assertTrue(stderr_event.wait(5), "adb stderr not closed") stdout_thread.join() stderr_thread.join() finally: # If we started a server, kill it. subprocess.check_output(['adb', '-P', str(port), 'kill-server'], subprocess.check_output(["adb", "-P", str(port), "kill-server"], stderr=subprocess.STDOUT) Loading @@ -271,7 +272,7 @@ class EmulatorTest(unittest.TestCase): def _reset_socket_on_close(self, sock): """Use SO_LINGER to cause TCP RST segment to be sent on socket close.""" # The linger structure is two shorts on Windows, but two ints on Unix. linger_format = 'hh' if os.name == 'nt' else 'ii' linger_format = "hh" if os.name == "nt" else "ii" l_onoff = 1 l_linger = 0 Loading @@ -292,33 +293,33 @@ class EmulatorTest(unittest.TestCase): # Use SO_REUSEADDR so subsequent runs of the test can grab the port # even if it is in TIME_WAIT. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('127.0.0.1', 0)) listener.bind(("127.0.0.1", 0)) listener.listen(4) port = listener.getsockname()[1] # Now that listening has started, start adb emu kill, telling it to # connect to our mock emulator. proc = subprocess.Popen( ['adb', '-s', 'emulator-' + str(port), 'emu', 'kill'], ["adb", "-s", "emulator-" + str(port), "emu", "kill"], stderr=subprocess.STDOUT) accepted_connection, addr = listener.accept() with contextlib.closing(accepted_connection) as conn: # If WSAECONNABORTED (10053) is raised by any socket calls, # then adb probably isn't reading the data that we sent it. conn.sendall('Android Console: type \'help\' for a list ' + 'of commands\r\n') conn.sendall('OK\r\n') conn.sendall(("Android Console: type 'help' for a list " "of commands\r\n").encode("utf8")) conn.sendall(b"OK\r\n") with contextlib.closing(conn.makefile()) as connf: line = connf.readline() if line.startswith('auth'): if line.startswith("auth"): # Ignore the first auth line. line = connf.readline() self.assertEqual('kill\n', line) self.assertEqual('quit\n', connf.readline()) self.assertEqual("kill\n", line) self.assertEqual("quit\n", connf.readline()) conn.sendall('OK: killing emulator, bye bye\r\n') conn.sendall(b"OK: killing emulator, bye bye\r\n") # Use SO_LINGER to send TCP RST segment to test whether adb # ignores WSAECONNRESET on Windows. This happens with the Loading @@ -342,31 +343,31 @@ class EmulatorTest(unittest.TestCase): """ with adb_server() as server_port: with fake_adbd() as port: serial = 'emulator-{}'.format(port - 1) serial = "emulator-{}".format(port - 1) # Ensure that the emulator is not there. try: subprocess.check_output(['adb', '-P', str(server_port), '-s', serial, 'get-state'], subprocess.check_output(["adb", "-P", str(server_port), "-s", serial, "get-state"], stderr=subprocess.STDOUT) self.fail('Device should not be available') self.fail("Device should not be available") except subprocess.CalledProcessError as err: self.assertEqual( err.output.strip(), 'error: device \'{}\' not found'.format(serial)) "error: device '{}' not found".format(serial).encode("utf8")) # Let the ADB server know that the emulator has started. with contextlib.closing( socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: sock.connect(('localhost', server_port)) command = 'host:emulator:{}'.format(port) sock.sendall('%04x%s' % (len(command), command)) sock.connect(("localhost", server_port)) command = "host:emulator:{}".format(port).encode("utf8") sock.sendall(b"%04x%s" % (len(command), command)) # Ensure the emulator is there. subprocess.check_call(['adb', '-P', str(server_port), '-s', serial, 'wait-for-device']) output = subprocess.check_output(['adb', '-P', str(server_port), '-s', serial, 'get-state']) self.assertEqual(output.strip(), 'device') subprocess.check_call(["adb", "-P", str(server_port), "-s", serial, "wait-for-device"]) output = subprocess.check_output(["adb", "-P", str(server_port), "-s", serial, "get-state"]) self.assertEqual(output.strip(), b"device") class ConnectionTest(unittest.TestCase): Loading @@ -380,7 +381,7 @@ class ConnectionTest(unittest.TestCase): for protocol in (socket.AF_INET, socket.AF_INET6): try: with fake_adbd(protocol=protocol) as port: serial = 'localhost:{}'.format(port) serial = "localhost:{}".format(port) with adb_connect(self, serial): pass except socket.error: Loading @@ -391,49 +392,51 @@ class ConnectionTest(unittest.TestCase): """Ensure that an already-connected device stays connected.""" with fake_adbd() as port: serial = 'localhost:{}'.format(port) serial = "localhost:{}".format(port) with adb_connect(self, serial): # b/31250450: this always returns 0 but probably shouldn't. output = subprocess.check_output(['adb', 'connect', serial]) output = subprocess.check_output(["adb", "connect", serial]) self.assertEqual( output.strip(), 'already connected to {}'.format(serial)) output.strip(), "already connected to {}".format(serial).encode("utf8")) def test_reconnect(self): """Ensure that a disconnected device reconnects.""" with fake_adbd() as port: serial = 'localhost:{}'.format(port) serial = "localhost:{}".format(port) with adb_connect(self, serial): output = subprocess.check_output(['adb', '-s', serial, 'get-state']) self.assertEqual(output.strip(), 'device') output = subprocess.check_output(["adb", "-s", serial, "get-state"]) self.assertEqual(output.strip(), b"device") # This will fail. proc = subprocess.Popen(['adb', '-s', serial, 'shell', 'true'], proc = subprocess.Popen(["adb", "-s", serial, "shell", "true"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = proc.communicate() self.assertEqual(output.strip(), 'error: closed') self.assertEqual(output.strip(), b"error: closed") subprocess.check_call(['adb', '-s', serial, 'wait-for-device']) subprocess.check_call(["adb", "-s", serial, "wait-for-device"]) output = subprocess.check_output(['adb', '-s', serial, 'get-state']) self.assertEqual(output.strip(), 'device') output = subprocess.check_output(["adb", "-s", serial, "get-state"]) self.assertEqual(output.strip(), b"device") # Once we explicitly kick a device, it won't attempt to # reconnect. output = subprocess.check_output(['adb', 'disconnect', serial]) output = subprocess.check_output(["adb", "disconnect", serial]) self.assertEqual( output.strip(), 'disconnected {}'.format(serial)) output.strip(), "disconnected {}".format(serial).encode("utf8")) try: subprocess.check_output(['adb', '-s', serial, 'get-state'], subprocess.check_output(["adb", "-s", serial, "get-state"], stderr=subprocess.STDOUT) self.fail('Device should not be available') self.fail("Device should not be available") except subprocess.CalledProcessError as err: self.assertEqual( err.output.strip(), 'error: device \'{}\' not found'.format(serial)) "error: device '{}' not found".format(serial).encode("utf8")) def main(): Loading @@ -442,5 +445,5 @@ def main(): unittest.main(verbosity=3) if __name__ == '__main__': if __name__ == "__main__": main()
adb/test_device.py +5 −3 Original line number Diff line number Diff line Loading @@ -762,9 +762,10 @@ class FileOperationsTest(DeviceTest): os.chmod(host_dir, 0o700) # Create an empty directory. os.mkdir(os.path.join(host_dir, 'empty')) empty_dir_path = os.path.join(host_dir, 'empty') os.mkdir(empty_dir_path); self.device.push(host_dir, self.DEVICE_TEMP_DIR) self.device.push(empty_dir_path, self.DEVICE_TEMP_DIR) test_empty_cmd = ['[', '-d', os.path.join(self.DEVICE_TEMP_DIR, 'empty')] Loading Loading @@ -1032,7 +1033,8 @@ class FileOperationsTest(DeviceTest): if host_dir is not None: shutil.rmtree(host_dir) def test_pull_symlink_dir(self): # selinux prevents adbd from accessing symlinks on /data/local/tmp. def disabled_test_pull_symlink_dir(self): """Pull a symlink to a directory of symlinks to files.""" try: host_dir = tempfile.mkdtemp() Loading
adb/transport.cpp +20 −7 Original line number Diff line number Diff line Loading @@ -1190,14 +1190,15 @@ void close_usb_devices() { } #endif // ADB_HOST int register_socket_transport(unique_fd s, std::string serial, int port, int local, atransport::ReconnectCallback reconnect) { bool register_socket_transport(unique_fd s, std::string serial, int port, int local, atransport::ReconnectCallback reconnect, int* error) { atransport* t = new atransport(std::move(reconnect), kCsOffline); D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port); if (init_socket_transport(t, std::move(s), port, local) < 0) { delete t; return -1; if (error) *error = errno; return false; } std::unique_lock<std::recursive_mutex> lock(transport_lock); Loading @@ -1206,7 +1207,8 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc VLOG(TRANSPORT) << "socket transport " << transport->serial << " is already in pending_list and fails to register"; delete t; return -EALREADY; if (error) *error = EALREADY; return false; } } Loading @@ -1215,7 +1217,8 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc VLOG(TRANSPORT) << "socket transport " << transport->serial << " is already in transport_list and fails to register"; delete t; return -EALREADY; if (error) *error = EALREADY; return false; } } Loading @@ -1229,10 +1232,20 @@ int register_socket_transport(unique_fd s, std::string serial, int port, int loc if (local == 1) { // Do not wait for emulator transports. return 0; return true; } return waitable->WaitForConnection(std::chrono::seconds(10)) ? 0 : -1; if (!waitable->WaitForConnection(std::chrono::seconds(10))) { if (error) *error = ETIMEDOUT; return false; } if (t->GetConnectionState() == kCsUnauthorized) { if (error) *error = EPERM; return false; } return true; } #if ADB_HOST Loading