1#!/bin/bash 2# 3# Copyright (c) 2017, 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 thread commissioning along with openthread. 30# 31# Usage: 32# ./meshcop # test with latest openthread. 33# NO_CLEAN=1 ./meshcop # test with existing binaries in ${TEST_BASE}. 34# TEST_CASE=mdns_service ./meshcop # test the meshcop mDNS service. 35set -euxo pipefail 36 37# The test case to run. available cases are: 38# - commissioning 39# - mdns_service 40TEST_CASE="${TEST_CASE:-commissioning}" 41readonly TEST_CASE 42 43# Get our starting directory and remember it 44ORIGIN_PWD="$(pwd)" 45readonly ORIGIN_PWD 46 47SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" 48readonly SCRIPT_DIR 49 50#--------------------------------------- 51# Configurations 52#--------------------------------------- 53OT_RCP="ot-rcp" 54readonly OT_RCP 55 56OT_CLI="${OT_CLI:-ot-cli-ftd}" 57readonly OT_CLI 58 59ABS_TOP_BUILDDIR="$(cd "${top_builddir:-"${SCRIPT_DIR}"/../../}" && pwd)" 60readonly ABS_TOP_BUILDDIR 61 62ABS_TOP_SRCDIR="$(cd "${top_srcdir:-"${SCRIPT_DIR}"/../../}" && pwd)" 63readonly ABS_TOP_SRCDIR 64 65NO_CLEAN="${NO_CLEAN:-1}" 66readonly NO_CLEAN 67 68IGNORE_INSTALLED="${IGNORE_INSTALLED:-0}" 69readonly IGNORE_INSTALLED 70 71OTBR_USE_WEB_COMMISSIONER="${USE_WEB_COMMISSIONER:-0}" 72readonly OTBR_USE_WEB_COMMISSIONER 73 74#---------------------------------------- 75# Test constants 76#---------------------------------------- 77TEST_BASE=/tmp/test-otbr 78readonly TEST_BASE 79 80OTBR_AGENT=otbr-agent 81readonly OTBR_AGENT 82 83OTBR_WEB=otbr-web 84readonly OTBR_WEB 85 86OT_COMMISSIONER_CLI=commissioner-cli 87readonly OT_COMMISSIONER_CLI 88 89STAGE_DIR="${TEST_BASE}/stage" 90readonly STAGE_DIR 91 92BUILD_DIR="${TEST_BASE}/build" 93readonly BUILD_DIR 94 95OTBR_PSKC_PATH="${ABS_TOP_BUILDDIR}/tools/pskc" 96readonly OTBR_PSKC_PATH 97 98OTBR_AGENT_PATH="${ABS_TOP_BUILDDIR}/src/agent/${OTBR_AGENT}" 99readonly OTBR_AGENT_PATH 100 101OTBR_DBUS_CONF="${ABS_TOP_BUILDDIR}/src/agent/otbr-agent.conf" 102readonly OTBR_DBUS_CONF 103 104OTBR_WEB_PATH="${ABS_TOP_BUILDDIR}/src/web/${OTBR_WEB}" 105readonly OTBR_WEB_PATH 106 107OT_CTL="${ABS_TOP_BUILDDIR}/third_party/openthread/repo/src/posix/ot-ctl" 108readonly OT_CTL 109 110# The node ids 111LEADER_NODE_ID=1 112readonly LEADER_NODE_ID 113 114JOINER_NODE_ID=2 115readonly JOINER_NODE_ID 116 117# Web GUI 118OTBR_WEB_HOST=127.0.0.1 119readonly OTBR_WEB_HOST 120 121OTBR_WEB_PORT=8773 122readonly OTBR_WEB_PORT 123 124OTBR_WEB_URL="http://${OTBR_WEB_HOST}:${OTBR_WEB_PORT}" 125readonly OTBR_WEB_URL 126 127# External commissioner 128OT_COMMISSIONER_PATH=${BUILD_DIR}/ot-commissioner/build/src/app/cli/commissioner-cli 129readonly OT_COMMISSIONER_PATH 130 131OT_COMMISSIONER_CONFIG=${BUILD_DIR}/ot-commissioner/src/app/etc/commissioner/non-ccm-config.json 132readonly OT_COMMISSIONER_CONFIG 133 134# 135# NOTE Joiner pass phrase: 136# Must be at least 6 bytes long 137# And this example has: J ZERO ONE N E R 138# We cannot use letter O and I because Q O I Z are not allowed per spec 139OT_JOINER_PASSPHRASE=J01NER 140readonly OT_JOINER_PASSPHRASE 141 142# 18b430 is the nest EUI prefix. 143OT_JOINER_EUI64="18b430000000000${JOINER_NODE_ID}" 144readonly OT_JOINER_EUI64 145 146# The border agent, and ncp needs a pass phrase. 147OT_AGENT_PASSPHRASE=MYPASSPHRASE 148readonly OT_AGENT_PASSPHRASE 149 150# The network needs a name. 151OT_NETWORK_NAME=MyTestNetwork 152readonly OT_NETWORK_NAME 153 154# The TUN device for OpenThread border router. 155TUN_NAME=wpan0 156readonly TUN_NAME 157 158# The default meshcop service instance name 159OT_SERVICE_INSTANCE='OpenThread\(\\032\| \)BorderRouter\(\\032\| \)#[0-9A-F][0-9A-F][0-9A-F][0-9A-F]' 160readonly OT_SERVICE_INSTANCE 161 162echo "ORIGIN_PWD: ${ORIGIN_PWD}" 163echo "TEST_BASE: ${TEST_BASE}" 164echo "ABS_TOP_SRCDIR=${ABS_TOP_SRCDIR}" 165echo "ABS_TOP_BUILDDIR=${ABS_TOP_BUILDDIR}" 166 167#---------------------------------------- 168# Helper functions 169#---------------------------------------- 170 171die() 172{ 173 exit_message="$*" 174 echo " *** ERROR: $*" 175 exit 1 176} 177 178exists_or_die() 179{ 180 [[ -f $1 ]] || die "Missing file: $1" 181} 182 183executable_or_die() 184{ 185 [[ -x $1 ]] || die "Missing executable: $1" 186} 187 188random_channel() 189{ 190 echo $((11 + "${RANDOM}" % 16)) 191} 192 193random_panid() 194{ 195 printf "0x%04x" "${RANDOM}" 196} 197 198random_xpanid() 199{ 200 printf "%04x%04x%04x%04x" "${RANDOM}" "${RANDOM}" "${RANDOM}" "${RANDOM}" 201} 202 203random_networkkey() 204{ 205 printf "%04x%04x%04x%04x%04x%04x%04x%04x" "${RANDOM}" "${RANDOM}" "${RANDOM}" "${RANDOM}" "${RANDOM}" "${RANDOM}" "${RANDOM}" "${RANDOM}" 206} 207 208write_syslog() 209{ 210 logger -s -p syslog.alert "OPENTHREAD_TEST: $*" 211} 212 213output_logs() 214{ 215 write_syslog 'All apps should be dead now' 216 217 # part 1 218 # ------ 219 # 220 # On travis (the CI server), we can't see what went into the 221 # syslog. So this is here so we can see the output. 222 # 223 # part 2 224 # ------ 225 # 226 # If we run locally, it is sometimes helpful for our victim (you 227 # the developer) to have logs split upto various files to help 228 # that victim, we'll GREP the log files according. 229 # 230 # Wait 5 seconds for the "logs to flush" 231 sleep 5 232 233 cd "${ORIGIN_PWD}" 234 echo 'START_LOG: SYSLOG ===================' 235 tee complete-syslog.log </var/log/syslog 236 echo 'START_LOG: BR-AGENT =================' 237 grep "${OTBR_AGENT}" /var/log/syslog | tee otbr-agent.log 238 echo 'START_LOG: OT-COMISSIONER =========' 239 cat "${OT_COMMISSIONER_LOG}" 240 echo 'START_LOG: OT-RCP ===================' 241 grep "${OT_RCP}" /var/log/syslog | tee "${OT_RCP}.log" 242 echo 'START_LOG: OT-CLI ===================' 243 grep "${OT_CLI}" /var/log/syslog | tee "${OT_CLI}.log" 244 echo '=====================================' 245 echo 'Hint, for each log Search backwards for: "START_LOG: <NAME>"' 246 echo '=====================================' 247} 248 249build_dependencies() 250{ 251 # Clean up old stuff 252 if [[ ${NO_CLEAN} != 1 ]]; then 253 [[ ! -d ${STAGE_DIR} ]] || rm -rf "${STAGE_DIR}" 254 [[ ! -d ${BUILD_DIR} ]] || rm -rf "${BUILD_DIR}" 255 fi 256 257 [[ -d ${STAGE_DIR} ]] || mkdir -p "${STAGE_DIR}" 258 [[ -d ${BUILD_DIR} ]] || mkdir -p "${BUILD_DIR}" 259 260 # As above, these steps are broken up 261 ot_cli=$(command -v "${OT_CLI}") 262 ot_rcp=$(command -v "${OT_RCP}") 263 264 if 265 [ "${TEST_CASE}" == "commissioning" ] \ 266 && [[ ${OTBR_USE_WEB_COMMISSIONER} != 1 ]] 267 then 268 ot_commissioner_build 269 fi 270 271 write_syslog "TEST: BUILD COMPLETE" 272} 273 274test_setup() 275{ 276 # message for general failures 277 exit_message="JOINER FAILED" 278 279 executable_or_die "${OTBR_AGENT_PATH}" 280 executable_or_die "${OTBR_WEB_PATH}" 281 282 # Remove flashes 283 sudo rm -vrf "${TEST_BASE}/tmp" 284 # OPENTHREAD_POSIX_DAEMON_SOCKET_LOCK 285 sudo rm -vf "/tmp/openthread.lock" 286 287 build_dependencies 288 289 # We will be creating a lot of log information 290 # Rotate logs so we have a clean and empty set of logs uncluttered with other stuff 291 if [[ -f /etc/logrotate.conf ]]; then 292 sudo logrotate -f /etc/logrotate.conf || true 293 fi 294 295 # From now on - all exits are TRAPPED 296 # When they occur, we call the function: output_logs'. 297 trap test_teardown EXIT 298} 299 300test_teardown() 301{ 302 # Capture the exit code so we can return it below 303 EXIT_CODE=$? 304 readonly EXIT_CODE 305 write_syslog "EXIT ${EXIT_CODE} - output logs" 306 307 sudo pkill -f "${OTBR_AGENT}" || true 308 sudo pkill -f "${OTBR_WEB}" || true 309 sudo pkill -f "${OT_COMMISSIONER_CLI}" || true 310 sudo pkill -f "${OT_CLI}" || true 311 wait 312 313 if [[ ${NO_CLEAN} != 1 ]]; then 314 echo 'clearing all' 315 sudo rm /etc/dbus-1/system.d/otbr-agent.conf || true 316 sudo rm -rf "${STAGE_DIR}" || true 317 sudo rm -rf "${BUILD_DIR}" || true 318 319 output_logs 320 fi 321 322 echo "EXIT ${EXIT_CODE}: MESSAGE: ${exit_message}" 323 exit ${EXIT_CODE} 324} 325 326ba_start() 327{ 328 exists_or_die "${OTBR_DBUS_CONF}" 329 sudo cp "${OTBR_DBUS_CONF}" /etc/dbus-1/system.d 330 331 write_syslog "AGENT: kill old" 332 sudo killall "${OTBR_AGENT}" || true 333 sleep 5 334 write_syslog "AGENT: starting" 335 336 # we launch this in the background 337 ( 338 set -e 339 set -x 340 341 cd "${ORIGIN_PWD}" 342 343 # check version 344 sudo "${OTBR_AGENT_PATH}" -V 345 # check invalid arguments 346 sudo "${OTBR_AGENT_PATH}" -x && exit $? 347 348 [[ ! -d tmp ]] || sudo rm -rf tmp 349 sudo "${OTBR_AGENT_PATH}" -I "${TUN_NAME}" -v -d 6 "spinel+hdlc+forkpty://${ot_rcp}?forkpty-arg=${LEADER_NODE_ID}" & 350 ) 351 352 # wait for it to complete 353 sleep 10 354 355 pidof ${OTBR_AGENT} || die "AGENT: failed to start" 356 write_syslog "AGENT: start complete" 357} 358 359web_start() 360{ 361 write_syslog "WEB: kill old" 362 sudo killall "${OTBR_WEB}" || true 363 write_syslog "WEB: starting" 364 ( 365 set -e 366 set -x 367 368 cd "${ORIGIN_PWD}" 369 sudo "${OTBR_WEB_PATH}" -I "${TUN_NAME}" -p "${OTBR_WEB_PORT}" -a "${OTBR_WEB_HOST}" & 370 ) 371 sleep 15 372 373 pidof ${OTBR_WEB} || die "WEB: failed to start" 374 write_syslog "WEB: start complete" 375} 376 377network_form() 378{ 379 OT_PANID="$(random_panid)" 380 readonly OT_PANID 381 382 OT_XPANID="$(random_xpanid)" 383 readonly OT_XPANID 384 385 OT_NETWORK_KEY="$(random_networkkey)" 386 readonly OT_NETWORK_KEY 387 388 OT_CHANNEL="$(random_channel)" 389 readonly OT_CHANNEL 390 391 curl --header "Content-Type: application/json" --request POST --data "{\"networkKey\":\"${OT_NETWORK_KEY}\",\"prefix\":\"fd11:22::\",\"defaultRoute\":true,\"extPanId\":\"${OT_XPANID}\",\"panId\":\"${OT_PANID}\",\"passphrase\":\"${OT_AGENT_PASSPHRASE}\",\"channel\":${OT_CHANNEL},\"networkName\":\"${OT_NETWORK_NAME}\"}" "${OTBR_WEB_URL}"/form_network | grep "success" || die "WEB: form failed" 392 sleep 15 393 # verify mDNS is working as expected. 394 local mdns_result="${TEST_BASE}"/mdns_result.log 395 avahi-browse -art | tee "${mdns_result}" 396 OT_BORDER_AGENT_PORT=$(grep -GA3 '^=.\+'"${OT_SERVICE_INSTANCE}"'.\+_meshcop._udp' "${mdns_result}" | head -n4 | grep port | grep -ao '[0-9]\{5\}') 397 rm "${mdns_result}" 398} 399 400ot_commissioner_build() 401{ 402 if [[ -x ${OT_COMMISSIONER_PATH} ]]; then 403 return 0 404 fi 405 406 (mkdir -p "${BUILD_DIR}/ot-commissioner" \ 407 && cd "${BUILD_DIR}/ot-commissioner" \ 408 && (git --git-dir=.git rev-parse --is-inside-work-tree || git --git-dir=.git init .) \ 409 && git fetch --depth 1 https://github.com/openthread/ot-commissioner.git main \ 410 && git checkout FETCH_HEAD \ 411 && ./script/bootstrap.sh \ 412 && mkdir build && cd build \ 413 && cmake -GNinja -DCMAKE_BUILD_TYPE=Release .. \ 414 && ninja) 415} 416 417ot_commissioner_start() 418{ 419 write_syslog "COMMISSIONER: kill old" 420 sudo killall "${OT_COMMISSIONER_CLI}" || true 421 422 OT_PSKC="$("${OTBR_PSKC_PATH}" "${OT_AGENT_PASSPHRASE}" "${OT_XPANID}" "${OT_NETWORK_NAME}")" 423 readonly OT_PSKC 424 425 OT_COMMISSIONER_LOG="${TEST_BASE}"/commissioner.log 426 readonly OT_COMMISSIONER_LOG 427 428 local commissioner_config_file="${TEST_BASE}"/ot-commissioner.json 429 local commissioner_config_file="${TEST_BASE}"/ot-commissioner.json 430 431 sed "s/3aa55f91ca47d1e4e71a08cb35e91591/${OT_PSKC}/g" "${OT_COMMISSIONER_CONFIG}" >"${commissioner_config_file}" 432 433 expect -f- <<EOF & 434spawn ${OT_COMMISSIONER_PATH} ${commissioner_config_file} 435set timeout 1 436expect_after { 437 timeout { exit 1 } 438} 439send "start :: $OT_BORDER_AGENT_PORT\n" 440expect "done" 441sleep 5 442send "active\n" 443expect "true" 444send "joiner enable meshcop 0x${OT_JOINER_EUI64} ${OT_JOINER_PASSPHRASE}\n" 445expect "done" 446wait 447EOF 448 449 sleep 10 450} 451 452web_commissioner_start() 453{ 454 curl --header "Content-Type: application/json" --request POST --data "{\"pskd\":\"${OT_JOINER_PASSPHRASE}\", \"passphrase\":\"${OT_AGENT_PASSPHRASE}\"}" "${OTBR_WEB_URL}"/commission 455 sleep 15 456} 457 458joiner_start() 459{ 460 write_syslog 'JOINER START' 461 cd ${TEST_BASE} 462 sudo expect -f- <<EOF || die 'JOINER FAILED' 463spawn ${ot_cli} ${JOINER_NODE_ID} 464send "ifconfig up\r\n" 465expect "Done" 466send "joiner start ${OT_JOINER_PASSPHRASE}\r\n" 467set timeout 20 468expect { 469 "Join success" { 470 send_user "succeeded to find join success" 471 send "exit\r\n" 472 } 473 timeout { 474 send_user "Failed to find join success" 475 exit 1 476 } 477} 478EOF 479 exit_message="JOINER SUCCESS COMPLETE" 480} 481 482scan_meshcop_service() 483{ 484 if command -v dns-sd; then 485 timeout 2 dns-sd -Z _meshcop._udp local. || true 486 else 487 avahi-browse -aprt || true 488 fi 489} 490 491test_meshcop_service() 492{ 493 local network_name="ot-test-net" 494 local xpanid="4142434445464748" 495 local xpanid_txt="ABCDEFGH" 496 local extaddr="4142434445464748" 497 local extaddr_txt="ABCDEFGH" 498 local passphrase="SECRET" 499 local service 500 501 test_setup 502 ba_start 503 sudo "${OT_CTL}" factoryreset 504 sleep 1 505 sudo "${OT_CTL}" dataset init new 506 sudo "${OT_CTL}" dataset networkname ${network_name} 507 sudo "${OT_CTL}" dataset extpanid ${xpanid} 508 sudo "${OT_CTL}" dataset pskc -p ${passphrase} 509 sudo "${OT_CTL}" dataset commit active 510 sudo "${OT_CTL}" ifconfig up 511 sudo "${OT_CTL}" extaddr ${extaddr} 512 sudo "${OT_CTL}" thread start 513 sleep 20 514 515 sudo "${OT_CTL}" state | grep "leader" 516 517 service="$(scan_meshcop_service)" 518 grep "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 519 grep "rv=1" <<<"${service}" 520 grep "tv=1\.3\.0" <<<"${service}" 521 grep "nn=${network_name}" <<<"${service}" 522 grep "xp=${xpanid_txt}" <<<"${service}" 523 grep "xa=${extaddr_txt}" <<<"${service}" 524 525 # TODO: enable the checks after enabling Thread 1.2 for tests. 526 #grep "dn=${domain_name}" <<< "${service}" 527 #grep "sq=" <<< "${service}" 528 #grep "bb=" <<< "${service}" 529 530 # The binary values are not printable with dns-sd. 531 grep "sb=" <<<"${service}" 532 grep "at=" <<<"${service}" 533 grep "pt=" <<<"${service}" 534 535 # Test if the meshcop service is published when thread is not on 536 sudo "${OT_CTL}" dataset init active 537 sudo "${OT_CTL}" dataset pskc 00000000000000000000000000000000 538 sudo "${OT_CTL}" dataset commit active 539 sleep 2 540 service="$(scan_meshcop_service)" 541 grep -q "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 542 543 # Test if the meshcop service is published again when a non-zero 544 # PSKc is set back. 545 sudo "${OT_CTL}" dataset init active 546 sudo "${OT_CTL}" dataset pskc 11223344556677889900aabbccddeeff 547 sudo "${OT_CTL}" dataset commit active 548 sleep 2 549 service="$(scan_meshcop_service)" 550 grep "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 551 552 # Test if the meshcop service's 'nn' field is updated 553 # when the network name is changed. 554 local new_network_name="ot-test-net-new" 555 sudo "${OT_CTL}" dataset init active 556 sudo "${OT_CTL}" dataset networkname ${new_network_name} 557 sudo "${OT_CTL}" dataset commit active 558 sleep 2 559 service="$(scan_meshcop_service)" 560 grep "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 561 grep "nn=${new_network_name}" <<<"${service}" 562 563 # Test if the discriminator is updated when extaddr is changed. 564 local new_extaddr="4847464544434241" 565 local new_extaddr_txt="HGFEDCBA" 566 sudo "${OT_CTL}" thread stop 567 sudo "${OT_CTL}" extaddr ${new_extaddr} 568 sudo "${OT_CTL}" thread start 569 sleep 5 570 service="$(scan_meshcop_service)" 571 grep "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 572 grep "xa=${new_extaddr_txt}" <<<"${service}" 573 574 # Test if the meshcop service is published when Thread is stopped. 575 sudo "${OT_CTL}" thread stop 576 sleep 2 577 service="$(scan_meshcop_service)" 578 grep -q "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 579 580 sudo "${OT_CTL}" thread start 581 sleep 5 582 service="$(scan_meshcop_service)" 583 grep "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}" 584 585 # Test if the the meshcop service is unpublished when otbr-agent stops. 586 sudo killall "${OTBR_AGENT}" 587 sleep 10 588 service="$(scan_meshcop_service)" 589 if grep -q "${OT_SERVICE_INSTANCE}._meshcop\._udp" <<<"${service}"; then 590 die "unexpect meshcop service when otbr-agent exits!" 591 fi 592} 593 594test_commissioning() 595{ 596 test_setup 597 ba_start 598 web_start 599 network_form 600 if [[ ${OTBR_USE_WEB_COMMISSIONER} == 1 ]]; then 601 web_commissioner_start 602 else 603 ot_commissioner_start 604 fi 605 joiner_start 606} 607 608main() 609{ 610 if [ "${TEST_CASE}" == "mdns_service" ]; then 611 test_meshcop_service 612 else 613 test_commissioning 614 fi 615} 616 617main "$@" 618