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