1*4a64e381SAndroid Build Coastguard Worker#!/bin/bash 2*4a64e381SAndroid Build Coastguard Worker# 3*4a64e381SAndroid Build Coastguard Worker# Copyright (c) 2024, The OpenThread Authors. 4*4a64e381SAndroid Build Coastguard Worker# All rights reserved. 5*4a64e381SAndroid Build Coastguard Worker# 6*4a64e381SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without 7*4a64e381SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met: 8*4a64e381SAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright 9*4a64e381SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer. 10*4a64e381SAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright 11*4a64e381SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer in the 12*4a64e381SAndroid Build Coastguard Worker# documentation and/or other materials provided with the distribution. 13*4a64e381SAndroid Build Coastguard Worker# 3. Neither the name of the copyright holder nor the 14*4a64e381SAndroid Build Coastguard Worker# names of its contributors may be used to endorse or promote products 15*4a64e381SAndroid Build Coastguard Worker# derived from this software without specific prior written permission. 16*4a64e381SAndroid Build Coastguard Worker# 17*4a64e381SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18*4a64e381SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*4a64e381SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*4a64e381SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21*4a64e381SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*4a64e381SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*4a64e381SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*4a64e381SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*4a64e381SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*4a64e381SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*4a64e381SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE. 28*4a64e381SAndroid Build Coastguard Worker# 29*4a64e381SAndroid Build Coastguard Worker# Test basic functionality of otbr-agent under NCP mode. 30*4a64e381SAndroid Build Coastguard Worker# 31*4a64e381SAndroid Build Coastguard Worker# Usage: 32*4a64e381SAndroid Build Coastguard Worker# ./ncp_mode 33*4a64e381SAndroid Build Coastguard Workerset -euxo pipefail 34*4a64e381SAndroid Build Coastguard Worker 35*4a64e381SAndroid Build Coastguard WorkerSCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" 36*4a64e381SAndroid Build Coastguard Workerreadonly SCRIPT_DIR 37*4a64e381SAndroid Build Coastguard WorkerEXPECT_SCRIPT_DIR="${SCRIPT_DIR}/expect" 38*4a64e381SAndroid Build Coastguard Workerreadonly EXPECT_SCRIPT_DIR 39*4a64e381SAndroid Build Coastguard Worker 40*4a64e381SAndroid Build Coastguard Worker#--------------------------------------- 41*4a64e381SAndroid Build Coastguard Worker# Configurations 42*4a64e381SAndroid Build Coastguard Worker#--------------------------------------- 43*4a64e381SAndroid Build Coastguard WorkerOT_CLI="${OT_CLI:-ot-cli-ftd}" 44*4a64e381SAndroid Build Coastguard Workerreadonly OT_CLI 45*4a64e381SAndroid Build Coastguard Worker 46*4a64e381SAndroid Build Coastguard WorkerOT_NCP="${OT_NCP:-ot-ncp-ftd}" 47*4a64e381SAndroid Build Coastguard Workerreadonly OT_NCP 48*4a64e381SAndroid Build Coastguard Worker 49*4a64e381SAndroid Build Coastguard WorkerABS_TOP_BUILDDIR="$(cd "${top_builddir:-"${SCRIPT_DIR}"/../../}" && pwd)" 50*4a64e381SAndroid Build Coastguard Workerreadonly ABS_TOP_BUILDDIR 51*4a64e381SAndroid Build Coastguard Worker 52*4a64e381SAndroid Build Coastguard WorkerABS_TOP_SRCDIR="$(cd "${top_srcdir:-"${SCRIPT_DIR}"/../../}" && pwd)" 53*4a64e381SAndroid Build Coastguard Workerreadonly ABS_TOP_SRCDIR 54*4a64e381SAndroid Build Coastguard Worker 55*4a64e381SAndroid Build Coastguard WorkerABS_TOP_OT_SRCDIR="${ABS_TOP_SRCDIR}/third_party/openthread/repo" 56*4a64e381SAndroid Build Coastguard Workerreadonly ABS_TOP_OT_SRCDIR 57*4a64e381SAndroid Build Coastguard Worker 58*4a64e381SAndroid Build Coastguard WorkerABS_TOP_OT_BUILDDIR="${ABS_TOP_BUILDDIR}/../simulation" 59*4a64e381SAndroid Build Coastguard Workerreadonly ABS_TOP_BUILDDIR 60*4a64e381SAndroid Build Coastguard Worker 61*4a64e381SAndroid Build Coastguard WorkerOTBR_COLOR_PASS='\033[0;32m' 62*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_COLOR_PASS 63*4a64e381SAndroid Build Coastguard Worker 64*4a64e381SAndroid Build Coastguard WorkerOTBR_COLOR_FAIL='\033[0;31m' 65*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_COLOR_FAIL 66*4a64e381SAndroid Build Coastguard Worker 67*4a64e381SAndroid Build Coastguard WorkerOTBR_COLOR_NONE='\033[0m' 68*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_COLOR_NONE 69*4a64e381SAndroid Build Coastguard Worker 70*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_VERBOSE="${OTBR_VERBOSE:-0}" 71*4a64e381SAndroid Build Coastguard Worker 72*4a64e381SAndroid Build Coastguard Worker#---------------------------------------- 73*4a64e381SAndroid Build Coastguard Worker# Helper functions 74*4a64e381SAndroid Build Coastguard Worker#---------------------------------------- 75*4a64e381SAndroid Build Coastguard Workerdie() 76*4a64e381SAndroid Build Coastguard Worker{ 77*4a64e381SAndroid Build Coastguard Worker exit_message="$*" 78*4a64e381SAndroid Build Coastguard Worker echo " *** ERROR: $*" 79*4a64e381SAndroid Build Coastguard Worker exit 1 80*4a64e381SAndroid Build Coastguard Worker} 81*4a64e381SAndroid Build Coastguard Worker 82*4a64e381SAndroid Build Coastguard Workerexists_or_die() 83*4a64e381SAndroid Build Coastguard Worker{ 84*4a64e381SAndroid Build Coastguard Worker [[ -f $1 ]] || die "Missing file: $1" 85*4a64e381SAndroid Build Coastguard Worker} 86*4a64e381SAndroid Build Coastguard Worker 87*4a64e381SAndroid Build Coastguard Workerexecutable_or_die() 88*4a64e381SAndroid Build Coastguard Worker{ 89*4a64e381SAndroid Build Coastguard Worker [[ -x $1 ]] || die "Missing executable: $1" 90*4a64e381SAndroid Build Coastguard Worker} 91*4a64e381SAndroid Build Coastguard Worker 92*4a64e381SAndroid Build Coastguard Workerwrite_syslog() 93*4a64e381SAndroid Build Coastguard Worker{ 94*4a64e381SAndroid Build Coastguard Worker logger -s -p syslog.alert "OTBR_TEST: $*" 95*4a64e381SAndroid Build Coastguard Worker} 96*4a64e381SAndroid Build Coastguard Worker 97*4a64e381SAndroid Build Coastguard Worker#---------------------------------------- 98*4a64e381SAndroid Build Coastguard Worker# Test constants 99*4a64e381SAndroid Build Coastguard Worker#---------------------------------------- 100*4a64e381SAndroid Build Coastguard WorkerTEST_BASE=/tmp/test-otbr 101*4a64e381SAndroid Build Coastguard Workerreadonly TEST_BASE 102*4a64e381SAndroid Build Coastguard Worker 103*4a64e381SAndroid Build Coastguard WorkerOTBR_AGENT=otbr-agent 104*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_AGENT 105*4a64e381SAndroid Build Coastguard Worker 106*4a64e381SAndroid Build Coastguard WorkerSTAGE_DIR="${TEST_BASE}/stage" 107*4a64e381SAndroid Build Coastguard Workerreadonly STAGE_DIR 108*4a64e381SAndroid Build Coastguard Worker 109*4a64e381SAndroid Build Coastguard WorkerBUILD_DIR="${TEST_BASE}/build" 110*4a64e381SAndroid Build Coastguard Workerreadonly BUILD_DIR 111*4a64e381SAndroid Build Coastguard Worker 112*4a64e381SAndroid Build Coastguard WorkerOTBR_DBUS_CONF="${ABS_TOP_BUILDDIR}/src/agent/otbr-agent.conf" 113*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_DBUS_CONF 114*4a64e381SAndroid Build Coastguard Worker 115*4a64e381SAndroid Build Coastguard WorkerOTBR_AGENT_PATH="${ABS_TOP_BUILDDIR}/src/agent/${OTBR_AGENT}" 116*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_AGENT_PATH 117*4a64e381SAndroid Build Coastguard Worker 118*4a64e381SAndroid Build Coastguard Worker# The node ids 119*4a64e381SAndroid Build Coastguard WorkerLEADER_NODE_ID=1 120*4a64e381SAndroid Build Coastguard Workerreadonly LEADER_NODE_ID 121*4a64e381SAndroid Build Coastguard Worker 122*4a64e381SAndroid Build Coastguard Worker# The TUN device for OpenThread border router. 123*4a64e381SAndroid Build Coastguard WorkerTUN_NAME=wpan0 124*4a64e381SAndroid Build Coastguard Workerreadonly TUN_NAME 125*4a64e381SAndroid Build Coastguard Worker 126*4a64e381SAndroid Build Coastguard Worker#---------------------------------------- 127*4a64e381SAndroid Build Coastguard Worker# Test steps 128*4a64e381SAndroid Build Coastguard Worker#---------------------------------------- 129*4a64e381SAndroid Build Coastguard Workerbuild_ot_simulation() 130*4a64e381SAndroid Build Coastguard Worker{ 131*4a64e381SAndroid Build Coastguard Worker sudo rm -rf "${ABS_TOP_OT_BUILDDIR}/ncp" 132*4a64e381SAndroid Build Coastguard Worker sudo rm -rf "${ABS_TOP_OT_BUILDDIR}/cli" 133*4a64e381SAndroid Build Coastguard Worker OT_CMAKE_BUILD_DIR=${ABS_TOP_OT_BUILDDIR}/ncp "${ABS_TOP_OT_SRCDIR}"/script/cmake-build simulation -DOT_MTD=OFF -DOT_APP_CLI=OFF -DOT_APP_RCP=OFF 134*4a64e381SAndroid Build Coastguard Worker OT_CMAKE_BUILD_DIR=${ABS_TOP_OT_BUILDDIR}/cli "${ABS_TOP_OT_SRCDIR}"/script/cmake-build simulation -DOT_MTD=OFF -DOT_APP_NCP=OFF -DOT_APP_RCP=OFF -DOT_RCP=OFF 135*4a64e381SAndroid Build Coastguard Worker} 136*4a64e381SAndroid Build Coastguard Worker 137*4a64e381SAndroid Build Coastguard Workertest_setup() 138*4a64e381SAndroid Build Coastguard Worker{ 139*4a64e381SAndroid Build Coastguard Worker executable_or_die "${OTBR_AGENT_PATH}" 140*4a64e381SAndroid Build Coastguard Worker 141*4a64e381SAndroid Build Coastguard Worker # Remove flashes 142*4a64e381SAndroid Build Coastguard Worker sudo rm -vrf "${TEST_BASE}/tmp" 143*4a64e381SAndroid Build Coastguard Worker # OPENTHREAD_POSIX_DAEMON_SOCKET_LOCK 144*4a64e381SAndroid Build Coastguard Worker sudo rm -vf "/tmp/openthread.lock" 145*4a64e381SAndroid Build Coastguard Worker 146*4a64e381SAndroid Build Coastguard Worker [[ ${BUILD_OT_SIM} == 1 ]] && build_ot_simulation 147*4a64e381SAndroid Build Coastguard Worker ot_cli=$(find "${ABS_TOP_OT_BUILDDIR}" -name "${OT_CLI}") 148*4a64e381SAndroid Build Coastguard Worker ot_ncp=$(find "${ABS_TOP_OT_BUILDDIR}" -name "${OT_NCP}") 149*4a64e381SAndroid Build Coastguard Worker 150*4a64e381SAndroid Build Coastguard Worker # We will be creating a lot of log information 151*4a64e381SAndroid Build Coastguard Worker # Rotate logs so we have a clean and empty set of logs uncluttered with other stuff 152*4a64e381SAndroid Build Coastguard Worker if [[ -f /etc/logrotate.conf ]]; then 153*4a64e381SAndroid Build Coastguard Worker sudo logrotate -f /etc/logrotate.conf || true 154*4a64e381SAndroid Build Coastguard Worker fi 155*4a64e381SAndroid Build Coastguard Worker 156*4a64e381SAndroid Build Coastguard Worker # Preparation for otbr-agent 157*4a64e381SAndroid Build Coastguard Worker exists_or_die "${OTBR_DBUS_CONF}" 158*4a64e381SAndroid Build Coastguard Worker sudo cp "${OTBR_DBUS_CONF}" /etc/dbus-1/system.d 159*4a64e381SAndroid Build Coastguard Worker 160*4a64e381SAndroid Build Coastguard Worker write_syslog "AGENT: kill old" 161*4a64e381SAndroid Build Coastguard Worker sudo killall "${OTBR_AGENT}" || true 162*4a64e381SAndroid Build Coastguard Worker 163*4a64e381SAndroid Build Coastguard Worker # From now on - all exits are TRAPPED 164*4a64e381SAndroid Build Coastguard Worker # When they occur, we call the function: output_logs'. 165*4a64e381SAndroid Build Coastguard Worker trap test_teardown EXIT 166*4a64e381SAndroid Build Coastguard Worker} 167*4a64e381SAndroid Build Coastguard Worker 168*4a64e381SAndroid Build Coastguard Workertest_teardown() 169*4a64e381SAndroid Build Coastguard Worker{ 170*4a64e381SAndroid Build Coastguard Worker # Capture the exit code so we can return it below 171*4a64e381SAndroid Build Coastguard Worker EXIT_CODE=$? 172*4a64e381SAndroid Build Coastguard Worker readonly EXIT_CODE 173*4a64e381SAndroid Build Coastguard Worker write_syslog "EXIT ${EXIT_CODE} - output logs" 174*4a64e381SAndroid Build Coastguard Worker 175*4a64e381SAndroid Build Coastguard Worker sudo pkill -f "${OTBR_AGENT}" || true 176*4a64e381SAndroid Build Coastguard Worker sudo pkill -f "${OT_CLI}" || true 177*4a64e381SAndroid Build Coastguard Worker sudo pkill -f "${OT_NCP}" || true 178*4a64e381SAndroid Build Coastguard Worker wait 179*4a64e381SAndroid Build Coastguard Worker 180*4a64e381SAndroid Build Coastguard Worker echo 'clearing all' 181*4a64e381SAndroid Build Coastguard Worker sudo rm /etc/dbus-1/system.d/otbr-agent.conf || true 182*4a64e381SAndroid Build Coastguard Worker sudo rm -rf "${STAGE_DIR}" || true 183*4a64e381SAndroid Build Coastguard Worker sudo rm -rf "${BUILD_DIR}" || true 184*4a64e381SAndroid Build Coastguard Worker 185*4a64e381SAndroid Build Coastguard Worker exit_message="Test teardown" 186*4a64e381SAndroid Build Coastguard Worker echo "EXIT ${EXIT_CODE}: MESSAGE: ${exit_message}" 187*4a64e381SAndroid Build Coastguard Worker exit ${EXIT_CODE} 188*4a64e381SAndroid Build Coastguard Worker} 189*4a64e381SAndroid Build Coastguard Worker 190*4a64e381SAndroid Build Coastguard Workerotbr_exec_expect_script() 191*4a64e381SAndroid Build Coastguard Worker{ 192*4a64e381SAndroid Build Coastguard Worker local log_file="tmp/log_expect" 193*4a64e381SAndroid Build Coastguard Worker 194*4a64e381SAndroid Build Coastguard Worker for script in "$@"; do 195*4a64e381SAndroid Build Coastguard Worker echo -e "\n${OTBR_COLOR_PASS}EXEC${OTBR_COLOR_NONE} ${script}" 196*4a64e381SAndroid Build Coastguard Worker sudo killall ot-rcp || true 197*4a64e381SAndroid Build Coastguard Worker sudo killall ot-cli || true 198*4a64e381SAndroid Build Coastguard Worker sudo killall ot-cli-ftd || true 199*4a64e381SAndroid Build Coastguard Worker sudo killall ot-cli-mtd || true 200*4a64e381SAndroid Build Coastguard Worker sudo killall ot-ncp-ftd || true 201*4a64e381SAndroid Build Coastguard Worker sudo killall ot-ncp-mtd || true 202*4a64e381SAndroid Build Coastguard Worker sudo rm -rf tmp 203*4a64e381SAndroid Build Coastguard Worker mkdir tmp 204*4a64e381SAndroid Build Coastguard Worker { 205*4a64e381SAndroid Build Coastguard Worker sudo -E expect -df "${script}" 2>"${log_file}" 206*4a64e381SAndroid Build Coastguard Worker } || { 207*4a64e381SAndroid Build Coastguard Worker local EXIT_CODE=$? 208*4a64e381SAndroid Build Coastguard Worker 209*4a64e381SAndroid Build Coastguard Worker echo -e "\n${OTBR_COLOR_FAIL}FAIL${OTBR_COLOR_NONE} ${script}" 210*4a64e381SAndroid Build Coastguard Worker cat "${log_file}" >&2 211*4a64e381SAndroid Build Coastguard Worker return "${EXIT_CODE}" 212*4a64e381SAndroid Build Coastguard Worker } 213*4a64e381SAndroid Build Coastguard Worker echo -e "\n${OTBR_COLOR_PASS}PASS${OTBR_COLOR_NONE} ${script}" 214*4a64e381SAndroid Build Coastguard Worker if [[ ${OTBR_VERBOSE} == 1 ]]; then 215*4a64e381SAndroid Build Coastguard Worker cat "${log_file}" >&2 216*4a64e381SAndroid Build Coastguard Worker fi 217*4a64e381SAndroid Build Coastguard Worker done 218*4a64e381SAndroid Build Coastguard Worker} 219*4a64e381SAndroid Build Coastguard Worker 220*4a64e381SAndroid Build Coastguard Workerparse_args() 221*4a64e381SAndroid Build Coastguard Worker{ 222*4a64e381SAndroid Build Coastguard Worker BUILD_OT_SIM=1 223*4a64e381SAndroid Build Coastguard Worker RUN_ALL_TESTS=1 224*4a64e381SAndroid Build Coastguard Worker 225*4a64e381SAndroid Build Coastguard Worker while [[ $# -gt 0 ]]; do 226*4a64e381SAndroid Build Coastguard Worker case $1 in 227*4a64e381SAndroid Build Coastguard Worker --build-ot-sim) 228*4a64e381SAndroid Build Coastguard Worker BUILD_OT_SIM="$2" 229*4a64e381SAndroid Build Coastguard Worker shift 230*4a64e381SAndroid Build Coastguard Worker ;; 231*4a64e381SAndroid Build Coastguard Worker --one-test) 232*4a64e381SAndroid Build Coastguard Worker RUN_ALL_TESTS=0 233*4a64e381SAndroid Build Coastguard Worker TEST_NAME="$2" 234*4a64e381SAndroid Build Coastguard Worker shift 235*4a64e381SAndroid Build Coastguard Worker ;; 236*4a64e381SAndroid Build Coastguard Worker esac 237*4a64e381SAndroid Build Coastguard Worker shift 238*4a64e381SAndroid Build Coastguard Worker done 239*4a64e381SAndroid Build Coastguard Worker} 240*4a64e381SAndroid Build Coastguard Worker 241*4a64e381SAndroid Build Coastguard Workermain() 242*4a64e381SAndroid Build Coastguard Worker{ 243*4a64e381SAndroid Build Coastguard Worker parse_args "$@" 244*4a64e381SAndroid Build Coastguard Worker 245*4a64e381SAndroid Build Coastguard Worker test_setup 246*4a64e381SAndroid Build Coastguard Worker 247*4a64e381SAndroid Build Coastguard Worker export EXP_OTBR_AGENT_PATH="${OTBR_AGENT_PATH}" 248*4a64e381SAndroid Build Coastguard Worker export EXP_TUN_NAME="${TUN_NAME}" 249*4a64e381SAndroid Build Coastguard Worker export EXP_LEADER_NODE_ID="${LEADER_NODE_ID}" 250*4a64e381SAndroid Build Coastguard Worker export EXP_OT_CLI_PATH="${ot_cli}" 251*4a64e381SAndroid Build Coastguard Worker export EXP_OT_NCP_PATH="${ot_ncp}" 252*4a64e381SAndroid Build Coastguard Worker 253*4a64e381SAndroid Build Coastguard Worker if [[ ${RUN_ALL_TESTS} == 0 ]]; then 254*4a64e381SAndroid Build Coastguard Worker otbr_exec_expect_script "${EXPECT_SCRIPT_DIR}/${TEST_NAME}" || die "ncp expect script failed!" 255*4a64e381SAndroid Build Coastguard Worker else 256*4a64e381SAndroid Build Coastguard Worker mapfile -t test_files < <(find "${EXPECT_SCRIPT_DIR}" -type f -name "ncp_*.exp") 257*4a64e381SAndroid Build Coastguard Worker otbr_exec_expect_script "${test_files[@]}" || die "ncp expect script failed!" 258*4a64e381SAndroid Build Coastguard Worker fi 259*4a64e381SAndroid Build Coastguard Worker} 260*4a64e381SAndroid Build Coastguard Worker 261*4a64e381SAndroid Build Coastguard Workermain "$@" 262