blob: cde9b6072cbfd37a6bb04f950b86589b9c48f0fa [file] [log] [blame]
Josh Gaoc0d86862020-04-20 17:23:32 -07001#!/bin/bash
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -euxo pipefail
17
Josh Gao03bee482020-04-20 19:21:41 -070018ADB_TESTS="adbd_test adb_crypto_test adb_pairing_auth_test adb_pairing_connection_test adb_tls_connection_test"
Josh Gao03bee482020-04-20 19:21:41 -070019TRACEDIR=`mktemp -d`
20
Josh Gao79d4f142020-04-21 14:22:50 -070021### Make sure we can connect to the device.
22
23# Get the device's wlan0 address.
24IP_ADDR=$(adb shell ip route get 0.0.0.0 oif wlan0 | sed -En -e 's/.*src (\S+)\s.*/\1/p')
25REMOTE_PORT=5555
26REMOTE=$IP_ADDR:$REMOTE_PORT
27LOCAL_SERIAL=$(adb shell getprop ro.serialno)
28
29# Check that we can connect to it.
30adb disconnect
31adb tcpip $REMOTE_PORT
32
33# TODO: Add `adb transport-id` and wait-for-offline on it.
34sleep 5
35
36adb connect $REMOTE
37
38REMOTE_FETCHED_SERIAL=$(adb -s $REMOTE shell getprop ro.serialno)
39
40if [[ "$LOCAL_SERIAL" != "$REMOTE_FETCHED_SERIAL" ]]; then
41 echo "Mismatch: local serial = $LOCAL_SERIAL, remote serial = $REMOTE_FETCHED_SERIAL"
42 exit 1
43fi
44
45# Back to USB, and make sure adbd is root.
46adb disconnect $REMOTE
47
Josh Gaoc0d86862020-04-20 17:23:32 -070048adb root
Josh Gao79d4f142020-04-21 14:22:50 -070049adb wait-for-device usb
50
51# TODO: Add `adb transport-id` and wait-for-offline on it.
52sleep 5
53
54adb wait-for-device
Josh Gao03bee482020-04-20 19:21:41 -070055
56### Run the adb unit tests and fetch traces from them.
57mkdir "$TRACEDIR"/test_traces
58adb shell rm -rf /data/local/tmp/adb_coverage
59adb shell mkdir /data/local/tmp/adb_coverage
60
61for TEST in $ADB_TESTS; do
62 adb shell LLVM_PROFILE_FILE=/data/local/tmp/adb_coverage/$TEST.profraw /data/nativetest64/$TEST/$TEST
63 adb pull /data/local/tmp/adb_coverage/$TEST.profraw "$TRACEDIR"/test_traces/
64done
65
66adb pull /data/local/tmp/adb_coverage "$TRACEDIR"/test_traces
67
Josh Gao79d4f142020-04-21 14:22:50 -070068# Clear logcat and increase the buffer to something ridiculous so we can fetch the pids of adbd later.
Josh Gaoc0d86862020-04-20 17:23:32 -070069adb shell logcat -c -G128M
Josh Gao79d4f142020-04-21 14:22:50 -070070
71# Turn on extremely verbose logging so as to not count debug logging against us.
Josh Gaoc0d86862020-04-20 17:23:32 -070072adb shell setprop persist.adb.trace_mask 1
Josh Gao79d4f142020-04-21 14:22:50 -070073
74### Run test_device.py over USB.
Josh Gaoc0d86862020-04-20 17:23:32 -070075adb shell killall adbd
76
77# TODO: Add `adb transport-id` and wait-for-offline on it.
78sleep 5
79
Josh Gao03bee482020-04-20 19:21:41 -070080adb wait-for-device shell rm -rf "/data/misc/trace/*" /data/local/tmp/adb_coverage/
Josh Gaoc0d86862020-04-20 17:23:32 -070081./test_device.py
82
Josh Gao79d4f142020-04-21 14:22:50 -070083# Do a usb reset to exercise the disconnect code.
84adb_usbreset
85adb wait-for-device
86
Josh Gaoc0d86862020-04-20 17:23:32 -070087# Dump traces from the currently running adbd.
88adb shell killall -37 adbd
89
90echo Waiting for adbd to finish dumping traces
91sleep 5
92
Josh Gao79d4f142020-04-21 14:22:50 -070093# Restart adbd in tcp mode.
94adb tcpip $REMOTE_PORT
95sleep 5
96adb connect $REMOTE
97adb -s $REMOTE wait-for-device
98
99# Run test_device.py again.
100ANDROID_SERIAL=$REMOTE ./test_device.py
101
102# Dump traces again.
103adb disconnect $REMOTE
104adb shell killall -37 adbd
105
106echo Waiting for adbd to finish dumping traces
107sleep 5
108
Josh Gaoc0d86862020-04-20 17:23:32 -0700109adb pull /data/misc/trace "$TRACEDIR"/
110echo Pulled traces to $TRACEDIR
111
Josh Gaoc0d86862020-04-20 17:23:32 -0700112# Identify which of the trace files are actually adbd, in case something else exited simultaneously.
113ADBD_PIDS=$(adb shell "logcat -d -s adbd --format=process | grep 'adbd started' | cut -c 3-7 | tr -d ' ' | sort | uniq")
114mkdir "$TRACEDIR"/adbd_traces
115
116adb shell 'setprop persist.adb.trace_mask 0; killall adbd'
117
118IFS=$'\n'
119for PID in $ADBD_PIDS; do
120 cp "$TRACEDIR"/trace/clang-$PID-*.profraw "$TRACEDIR"/adbd_traces 2>/dev/null || true
121done
Josh Gao03bee482020-04-20 19:21:41 -0700122unset IFS
Josh Gaoc0d86862020-04-20 17:23:32 -0700123
Josh Gao03bee482020-04-20 19:21:41 -0700124ADB_TEST_BINARIES=""
125for TEST in $ADB_TESTS; do
126 ADB_TEST_BINARIES="--object=$ANDROID_PRODUCT_OUT/data/nativetest64/$TEST/$TEST $ADB_TEST_BINARIES"
127done
128
129### Merge the traces and generate a report.
130llvm-profdata merge --output="$TRACEDIR"/adbd.profdata "$TRACEDIR"/adbd_traces/* "$TRACEDIR"/test_traces/*
Josh Gaoc0d86862020-04-20 17:23:32 -0700131
132cd $ANDROID_BUILD_TOP
133llvm-cov report --instr-profile="$TRACEDIR"/adbd.profdata \
134 $ANDROID_PRODUCT_OUT/apex/com.android.adbd/bin/adbd \
135 --show-region-summary=false \
Josh Gao03bee482020-04-20 19:21:41 -0700136 /proc/self/cwd/system/core/adb \
137 $ADB_TEST_BINARIES
Josh Gaoc0d86862020-04-20 17:23:32 -0700138
139llvm-cov show --instr-profile="$TRACEDIR"/adbd.profdata \
140 $ANDROID_PRODUCT_OUT/apex/com.android.adbd/bin/adbd \
141 --format=html \
Josh Gao03bee482020-04-20 19:21:41 -0700142 /proc/self/cwd/system/core/adb \
143 $ADB_TEST_BINARIES > $TRACEDIR/report.html