adbd: test TCP in coverage.

Test: ./coverage.sh
Change-Id: I82a0d54fcc1e19585a27c2d049be40289bf15f60
diff --git a/adb/coverage.sh b/adb/coverage.sh
index bf46f79..cde9b60 100755
--- a/adb/coverage.sh
+++ b/adb/coverage.sh
@@ -16,10 +16,42 @@
 set -euxo pipefail
 
 ADB_TESTS="adbd_test adb_crypto_test adb_pairing_auth_test adb_pairing_connection_test adb_tls_connection_test"
-
 TRACEDIR=`mktemp -d`
 
+### Make sure we can connect to the device.
+
+# Get the device's wlan0 address.
+IP_ADDR=$(adb shell ip route get 0.0.0.0 oif wlan0 | sed -En -e 's/.*src (\S+)\s.*/\1/p')
+REMOTE_PORT=5555
+REMOTE=$IP_ADDR:$REMOTE_PORT
+LOCAL_SERIAL=$(adb shell getprop ro.serialno)
+
+# Check that we can connect to it.
+adb disconnect
+adb tcpip $REMOTE_PORT
+
+# TODO: Add `adb transport-id` and wait-for-offline on it.
+sleep 5
+
+adb connect $REMOTE
+
+REMOTE_FETCHED_SERIAL=$(adb -s $REMOTE shell getprop ro.serialno)
+
+if [[ "$LOCAL_SERIAL" != "$REMOTE_FETCHED_SERIAL" ]]; then
+  echo "Mismatch: local serial = $LOCAL_SERIAL, remote serial = $REMOTE_FETCHED_SERIAL"
+  exit 1
+fi
+
+# Back to USB, and make sure adbd is root.
+adb disconnect $REMOTE
+
 adb root
+adb wait-for-device usb
+
+# TODO: Add `adb transport-id` and wait-for-offline on it.
+sleep 5
+
+adb wait-for-device
 
 ### Run the adb unit tests and fetch traces from them.
 mkdir "$TRACEDIR"/test_traces
@@ -33,24 +65,47 @@
 
 adb pull /data/local/tmp/adb_coverage "$TRACEDIR"/test_traces
 
-### Run test_device.py, and fetch traces from adbd itself.
+# Clear logcat and increase the buffer to something ridiculous so we can fetch the pids of adbd later.
 adb shell logcat -c -G128M
+
+# Turn on extremely verbose logging so as to not count debug logging against us.
 adb shell setprop persist.adb.trace_mask 1
+
+### Run test_device.py over USB.
 adb shell killall adbd
 
 # TODO: Add `adb transport-id` and wait-for-offline on it.
 sleep 5
 
 adb wait-for-device shell rm -rf "/data/misc/trace/*" /data/local/tmp/adb_coverage/
-
 ./test_device.py
 
+# Do a usb reset to exercise the disconnect code.
+adb_usbreset
+adb wait-for-device
+
 # Dump traces from the currently running adbd.
 adb shell killall -37 adbd
 
 echo Waiting for adbd to finish dumping traces
 sleep 5
 
+# Restart adbd in tcp mode.
+adb tcpip $REMOTE_PORT
+sleep 5
+adb connect $REMOTE
+adb -s $REMOTE wait-for-device
+
+# Run test_device.py again.
+ANDROID_SERIAL=$REMOTE ./test_device.py
+
+# Dump traces again.
+adb disconnect $REMOTE
+adb shell killall -37 adbd
+
+echo Waiting for adbd to finish dumping traces
+sleep 5
+
 adb pull /data/misc/trace "$TRACEDIR"/
 echo Pulled traces to $TRACEDIR