xref: /aosp_15_r20/external/ot-br-posix/tests/scripts/infra-link-selector (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker#!/bin/bash
2*4a64e381SAndroid Build Coastguard Worker#
3*4a64e381SAndroid Build Coastguard Worker#  Copyright (c) 2022, The OpenThread Authors.
4*4a64e381SAndroid Build Coastguard Worker#  All rights reserved.
5*4a64e381SAndroid Build Coastguard Worker#
6*4a64e381SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*4a64e381SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*4a64e381SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*4a64e381SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*4a64e381SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*4a64e381SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*4a64e381SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*4a64e381SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*4a64e381SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*4a64e381SAndroid Build Coastguard Worker#
17*4a64e381SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*4a64e381SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4a64e381SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4a64e381SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*4a64e381SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*4a64e381SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*4a64e381SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*4a64e381SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*4a64e381SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*4a64e381SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*4a64e381SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*4a64e381SAndroid Build Coastguard Worker
29*4a64e381SAndroid Build Coastguard Workerset -euxo pipefail
30*4a64e381SAndroid Build Coastguard Worker
31*4a64e381SAndroid Build Coastguard WorkerSCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
32*4a64e381SAndroid Build Coastguard Workerreadonly SCRIPT_DIR
33*4a64e381SAndroid Build Coastguard Worker
34*4a64e381SAndroid Build Coastguard WorkerABS_TOP_BUILDDIR="$(cd "${top_builddir:-"${SCRIPT_DIR}"/../../}" && pwd)"
35*4a64e381SAndroid Build Coastguard Workerreadonly ABS_TOP_BUILDDIR
36*4a64e381SAndroid Build Coastguard Worker
37*4a64e381SAndroid Build Coastguard WorkerOTBR_AGENT="${ABS_TOP_BUILDDIR}/src/agent/otbr-agent"
38*4a64e381SAndroid Build Coastguard Workerreadonly OTBR_AGENT
39*4a64e381SAndroid Build Coastguard Worker
40*4a64e381SAndroid Build Coastguard WorkerOT_RCP=$(command -v ot-rcp)
41*4a64e381SAndroid Build Coastguard Workerreadonly OT_RCP
42*4a64e381SAndroid Build Coastguard Worker
43*4a64e381SAndroid Build Coastguard Workerat_exit()
44*4a64e381SAndroid Build Coastguard Worker{
45*4a64e381SAndroid Build Coastguard Worker    EXIT_CODE=$?
46*4a64e381SAndroid Build Coastguard Worker
47*4a64e381SAndroid Build Coastguard Worker    sudo killall otbr-agent || true
48*4a64e381SAndroid Build Coastguard Worker
49*4a64e381SAndroid Build Coastguard Worker    sudo ip link del ilstest0 || true
50*4a64e381SAndroid Build Coastguard Worker    sudo ip link del ilstest1 || true
51*4a64e381SAndroid Build Coastguard Worker    sudo ip link del ilstest2 || true
52*4a64e381SAndroid Build Coastguard Worker
53*4a64e381SAndroid Build Coastguard Worker    exit $EXIT_CODE
54*4a64e381SAndroid Build Coastguard Worker}
55*4a64e381SAndroid Build Coastguard Worker
56*4a64e381SAndroid Build Coastguard Workertrap at_exit INT TERM EXIT
57*4a64e381SAndroid Build Coastguard Worker
58*4a64e381SAndroid Build Coastguard Workersudo cp "${ABS_TOP_BUILDDIR}/src/agent/otbr-agent.conf" /etc/dbus-1/system.d/
59*4a64e381SAndroid Build Coastguard Workersudo chmod +r /etc/dbus-1/system.d/otbr-agent.conf
60*4a64e381SAndroid Build Coastguard Workersudo systemctl reload dbus
61*4a64e381SAndroid Build Coastguard Worker
62*4a64e381SAndroid Build Coastguard Workersudo modprobe dummy
63*4a64e381SAndroid Build Coastguard Worker
64*4a64e381SAndroid Build Coastguard Workerprepare_infra_link()
65*4a64e381SAndroid Build Coastguard Worker{
66*4a64e381SAndroid Build Coastguard Worker    local netif="$1"
67*4a64e381SAndroid Build Coastguard Worker    local mac="$2"
68*4a64e381SAndroid Build Coastguard Worker
69*4a64e381SAndroid Build Coastguard Worker    sudo ip link add "${netif}" type dummy
70*4a64e381SAndroid Build Coastguard Worker    sudo ifconfig "${netif}" hw ether "${mac}"
71*4a64e381SAndroid Build Coastguard Worker    sudo ifconfig "${netif}" up
72*4a64e381SAndroid Build Coastguard Worker}
73*4a64e381SAndroid Build Coastguard Worker
74*4a64e381SAndroid Build Coastguard Workersudo ip link del ilstest0 || true
75*4a64e381SAndroid Build Coastguard Workersudo ip link del ilstest1 || true
76*4a64e381SAndroid Build Coastguard Workersudo ip link del ilstest2 || true
77*4a64e381SAndroid Build Coastguard Worker
78*4a64e381SAndroid Build Coastguard Workerprepare_infra_link "ilstest0" "C8:D7:4A:4E:47:00"
79*4a64e381SAndroid Build Coastguard Workerprepare_infra_link "ilstest1" "C8:D7:4A:4E:47:01"
80*4a64e381SAndroid Build Coastguard Workerprepare_infra_link "ilstest2" "C8:D7:4A:4E:47:02"
81*4a64e381SAndroid Build Coastguard Worker
82*4a64e381SAndroid Build Coastguard Workersleep 10
83*4a64e381SAndroid Build Coastguard Workerifconfig
84*4a64e381SAndroid Build Coastguard Workerip link list
85*4a64e381SAndroid Build Coastguard Worker
86*4a64e381SAndroid Build Coastguard Workersudo "${OTBR_AGENT}" -I wpan0 -v -d7 -B ilstest0 -B ilstest1 -B ilstest2 "spinel+hdlc+forkpty://${OT_RCP}?forkpty-arg=1" 2>&1 | tee output &
87*4a64e381SAndroid Build Coastguard Worker
88*4a64e381SAndroid Build Coastguard Workerfunction check_infra_link()
89*4a64e381SAndroid Build Coastguard Worker{
90*4a64e381SAndroid Build Coastguard Worker    grep "\-ILS\-\-\-\-\-: Infra link \(selected\|unchanged\|switched\)" output | tail -1
91*4a64e381SAndroid Build Coastguard Worker}
92*4a64e381SAndroid Build Coastguard Worker
93*4a64e381SAndroid Build Coastguard Workerfunction verify_otbr_agent_exited()
94*4a64e381SAndroid Build Coastguard Worker{
95*4a64e381SAndroid Build Coastguard Worker    if pgrep otbr-agent; then
96*4a64e381SAndroid Build Coastguard Worker        return 1
97*4a64e381SAndroid Build Coastguard Worker    fi
98*4a64e381SAndroid Build Coastguard Worker}
99*4a64e381SAndroid Build Coastguard Worker
100*4a64e381SAndroid Build Coastguard Workersleep 3
101*4a64e381SAndroid Build Coastguard Worker# Verify ILS selects ilstest0
102*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "selected: ilstest0"
103*4a64e381SAndroid Build Coastguard Worker
104*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest1 down
105*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest2 down
106*4a64e381SAndroid Build Coastguard Worker
107*4a64e381SAndroid Build Coastguard Workersleep 3
108*4a64e381SAndroid Build Coastguard Worker# Verify ILS keeps using ilstest0
109*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "unchanged: ilstest0"
110*4a64e381SAndroid Build Coastguard Worker
111*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest2 up
112*4a64e381SAndroid Build Coastguard Worker
113*4a64e381SAndroid Build Coastguard Workersleep 3
114*4a64e381SAndroid Build Coastguard Worker# Verify ILS keeps using ilstest0 because ilstest0 is still RUNNING
115*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "unchanged: ilstest0"
116*4a64e381SAndroid Build Coastguard Worker
117*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest0 down
118*4a64e381SAndroid Build Coastguard Workersleep 3
119*4a64e381SAndroid Build Coastguard Worker# Verify ILS keeps using ilstest0 because ilstest0 was RUNNING recently
120*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "unchanged: ilstest0"
121*4a64e381SAndroid Build Coastguard Worker
122*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest0 up
123*4a64e381SAndroid Build Coastguard Workersleep 11
124*4a64e381SAndroid Build Coastguard Worker
125*4a64e381SAndroid Build Coastguard Worker# Verify ILS keeps using ilstest0 because ilstest0 is RUNNING again
126*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "unchanged: ilstest0"
127*4a64e381SAndroid Build Coastguard Worker
128*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest0 down
129*4a64e381SAndroid Build Coastguard Workersleep 11
130*4a64e381SAndroid Build Coastguard Worker# Verify ILS switches to ilstest2 after ilstest0 is DOWN for more than 10s
131*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "switched from ilstest0 to ilstest2"
132*4a64e381SAndroid Build Coastguard Workerverify_otbr_agent_exited
133*4a64e381SAndroid Build Coastguard Worker
134*4a64e381SAndroid Build Coastguard Worker# Now, only ilstest2 is RUNNING
135*4a64e381SAndroid Build Coastguard Worker
136*4a64e381SAndroid Build Coastguard Workersudo "${OTBR_AGENT}" -I wpan0 -v -d7 -B ilstest0 -B ilstest1 -B ilstest2 "spinel+hdlc+forkpty://${OT_RCP}?forkpty-arg=1" 2>&1 | tee output &
137*4a64e381SAndroid Build Coastguard Worker
138*4a64e381SAndroid Build Coastguard Workersleep 3
139*4a64e381SAndroid Build Coastguard Worker# Verify ILS selects ilstest2 after reboot
140*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "selected: ilstest2"
141*4a64e381SAndroid Build Coastguard Worker
142*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest2 down
143*4a64e381SAndroid Build Coastguard Workersleep 3
144*4a64e381SAndroid Build Coastguard Worker# Verify ILS keeps using ilstest2 because ilstest2 was RUNNING recently
145*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "unchanged: ilstest2"
146*4a64e381SAndroid Build Coastguard Worker
147*4a64e381SAndroid Build Coastguard Workersleep 8
148*4a64e381SAndroid Build Coastguard Workersudo ifconfig ilstest1 up
149*4a64e381SAndroid Build Coastguard Workersleep 3
150*4a64e381SAndroid Build Coastguard Worker# Verify ILS switches to ilstest1 because ilstest2 was not RUNNING for more than 10s
151*4a64e381SAndroid Build Coastguard Workercheck_infra_link | grep "switched from ilstest2 to ilstest1"
152*4a64e381SAndroid Build Coastguard Workerverify_otbr_agent_exited
153