Commit graph

22 commits

Author SHA1 Message Date
Callum Ryan
8539cb3f75 Allow the adb server to bind on ::1
Currently the adb server can only bind on 127.0.0.1 or * if -a is
provided. This diff adds the ability for adb to bind on ::1 as
well for use cases where you might want to force adb to be IPv6
only.

To bind the adb server on ::1
```
$ ./adb -L tcp:[::1]:1234 server
$ lsof -nPi :1234
COMMAND ... NODE NAME
adb     ...  TCP [::1]:1234 (LISTEN)
```
The original behaviour is also retained, so this would only affect
users explicitly specifying ::1 in the socket spec
```
$ export ANDROID_ADB_SERVER_PORT=1234
$ ./adb server
$ lsof -nPi :1234
COMMAND ... NODE NAME
adb     ...  TCP 127.0.0.1:1234 (LISTEN)
```

Note: I've only implemented this behaviour for posix base systems,
due to my limited understanding of networking on windows. If
needed I can do some research to implement it there as well, but
there is currently no IPv6 support at all on the windows side.

Test: New unit test and the commands in the summary

Change-Id: I23e4531e8dfda4de9348124ad7491d728aecdbf7
2019-11-07 08:32:01 -08:00
Julien Desprez
fb4c13500f Temporarily @ignore test_reconnect
For the purpose of on-boarding all the other tests,
skip test_reconnect for now.

Test: atest -v adb_integration_test_adb
Bug: 123247844
Change-Id: I50269272adbfc3f946d71bae13677546566d541f
2019-02-20 09:42:49 -08:00
Josh Gao
aa95aa063f adb: hopefully deflake test_adb.py.
The reconnection test is spuriously failing on test infrastructure for
unclear reasons, which might be due to a race between the connection
attempt and the first command we send. Insert a sleep to hopefully
reduce the flakiness.

Bug: http://b/123247844
Test: ./test_adb.py (but it didn't fail for me in the first place)
Change-Id: Ic36924c16bae424accfec700af4623794fd1f123
2019-02-07 17:53:29 -08:00
Josh Gao
42c86722fc test_adb.py: silence ResourceWarning.
Test: ./adb_test.py
Change-Id: If12558492e4edafd3568530a96eac2032526877d
2018-10-16 11:00:39 -07:00
Spencer Low
8f6bdc9eb5 adb: win32: test USB kick on resume from sleep/hibernation
Unittest for a5b06b0ff8. Works by
simulating resume from sleep/hibernation by sending a Windows message.

Test: python3 -m unittest test_adb.PowerTest

Change-Id: I78510f30c012f68eda39764da522dbf8d03f2576
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2018-08-31 19:49:46 -07:00
Josh Gao
92cd59fc31 adb: fix test_adb.py's adb server spawning on Windows.
Test: test_adb.py on windows vm
Change-Id: I918678be7ececd167969789ecff7cfb58829fa1d
2018-08-21 14:28:56 -07:00
Josh Gao
902dace132 adb: make disconnect stop reconnection immediately.
Make `adb disconnect` remove transports immediately, instead of on
their next reconnection cycle.

Test: adb connect unreachable:12345; adb devices; adb disconnect; adb devices
Change-Id: I35c8b57344e847575596d09216fc636be47dde64
2018-08-13 16:33:54 -07:00
Josh Gao
b3610734f8 adb: switch test_adb.py over to double quotes.
Test: ./test_adb.py
Change-Id: I3a568361d54f32cc895cea439de0f2c38aee5e2d
2018-08-08 17:40:15 -07:00
Josh Gao
676f375d9b adb: make test_adb.py work on windows.
Switch from os.pipe to socket.socketpair for the fake adbd termination
termination signaller, that we can select on it on windows.

Test: test_adb.py on windows (2 failures on windows)
Change-Id: I37df06e465ec8be28cfb18a5c21ef30125195004
2018-08-08 17:40:15 -07:00
Josh Gao
9b6522b357 adb: switch test_adb.py to python3.
Currently, test_adb.py doesn't work on Windows, because we're selecting
on a pipe, which doesn't work on Windows. Python 3.5 adds socketpair
support to Windows, so switch over to Python 3 to make it so we can use
that.

Test: python3 test_adb.py
Change-Id: I0fbd1bca0b28324658831d5ef8ee08aefe2b0596
2018-08-08 17:40:15 -07:00
Luis Hector Chavez
3255974169 adb: Add a test for emulator connection
This should prevent regressions in the future.

Bug: 78991667
Test: python system/core/adb/test_adb.py
Change-Id: I4d6da40da82c6d79797cec82ffaf071d4b56ddc7
2018-05-16 15:20:48 -07:00
Luis Hector Chavez
fbee0a9133 adb: Improve test_adb a bit more
This change:

* uses unittest.main(), which allows for a subset of the tests to be
  selected.
* drops the requirement to have a device already connected since all the
  tests that need a device now spin their own mock device.
* Splits the monolithic test class into more granular classes.
* Makes this file be pylint-compliant.

Bug: None
Test: python system/core/adb/test_adb.py
Test: pylint system/core/adb/test_adb.py
Change-Id: I91c7ced520c3c69f855d639e0dbf7e57bb690e97
2018-05-16 15:20:48 -07:00
Luis Hector Chavez
454bc7c0be adb: Add a way to reconnect TCP transports
This change adds a reconnect handler that tracks all TCP transports that
were connected at some point, but became disconnected. It does so by
attempting to reconnect every 10s for up to a minute.

Bug: 74411879
Test: system/core/adb/test_adb.py
Test: adb connect chromebook:22  # This runs with sslh
Test: CtsBootStatsTestCases
Test: emulator -show-kernel ; adb -s emulator-5554 shell

Change-Id: I7b9f6d181b71ccf5c26ff96c45d36aaf6409b992
2018-05-16 15:20:48 -07:00
Luis Hector Chavez
56fe753070 adb: Add a way to distinguish between connection failures and successes
This change adds a callback that is invoked exactly once, either when
the connection is fully established (i.e. CNXN packets have been sent
and received) or the atransport object is deleted before that (because
the connection failed).

This helps in distinguishing between successful and failing connections
for TCP. Especially when there is some kind of port
forwarding/multiplexing in between (like an SSH tunnel or SSLH proxy).

Bug: 74411879
Test: adb connect chromebook:22 (which runs an sslh tunnel to adbd).
      either succeeds or fails, but not fake-succeeds.

Change-Id: I7e826c6f5d4c30338a03b2d376a857ac5d05672a
2018-04-26 13:53:35 -07:00
Luis Hector Chavez
8b67c52099 Improve test_adb.py
This change uses a context manager to create the fake ADB servers (and
cleanly tear them down.

Bug: 74411879
Test: python system/core/adb/test_adb.py

Change-Id: I722d2c4135259b1b0ef00a1510aa8402e87ecf72
2018-04-17 21:49:43 -07:00
Josh Gao
c251ec55d3 adb: don't abort when connecting to the same address twice.
When connecting to an address, we construct a transport first, and then
check whether we've already connected to that address. The consequent
destruction of the BlockingConnectionAdapter attempts to join threads
that haven't been started, which aborts.

Make it safe to destruct a BlockingConnectionAdapter without calling
Start on it first, to solve this.

Bug: http://b/69137547
Test: nc -l 12345 & (adb connect localhost:12345; adb connect localhost:12345)
Test: python test_adb.py
Change-Id: I6cb968a62dbac6332907e06575893d764905ee62
2018-04-03 13:58:21 -07:00
Josh Gao
4abb5074be adb: skip IPv6 test if IPv6 isn't available.
Bug: http://b/69813298
Test: none
Change-Id: I0793e793bd52c5f1c639faedf09a513df263db78
2018-03-19 18:19:47 -07:00
Elliott Hughes
e163298ef2 Fix adb tcpip tests.
The test was assuming we still output the full help for every syntax error.
While I'm here, make the diagnostics suck less.

Bug: N/A
Test: ran tests
Change-Id: Idc28616f20c66391f32046cf4216f122998a84bd
2017-08-23 15:43:34 -07:00
Josh Gao
78cc20f007 libcutils: try all addresses in socket_network_client_timeout.
If a connection fails to an address that resolves to multiple
sockaddrs, attempt connecting to subsequent addresses if the initial
connection fails to a reason other than timeout. This is primarily
useful for localhost, which can resolve to both an IPv4 and and IPv6
address.

Also, add an adb test to verify that this behavior.

Bug: http://b/30313466
Change-Id: Ib2df706a66cf6ef8c1097fdfd7aedb69b8df2d6e
Test: python test_adb.py (+ the test fails before this patch)
2016-09-01 15:56:58 -07:00
Spencer Low
351ecd15b2 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-30 16:23:10 -07:00
Spencer Low
1ce06087db adb unittest for win32 handle inheritance
adb.cpp: launch_server() has a long comment about how
stdin/stdout/stderr handles have to be made non-inheritable to prevent
hangs in callers to adb.exe.

It would be disastrous to do this wrong, and I've modified this code, so
here's a unittest to verify that I'm doing it right.

The test also runs fine on unix.

Change-Id: I3672c3066bc7498635c19212f9e5c50757942439
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-09-16 20:50:53 -07:00
Dan Albert
8e1fdd7806 Create adb Python package.
This is mostly just the AdbWrapper that we used in our tests, but I've
cleaned up the API to be a little more Pythonic (mostly in the sense
that commands are passed as lists rather than strings that are
shlex.split() by the shell command), and implemented the workaround
error checking for adb shell.

Move the tests up a directory. Having them buried a level down has
only been annoying.

There are now two files containing Python tests. test_device.py
contains tests specifically checking the AndroidDevice API, and
test_adb.py checks the ADB client program. To run both, use

    python -m unittest discover [-v]

Change-Id: Ibd158c528d31126a5b048bd00bc93039dbc468bc
2015-07-27 15:52:15 -07:00