xref: /aosp_15_r20/external/openthread/tests/scripts/expect/_common.exp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/expect -f
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2020, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Worker
30*cfb92d14SAndroid Build Coastguard Workerproc skip_on_macos {} {
31*cfb92d14SAndroid Build Coastguard Worker    set OSTYPE [lindex $::tcl_platform(os) 0]
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker    if { $OSTYPE == "Darwin" } {
34*cfb92d14SAndroid Build Coastguard Worker        exit 77
35*cfb92d14SAndroid Build Coastguard Worker    }
36*cfb92d14SAndroid Build Coastguard Worker}
37*cfb92d14SAndroid Build Coastguard Worker
38*cfb92d14SAndroid Build Coastguard Workerproc wait_for {command success {failure {[\r\n]FAILURE_NOT_EXPECTED[\r\n]}}} {
39*cfb92d14SAndroid Build Coastguard Worker    set timeout 1
40*cfb92d14SAndroid Build Coastguard Worker    for {set i 0} {$i < 40} {incr i} {
41*cfb92d14SAndroid Build Coastguard Worker        if {$command != ""} {
42*cfb92d14SAndroid Build Coastguard Worker            send "$command\n"
43*cfb92d14SAndroid Build Coastguard Worker        }
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Worker        expect {
46*cfb92d14SAndroid Build Coastguard Worker            -re $success {
47*cfb92d14SAndroid Build Coastguard Worker                return 0
48*cfb92d14SAndroid Build Coastguard Worker            }
49*cfb92d14SAndroid Build Coastguard Worker            -re $failure {
50*cfb92d14SAndroid Build Coastguard Worker                fail "Failed due to '$failure' found"
51*cfb92d14SAndroid Build Coastguard Worker            }
52*cfb92d14SAndroid Build Coastguard Worker            timeout {
53*cfb92d14SAndroid Build Coastguard Worker                # Do nothing
54*cfb92d14SAndroid Build Coastguard Worker            }
55*cfb92d14SAndroid Build Coastguard Worker        }
56*cfb92d14SAndroid Build Coastguard Worker    }
57*cfb92d14SAndroid Build Coastguard Worker    fail "Failed due to '$success' not found"
58*cfb92d14SAndroid Build Coastguard Worker}
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Workerproc expect_line {line} {
61*cfb92d14SAndroid Build Coastguard Worker    set timeout 10
62*cfb92d14SAndroid Build Coastguard Worker    expect -re "\[\r\n \]($line)(?=\[\r\n>\])"
63*cfb92d14SAndroid Build Coastguard Worker    return $expect_out(1,string)
64*cfb92d14SAndroid Build Coastguard Worker}
65*cfb92d14SAndroid Build Coastguard Worker
66*cfb92d14SAndroid Build Coastguard Workerproc spawn_node {id {type ""} {radio_url ""}} {
67*cfb92d14SAndroid Build Coastguard Worker    global spawn_id
68*cfb92d14SAndroid Build Coastguard Worker    global spawn_ids
69*cfb92d14SAndroid Build Coastguard Worker    global argv0
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Worker    if {${type} == ""} {
72*cfb92d14SAndroid Build Coastguard Worker        if {[info exists ::env(OT_NODE_TYPE)]} {
73*cfb92d14SAndroid Build Coastguard Worker            set type $::env(OT_NODE_TYPE)
74*cfb92d14SAndroid Build Coastguard Worker        } else {
75*cfb92d14SAndroid Build Coastguard Worker            set type "cli"
76*cfb92d14SAndroid Build Coastguard Worker        }
77*cfb92d14SAndroid Build Coastguard Worker    }
78*cfb92d14SAndroid Build Coastguard Worker
79*cfb92d14SAndroid Build Coastguard Worker    if {[info exists ::env(OT_POSIX_APPS)]} {
80*cfb92d14SAndroid Build Coastguard Worker        set ot_posix_apps $::env(OT_POSIX_APPS)
81*cfb92d14SAndroid Build Coastguard Worker    } else {
82*cfb92d14SAndroid Build Coastguard Worker        set ot_posix_apps "build/posix/src/posix"
83*cfb92d14SAndroid Build Coastguard Worker    }
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker    if {[info exists ::env(OT_SIMULATION_APPS)]} {
86*cfb92d14SAndroid Build Coastguard Worker        set ot_simulation_apps $::env(OT_SIMULATION_APPS)
87*cfb92d14SAndroid Build Coastguard Worker    } else {
88*cfb92d14SAndroid Build Coastguard Worker        set ot_simulation_apps "build/simulation/examples/apps"
89*cfb92d14SAndroid Build Coastguard Worker    }
90*cfb92d14SAndroid Build Coastguard Worker
91*cfb92d14SAndroid Build Coastguard Worker    if {${radio_url} == ""} {
92*cfb92d14SAndroid Build Coastguard Worker        set radio_url "spinel+hdlc+uart://$ot_simulation_apps/ncp/ot-rcp?forkpty-arg=$id"
93*cfb92d14SAndroid Build Coastguard Worker    }
94*cfb92d14SAndroid Build Coastguard Worker
95*cfb92d14SAndroid Build Coastguard Worker    send_user "\n# ${id} ${type}\n"
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker    if {[info exists ::env(CC)] && $::env(CC) == "clang"} {
98*cfb92d14SAndroid Build Coastguard Worker        set gcov_prefix ""
99*cfb92d14SAndroid Build Coastguard Worker    } else {
100*cfb92d14SAndroid Build Coastguard Worker        set gcov_prefix "ot-run/$argv0/ot-gcda.$id"
101*cfb92d14SAndroid Build Coastguard Worker    }
102*cfb92d14SAndroid Build Coastguard Worker
103*cfb92d14SAndroid Build Coastguard Worker    if {[info exists ::env(OT_SIMULATION_LOCAL_HOST)]} {
104*cfb92d14SAndroid Build Coastguard Worker        set ot_simulation_local_host $::env(OT_SIMULATION_LOCAL_HOST)
105*cfb92d14SAndroid Build Coastguard Worker        set radio_url "$radio_url&forkpty-arg=-L$ot_simulation_local_host"
106*cfb92d14SAndroid Build Coastguard Worker    } else {
107*cfb92d14SAndroid Build Coastguard Worker        set ot_simulation_local_host "127.0.0.1"
108*cfb92d14SAndroid Build Coastguard Worker    }
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker    switch -regexp ${type} {
111*cfb92d14SAndroid Build Coastguard Worker        {rcp|rcp-cli} {
112*cfb92d14SAndroid Build Coastguard Worker            # Sleep 0.2 seconds to ensure that the ot-rcp in the previous test has exited to
113*cfb92d14SAndroid Build Coastguard Worker            # avoid the error: "bind(sTxFd): Address already in use"
114*cfb92d14SAndroid Build Coastguard Worker            sleep 0.2
115*cfb92d14SAndroid Build Coastguard Worker            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $ot_posix_apps/ot-cli $radio_url
116*cfb92d14SAndroid Build Coastguard Worker            send "factoryreset\n"
117*cfb92d14SAndroid Build Coastguard Worker            wait_for "state" "disabled"
118*cfb92d14SAndroid Build Coastguard Worker            expect_line "Done"
119*cfb92d14SAndroid Build Coastguard Worker            send "routerselectionjitter 1\n"
120*cfb92d14SAndroid Build Coastguard Worker            expect_line "Done"
121*cfb92d14SAndroid Build Coastguard Worker        }
122*cfb92d14SAndroid Build Coastguard Worker        cli {
123*cfb92d14SAndroid Build Coastguard Worker            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $ot_simulation_apps/cli/ot-cli-ftd \
124*cfb92d14SAndroid Build Coastguard Worker                -L$ot_simulation_local_host $id
125*cfb92d14SAndroid Build Coastguard Worker            send "factoryreset\n"
126*cfb92d14SAndroid Build Coastguard Worker            wait_for "state" "disabled"
127*cfb92d14SAndroid Build Coastguard Worker            expect_line "Done"
128*cfb92d14SAndroid Build Coastguard Worker            send "routerselectionjitter 1\n"
129*cfb92d14SAndroid Build Coastguard Worker            expect_line "Done"
130*cfb92d14SAndroid Build Coastguard Worker        }
131*cfb92d14SAndroid Build Coastguard Worker        mtd {
132*cfb92d14SAndroid Build Coastguard Worker            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $ot_simulation_apps/cli/ot-cli-mtd \
133*cfb92d14SAndroid Build Coastguard Worker                -L$ot_simulation_local_host $id
134*cfb92d14SAndroid Build Coastguard Worker            send "factoryreset\n"
135*cfb92d14SAndroid Build Coastguard Worker            wait_for "state" "disabled"
136*cfb92d14SAndroid Build Coastguard Worker            expect_line "Done"
137*cfb92d14SAndroid Build Coastguard Worker        }
138*cfb92d14SAndroid Build Coastguard Worker    }
139*cfb92d14SAndroid Build Coastguard Worker
140*cfb92d14SAndroid Build Coastguard Worker    expect_after {
141*cfb92d14SAndroid Build Coastguard Worker        timeout { fail "Timed out" }
142*cfb92d14SAndroid Build Coastguard Worker    }
143*cfb92d14SAndroid Build Coastguard Worker
144*cfb92d14SAndroid Build Coastguard Worker    set spawn_ids($id) $spawn_id
145*cfb92d14SAndroid Build Coastguard Worker
146*cfb92d14SAndroid Build Coastguard Worker    return $spawn_id
147*cfb92d14SAndroid Build Coastguard Worker}
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Workerproc switch_node {id} {
150*cfb92d14SAndroid Build Coastguard Worker    global spawn_ids
151*cfb92d14SAndroid Build Coastguard Worker    global spawn_id
152*cfb92d14SAndroid Build Coastguard Worker
153*cfb92d14SAndroid Build Coastguard Worker    send_user "\n# ${id}\n"
154*cfb92d14SAndroid Build Coastguard Worker    set spawn_id $spawn_ids($id)
155*cfb92d14SAndroid Build Coastguard Worker}
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Workerproc attach {{role "leader"}} {
158*cfb92d14SAndroid Build Coastguard Worker    send "ifconfig up\n"
159*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
160*cfb92d14SAndroid Build Coastguard Worker    send "thread start\n"
161*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
162*cfb92d14SAndroid Build Coastguard Worker    wait_for "state" $role
163*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
164*cfb92d14SAndroid Build Coastguard Worker}
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard Workerproc setup_leader {} {
167*cfb92d14SAndroid Build Coastguard Worker    send "dataset init new\n"
168*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
169*cfb92d14SAndroid Build Coastguard Worker    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
170*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
171*cfb92d14SAndroid Build Coastguard Worker    send "dataset commit active\n"
172*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
173*cfb92d14SAndroid Build Coastguard Worker    attach
174*cfb92d14SAndroid Build Coastguard Worker}
175*cfb92d14SAndroid Build Coastguard Worker
176*cfb92d14SAndroid Build Coastguard Workerproc dispose_node {id} {
177*cfb92d14SAndroid Build Coastguard Worker    switch_node $id
178*cfb92d14SAndroid Build Coastguard Worker    send "\x04"
179*cfb92d14SAndroid Build Coastguard Worker    expect eof
180*cfb92d14SAndroid Build Coastguard Worker}
181*cfb92d14SAndroid Build Coastguard Worker
182*cfb92d14SAndroid Build Coastguard Workerproc dispose_all {} {
183*cfb92d14SAndroid Build Coastguard Worker    global spawn_ids
184*cfb92d14SAndroid Build Coastguard Worker    set max_node [array size spawn_ids]
185*cfb92d14SAndroid Build Coastguard Worker    for {set i 1} {$i <= $max_node} {incr i} {
186*cfb92d14SAndroid Build Coastguard Worker        dispose_node $i
187*cfb92d14SAndroid Build Coastguard Worker    }
188*cfb92d14SAndroid Build Coastguard Worker    array unset spawn_ids
189*cfb92d14SAndroid Build Coastguard Worker}
190*cfb92d14SAndroid Build Coastguard Worker
191*cfb92d14SAndroid Build Coastguard Workerproc get_ipaddr {type} {
192*cfb92d14SAndroid Build Coastguard Worker    send "ipaddr $type\n"
193*cfb92d14SAndroid Build Coastguard Worker    expect "ipaddr $type"
194*cfb92d14SAndroid Build Coastguard Worker    set rval [expect_line {([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}}]
195*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
196*cfb92d14SAndroid Build Coastguard Worker
197*cfb92d14SAndroid Build Coastguard Worker    return $rval
198*cfb92d14SAndroid Build Coastguard Worker}
199*cfb92d14SAndroid Build Coastguard Worker
200*cfb92d14SAndroid Build Coastguard Workerproc get_extaddr {} {
201*cfb92d14SAndroid Build Coastguard Worker    send "extaddr\n"
202*cfb92d14SAndroid Build Coastguard Worker    set rval [expect_line {[0-9a-fA-F]{16}}]
203*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
204*cfb92d14SAndroid Build Coastguard Worker
205*cfb92d14SAndroid Build Coastguard Worker    return $rval
206*cfb92d14SAndroid Build Coastguard Worker}
207*cfb92d14SAndroid Build Coastguard Worker
208*cfb92d14SAndroid Build Coastguard Workerproc get_extpanid {} {
209*cfb92d14SAndroid Build Coastguard Worker    send "extpanid\n"
210*cfb92d14SAndroid Build Coastguard Worker    set rval [expect_line {[0-9a-fA-F]{16}}]
211*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
212*cfb92d14SAndroid Build Coastguard Worker
213*cfb92d14SAndroid Build Coastguard Worker    return $rval
214*cfb92d14SAndroid Build Coastguard Worker}
215*cfb92d14SAndroid Build Coastguard Worker
216*cfb92d14SAndroid Build Coastguard Workerproc get_panid {} {
217*cfb92d14SAndroid Build Coastguard Worker    send "panid\n"
218*cfb92d14SAndroid Build Coastguard Worker    expect -re {0x([0-9a-fA-F]{4})}
219*cfb92d14SAndroid Build Coastguard Worker    set rval $expect_out(1,string)
220*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
221*cfb92d14SAndroid Build Coastguard Worker
222*cfb92d14SAndroid Build Coastguard Worker    return $rval
223*cfb92d14SAndroid Build Coastguard Worker}
224*cfb92d14SAndroid Build Coastguard Worker
225*cfb92d14SAndroid Build Coastguard Workerproc get_meshlocal_prefix {} {
226*cfb92d14SAndroid Build Coastguard Worker    send "prefix meshlocal\n"
227*cfb92d14SAndroid Build Coastguard Worker    expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])}
228*cfb92d14SAndroid Build Coastguard Worker    set rval $expect_out(1,string)
229*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
230*cfb92d14SAndroid Build Coastguard Worker
231*cfb92d14SAndroid Build Coastguard Worker    return $rval
232*cfb92d14SAndroid Build Coastguard Worker}
233*cfb92d14SAndroid Build Coastguard Worker
234*cfb92d14SAndroid Build Coastguard Workerproc get_rloc16 {} {
235*cfb92d14SAndroid Build Coastguard Worker    send "rloc16\n"
236*cfb92d14SAndroid Build Coastguard Worker    expect "rloc16"
237*cfb92d14SAndroid Build Coastguard Worker    set rval [expect_line {[0-9a-fA-F]{4}}]
238*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
239*cfb92d14SAndroid Build Coastguard Worker
240*cfb92d14SAndroid Build Coastguard Worker    return $rval
241*cfb92d14SAndroid Build Coastguard Worker}
242*cfb92d14SAndroid Build Coastguard Worker
243*cfb92d14SAndroid Build Coastguard Workerproc setup_default_network {} {
244*cfb92d14SAndroid Build Coastguard Worker    send "dataset init new\n"
245*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
246*cfb92d14SAndroid Build Coastguard Worker    send "dataset channel 11\n"
247*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
248*cfb92d14SAndroid Build Coastguard Worker    send "dataset extpanid 000db80000000000\n"
249*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
250*cfb92d14SAndroid Build Coastguard Worker    send "dataset meshlocalprefix fd00:db8::\n"
251*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
252*cfb92d14SAndroid Build Coastguard Worker    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
253*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
254*cfb92d14SAndroid Build Coastguard Worker    send "dataset networkname OpenThread-face\n"
255*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
256*cfb92d14SAndroid Build Coastguard Worker    send "dataset panid 0xface\n"
257*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
258*cfb92d14SAndroid Build Coastguard Worker    send "dataset pskc c23a76e98f1a6483639b1ac1271e2e27\n"
259*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
260*cfb92d14SAndroid Build Coastguard Worker    send "dataset commit active\n"
261*cfb92d14SAndroid Build Coastguard Worker    expect_line "Done"
262*cfb92d14SAndroid Build Coastguard Worker}
263*cfb92d14SAndroid Build Coastguard Worker
264*cfb92d14SAndroid Build Coastguard Workerproc fail {message} {
265*cfb92d14SAndroid Build Coastguard Worker    dispose_all
266*cfb92d14SAndroid Build Coastguard Worker    error $message
267*cfb92d14SAndroid Build Coastguard Worker}
268*cfb92d14SAndroid Build Coastguard Worker
269*cfb92d14SAndroid Build Coastguard Workerset timeout 10
270