Merge changes I722d2c41,Ic216ddef
am: c4ad6b6adc
Change-Id: I773f941918a21118eddc09d59cdc667fe22833eb
This commit is contained in:
commit
2c3f1d6c43
3 changed files with 72 additions and 33 deletions
|
|
@ -21,9 +21,11 @@ things. Most of these tests involve specific error messages or the help text.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import binascii
|
||||||
import contextlib
|
import contextlib
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
import select
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
@ -33,6 +35,52 @@ import unittest
|
||||||
import adb
|
import adb
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def fake_adb_server(protocol=socket.AF_INET, port=0):
|
||||||
|
"""Creates a fake ADB server that just replies with a CNXN packet."""
|
||||||
|
|
||||||
|
serversock = socket.socket(protocol, socket.SOCK_STREAM)
|
||||||
|
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()
|
||||||
|
|
||||||
|
def _handle():
|
||||||
|
rlist = [readpipe, serversock]
|
||||||
|
while True:
|
||||||
|
ready, _, _ = select.select(rlist, [], [])
|
||||||
|
for r in ready:
|
||||||
|
if r == readpipe:
|
||||||
|
# Closure pipe
|
||||||
|
os.close(r)
|
||||||
|
serversock.shutdown(socket.SHUT_RDWR)
|
||||||
|
serversock.close()
|
||||||
|
return
|
||||||
|
elif r == serversock:
|
||||||
|
# Server socket
|
||||||
|
conn, _ = r.accept()
|
||||||
|
rlist.append(conn)
|
||||||
|
else:
|
||||||
|
# Client socket
|
||||||
|
data = r.recv(1024)
|
||||||
|
if not data:
|
||||||
|
rlist.remove(r)
|
||||||
|
|
||||||
|
port = serversock.getsockname()[1]
|
||||||
|
server_thread = threading.Thread(target=_handle)
|
||||||
|
server_thread.start()
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield port
|
||||||
|
finally:
|
||||||
|
os.close(writepipe)
|
||||||
|
server_thread.join()
|
||||||
|
|
||||||
|
|
||||||
class NonApiTest(unittest.TestCase):
|
class NonApiTest(unittest.TestCase):
|
||||||
"""Tests for ADB that aren't a part of the AndroidDevice API."""
|
"""Tests for ADB that aren't a part of the AndroidDevice API."""
|
||||||
|
|
||||||
|
|
@ -211,45 +259,32 @@ class NonApiTest(unittest.TestCase):
|
||||||
|
|
||||||
Bug: http://b/30313466
|
Bug: http://b/30313466
|
||||||
"""
|
"""
|
||||||
ipv4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
for protocol in (socket.AF_INET, socket.AF_INET6):
|
||||||
ipv4.bind(('127.0.0.1', 0))
|
try:
|
||||||
ipv4.listen(1)
|
with fake_adb_server(protocol=protocol) as port:
|
||||||
|
output = subprocess.check_output(
|
||||||
|
['adb', 'connect', 'localhost:{}'.format(port)])
|
||||||
|
|
||||||
ipv6 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
self.assertEqual(
|
||||||
try:
|
output.strip(), 'connected to localhost:{}'.format(port))
|
||||||
ipv6.bind(('::1', ipv4.getsockname()[1] + 1))
|
except socket.error:
|
||||||
ipv6.listen(1)
|
print("IPv6 not available, skipping")
|
||||||
except socket.error:
|
continue
|
||||||
print("IPv6 not available, skipping")
|
|
||||||
return
|
|
||||||
|
|
||||||
for s in (ipv4, ipv6):
|
def test_already_connected(self):
|
||||||
port = s.getsockname()[1]
|
with fake_adb_server() as port:
|
||||||
output = subprocess.check_output(
|
output = subprocess.check_output(
|
||||||
['adb', 'connect', 'localhost:{}'.format(port)])
|
['adb', 'connect', 'localhost:{}'.format(port)])
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output.strip(), 'connected to localhost:{}'.format(port))
|
output.strip(), 'connected to localhost:{}'.format(port))
|
||||||
s.close()
|
|
||||||
|
|
||||||
def test_already_connected(self):
|
# b/31250450: this always returns 0 but probably shouldn't.
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
output = subprocess.check_output(
|
||||||
s.bind(('127.0.0.1', 0))
|
['adb', 'connect', 'localhost:{}'.format(port)])
|
||||||
s.listen(2)
|
|
||||||
|
|
||||||
port = s.getsockname()[1]
|
self.assertEqual(
|
||||||
output = subprocess.check_output(
|
output.strip(), 'already connected to localhost:{}'.format(port))
|
||||||
['adb', 'connect', 'localhost:{}'.format(port)])
|
|
||||||
|
|
||||||
self.assertEqual(
|
|
||||||
output.strip(), 'connected to localhost:{}'.format(port))
|
|
||||||
|
|
||||||
# b/31250450: this always returns 0 but probably shouldn't.
|
|
||||||
output = subprocess.check_output(
|
|
||||||
['adb', 'connect', 'localhost:{}'.format(port)])
|
|
||||||
|
|
||||||
self.assertEqual(
|
|
||||||
output.strip(), 'already connected to localhost:{}'.format(port))
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
random.seed(0)
|
random.seed(0)
|
||||||
|
|
|
||||||
|
|
@ -974,7 +974,7 @@ int register_socket_transport(int s, const char* serial, int port, int local) {
|
||||||
VLOG(TRANSPORT) << "socket transport " << transport->serial
|
VLOG(TRANSPORT) << "socket transport " << transport->serial
|
||||||
<< " is already in pending_list and fails to register";
|
<< " is already in pending_list and fails to register";
|
||||||
delete t;
|
delete t;
|
||||||
return -1;
|
return -EALREADY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -983,7 +983,7 @@ int register_socket_transport(int s, const char* serial, int port, int local) {
|
||||||
VLOG(TRANSPORT) << "socket transport " << transport->serial
|
VLOG(TRANSPORT) << "socket transport " << transport->serial
|
||||||
<< " is already in transport_list and fails to register";
|
<< " is already in transport_list and fails to register";
|
||||||
delete t;
|
delete t;
|
||||||
return -1;
|
return -EALREADY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,11 @@ void connect_device(const std::string& address, std::string* response) {
|
||||||
int ret = register_socket_transport(fd, serial.c_str(), port, 0);
|
int ret = register_socket_transport(fd, serial.c_str(), port, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
adb_close(fd);
|
adb_close(fd);
|
||||||
*response = android::base::StringPrintf("already connected to %s", serial.c_str());
|
if (ret == -EALREADY) {
|
||||||
|
*response = android::base::StringPrintf("already connected to %s", serial.c_str());
|
||||||
|
} else {
|
||||||
|
*response = android::base::StringPrintf("failed to connect to %s", serial.c_str());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
*response = android::base::StringPrintf("connected to %s", serial.c_str());
|
*response = android::base::StringPrintf("connected to %s", serial.c_str());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue