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        if {[info exists ::env(OT_NODE_TYPE)]} {
73            set type $::env(OT_NODE_TYPE)
74        } else {
75            set type "cli"
76        }
77    }
78
79    if {[info exists ::env(OT_POSIX_APPS)]} {
80        set ot_posix_apps $::env(OT_POSIX_APPS)
81    } else {
82        set ot_posix_apps "build/posix/src/posix"
83    }
84
85    if {[info exists ::env(OT_SIMULATION_APPS)]} {
86        set ot_simulation_apps $::env(OT_SIMULATION_APPS)
87    } else {
88        set ot_simulation_apps "build/simulation/examples/apps"
89    }
90
91    if {${radio_url} == ""} {
92        set radio_url "spinel+hdlc+uart://$ot_simulation_apps/ncp/ot-rcp?forkpty-arg=$id"
93    }
94
95    send_user "\n# ${id} ${type}\n"
96
97    if {[info exists ::env(CC)] && $::env(CC) == "clang"} {
98        set gcov_prefix ""
99    } else {
100        set gcov_prefix "ot-run/$argv0/ot-gcda.$id"
101    }
102
103    if {[info exists ::env(OT_SIMULATION_LOCAL_HOST)]} {
104        set ot_simulation_local_host $::env(OT_SIMULATION_LOCAL_HOST)
105        set radio_url "$radio_url&forkpty-arg=-L$ot_simulation_local_host"
106    } else {
107        set ot_simulation_local_host "127.0.0.1"
108    }
109
110    switch -regexp ${type} {
111        {rcp|rcp-cli} {
112            # Sleep 0.2 seconds to ensure that the ot-rcp in the previous test has exited to
113            # avoid the error: "bind(sTxFd): Address already in use"
114            sleep 0.2
115            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $ot_posix_apps/ot-cli $radio_url
116            send "factoryreset\n"
117            wait_for "state" "disabled"
118            expect_line "Done"
119            send "routerselectionjitter 1\n"
120            expect_line "Done"
121        }
122        cli {
123            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $ot_simulation_apps/cli/ot-cli-ftd \
124                -L$ot_simulation_local_host $id
125            send "factoryreset\n"
126            wait_for "state" "disabled"
127            expect_line "Done"
128            send "routerselectionjitter 1\n"
129            expect_line "Done"
130        }
131        mtd {
132            spawn /usr/bin/env GCOV_PREFIX=$gcov_prefix $ot_simulation_apps/cli/ot-cli-mtd \
133                -L$ot_simulation_local_host $id
134            send "factoryreset\n"
135            wait_for "state" "disabled"
136            expect_line "Done"
137        }
138    }
139
140    expect_after {
141        timeout { fail "Timed out" }
142    }
143
144    set spawn_ids($id) $spawn_id
145
146    return $spawn_id
147}
148
149proc switch_node {id} {
150    global spawn_ids
151    global spawn_id
152
153    send_user "\n# ${id}\n"
154    set spawn_id $spawn_ids($id)
155}
156
157proc attach {{role "leader"}} {
158    send "ifconfig up\n"
159    expect_line "Done"
160    send "thread start\n"
161    expect_line "Done"
162    wait_for "state" $role
163    expect_line "Done"
164}
165
166proc setup_leader {} {
167    send "dataset init new\n"
168    expect_line "Done"
169    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
170    expect_line "Done"
171    send "dataset commit active\n"
172    expect_line "Done"
173    attach
174}
175
176proc dispose_node {id} {
177    switch_node $id
178    send "\x04"
179    expect eof
180}
181
182proc dispose_all {} {
183    global spawn_ids
184    set max_node [array size spawn_ids]
185    for {set i 1} {$i <= $max_node} {incr i} {
186        dispose_node $i
187    }
188    array unset spawn_ids
189}
190
191proc get_ipaddr {type} {
192    send "ipaddr $type\n"
193    expect "ipaddr $type"
194    set rval [expect_line {([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}}]
195    expect_line "Done"
196
197    return $rval
198}
199
200proc get_extaddr {} {
201    send "extaddr\n"
202    set rval [expect_line {[0-9a-fA-F]{16}}]
203    expect_line "Done"
204
205    return $rval
206}
207
208proc get_extpanid {} {
209    send "extpanid\n"
210    set rval [expect_line {[0-9a-fA-F]{16}}]
211    expect_line "Done"
212
213    return $rval
214}
215
216proc get_panid {} {
217    send "panid\n"
218    expect -re {0x([0-9a-fA-F]{4})}
219    set rval $expect_out(1,string)
220    expect_line "Done"
221
222    return $rval
223}
224
225proc get_meshlocal_prefix {} {
226    send "prefix meshlocal\n"
227    expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])}
228    set rval $expect_out(1,string)
229    expect_line "Done"
230
231    return $rval
232}
233
234proc get_rloc16 {} {
235    send "rloc16\n"
236    expect "rloc16"
237    set rval [expect_line {[0-9a-fA-F]{4}}]
238    expect_line "Done"
239
240    return $rval
241}
242
243proc setup_default_network {} {
244    send "dataset init new\n"
245    expect_line "Done"
246    send "dataset channel 11\n"
247    expect_line "Done"
248    send "dataset extpanid 000db80000000000\n"
249    expect_line "Done"
250    send "dataset meshlocalprefix fd00:db8::\n"
251    expect_line "Done"
252    send "dataset networkkey 00112233445566778899aabbccddeeff\n"
253    expect_line "Done"
254    send "dataset networkname OpenThread-face\n"
255    expect_line "Done"
256    send "dataset panid 0xface\n"
257    expect_line "Done"
258    send "dataset pskc c23a76e98f1a6483639b1ac1271e2e27\n"
259    expect_line "Done"
260    send "dataset commit active\n"
261    expect_line "Done"
262}
263
264proc fail {message} {
265    dispose_all
266    error $message
267}
268
269set timeout 10
270