1#!/bin/bash 2# 3# Copyright (c) 2020, 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 build and run otbr dbus server 30# 31 32set -euxo pipefail 33 34readonly OTBR_DBUS_SERVER_CONF=otbr-test-agent.conf 35 36on_exit() 37{ 38 local status=$? 39 40 sudo killall otbr-agent || true 41 sudo killall expect || true 42 sudo killall ot-cli-ftd || true 43 sudo killall ot-cli-mtd || true 44 sudo killall dbus-monitor || true 45 sudo rm "/etc/dbus-1/system.d/${OTBR_DBUS_SERVER_CONF}" || true 46 47 grep -iE 'ot-cli|otbr' </var/log/syslog 48 49 return "${status}" 50} 51 52scan_meshcop_service() 53{ 54 if command -v dns-sd; then 55 timeout 5 dns-sd -Z _meshcop._udp local. || true 56 else 57 avahi-browse -aprt || true 58 fi 59} 60 61update_meshcop_txt_and_check() 62{ 63 sudo gdbus call --system --dest io.openthread.BorderRouter.wpan0 --method=io.openthread.BorderRouter.UpdateVendorMeshCopTxtEntries --object-path /io/openthread/BorderRouter/wpan0 "[('nn',[97])]" || true 64 sleep 5 65 service="$(scan_meshcop_service)" 66 grep --binary-files=text "nn=OpenThread" <<<"${service}" 67 68 sudo gdbus call --system --dest io.openthread.BorderRouter.wpan0 --method=io.openthread.BorderRouter.UpdateVendorMeshCopTxtEntries --object-path /io/openthread/BorderRouter/wpan0 "[('vn',[118,101,110,100,111,114])]" 69 sleep 5 70 service="$(scan_meshcop_service)" 71 grep --binary-files=text "vn=vendor" <<<"${service}" 72 73 sudo gdbus call --system --dest io.openthread.BorderRouter.wpan0 --method=io.openthread.BorderRouter.UpdateVendorMeshCopTxtEntries --object-path /io/openthread/BorderRouter/wpan0 "[]" 74 sleep 5 75 service="$(scan_meshcop_service)" 76 grep --binary-files=text "vn=OpenThread" <<<"${service}" 77 78 sudo gdbus call --system --dest io.openthread.BorderRouter.wpan0 --method=io.openthread.BorderRouter.UpdateVendorMeshCopTxtEntries --object-path /io/openthread/BorderRouter/wpan0 "[('A',[97,98,99]),('B',[49,50])]" 79 sleep 5 80 service="$(scan_meshcop_service)" 81 grep --binary-files=text "A=abc" <<<"${service}" 82 grep --binary-files=text "B=12" <<<"${service}" 83 84 sudo gdbus call --system --dest io.openthread.BorderRouter.wpan0 --method=io.openthread.BorderRouter.Reset --object-path /io/openthread/BorderRouter/wpan0 85 sleep 5 86 service="$(scan_meshcop_service)" 87 grep --binary-files=text "A=abc" <<<"${service}" 88 grep --binary-files=text "B=12" <<<"${service}" 89 90 sudo gdbus call --system --dest io.openthread.BorderRouter.wpan0 --method=io.openthread.BorderRouter.UpdateVendorMeshCopTxtEntries --object-path /io/openthread/BorderRouter/wpan0 "[('A',[97,99]),('B',[49,50])]" 91 sleep 5 92 service="$(scan_meshcop_service)" 93 grep --binary-files=text "A=ac" <<<"${service}" 94 grep --binary-files=text "B=12" <<<"${service}" 95} 96 97main() 98{ 99 sudo rm -rf tmp 100 sudo install -m 644 "${CMAKE_BINARY_DIR}"/src/agent/otbr-agent.conf /etc/dbus-1/system.d/"${OTBR_DBUS_SERVER_CONF}" 101 sudo service dbus reload 102 trap on_exit EXIT 103 sudo "${CMAKE_BINARY_DIR}"/src/agent/otbr-agent -d7 -I wpan0 --radio-version "spinel+hdlc+forkpty://$(command -v ot-rcp)?forkpty-arg=1" | grep "OPENTHREAD" 104 105 local temp_dir 106 temp_dir=$(mktemp -d) 107 108 # Because we do want to run the command as root but redirect as the normal user. 109 # shellcheck disable=SC2024 110 sudo dbus-monitor --system path=/io/openthread/BorderRouter/wpan0,member=Ready >"${temp_dir}/dbus.out" & 111 112 sleep 1 113 114 sudo "${CMAKE_BINARY_DIR}"/src/agent/otbr-agent -d7 -I wpan0 "spinel+hdlc+forkpty://$(command -v ot-rcp)?forkpty-arg=1" & 115 if ! (tail -f "${temp_dir}/dbus.out" &) | timeout 10s grep -q Ready; then 116 cat "${temp_dir}/dbus.out" 117 exit 1 118 fi 119 sleep 5 120 121 local ot_version 122 local rcp_version 123 local thread_version 124 ot_version=$(sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl version | grep -oE '^OPENTHREAD.*$' | tr -d '\r\n') 125 rcp_version=$(sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl rcp version | grep -oE '^OPENTHREAD.*$' | tr -d '\r\n') 126 thread_version=$(sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl thread version | grep -oE '^[0-9]+' | tr -d '\r\n') 127 128 local property_names="array:string:" 129 property_names+="OtHostVersion," 130 property_names+="OtRcpVersion," 131 property_names+="ThreadVersion," 132 property_names+="Uptime," 133 property_names+="RadioCoexMetrics," 134 property_names+="RadioSpinelMetrics," 135 property_names+="RcpInterfaceMetrics" 136 local result_pattern="\s+variant\s+string\s+\"${ot_version}\"" 137 result_pattern+="\s+variant\s+string\s+\"${rcp_version}\"" 138 result_pattern+="\s+variant\s+uint16\s+${thread_version}" 139 result_pattern+="\s+variant\s+uint64\s+\d+" # Uptime 140 result_pattern+="\s+variant\s+struct\s+{(\s+uint32\s+\d+){18}\s+boolean\s+(true|false)\s+}" # RadioCoexMetrics 141 result_pattern+="\s+variant\s+struct\s+{(\s+uint32\s+\d+){4}\s+}" # RadioSpinelMetrics 142 result_pattern+="\s+variant\s+struct\s+{\s+byte\s+\d+(\s+uint64\s+\d+){7}\s+}" # RcpInterfaceMetrics 143 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 --print-reply \ 144 /io/openthread/BorderRouter/wpan0 \ 145 io.openthread.BorderRouter.GetProperties \ 146 "${property_names}" \ 147 | grep -oPz "${result_pattern}" 148 149 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl ifconfig up 150 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl thread start 151 152 sleep 12 153 154 update_meshcop_txt_and_check 155 156 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset 157 sleep 1 158 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 159 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 160 org.freedesktop.DBus.Introspectable.Introspect | grep JoinerStart 161 (sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 162 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 163 io.openthread.BorderRouter.JoinerStart \ 164 string:ABCDEF string:mock string:mock \ 165 string:mock string:mock string:mock 2>&1 || true) | grep NotFound 166 167 # Verify Eui64 property. 0x18b4300000000001 = 1780100529276321793 168 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 --print-reply /io/openthread/BorderRouter/wpan0 org.freedesktop.DBus.Properties.Get string:io.openthread.BorderRouter string:Eui64 \ 169 | grep '1780100529276321793' 170 171 # The ot-cli-ftd node is used to test Thread attach. 172 expect <<EOF & 173spawn ot-cli-ftd 3 174send "panid 0x7890\r\n" 175expect "Done" 176send "networkname Test1\r\n" 177expect "Done" 178send "channel 15\r\n" 179expect "Done" 180send "ifconfig up\r\n" 181expect "Done" 182send "thread start\r\n" 183expect "Done" 184sleep 12 185send "state\r\n" 186expect "leader" 187expect "Done" 188send "commissioner start\r\n" 189expect "Commissioner: active" 190send "commissioner joiner add * ABCDEF\r\n" 191expect "Done" 192expect "Joiner end" 193send "commissioner stop\r\n" 194set timeout -1 195expect eof 196EOF 197 # The ot-cli-mtd node is used to test the child and neighbor table. 198 expect <<EOF & 199spawn ot-cli-mtd 2 200send "panid 0x3456\r\n" 201expect "Done" 202send "networkkey 00112233445566778899aabbccddeeff\r\n" 203expect "Done" 204send "networkname Test\r\n" 205expect "Done" 206send "channel 11\r\n" 207expect "Done" 208send "ifconfig up\r\n" 209expect "Done" 210send "thread start\r\n" 211expect "Done" 212set timeout -1 213expect eof 214EOF 215 sleep 12 216 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 217 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 218 io.openthread.BorderRouter.JoinerStart \ 219 string:ABCDEF string:mock string:mock \ 220 string:mock string:mock string:mock 221 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl state | grep -e child -e router 222 223 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 224 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 225 io.openthread.BorderRouter.Detach 226 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl state | grep disabled 227 228 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset 229 sleep 1 230 231 sudo "${CMAKE_BINARY_DIR}"/tests/dbus/otbr-test-dbus-client 232 233 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset 234 sleep 1 235 236 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 237 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 238 io.openthread.BorderRouter.Attach \ 239 array:byte: \ 240 uint16:0xffff \ 241 string:OpenThread \ 242 uint64:0xffffffffffffffff \ 243 array:byte: \ 244 uint32:0xffffffff 245 246 local dataset="0x0e,0x08,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x18," 247 dataset+="0x35,0x06,0x00,0x04,0x00,0x1f,0xff,0xe0,0x02,0x08,0xef,0x18,0xe6,0xa1,0xf3,0xd6," 248 dataset+="0x86,0xc4,0x07,0x08,0xfd,0x16,0x72,0x24,0xc2,0x4e,0x16,0x00,0x05,0x10,0xbe,0x3b," 249 dataset+="0xd2,0x44,0xae,0x6d,0x99,0x70,0x20,0xa8,0x82,0xa2,0x4a,0x80,0x40,0xe2,0x03,0x0f," 250 dataset+="0x4f,0x70,0x65,0x6e,0x54,0x68,0x72,0x65,0x61,0x64,0x2d,0x30,0x36,0x62,0x37,0x01," 251 dataset+="0x02,0x06,0xb7,0x04,0x10,0xf9,0xc9,0x1b,0x11,0x45,0x02,0x54,0x67,0xbf,0x11,0xed," 252 dataset+="0xf9,0x01,0x1a,0x58,0x12,0x0c,0x04,0x02,0xa0,0xff,0xf8" 253 254 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 255 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 256 io.openthread.BorderRouter.AttachAllNodesTo \ 257 "array:byte:${dataset}" \ 258 | grep "int64 300000" 259 260 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl dataset pending | grep "Active Timestamp: 2" 261 262 sleep 310 263 264 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl dataset active | grep "Active Timestamp: 2" 265 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl networkkey | grep be3bd244ae6d997020a882a24a8040e2 266 267 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset 268 sleep 1 269 270 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 271 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 272 io.openthread.BorderRouter.AttachAllNodesTo \ 273 "array:byte:${dataset}" \ 274 | grep "int64 0" 275 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl state | grep "leader" 276 277 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset 278 sleep 1 279 280 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 281 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 282 io.openthread.BorderRouter.Attach \ 283 array:byte: \ 284 uint16:0xffff \ 285 string:OpenThread \ 286 uint64:0xffffffffffffffff \ 287 array:byte: \ 288 uint32:0xffffffff 289 290 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl state | grep "leader" 291 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl thread stop 292 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl state | grep "disabled" 293 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl dataset active | grep "Done" 294 295 sudo dbus-send --system --dest=io.openthread.BorderRouter.wpan0 \ 296 --type=method_call --print-reply /io/openthread/BorderRouter/wpan0 \ 297 io.openthread.BorderRouter.AttachAllNodesTo \ 298 "array:byte:${dataset}" \ 299 | grep "int64 300000" 300 301 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl state | grep "leader" 302 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl dataset pending | grep "Active Timestamp: 2" 303 304 sleep 310 305 306 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl dataset active | grep "Active Timestamp: 2" 307 sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl networkkey | grep be3bd244ae6d997020a882a24a8040e2 308} 309 310main "$@" 311