Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 1 | #!/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 | |
| 16 | set -euxo pipefail |
| 17 | |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 18 | ADB_TESTS="adbd_test adb_crypto_test adb_pairing_auth_test adb_pairing_connection_test adb_tls_connection_test" |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 19 | TRACEDIR=`mktemp -d` |
| 20 | |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 21 | ### Make sure we can connect to the device. |
| 22 | |
| 23 | # Get the device's wlan0 address. |
| 24 | IP_ADDR=$(adb shell ip route get 0.0.0.0 oif wlan0 | sed -En -e 's/.*src (\S+)\s.*/\1/p') |
| 25 | REMOTE_PORT=5555 |
| 26 | REMOTE=$IP_ADDR:$REMOTE_PORT |
| 27 | LOCAL_SERIAL=$(adb shell getprop ro.serialno) |
| 28 | |
| 29 | # Check that we can connect to it. |
| 30 | adb disconnect |
| 31 | adb tcpip $REMOTE_PORT |
| 32 | |
| 33 | # TODO: Add `adb transport-id` and wait-for-offline on it. |
| 34 | sleep 5 |
| 35 | |
| 36 | adb connect $REMOTE |
| 37 | |
| 38 | REMOTE_FETCHED_SERIAL=$(adb -s $REMOTE shell getprop ro.serialno) |
| 39 | |
| 40 | if [[ "$LOCAL_SERIAL" != "$REMOTE_FETCHED_SERIAL" ]]; then |
| 41 | echo "Mismatch: local serial = $LOCAL_SERIAL, remote serial = $REMOTE_FETCHED_SERIAL" |
| 42 | exit 1 |
| 43 | fi |
| 44 | |
| 45 | # Back to USB, and make sure adbd is root. |
| 46 | adb disconnect $REMOTE |
| 47 | |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 48 | adb root |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 49 | adb wait-for-device usb |
| 50 | |
| 51 | # TODO: Add `adb transport-id` and wait-for-offline on it. |
| 52 | sleep 5 |
| 53 | |
| 54 | adb wait-for-device |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 55 | |
| 56 | ### Run the adb unit tests and fetch traces from them. |
| 57 | mkdir "$TRACEDIR"/test_traces |
| 58 | adb shell rm -rf /data/local/tmp/adb_coverage |
| 59 | adb shell mkdir /data/local/tmp/adb_coverage |
| 60 | |
| 61 | for 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/ |
| 64 | done |
| 65 | |
| 66 | adb pull /data/local/tmp/adb_coverage "$TRACEDIR"/test_traces |
| 67 | |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 68 | # Clear logcat and increase the buffer to something ridiculous so we can fetch the pids of adbd later. |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 69 | adb shell logcat -c -G128M |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 70 | |
| 71 | # Turn on extremely verbose logging so as to not count debug logging against us. |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 72 | adb shell setprop persist.adb.trace_mask 1 |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 73 | |
| 74 | ### Run test_device.py over USB. |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 75 | adb shell killall adbd |
| 76 | |
| 77 | # TODO: Add `adb transport-id` and wait-for-offline on it. |
| 78 | sleep 5 |
| 79 | |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 80 | adb wait-for-device shell rm -rf "/data/misc/trace/*" /data/local/tmp/adb_coverage/ |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 81 | ./test_device.py |
| 82 | |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 83 | # Do a usb reset to exercise the disconnect code. |
| 84 | adb_usbreset |
| 85 | adb wait-for-device |
| 86 | |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 87 | # Dump traces from the currently running adbd. |
| 88 | adb shell killall -37 adbd |
| 89 | |
| 90 | echo Waiting for adbd to finish dumping traces |
| 91 | sleep 5 |
| 92 | |
Josh Gao | 79d4f14 | 2020-04-21 14:22:50 -0700 | [diff] [blame^] | 93 | # Restart adbd in tcp mode. |
| 94 | adb tcpip $REMOTE_PORT |
| 95 | sleep 5 |
| 96 | adb connect $REMOTE |
| 97 | adb -s $REMOTE wait-for-device |
| 98 | |
| 99 | # Run test_device.py again. |
| 100 | ANDROID_SERIAL=$REMOTE ./test_device.py |
| 101 | |
| 102 | # Dump traces again. |
| 103 | adb disconnect $REMOTE |
| 104 | adb shell killall -37 adbd |
| 105 | |
| 106 | echo Waiting for adbd to finish dumping traces |
| 107 | sleep 5 |
| 108 | |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 109 | adb pull /data/misc/trace "$TRACEDIR"/ |
| 110 | echo Pulled traces to $TRACEDIR |
| 111 | |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 112 | # Identify which of the trace files are actually adbd, in case something else exited simultaneously. |
| 113 | ADBD_PIDS=$(adb shell "logcat -d -s adbd --format=process | grep 'adbd started' | cut -c 3-7 | tr -d ' ' | sort | uniq") |
| 114 | mkdir "$TRACEDIR"/adbd_traces |
| 115 | |
| 116 | adb shell 'setprop persist.adb.trace_mask 0; killall adbd' |
| 117 | |
| 118 | IFS=$'\n' |
| 119 | for PID in $ADBD_PIDS; do |
| 120 | cp "$TRACEDIR"/trace/clang-$PID-*.profraw "$TRACEDIR"/adbd_traces 2>/dev/null || true |
| 121 | done |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 122 | unset IFS |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 123 | |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 124 | ADB_TEST_BINARIES="" |
| 125 | for TEST in $ADB_TESTS; do |
| 126 | ADB_TEST_BINARIES="--object=$ANDROID_PRODUCT_OUT/data/nativetest64/$TEST/$TEST $ADB_TEST_BINARIES" |
| 127 | done |
| 128 | |
| 129 | ### Merge the traces and generate a report. |
| 130 | llvm-profdata merge --output="$TRACEDIR"/adbd.profdata "$TRACEDIR"/adbd_traces/* "$TRACEDIR"/test_traces/* |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 131 | |
| 132 | cd $ANDROID_BUILD_TOP |
| 133 | llvm-cov report --instr-profile="$TRACEDIR"/adbd.profdata \ |
| 134 | $ANDROID_PRODUCT_OUT/apex/com.android.adbd/bin/adbd \ |
| 135 | --show-region-summary=false \ |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 136 | /proc/self/cwd/system/core/adb \ |
| 137 | $ADB_TEST_BINARIES |
Josh Gao | c0d8686 | 2020-04-20 17:23:32 -0700 | [diff] [blame] | 138 | |
| 139 | llvm-cov show --instr-profile="$TRACEDIR"/adbd.profdata \ |
| 140 | $ANDROID_PRODUCT_OUT/apex/com.android.adbd/bin/adbd \ |
| 141 | --format=html \ |
Josh Gao | 03bee48 | 2020-04-20 19:21:41 -0700 | [diff] [blame] | 142 | /proc/self/cwd/system/core/adb \ |
| 143 | $ADB_TEST_BINARIES > $TRACEDIR/report.html |