1#!/usr/bin/expect -f 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 30proc skip_on_macos {} { 31 set OSTYPE [lindex $::tcl_platform(os) 0] 32 33 if { $OSTYPE == "Darwin" } { 34 exit 77 35 } 36} 37 38proc wait_for {command success {failure {[\r\n]FAILURE_NOT_EXPECTED[\r\n]}}} { 39 set timeout 1 40 for {set i 0} {$i < 40} {incr i} { 41 if {$command != ""} { 42 send "$command\n" 43 } 44 45 expect { 46 -re $success { 47 return 0 48 } 49 -re $failure { 50 fail "Failed due to '$failure' found" 51 } 52 timeout { 53 # Do nothing 54 } 55 } 56 } 57 fail "Failed due to '$success' not found" 58} 59 60proc expect_line {line} { 61 set timeout 10 62 expect -re "\[\r\n \]($line)(?=\[\r\n>\])" 63 return $expect_out(1,string) 64} 65 66proc spawn_node {id {type ""} {radio_url ""}} { 67 global spawn_id 68 global spawn_ids 69 global argv0 70 71 if {${type} == ""} { 72 set type $::env(OT_NODE_TYPE) 73 } 74 75 if {${radio_url} == ""} { 76 set radio_url "spinel+hdlc+uart://$::env(OT_SIMULATION_APPS)/ncp/ot-rcp?forkpty-arg=$id" 77 } 78 79 send_user "\n# ${id} ${type}\n" 80 81 if {[info exists ::env(CC)] && $::env(CC) == "clang"} { 82 set gcov_prefix "" 83 } else { 84 set gcov_prefix "ot-run/$argv0/ot-gcda.$id" 85 } 86 87 switch -regexp ${type} { 88 {rcp|rcp-cli} { 89 # Sleep 0.2 seconds to ensure that the ot-rcp in the previous test has exited to 90 # avoid the error: "bind(sTxFd): Address already in use" 91 sleep 0.2 92 spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_POSIX_APPS)/ot-cli $radio_url 93 send "factoryreset\n" 94 wait_for "state" "disabled" 95 expect_line "Done" 96 send "routerselectionjitter 1\n" 97 expect_line "Done" 98 } 99 cli { 100 spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_SIMULATION_APPS)/cli/ot-cli-ftd $id 101 send "factoryreset\n" 102 wait_for "state" "disabled" 103 expect_line "Done" 104 send "routerselectionjitter 1\n" 105 expect_line "Done" 106 } 107 mtd { 108 spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $::env(OT_SIMULATION_APPS)/cli/ot-cli-mtd $id 109 send "factoryreset\n" 110 wait_for "state" "disabled" 111 expect_line "Done" 112 } 113 } 114 115 expect_after { 116 timeout { fail "Timed out" } 117 } 118 119 set spawn_ids($id) $spawn_id 120 121 return $spawn_id 122} 123 124proc switch_node {id} { 125 global spawn_ids 126 global spawn_id 127 128 send_user "\n# ${id}\n" 129 set spawn_id $spawn_ids($id) 130} 131 132proc attach {{role "leader"}} { 133 send "ifconfig up\n" 134 expect_line "Done" 135 send "thread start\n" 136 expect_line "Done" 137 wait_for "state" $role 138 expect_line "Done" 139} 140 141proc setup_leader {} { 142 send "dataset init new\n" 143 expect_line "Done" 144 send "dataset networkkey 00112233445566778899aabbccddeeff\n" 145 expect_line "Done" 146 send "dataset commit active\n" 147 expect_line "Done" 148 attach 149} 150 151proc dispose_node {id} { 152 switch_node $id 153 send "\x04" 154 expect eof 155} 156 157proc dispose_all {} { 158 global spawn_ids 159 set max_node [array size spawn_ids] 160 for {set i 1} {$i <= $max_node} {incr i} { 161 dispose_node $i 162 } 163 array unset spawn_ids 164} 165 166proc get_ipaddr {type} { 167 send "ipaddr $type\n" 168 expect "ipaddr $type" 169 set rval [expect_line {([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}}] 170 expect_line "Done" 171 172 return $rval 173} 174 175proc get_extaddr {} { 176 send "extaddr\n" 177 set rval [expect_line {[0-9a-fA-F]{16}}] 178 expect_line "Done" 179 180 return $rval 181} 182 183proc get_extpanid {} { 184 send "extpanid\n" 185 set rval [expect_line {[0-9a-fA-F]{16}}] 186 expect_line "Done" 187 188 return $rval 189} 190 191proc get_panid {} { 192 send "panid\n" 193 expect -re {0x([0-9a-fA-F]{4})} 194 set rval $expect_out(1,string) 195 expect_line "Done" 196 197 return $rval 198} 199 200proc get_meshlocal_prefix {} { 201 send "prefix meshlocal\n" 202 expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])} 203 set rval $expect_out(1,string) 204 expect_line "Done" 205 206 return $rval 207} 208 209proc get_rloc16 {} { 210 send "rloc16\n" 211 expect "rloc16" 212 set rval [expect_line {[0-9a-fA-F]{4}}] 213 expect_line "Done" 214 215 return $rval 216} 217 218proc setup_default_network {} { 219 send "dataset init new\n" 220 expect_line "Done" 221 send "dataset channel 11\n" 222 expect_line "Done" 223 send "dataset extpanid 000db80000000000\n" 224 expect_line "Done" 225 send "dataset meshlocalprefix fd00:db8::\n" 226 expect_line "Done" 227 send "dataset networkkey 00112233445566778899aabbccddeeff\n" 228 expect_line "Done" 229 send "dataset networkname OpenThread-face\n" 230 expect_line "Done" 231 send "dataset panid 0xface\n" 232 expect_line "Done" 233 send "dataset pskc c23a76e98f1a6483639b1ac1271e2e27\n" 234 expect_line "Done" 235 send "dataset commit active\n" 236 expect_line "Done" 237} 238 239proc fail {message} { 240 dispose_all 241 error $message 242} 243 244set timeout 10 245