xref: /aosp_15_r20/external/ot-br-posix/script/test (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1#!/bin/bash
2#
3#  Copyright (c) 2017, 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#   Description:
30#       This script runs border router tests.
31#
32
33# shellcheck source=script/_initrc
34. "$(dirname "$0")"/_initrc
35
36readonly OTBR_VERBOSE=${OTBR_VERBOSE:-0}
37
38OTBR_COLOR_PASS='\033[0;32m'
39readonly OTBR_COLOR_PASS
40
41OTBR_COLOR_FAIL='\033[0;31m'
42readonly OTBR_COLOR_FAIL
43
44OTBR_COLOR_NONE='\033[0m'
45readonly OTBR_COLOR_NONE
46
47QUIET=${QUIET:-1}
48readonly QUIET
49
50OTBR_BUILD_TYPE="${OTBR_BUILD_TYPE:-Debug}"
51readonly OTBR_BUILD_TYPE
52
53OTBR_COVERAGE="${OTBR_COVERAGE:-0}"
54readonly OTBR_COVERAGE
55
56OTBR_MDNS="${OTBR_MDNS-}"
57readonly OTBR_MDNS
58
59OTBR_REST="${OTBR_REST-}"
60readonly OTBR_REST
61
62OTBR_OPTIONS="${OTBR_OPTIONS-}"
63readonly OTBR_OPTIONS
64
65OTBR_TOP_BUILDDIR="${OTBR_TOP_BUILDDIR:-${BUILD_DIR}/otbr}"
66readonly OTBR_TOP_BUILDDIR
67
68#######################################
69# Run test and print result.
70# Globals:
71#   OTBR_COLOR_PASS
72#   OTBR_COLOR_FAIL
73#   OTBR_COLOR_NONE
74# Arguments:
75#   $@  - Test command
76# Returns:
77#   0           - Test passed.
78#   Otherwise   - Test failed!
79#######################################
80print_result()
81{
82    local exit_code=0
83
84    echo -e "$(date) Running $*"
85
86    if [[ ${OTBR_VERBOSE} == 0 ]]; then
87        "$@" &>test.log || exit_code=$?
88    else
89        "$@" || exit_code=$?
90    fi
91
92    if [[ $exit_code == 0 ]]; then
93        prefix="${OTBR_COLOR_PASS}PASS${OTBR_COLOR_NONE}"
94    else
95        prefix="${OTBR_COLOR_FAIL}FAIL${OTBR_COLOR_NONE}"
96    fi
97
98    echo -e "${prefix} $*"
99
100    # only output log on failure
101    if [[ ${OTBR_VERBOSE} == 0 && ${exit_code} != 0 ]]; then
102        cat test.log
103    fi
104
105    return ${exit_code}
106}
107
108print_usage()
109{
110    cat <<EOF
111USAGE: $0 COMMAND
112
113COMMAND:
114    build       Build project for running tests. This can be used to rebuild the project for changes.
115    clean       Clean built files to prepare new build.
116    meshcop     Run MeshCoP tests.
117    openwrt     Run OpenWRT tests.
118    help        Print this help.
119
120EXAMPLES:
121    $0 clean build
122EOF
123    exit "$1"
124}
125
126do_build()
127{
128    otbr_options=(
129        "-DCMAKE_BUILD_TYPE=${OTBR_BUILD_TYPE}"
130        "-DCMAKE_INSTALL_PREFIX=/usr"
131        "-DOT_THREAD_VERSION=1.3"
132        "-DOTBR_DBUS=ON"
133        "-DOTBR_FEATURE_FLAGS=ON"
134        "-DOTBR_TELEMETRY_DATA_API=ON"
135        "-DOTBR_WEB=ON"
136        "-DOTBR_UNSECURE_JOIN=ON"
137        "-DOTBR_TREL=ON"
138        "-DOTBR_LINK_METRICS_TELEMETRY=ON"
139        ${otbr_options[@]+"${otbr_options[@]}"}
140    )
141
142    ./script/cmake-build "${otbr_options[@]}"
143}
144
145do_clean()
146{
147    echo "Removing ${BUILD_DIR} (requiring sudo)"
148    rm -rf "${BUILD_DIR}" || sudo rm -rf "${BUILD_DIR}"
149}
150
151do_check()
152{
153    (cd "${OTBR_TOP_BUILDDIR}" \
154        && ninja && sudo ninja install \
155        && CTEST_OUTPUT_ON_FAILURE=1 ctest -LE sudo \
156        && CTEST_OUTPUT_ON_FAILURE=1 sudo ctest -L sudo) # Seperate running tests for sudo and non-sudo cases.
157}
158
159do_doxygen()
160{
161    otbr_options=(
162        "-DOTBR_DOC=ON"
163    )
164
165    OTBR_TARGET="doxygen" ./script/cmake-build "${otbr_options[@]}"
166}
167
168do_prepare()
169{
170    if [[ ${OTBR_OPTIONS} ]]; then
171        read -r -a otbr_options <<<"${OTBR_OPTIONS}"
172    else
173        otbr_options=()
174    fi
175
176    if [[ -n ${OTBR_MDNS} ]]; then
177        otbr_options+=("-DOTBR_MDNS=${OTBR_MDNS}")
178    fi
179
180    if [[ ${OTBR_COVERAGE} == 1 ]]; then
181        otbr_options+=("-DOTBR_COVERAGE=ON")
182    fi
183
184    if [[ ${OTBR_REST} == "rest-off" ]]; then
185        otbr_options+=("-DOTBR_REST=OFF")
186    else
187        otbr_options+=("-DOTBR_REST=ON")
188    fi
189}
190
191do_package()
192{
193    otbr_options=(
194        "-DBUILD_TESTING=OFF"
195        "-DCMAKE_INSTALL_PREFIX=/usr"
196        "-DCMAKE_BUILD_TYPE=Release"
197        "-DOTBR_WEB=ON"
198        ${otbr_options[@]+"${otbr_options[@]}"}
199    )
200
201    OTBR_TARGET="package" ./script/cmake-build "${otbr_options[@]}"
202}
203
204do_simulation()
205{
206    export top_builddir="${OTBR_TOP_BUILDDIR}"
207
208    print_result tests/scripts/auto-attach
209    print_result tests/scripts/infra-link-selector
210}
211
212main()
213{
214    if [[ $# == 0 ]]; then
215        print_usage 1
216    fi
217
218    do_prepare
219
220    while [[ $# != 0 ]]; do
221        case "$1" in
222            build)
223                do_build
224                ;;
225            check)
226                do_check
227                ;;
228            clean)
229                do_clean
230                ;;
231            doxygen)
232                do_doxygen
233                ;;
234            help)
235                print_usage
236                ;;
237            meshcop)
238                top_builddir="${OTBR_TOP_BUILDDIR}" print_result ./tests/scripts/meshcop
239                ;;
240            ncp_mode)
241                top_builddir="${OTBR_TOP_BUILDDIR}" print_result ./tests/scripts/ncp_mode
242                ;;
243            openwrt)
244                print_result ./tests/scripts/openwrt
245                ;;
246            simulation)
247                do_simulation
248                ;;
249            package)
250                do_package
251                ;;
252            *)
253                echo "Unknown test: ${1}"
254                print_usage 1
255                ;;
256        esac
257        shift
258    done
259}
260
261main "$@"
262