2015-07-25 02:08:33 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
"""Tests for the adb program itself.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-04-18 04:25:33 +02:00
|
|
|
import binascii
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
import contextlib
|
2015-09-17 05:45:53 +02:00
|
|
|
import os
|
2015-07-25 02:08:33 +02:00
|
|
|
import random
|
2018-04-18 04:25:33 +02:00
|
|
|
import select
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
import socket
|
|
|
|
import struct
|
2015-07-25 02:08:33 +02:00
|
|
|
import subprocess
|
2015-09-17 05:45:53 +02:00
|
|
|
import threading
|
2015-07-25 02:08:33 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import adb
|
|
|
|
|
|
|
|
|
2018-04-18 04:25:33 +02:00
|
|
|
@contextlib.contextmanager
|
2018-05-02 18:10:29 +02:00
|
|
|
def fake_adbd(protocol=socket.AF_INET, port=0):
|
|
|
|
"""Creates a fake ADB daemon that just replies with a CNXN packet."""
|
2018-04-18 04:25:33 +02:00
|
|
|
|
|
|
|
serversock = socket.socket(protocol, socket.SOCK_STREAM)
|
2018-05-02 18:10:29 +02:00
|
|
|
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2018-04-18 04:25:33 +02:00
|
|
|
if protocol == socket.AF_INET:
|
|
|
|
serversock.bind(('127.0.0.1', port))
|
|
|
|
else:
|
|
|
|
serversock.bind(('::1', port))
|
|
|
|
serversock.listen(1)
|
|
|
|
|
|
|
|
# A pipe that is used to signal the thread that it should terminate.
|
|
|
|
readpipe, writepipe = os.pipe()
|
|
|
|
|
2018-04-17 23:25:04 +02:00
|
|
|
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,
|
|
|
|
bin_command ^ 0xffffffff)
|
|
|
|
buf += data
|
|
|
|
return buf
|
|
|
|
|
2018-04-18 04:25:33 +02:00
|
|
|
def _handle():
|
|
|
|
rlist = [readpipe, serversock]
|
2018-04-17 23:25:04 +02:00
|
|
|
cnxn_sent = {}
|
2018-04-18 04:25:33 +02:00
|
|
|
while True:
|
2018-05-02 18:10:29 +02:00
|
|
|
read_ready, _, _ = select.select(rlist, [], [])
|
|
|
|
for ready in read_ready:
|
|
|
|
if ready == readpipe:
|
2018-04-18 04:25:33 +02:00
|
|
|
# Closure pipe
|
2018-05-02 18:10:29 +02:00
|
|
|
os.close(ready)
|
2018-04-18 04:25:33 +02:00
|
|
|
serversock.shutdown(socket.SHUT_RDWR)
|
|
|
|
serversock.close()
|
|
|
|
return
|
2018-05-02 18:10:29 +02:00
|
|
|
elif ready == serversock:
|
2018-04-18 04:25:33 +02:00
|
|
|
# Server socket
|
2018-05-02 18:10:29 +02:00
|
|
|
conn, _ = ready.accept()
|
2018-04-18 04:25:33 +02:00
|
|
|
rlist.append(conn)
|
|
|
|
else:
|
|
|
|
# Client socket
|
2018-05-02 18:10:29 +02:00
|
|
|
data = ready.recv(1024)
|
2018-04-20 19:31:29 +02:00
|
|
|
if not data or data.startswith('OPEN'):
|
2018-05-02 18:10:29 +02:00
|
|
|
if ready in cnxn_sent:
|
|
|
|
del cnxn_sent[ready]
|
|
|
|
ready.shutdown(socket.SHUT_RDWR)
|
|
|
|
ready.close()
|
|
|
|
rlist.remove(ready)
|
2018-04-17 23:25:04 +02:00
|
|
|
continue
|
2018-05-02 18:10:29 +02:00
|
|
|
if ready in cnxn_sent:
|
2018-04-17 23:25:04 +02:00
|
|
|
continue
|
2018-05-02 18:10:29 +02:00
|
|
|
cnxn_sent[ready] = True
|
|
|
|
ready.sendall(_adb_packet('CNXN', 0x01000001, 1024 * 1024,
|
|
|
|
'device::ro.product.name=fakeadb'))
|
2018-04-18 04:25:33 +02:00
|
|
|
|
|
|
|
port = serversock.getsockname()[1]
|
|
|
|
server_thread = threading.Thread(target=_handle)
|
|
|
|
server_thread.start()
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield port
|
|
|
|
finally:
|
|
|
|
os.close(writepipe)
|
|
|
|
server_thread.join()
|
|
|
|
|
|
|
|
|
2018-04-20 19:31:29 +02:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def adb_connect(unittest, serial):
|
|
|
|
"""Context manager for an ADB connection.
|
|
|
|
|
|
|
|
This automatically disconnects when done with the connection.
|
|
|
|
"""
|
|
|
|
|
|
|
|
output = subprocess.check_output(['adb', 'connect', serial])
|
|
|
|
unittest.assertEqual(output.strip(), 'connected to {}'.format(serial))
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
# Perform best-effort disconnection. Discard the output.
|
2018-05-02 18:10:29 +02:00
|
|
|
subprocess.Popen(['adb', 'disconnect', serial],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE).communicate()
|
2018-04-20 19:31:29 +02:00
|
|
|
|
|
|
|
|
2018-05-02 19:47:01 +02:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def adb_server():
|
|
|
|
"""Context manager for an ADB server.
|
|
|
|
|
|
|
|
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'],
|
|
|
|
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)])
|
|
|
|
try:
|
|
|
|
os.close(write_pipe)
|
|
|
|
greeting = os.read(read_pipe, 1024)
|
|
|
|
assert greeting == 'OK\n', repr(greeting)
|
|
|
|
yield port
|
|
|
|
finally:
|
|
|
|
proc.terminate()
|
|
|
|
proc.wait()
|
|
|
|
|
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
class CommandlineTest(unittest.TestCase):
|
|
|
|
"""Tests for the ADB commandline."""
|
2015-07-25 02:08:33 +02:00
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
"""Make sure we get _something_ out of help."""
|
|
|
|
out = subprocess.check_output(
|
|
|
|
['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()
|
|
|
|
version_line = lines[0]
|
|
|
|
self.assertRegexpMatches(
|
|
|
|
version_line, r'^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$')
|
|
|
|
|
|
|
|
def test_tcpip_error_messages(self):
|
2018-05-02 18:10:29 +02:00
|
|
|
"""Make sure 'adb tcpip' parsing is sane."""
|
|
|
|
proc = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
out, _ = proc.communicate()
|
|
|
|
self.assertEqual(1, proc.returncode)
|
2017-08-24 00:42:28 +02:00
|
|
|
self.assertIn('requires an argument', out)
|
2015-07-25 02:08:33 +02:00
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
proc = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
out, _ = proc.communicate()
|
|
|
|
self.assertEqual(1, proc.returncode)
|
2017-08-24 00:42:28 +02:00
|
|
|
self.assertIn('invalid port', out)
|
2015-07-25 02:08:33 +02:00
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
|
|
|
|
class ServerTest(unittest.TestCase):
|
|
|
|
"""Tests for the ADB server."""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _read_pipe_and_set_event(pipe, event):
|
|
|
|
"""Reads a pipe until it is closed, then sets the event."""
|
|
|
|
pipe.read()
|
2015-09-17 05:45:53 +02:00
|
|
|
event.set()
|
|
|
|
|
|
|
|
def test_handle_inheritance(self):
|
2018-05-02 18:10:29 +02:00
|
|
|
"""Test that launch_server() does not inherit handles.
|
|
|
|
|
|
|
|
launch_server() should not let the adb server inherit
|
|
|
|
stdin/stdout/stderr handles, which can cause callers of adb.exe to hang.
|
|
|
|
This test also runs fine on unix even though the impetus is an issue
|
|
|
|
unique to Windows.
|
|
|
|
"""
|
2015-09-17 05:45:53 +02:00
|
|
|
# This test takes 5 seconds to run on Windows: if there is no adb server
|
|
|
|
# running on the the port used below, adb kill-server tries to make a
|
|
|
|
# TCP connection to a closed port and that takes 1 second on Windows;
|
|
|
|
# adb start-server does the same TCP connection which takes another
|
|
|
|
# second, and it waits 3 seconds after starting the server.
|
|
|
|
|
|
|
|
# Start adb client with redirected stdin/stdout/stderr to check if it
|
|
|
|
# passes those redirections to the adb server that it starts. To do
|
|
|
|
# this, run an instance of the adb server on a non-default port so we
|
|
|
|
# don't conflict with a pre-existing adb server that may already be
|
|
|
|
# setup with adb TCP/emulator connections. If there is a pre-existing
|
|
|
|
# adb server, this also tests whether multiple instances of the adb
|
|
|
|
# server conflict on adb.log.
|
|
|
|
|
|
|
|
port = 5038
|
|
|
|
# Kill any existing server on this non-default port.
|
|
|
|
subprocess.check_output(['adb', '-P', str(port), 'kill-server'],
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Run the adb client and have it start the adb server.
|
2018-05-02 18:10:29 +02:00
|
|
|
proc = subprocess.Popen(['adb', '-P', str(port), 'start-server'],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2015-09-17 05:45:53 +02:00
|
|
|
|
|
|
|
# Start threads that set events when stdout/stderr are closed.
|
|
|
|
stdout_event = threading.Event()
|
|
|
|
stdout_thread = threading.Thread(
|
2018-05-02 18:10:29 +02:00
|
|
|
target=ServerTest._read_pipe_and_set_event,
|
|
|
|
args=(proc.stdout, stdout_event))
|
2015-09-17 05:45:53 +02:00
|
|
|
stdout_thread.daemon = True
|
|
|
|
stdout_thread.start()
|
|
|
|
|
|
|
|
stderr_event = threading.Event()
|
|
|
|
stderr_thread = threading.Thread(
|
2018-05-02 18:10:29 +02:00
|
|
|
target=ServerTest._read_pipe_and_set_event,
|
|
|
|
args=(proc.stderr, stderr_event))
|
2015-09-17 05:45:53 +02:00
|
|
|
stderr_thread.daemon = True
|
|
|
|
stderr_thread.start()
|
|
|
|
|
|
|
|
# Wait for the adb client to finish. Once that has occurred, if
|
|
|
|
# stdin/stderr/stdout are still open, it must be open in the adb
|
|
|
|
# server.
|
2018-05-02 18:10:29 +02:00
|
|
|
proc.wait()
|
2015-09-17 05:45:53 +02:00
|
|
|
|
|
|
|
# Try to write to stdin which we expect is closed. If it isn't
|
|
|
|
# closed, we should get an IOError. If we don't get an IOError,
|
|
|
|
# stdin must still be open in the adb server. The adb client is
|
|
|
|
# probably letting the adb server inherit stdin which would be
|
|
|
|
# wrong.
|
|
|
|
with self.assertRaises(IOError):
|
2018-05-02 18:10:29 +02:00
|
|
|
proc.stdin.write('x')
|
2015-09-17 05:45:53 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
# stdout/stderr were not closed and and they must be open in the adb
|
|
|
|
# server, suggesting that the adb client is letting the adb server
|
|
|
|
# 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")
|
|
|
|
finally:
|
|
|
|
# If we started a server, kill it.
|
|
|
|
subprocess.check_output(['adb', '-P', str(port), 'kill-server'],
|
|
|
|
stderr=subprocess.STDOUT)
|
2015-07-25 02:08:33 +02:00
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
|
|
|
|
class EmulatorTest(unittest.TestCase):
|
|
|
|
"""Tests for the emulator connection."""
|
|
|
|
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
def _reset_socket_on_close(self, sock):
|
2018-05-02 18:10:29 +02:00
|
|
|
"""Use SO_LINGER to cause TCP RST segment to be sent on socket close."""
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
# The linger structure is two shorts on Windows, but two ints on Unix.
|
|
|
|
linger_format = 'hh' if os.name == 'nt' else 'ii'
|
|
|
|
l_onoff = 1
|
|
|
|
l_linger = 0
|
|
|
|
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
|
|
|
|
struct.pack(linger_format, l_onoff, l_linger))
|
|
|
|
# Verify that we set the linger structure properly by retrieving it.
|
|
|
|
linger = sock.getsockopt(socket.SOL_SOCKET, socket.SO_LINGER, 16)
|
|
|
|
self.assertEqual((l_onoff, l_linger),
|
|
|
|
struct.unpack_from(linger_format, linger))
|
|
|
|
|
|
|
|
def test_emu_kill(self):
|
|
|
|
"""Ensure that adb emu kill works.
|
|
|
|
|
|
|
|
Bug: https://code.google.com/p/android/issues/detail?id=21021
|
|
|
|
"""
|
|
|
|
with contextlib.closing(
|
2018-05-02 18:10:29 +02:00
|
|
|
socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as listener:
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
# 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)
|
2018-04-03 21:55:18 +02:00
|
|
|
listener.bind(('127.0.0.1', 0))
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
listener.listen(4)
|
2018-04-03 21:55:18 +02:00
|
|
|
port = listener.getsockname()[1]
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
|
|
|
|
# Now that listening has started, start adb emu kill, telling it to
|
|
|
|
# connect to our mock emulator.
|
2018-05-02 18:10:29 +02:00
|
|
|
proc = subprocess.Popen(
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
['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 ' +
|
2018-05-02 18:10:29 +02:00
|
|
|
'of commands\r\n')
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
conn.sendall('OK\r\n')
|
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
with contextlib.closing(conn.makefile()) as connf:
|
|
|
|
line = connf.readline()
|
|
|
|
if line.startswith('auth'):
|
|
|
|
# Ignore the first auth line.
|
|
|
|
line = connf.readline()
|
|
|
|
self.assertEqual('kill\n', line)
|
|
|
|
self.assertEqual('quit\n', connf.readline())
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
|
|
|
|
conn.sendall('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
|
|
|
|
# real emulator because it just calls exit() without closing
|
|
|
|
# the socket or calling shutdown(SD_SEND). At process
|
|
|
|
# termination, Windows sends a TCP RST segment for every
|
|
|
|
# open socket that shutdown(SD_SEND) wasn't used on.
|
|
|
|
self._reset_socket_on_close(conn)
|
|
|
|
|
|
|
|
# Wait for adb to finish, so we can check return code.
|
2018-05-02 18:10:29 +02:00
|
|
|
proc.communicate()
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
|
|
|
|
# If this fails, adb probably isn't ignoring WSAECONNRESET when
|
|
|
|
# reading the response from the adb emu kill command (on Windows).
|
2018-05-02 18:10:29 +02:00
|
|
|
self.assertEqual(0, proc.returncode)
|
|
|
|
|
2018-05-02 19:47:01 +02:00
|
|
|
def test_emulator_connect(self):
|
|
|
|
"""Ensure that the emulator can connect.
|
|
|
|
|
|
|
|
Bug: http://b/78991667
|
|
|
|
"""
|
|
|
|
with adb_server() as server_port:
|
|
|
|
with fake_adbd() as port:
|
|
|
|
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'],
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
self.fail('Device should not be available')
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
self.assertEqual(
|
|
|
|
err.output.strip(),
|
|
|
|
'error: device \'{}\' not found'.format(serial))
|
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
|
|
|
# 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')
|
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
|
|
|
|
class ConnectionTest(unittest.TestCase):
|
|
|
|
"""Tests for adb connect."""
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
|
2016-09-01 23:54:18 +02:00
|
|
|
def test_connect_ipv4_ipv6(self):
|
|
|
|
"""Ensure that `adb connect localhost:1234` will try both IPv4 and IPv6.
|
|
|
|
|
|
|
|
Bug: http://b/30313466
|
|
|
|
"""
|
2018-04-18 04:25:33 +02:00
|
|
|
for protocol in (socket.AF_INET, socket.AF_INET6):
|
|
|
|
try:
|
2018-05-02 18:10:29 +02:00
|
|
|
with fake_adbd(protocol=protocol) as port:
|
2018-04-20 19:31:29 +02:00
|
|
|
serial = 'localhost:{}'.format(port)
|
|
|
|
with adb_connect(self, serial):
|
|
|
|
pass
|
2018-04-18 04:25:33 +02:00
|
|
|
except socket.error:
|
|
|
|
print("IPv6 not available, skipping")
|
|
|
|
continue
|
2016-09-01 23:54:18 +02:00
|
|
|
|
2018-04-18 04:25:33 +02:00
|
|
|
def test_already_connected(self):
|
2018-04-20 19:31:29 +02:00
|
|
|
"""Ensure that an already-connected device stays connected."""
|
2016-09-01 23:54:18 +02:00
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
with fake_adbd() as port:
|
2018-04-20 19:31:29 +02:00
|
|
|
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])
|
|
|
|
self.assertEqual(
|
|
|
|
output.strip(), 'already connected to {}'.format(serial))
|
2016-09-01 23:54:18 +02:00
|
|
|
|
2018-04-20 19:31:29 +02:00
|
|
|
def test_reconnect(self):
|
|
|
|
"""Ensure that a disconnected device reconnects."""
|
2018-04-03 21:55:18 +02:00
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
with fake_adbd() as port:
|
2018-04-20 19:31:29 +02:00
|
|
|
serial = 'localhost:{}'.format(port)
|
|
|
|
with adb_connect(self, serial):
|
|
|
|
output = subprocess.check_output(['adb', '-s', serial,
|
|
|
|
'get-state'])
|
|
|
|
self.assertEqual(output.strip(), 'device')
|
|
|
|
|
|
|
|
# This will fail.
|
2018-05-02 18:10:29 +02:00
|
|
|
proc = subprocess.Popen(['adb', '-s', serial, 'shell', 'true'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
output, _ = proc.communicate()
|
2018-04-20 19:31:29 +02:00
|
|
|
self.assertEqual(output.strip(), 'error: closed')
|
|
|
|
|
|
|
|
subprocess.check_call(['adb', '-s', serial, 'wait-for-device'])
|
|
|
|
|
|
|
|
output = subprocess.check_output(['adb', '-s', serial,
|
|
|
|
'get-state'])
|
|
|
|
self.assertEqual(output.strip(), 'device')
|
|
|
|
|
|
|
|
# Once we explicitly kick a device, it won't attempt to
|
|
|
|
# reconnect.
|
|
|
|
output = subprocess.check_output(['adb', 'disconnect', serial])
|
|
|
|
self.assertEqual(
|
|
|
|
output.strip(), 'disconnected {}'.format(serial))
|
|
|
|
try:
|
|
|
|
subprocess.check_output(['adb', '-s', serial, 'get-state'],
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
self.fail('Device should not be available')
|
2018-05-02 18:10:29 +02:00
|
|
|
except subprocess.CalledProcessError as err:
|
2018-04-20 19:31:29 +02:00
|
|
|
self.assertEqual(
|
2018-05-02 18:10:29 +02:00
|
|
|
err.output.strip(),
|
2018-04-20 19:31:29 +02:00
|
|
|
'error: device \'{}\' not found'.format(serial))
|
adb: fix adb client running out of sockets on Windows
Background
==========
On Windows, if you run "adb shell exit" in a loop in two windows,
eventually the adb client will be unable to connect to the adb server. I
think connect() is returning WSAEADDRINUSE: "Only one usage of each
socket address (protocol/network address/port) is normally permitted.
(10048)". The Windows System Event Log may also show Event 4227, Tcpip.
Netstat output is filled with:
# for the adb server
TCP 127.0.0.1:5037 127.0.0.1:65523 TIME_WAIT
# for the adb client
TCP 127.0.0.1:65523 127.0.0.1:5037 TIME_WAIT
The error probably means that the client is running out of free
address:port pairs.
The first netstat line is unavoidable, but the second line exists
because the adb client is not waiting for orderly/graceful shutdown of
the socket, and that is apparently required on Windows to get rid of the
second line. For more info, see
https://github.com/CompareAndSwap/SocketCloseTest .
This is exacerbated by the fact that "adb shell exit" makes 4 socket
connections to the adb server: 1) host:version, 2) host:features, 3)
host:version (again), 4) shell:exit. Also exacerbating is the fact that
the adb protocol is length-prefixed so the client typically does not
have to 'read() until zero' which effectively waits for orderly/graceful
shutdown.
The Fix
=======
Introduce a function, ReadOrderlyShutdown(), that should be called in
the adb client to wait for the server to close its socket, before
closing the client socket.
I reviewed all code where the adb client makes a connection to the adb
server and added ReadOrderlyShutdown() when it made sense. I wasn't able
to add it to the following:
* interactive_shell: this doesn't matter because this is interactive and
thus can't be run fast enough to use up ports.
* adb sideload: I couldn't get enough test coverage and I don't think
this is being called frequently enough to be a problem.
* send_shell_command, backup, adb_connect_command, adb shell, adb
exec-out, install_multiple_app, adb_send_emulator_command: These
already wait for server socket shutdown since they already call
recv() until zero.
* restore, adb exec-in: protocol design can't have the server close
first.
* adb start-server: no fd is actually returned
* create_local_service_socket, local_connect_arbitrary_ports,
connect_device: probably called rarely enough not to be a problem.
Also in this change
===================
* Clarify comments in when adb_shutdown() is called before exit().
* add some missing adb_close() in adb sideload.
* Fixup error handling and comments in adb_send_emulator_command().
* Make SyncConnection::SendQuit return a success boolean.
* Add unittest for adb emu kill command. This gets code coverage over
this very careful piece of code.
Change-Id: Iad0b1336f5b74186af2cd35f7ea827d0fa77a17c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-10-15 02:32:44 +02:00
|
|
|
|
2018-05-02 18:10:29 +02:00
|
|
|
|
2015-07-25 02:08:33 +02:00
|
|
|
def main():
|
2018-05-02 18:10:29 +02:00
|
|
|
"""Main entrypoint."""
|
2015-07-25 02:08:33 +02:00
|
|
|
random.seed(0)
|
2018-05-02 18:10:29 +02:00
|
|
|
unittest.main(verbosity=3)
|
2015-07-25 02:08:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|