1#! /bin/bash 2 3YELLOW="\033[1;33m" 4NOCOLOR="\033[0m" 5BLUE="\033[1;34m" 6RED="\033[1;91m" 7 8function happy_hedgehog { 9 echo -e "\t${BLUE}" 10 echo -e "\t ___------__" 11 echo -e "\t |\__-- /\ _-" 12 echo -e "\t |/_ __ -" 13 echo -e "\t // \ / \ /__" 14 echo -e "\t | 0 | 0 |__ --_ Gotta go fast!" 15 echo -e "\t \\____-- __ \ ___-" 16 echo -e "\t ( @ __/ / /_" 17 echo -e "\t -_____--- --_" 18 echo -e "\t // \ \\ ___-" 19 echo -e "\t //|\__/ \\ \\" 20 echo -e "\t \_-\_____/ \-\\" 21 echo -e "\t // \\--\|" 22 echo -e "\t ${RED}____${BLUE}// ||${RED}_" 23 echo -e "\t${RED} /_____\ /___\\" 24 echo -e "${NOCOLOR}" 25} 26 27function sad_hedgehog { 28 echo -e "\t${BLUE}" 29 echo -e "\t ___------__" 30 echo -e "\t |\__-- /\ _-" 31 echo -e "\t |/_ __ -" 32 echo -e "\t // \ / \ /__" 33 echo -e "\t | 0 | 0 |__ --_ Gotta go sllloowwww!" 34 echo -e "\t \\____-- __ \ ___-" 35 echo -e "\t ( @ __ / /_" 36 echo -e "\t -_____--- --_" 37 echo -e "\t // \ \\ ___-" 38 echo -e "\t //|\__/ \\ \\" 39 echo -e "\t \_-\_____/ \-\\" 40 echo -e "\t // \\--\|" 41 echo -e "\t ${RED} ____${BLUE}// ||${RED}_" 42 echo -e "\t${RED} /_____\ /___\\" 43 echo -e "{$NOCOLOR}" 44} 45 46PYTHON_BIN="python3.11" 47 48function check_environment { 49 if [[ -z "${ANDROID_BUILD_TOP}" ]] || [[ -z "${ANDROID_HOST_OUT}" ]] ; then 50 echo -e "${RED}ANDROID_BUILD_TOP${NOCOLOR} or ${RED}ANDROID_HOST_OUT${NOCOLOR} is not set for host run" 51 echo -e "Navigate to android root and run:" 52 echo -e "${YELLOW}" 53 echo -e ". build/envsetup.sh" 54 echo -e "lunch <fish>" 55 echo -e "${NOCOLOR}" 56 echo 57 exit 1 58 fi 59 if ! [ -x "$(command -v ${PYTHON_BIN})" ] ; then 60 echo -e "${RED}You must have ${PYTHON_BIN} installed${NOCOLOR}" 61 exit 1 62 fi 63 ${PYTHON_BIN} -m venv -h > /dev/null 64 if [[ $? -ne 0 ]] ; then 65 echo -e "${RED}venv not available for ${PYTHON_BIN}${NOCOLOR}" 66 exit 1 67 fi 68} 69 70ASHMEM_OUT="/dev/shm/out" 71ASHMEM_DIST="${ASHMEM_OUT}/dist" 72ASHMEM_VENV="${ASHMEM_DIST}/bluetooth_venv" 73ASHMEM_GOTTA_GO_FAST="/dev/shm/gottagofast" 74ASHMEM_HOST_LOGS="${ASHMEM_GOTTA_GO_FAST}/logs" 75ASHMEM_OUT_TARGET="${ASHMEM_GOTTA_GO_FAST}/target" 76ASHMEM_SOONG="${ASHMEM_GOTTA_GO_FAST}/out/soong" 77CERT_HOST_LOGS="/tmp/logs/HostOnlyCert" 78CERT_DEVICE_LOGS="TODO: Add this" 79CERT_TEST_VENV=${ANDROID_BUILD_TOP}/out/dist/bluetooth_venv 80OUT_TARGET="${ANDROID_BUILD_TOP}/out/target" 81TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/gd/host_config.yaml" 82TEST_FILTER="--presubmit" 83TEST_RUNNER="blueberry/tests/gd/gd_test_runner.py" 84CPP_BUILD_TARGET="bluetooth_stack_with_facade root-canal gd_hci_packets_python3_gen gd_smp_packets_python3_gen" 85RUST_BUILD_TARGET="bluetooth_with_facades root-canal bt_topshim_facade gd_hci_packets_python3_gen gd_smp_packets_python3_gen" 86BUILD_TARGET=$CPP_BUILD_TARGET 87 88CLEAN_VENV=false 89GOTTA_GO_FAST=false 90NUM_REPETITIONS="1" 91SKIP_SOONG_BUILD=false 92USE_ASHMEM_VENV=true 93VERBOSE_MODE=false 94DEVICE_TEST=false 95 96## Verify devices connected and valid 97DUT_SERIAL="DUT Not Set" 98DUT_ADB="DUT Not Set" 99DUT_NAME="DUT Not Set" 100 101# Used for position arguments needed for later 102POSITIONAL=() 103function parse_options { 104 while [[ $# -gt 0 ]] 105 do 106 key="$1" 107 case $key in 108 # This will delete the existing venv before running the test 109 # If you updated external libraries such as ACTS, you need to add this flag 110 --clean) 111 CLEAN_VENV=true 112 shift # past argument 113 ;; 114 --help) 115 echo 116 echo -e "${YELLOW}Help menu${NOCOLOR}" 117 echo -e "===================================" 118 echo -e "${BLUE} --clean${NOCOLOR}" 119 echo -e " Clean the virtul environment; use if ACTS has been updated." 120 echo -e "${BLUE} --disable-ashmem-venv${NOCOLOR}" 121 echo -e " Places the virtual environment on disk rather than in ashmem which is default." 122 echo -e "${BLUE} --gotta-go-fast${NOCOLOR}" 123 echo -e " Makes use of ashmem as best as possible for targeted speed increases." 124 echo -e "${BLUE} --device${NOCOLOR}" 125 echo -e " Run the test on the 2 real devices." 126 echo -e "${BLUE} --sl4a${NOCOLOR}" 127 echo -e " Run GD Sl4A combination tests using the default gd_sl4a config." 128 echo -e " Please install the correct SL4A build to DUT manually before running tests." 129 echo -e "${BLUE} --sl4a_sl4a${NOCOLOR}" 130 echo -e " Run SL$A combination tests using the default sl4a_sl4a config." 131 echo -e " Please install the correct SL4A build to DUT and cert manually before running tests." 132 echo -e "${BLUE} --topshim${NOCOLOR}" 133 echo -e " Run Topshim combination tests using the default topshim config." 134 echo -e "${BLUE} --rust${NOCOLOR}" 135 echo -e " Run the test using the rust implementation on the 2 real devices." 136 echo -e "${BLUE} --rhost${NOCOLOR}" 137 echo -e " Run the test using the rust implementation on the host." 138 echo -e "${BLUE} --repeat=<N>${NOCOLOR}" 139 echo -e " Repeat the test sequence N (int) number of times." 140 echo -e "${BLUE} --skip-soong-build${NOCOLOR}" 141 echo -e " Skips building soong targets. Use when you are just modifying simple python files." 142 echo -e "${BLUE} --test_config=<configfile>${NOCOLOR}" 143 echo -e " Override default test configuration." 144 echo -e "${BLUE} --verbose${NOCOLOR}" 145 echo -e " Displays device logs and test logs to output." 146 echo 147 echo -e "Usage: $0 [--clean|--repeat=<N>|--test_config=<config>] [TestGroupName[.IndividualTestName]]" 148 echo -e " ${YELLOW}e.g." 149 echo -e " $0 --clean SecurityTest" 150 echo -e " $0 --verbose SecurityTest:test_dut_initiated_display_only_display_only ${NOCOLOR}" 151 echo 152 shift 153 exit 0 154 ;; 155 # This will cause the bluetooth_venv to NOT be created in ashmem 156 # Using ashmem increases --clean build times by 40% (~21 seconds on my machine) 157 --disable-ashmem-venv) 158 USE_ASHMEM_VENV=false 159 shift # past argument 160 ;; 161 --gotta-go-fast) 162 GOTTA_GO_FAST=true 163 shift # past argument 164 ;; 165 --device) 166 DEVICE_TEST=true 167 TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/gd/devices_config.yaml" 168 shift # past argument 169 ;; 170 # Repeat running the specified test cases by N times in one single setup 171 --repeat=*) 172 NUM_REPETITIONS="${key#*=}" 173 shift # past argument 174 ;; 175 --skip-soong-build) 176 SKIP_SOONG_BUILD=true 177 shift 178 ;; 179 --test_config=*) 180 TEST_CONFIG="${key#*=}" 181 shift # past argument 182 ;; 183 --rust) 184 BUILD_TARGET=$RUST_BUILD_TARGET 185 export RUST_BACKTRACE=1 186 TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/gd/rust_devices_config.yaml" 187 DEVICE_TEST=true 188 shift # past argument 189 ;; 190 --rhost) 191 export RUST_BACKTRACE=1 192 BUILD_TARGET=$RUST_BUILD_TARGET 193 TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/gd/rust_host_config.yaml" 194 shift # past argument 195 ;; 196 --sl4a) 197 TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/gd_sl4a/gd_sl4a_device_config.yaml" 198 TEST_RUNNER="blueberry/tests/gd_sl4a/gd_sl4a_test_runner.py" 199 DEVICE_TEST=true 200 shift # past argument 201 ;; 202 --sl4a_sl4a) 203 TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/sl4a_sl4a/sl4a_sl4a_device_config.yaml" 204 TEST_RUNNER="blueberry/tests/sl4a_sl4a/sl4a_sl4a_test_runner.py" 205 DEVICE_TEST=true 206 shift # past argument 207 ;; 208 --topshim) 209 TEST_CONFIG="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry/tests/topshim/topshim_host_config.yaml" 210 TEST_RUNNER="blueberry/tests/topshim/topshim_test_runner.py" 211 shift 212 ;; 213 # This will log everything to both log file and stdout 214 --verbose) 215 VERBOSE_MODE=true 216 shift # past argument 217 ;; 218 --*) # unknown argument 219 echo "$0: unrecognized argument '$1'" 220 echo "Try '$0 --help' for more information" 221 exit 1 222 shift 223 ;; 224 *) # unknown option 225 POSITIONAL+=("$1") # save it in an array for later 226 shift # past argument 227 ;; 228 esac 229 done 230 set -- "${POSITIONAL[@]}" # restore positional parameters 231 232 # Set the test filter 233 if [[ -n "$1" ]] ; then 234 TEST_FILTER="--tests $1" 235 fi 236 237 INSTALL_ARGS="--reuse-libraries" 238 if [ "$CLEAN_VENV" == true ] ; then 239 echo -e "${YELLOW}Cleaning up existing virtualenv${NOCOLOR}" 240 rm -rf $CERT_TEST_VENV/* 241 rm -rf $CERT_TEST_VENV 242 mkdir -p ${CERT_TEST_VENV} 243 INSTALL_ARGS="" 244 else 245 echo -e "${YELLOW}Try to reuse existing virtualenv at ${CERT_TEST_VENV}${NOCOLOR}" 246 fi 247 248} 249 250function select_devices { 251 if [ "$DEVICE_TEST" == true ] ; then 252 RR="$(cat ${TEST_CONFIG}|grep \'CERT\\\|DUT\')" 253 if [ "$RR" != "" ]; then 254 DUT_SERIAL="$(menu-adb DUT)" 255 DUT_ADB="adb -s ${DUT_SERIAL}" 256 DUT_NAME="$(adb devices -l | grep -v "List of device" | grep ${DUT_SERIAL} | awk '{ print $6 }' | cut -d ':' -f 2)" 257 CERT_SERIAL="$(menu-adb CERT)" 258 CERT_ADB="adb -s ${CERT_SERIAL}" 259 CERT_NAME="$(adb devices -l | grep -v "List of device" | grep ${CERT_SERIAL} | awk '{ print $6 }' | cut -d ':' -f 2)" 260 261 if [ "${CERT_SERIAL}" == "${DUT_SERIAL}" ]; then 262 echo 263 echo -e "${RED}ERROR: CERT and DUT cannot be the same device, or you only have one device connected!${NOCOLOR}" 264 echo 265 exit 1 266 fi 267 268 ## Set android devices in config 269 sed -i "s/'DUT'/'${DUT_SERIAL}'/g" ${TEST_CONFIG} 270 sed -i "s/'CERT'/'${CERT_SERIAL}'/g" ${TEST_CONFIG} 271 fi 272 fi 273} 274 275function soong_build { 276 if [ "$CLEAN_VENV" == true ] ; then 277 $ANDROID_BUILD_TOP/build/soong/soong_ui.bash --build-mode --"modules-in-a-dir" --dir="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system" dist $BUILD_TARGET -j $(nproc) 278 if [[ $? -ne 0 ]] ; then 279 echo -e "${RED}Failed to build ${BUILD_TARGET}${NOCOLOR}" 280 exit 1 281 fi 282 else 283 $ANDROID_BUILD_TOP/build/soong/soong_ui.bash --build-mode --"all-modules" --dir="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system" dist $BUILD_TARGET -j $(nproc) 284 if [[ $? -ne 0 ]] ; then 285 echo -e "${RED}Failed to build ${BUILD_TARGET}${NOCOLOR}" 286 exit 1 287 fi 288 fi 289} 290 291function setup_venv { 292 # Make venv in memory, decreases --clean build times by 40% 293 # Caveat is you lose the venv if the computer reboots 294 if [ "${USE_ASHMEM_VENV}" == true ] ; then 295 echo -e "${BLUE}Using ashmem virtual environment.${NOCOLOR}" 296 if [[ ! -L ${CERT_TEST_VENV} ]] ; then 297 echo -e "${BLUE}" 298 echo -ne "Creating ashmem dist folder..." 299 mkdir -p "${ASHMEM_VENV}" 300 # Ensure the directory doesn't exist 301 rm -rf "${CERT_TEST_VENV}" 302 echo -e "Done" 303 echo -ne "Sym linking ${ASHMEM_VENV} to ${CERT_TEST_VENV}..." 304 ln -s "${ASHMEM_VENV}" "${CERT_TEST_VENV}" 305 echo -e "Done" 306 echo -e "${NOCOLOR}" 307 fi 308 else 309 echo -e "${RED}Not using ashmem virtual environment.${NOCOLOR}" 310 if [[ -L ${CERT_TEST_VENV} ]] ; then 311 echo -e "${RED}" 312 echo -en "Removing sym link from ${ASHMEM_VENV} to ${CERT_TEST_VENV}..." 313 rm -rf ""${ASHMEM_VENV} "${CERT_TEST_VENV}" 314 echo -e "Done" 315 echo -en "Cleaning up memory..." 316 rm -rf "${ASHMEM_VENV}" 317 echo -e "Done" 318 echo -e "${NOCOLOR}" 319 fi 320 fi 321 ${PYTHON_BIN} -m venv --clear "${CERT_TEST_VENV}" 322 if [[ $? -ne 0 ]] ; then 323 echo -e "${RED}Error setting up virtualenv${NOCOLOR}" 324 exit 1 325 fi 326 327 unzip -o -q "${ANDROID_BUILD_TOP}/out/dist/bluetooth_cert_tests.zip" -d "${CERT_TEST_VENV}/sources" 328 if [[ $? -ne 0 ]] ; then 329 echo -e "${RED}Error unzipping bluetooth_cert_tests.zip${NOCOLOR}" 330 exit 1 331 fi 332 333 venv_common 334} 335 336function incremental_venv { 337#LINT.IfChange 338 HOST_BIN="${ANDROID_BUILD_TOP}/out/host/linux-x86/bin" 339 HOST_LIB="${ANDROID_BUILD_TOP}/out/host/linux-x86/lib64" 340 DEST_DIR="${ANDROID_BUILD_TOP}/out/dist/bluetooth_venv/sources" 341 DEST_LIB_DIR="${DEST_DIR}/lib64" 342 cp {$HOST_BIN,$DEST_DIR}/bluetooth_stack_with_facade 343 cp {$HOST_BIN,$DEST_DIR}/bluetooth_with_facades 344 cp {$HOST_BIN,$DEST_DIR}/bt_topshim_facade 345 cp {$HOST_BIN,$DEST_DIR}/root-canal 346 347 cp {$HOST_LIB,$DEST_LIB_DIR}/libbase.so 348 cp {$HOST_LIB,$DEST_LIB_DIR}/libbluetooth_gd.so 349 cp {$HOST_LIB,$DEST_LIB_DIR}/libc++.so 350 cp {$HOST_LIB,$DEST_LIB_DIR}/libchrome.so 351 cp {$HOST_LIB,$DEST_LIB_DIR}/libcrypto-host.so 352 cp {$HOST_LIB,$DEST_LIB_DIR}/libevent-host.so 353 cp {$HOST_LIB,$DEST_LIB_DIR}/libgrpc++.so 354 cp {$HOST_LIB,$DEST_LIB_DIR}/libgrpc_wrap.so 355 cp {$HOST_LIB,$DEST_LIB_DIR}/liblog.so 356 cp {$HOST_LIB,$DEST_LIB_DIR}/libssl-host.so 357 cp {$HOST_LIB,$DEST_LIB_DIR}/libz-host.so 358 cp {$HOST_LIB,$DEST_LIB_DIR}/libprotobuf-cpp-full.so 359 cp {$HOST_LIB,$DEST_LIB_DIR}/libunwindstack.so 360 cp {$HOST_LIB,$DEST_LIB_DIR}/liblzma.so 361 362 i="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/setup.py" 363 cp {${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system,$DEST_DIR}${i#${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system} 364 for i in `find ${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system/blueberry -name "*.py" -type f`; do 365 cp {${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system,$DEST_DIR}${i#${ANDROID_BUILD_TOP}/packages/modules/Bluetooth/system} 366 done 367#LINT.ThenChange(../../Android.mk) 368 369 venv_common 370} 371 372function venv_common { 373 $(echo "${CERT_TEST_VENV}/bin/python" "${CERT_TEST_VENV}/sources/setup.py" --quiet build --force "${INSTALL_ARGS}") 374 if [[ $? -ne 0 ]] ; then 375 echo -e "${RED}Error building GD Python libraries${NOCOLOR}" 376 echo -e "${YELLOW}NOTE:${NOCOLOR} To build external libraries the first time, please add --clean option." 377 exit 1 378 fi 379 380 $(echo "${CERT_TEST_VENV}/bin/python" "${CERT_TEST_VENV}/sources/setup.py" --quiet install --skip-build --force "${INSTALL_ARGS}") 381 if [[ $? -ne 0 ]] ; then 382 echo -e "${RED}Error installing GD Python libraries${NOCOLOR}" 383 exit 1 384 fi 385 386if [ "${VERBOSE_MODE}" == true ] ; then 387 TEMP_CONFIG=/tmp/temp_mobly_config.yaml 388 cat "${TEST_CONFIG}" | "${CERT_TEST_VENV}/bin/python" -c " 389import sys 390import yaml 391config = yaml.load(sys.stdin) 392config['verbose_mode'] = True 393print(yaml.dump(config)) 394 " > "${TEMP_CONFIG}" 395 TEST_CONFIG="${TEMP_CONFIG}" 396 if [[ $? -ne 0 ]] ; then 397 echo -e "${RED}Setup failed as verbose mode is chosen but cannot be enabled${NOCOLOR}" 398 exit 1 399 fi 400fi 401} 402 403function gotta_go_fast { 404 if [ "${GOTTA_GO_FAST}" == true ] ; then 405 # Call here to explicitly note the flag is in use 406 happy_hedgehog 407 if [[ ! -L "${CERT_HOST_LOGS}" ]] ; then 408 rm -rf "${CERT_HOST_LOGS}" 409 mkdir -p "${ASHMEM_HOST_LOGS}" 410 ln -s "${ASHMEM_HOST_LOGS}" "${CERT_HOST_LOGS}" 411 fi 412 413 if [[ ! -L "${OUT_TARGET}" ]] ; then 414 rm -rf "${OUT_TARGET}" 415 mkdir -p "${ASHMEM_OUT_TARGET}" 416 ln -s "${ASHMEM_OUT_TARGET}" "${OUT_TARGET}" 417 fi 418 else 419 if [[ -L "${CERT_HOST_LOGS}" ]] ; then 420 # Call here so we don't spam anyone not using the flag 421 sad_hedgehog 422 rm -rf "${CERT_HOST_LOGS}" 423 rm -rf "${ASHMEM_HOST_LOGS}" 424 fi 425 426 if [[ -L "${OUT_TARGET}" ]] ; then 427 rm -rf "${OUT_TARGET}" 428 rm -rf "${ASHMEM_OUT_TARGET}" 429 fi 430 fi 431} 432 433function run_tests { 434 for n in $(seq "${NUM_REPETITIONS}"); do 435 $(echo "${CERT_TEST_VENV}/bin/python" "${CERT_TEST_VENV}/sources/${TEST_RUNNER}" \ 436 "-c ${TEST_CONFIG}" "${TEST_FILTER}") 437 done 438 439 if [ "${CLEAN_VENV}" != true ] ; then 440 echo -e "${YELLOW}NOTE:${NOCOLOR} Completed tests using existing external libraries in virtualenv." 441 echo -e "${YELLOW}NOTE:${NOCOLOR} To update external libraries, please add --clean option." 442 fi 443} 444 445function menu-adb() { 446 TMP=$(adb devices -l | grep -v "List of device" | awk '{ print $1 }') 447 # TODO(optedoblivion): If the device doesn't have a name (offline), it misnames them 448 NTMP=$(adb devices -l | grep -v "List of device" | awk '{ print $6 }' | cut -d ':' -f 2) 449 SERIALS=($TMP) 450 DEVICES=($NTMP) 451 LEN=${#SERIALS[@]} 452 result=0 453 if [ $LEN -lt 1 ]; then 454 echo -e "${YELLOW}No devices connected!${NOCOLOR}" 455 return 1 456 fi 457 458 if [ "$LEN" == "" ]; then 459 LEN=0 460 fi 461 462 answer=0 463 464 DEVICE_NAME="$1 device" 465 466 if [ $LEN -gt 1 ]; then 467 echo "+-------------------------------------------------+" 1>&2 468 echo "| Choose a ${DEVICE_NAME}: " 1>&2 469 echo "+-------------------------------------------------+" 1>&2 470 echo "| |" 1>&2 471 let fixed_len=$LEN-1 472 for i in `seq 0 $fixed_len`; 473 do 474 serial=${SERIALS[i]} 475 device=${DEVICES[i]} 476 echo "| $i) $serial $device" 1>&2 477 ## TODO[MSB]: Find character count, fill with space and ending box wall 478 done 479 echo "| |" 1>&2 480 echo "+-------------------------------------------------+" 1>&2 481 echo 1>&2 482 echo -n "Index number: " 1>&2 483 read answer 484 fi 485 486 if [ $answer -ge $LEN ]; then 487 echo 488 echo "Please choose a correct index!" 1>&2 489 echo 490 return 1 491 fi 492 493 SERIAL=${SERIALS[$answer]} 494 echo $SERIAL 495} 496 497function main { 498 check_environment 499 parse_options $@ 500 select_devices 501 if [[ "${SKIP_SOONG_BUILD}" != true ]] ; then 502 soong_build 503 fi 504 if [ "$CLEAN_VENV" == true ] ; then 505 setup_venv 506 else 507 incremental_venv 508 fi 509 gotta_go_fast 510 run_tests 511} 512 513main $@ 514