xref: /aosp_15_r20/external/ot-br-posix/tests/scripts/infra-link-selector (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1#!/bin/bash
2#
3#  Copyright (c) 2022, 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
29set -euxo pipefail
30
31SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
32readonly SCRIPT_DIR
33
34ABS_TOP_BUILDDIR="$(cd "${top_builddir:-"${SCRIPT_DIR}"/../../}" && pwd)"
35readonly ABS_TOP_BUILDDIR
36
37OTBR_AGENT="${ABS_TOP_BUILDDIR}/src/agent/otbr-agent"
38readonly OTBR_AGENT
39
40OT_RCP=$(command -v ot-rcp)
41readonly OT_RCP
42
43at_exit()
44{
45    EXIT_CODE=$?
46
47    sudo killall otbr-agent || true
48
49    sudo ip link del ilstest0 || true
50    sudo ip link del ilstest1 || true
51    sudo ip link del ilstest2 || true
52
53    exit $EXIT_CODE
54}
55
56trap at_exit INT TERM EXIT
57
58sudo cp "${ABS_TOP_BUILDDIR}/src/agent/otbr-agent.conf" /etc/dbus-1/system.d/
59sudo chmod +r /etc/dbus-1/system.d/otbr-agent.conf
60sudo systemctl reload dbus
61
62sudo modprobe dummy
63
64prepare_infra_link()
65{
66    local netif="$1"
67    local mac="$2"
68
69    sudo ip link add "${netif}" type dummy
70    sudo ifconfig "${netif}" hw ether "${mac}"
71    sudo ifconfig "${netif}" up
72}
73
74sudo ip link del ilstest0 || true
75sudo ip link del ilstest1 || true
76sudo ip link del ilstest2 || true
77
78prepare_infra_link "ilstest0" "C8:D7:4A:4E:47:00"
79prepare_infra_link "ilstest1" "C8:D7:4A:4E:47:01"
80prepare_infra_link "ilstest2" "C8:D7:4A:4E:47:02"
81
82sleep 10
83ifconfig
84ip link list
85
86sudo "${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
88function check_infra_link()
89{
90    grep "\-ILS\-\-\-\-\-: Infra link \(selected\|unchanged\|switched\)" output | tail -1
91}
92
93function verify_otbr_agent_exited()
94{
95    if pgrep otbr-agent; then
96        return 1
97    fi
98}
99
100sleep 3
101# Verify ILS selects ilstest0
102check_infra_link | grep "selected: ilstest0"
103
104sudo ifconfig ilstest1 down
105sudo ifconfig ilstest2 down
106
107sleep 3
108# Verify ILS keeps using ilstest0
109check_infra_link | grep "unchanged: ilstest0"
110
111sudo ifconfig ilstest2 up
112
113sleep 3
114# Verify ILS keeps using ilstest0 because ilstest0 is still RUNNING
115check_infra_link | grep "unchanged: ilstest0"
116
117sudo ifconfig ilstest0 down
118sleep 3
119# Verify ILS keeps using ilstest0 because ilstest0 was RUNNING recently
120check_infra_link | grep "unchanged: ilstest0"
121
122sudo ifconfig ilstest0 up
123sleep 11
124
125# Verify ILS keeps using ilstest0 because ilstest0 is RUNNING again
126check_infra_link | grep "unchanged: ilstest0"
127
128sudo ifconfig ilstest0 down
129sleep 11
130# Verify ILS switches to ilstest2 after ilstest0 is DOWN for more than 10s
131check_infra_link | grep "switched from ilstest0 to ilstest2"
132verify_otbr_agent_exited
133
134# Now, only ilstest2 is RUNNING
135
136sudo "${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
138sleep 3
139# Verify ILS selects ilstest2 after reboot
140check_infra_link | grep "selected: ilstest2"
141
142sudo ifconfig ilstest2 down
143sleep 3
144# Verify ILS keeps using ilstest2 because ilstest2 was RUNNING recently
145check_infra_link | grep "unchanged: ilstest2"
146
147sleep 8
148sudo ifconfig ilstest1 up
149sleep 3
150# Verify ILS switches to ilstest1 because ilstest2 was not RUNNING for more than 10s
151check_infra_link | grep "switched from ilstest2 to ilstest1"
152verify_otbr_agent_exited
153