xref: /freebsd/sbin/route/tests/basic.sh (revision ca7edd0d2c339b8e6e5e52257ea6d4f22db02550)
13b01bf88STom Jones#-
24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
33b01bf88STom Jones#
43b01bf88STom Jones# Copyright (c) 2020 Ahsan Barkati
53b01bf88STom Jones#
63b01bf88STom Jones# Redistribution and use in source and binary forms, with or without
73b01bf88STom Jones# modification, are permitted provided that the following conditions
83b01bf88STom Jones# are met:
93b01bf88STom Jones# 1. Redistributions of source code must retain the above copyright
103b01bf88STom Jones#    notice, this list of conditions and the following disclaimer.
113b01bf88STom Jones# 2. Redistributions in binary form must reproduce the above copyright
123b01bf88STom Jones#    notice, this list of conditions and the following disclaimer in the
133b01bf88STom Jones#    documentation and/or other materials provided with the distribution.
143b01bf88STom Jones#
153b01bf88STom Jones# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163b01bf88STom Jones# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173b01bf88STom Jones# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183b01bf88STom Jones# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193b01bf88STom Jones# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203b01bf88STom Jones# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213b01bf88STom Jones# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223b01bf88STom Jones# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233b01bf88STom Jones# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243b01bf88STom Jones# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253b01bf88STom Jones# SUCH DAMAGE.
263b01bf88STom Jones#
273b01bf88STom Jones#
283b01bf88STom Jones
293b01bf88STom Jones. $(atf_get_srcdir)/utils.subr
303b01bf88STom Jones
313b01bf88STom Jonesatf_test_case "basic_v4" "cleanup"
323b01bf88STom Jonesbasic_v4_head()
333b01bf88STom Jones{
343b01bf88STom Jones	atf_set descr 'add/change/delete route test for v4'
353b01bf88STom Jones	atf_set require.user root
36784d8d8dSOlivier Cochard	atf_set require.progs jail jq
373b01bf88STom Jones}
383b01bf88STom Jones
393b01bf88STom Jonesbasic_v4_body()
403b01bf88STom Jones{
413b01bf88STom Jones	epair=$(vnet_mkepair)
423b01bf88STom Jones	ifconfig ${epair}a 192.0.2.2/24 up
433b01bf88STom Jones	vnet_mkjail alcatraz ${epair}b
443b01bf88STom Jones	jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up
453b01bf88STom Jones
463b01bf88STom Jones	# add a new route in the jail
473b01bf88STom Jones	jexec alcatraz route add 192.0.2.3 192.0.2.2
483b01bf88STom Jones	gateway=$(check_route "alcatraz" "192.0.2.3")
493b01bf88STom Jones
503b01bf88STom Jones	if [ "${gateway}" != "192.0.2.2" ]; then
513b01bf88STom Jones		atf_fail "Failed to add new route."
523b01bf88STom Jones	fi
533b01bf88STom Jones
543b01bf88STom Jones	# change the added route
553b01bf88STom Jones	jexec alcatraz route change 192.0.2.3 192.0.2.4
563b01bf88STom Jones	gateway=$(check_route "alcatraz" "192.0.2.3")
573b01bf88STom Jones
583b01bf88STom Jones	if [ "${gateway}" != "192.0.2.4" ]; then
593b01bf88STom Jones		atf_fail "Failed to change route."
603b01bf88STom Jones	fi
613b01bf88STom Jones
623b01bf88STom Jones	# delete the route
633b01bf88STom Jones	jexec alcatraz route delete 192.0.2.3
643b01bf88STom Jones	gateway=$(check_route "alcatraz" "192.0.2.3")
653b01bf88STom Jones
663b01bf88STom Jones	if [ "${gateway}" != "" ]; then
673b01bf88STom Jones		atf_fail "Failed to delete route."
683b01bf88STom Jones	fi
693b01bf88STom Jones}
703b01bf88STom Jones
713b01bf88STom Jonesbasic_v4_cleanup()
723b01bf88STom Jones{
733b01bf88STom Jones	vnet_cleanup
743b01bf88STom Jones}
753b01bf88STom Jones
763b01bf88STom Jonesatf_test_case "basic_v6" "cleanup"
773b01bf88STom Jonesbasic_v6_head()
783b01bf88STom Jones{
793b01bf88STom Jones	atf_set descr 'add/change/delete route test for v6'
803b01bf88STom Jones	atf_set require.user root
81784d8d8dSOlivier Cochard	atf_set require.progs jail jq
823b01bf88STom Jones}
833b01bf88STom Jones
843b01bf88STom Jonesbasic_v6_body()
853b01bf88STom Jones{
863b01bf88STom Jones	epair=$(vnet_mkepair)
873b01bf88STom Jones	ifconfig ${epair}a inet6 2001:db8:cc4b::1/64 up no_dad
883b01bf88STom Jones	vnet_mkjail alcatraz ${epair}b
893b01bf88STom Jones	jexec alcatraz ifconfig ${epair}b inet6 2001:db8:cc4b::2/64 up no_dad
903b01bf88STom Jones
913b01bf88STom Jones	# add a new route in the jail
923b01bf88STom Jones	jexec alcatraz route add -6 2001:db8:cc4b::3 2001:db8:cc4b::1
933b01bf88STom Jones	gateway=$(check_route "alcatraz" "2001:db8:cc4b::3")
943b01bf88STom Jones
953b01bf88STom Jones	if [ "${gateway}" != "2001:db8:cc4b::1" ]; then
963b01bf88STom Jones		atf_fail "Failed to add new route."
973b01bf88STom Jones	fi
983b01bf88STom Jones
993b01bf88STom Jones	# change the added route
1003b01bf88STom Jones	jexec alcatraz route change -6 2001:db8:cc4b::3 2001:db8:cc4b::4
1013b01bf88STom Jones	gateway=$(check_route "alcatraz" "2001:db8:cc4b::3")
1023b01bf88STom Jones	if [ "${gateway}" != "2001:db8:cc4b::4" ]; then
1033b01bf88STom Jones		atf_fail "Failed to change route."
1043b01bf88STom Jones	fi
1053b01bf88STom Jones
1063b01bf88STom Jones	# delete the route
1073b01bf88STom Jones	jexec alcatraz route -6 delete 2001:db8:cc4b::3
1083b01bf88STom Jones	gateway=$(check_route "alcatraz" "2001:db8:cc4b::3")
1093b01bf88STom Jones
1103b01bf88STom Jones	if [ "${gateway}" != "" ]; then
1113b01bf88STom Jones		atf_fail "Failed to delete route."
1123b01bf88STom Jones	fi
1133b01bf88STom Jones}
1143b01bf88STom Jones
1153b01bf88STom Jonesbasic_v6_cleanup()
1163b01bf88STom Jones{
1173b01bf88STom Jones	vnet_cleanup
1183b01bf88STom Jones}
1193b01bf88STom Jones
120*ca7edd0dSR. Christian McDonaldatf_test_case "interface_route_v4" "cleanup"
121*ca7edd0dSR. Christian McDonaldinterface_route_v4_head()
122*ca7edd0dSR. Christian McDonald{
123*ca7edd0dSR. Christian McDonald	atf_set descr 'add interface route for v4'
124*ca7edd0dSR. Christian McDonald	atf_set require.user root
125*ca7edd0dSR. Christian McDonald	atf_set require.progs jail jq
126*ca7edd0dSR. Christian McDonald}
127*ca7edd0dSR. Christian McDonald
128*ca7edd0dSR. Christian McDonaldinterface_route_v4_body()
129*ca7edd0dSR. Christian McDonald{
130*ca7edd0dSR. Christian McDonald	epair=$(vnet_mkepair)
131*ca7edd0dSR. Christian McDonald	ifconfig ${epair}a up
132*ca7edd0dSR. Christian McDonald	vnet_mkjail alcatraz ${epair}b
133*ca7edd0dSR. Christian McDonald
134*ca7edd0dSR. Christian McDonald	# add interface route
135*ca7edd0dSR. Christian McDonald	jexec alcatraz route add "192.0.2.1" -iface ${epair}b
136*ca7edd0dSR. Christian McDonald	gateway=$(check_route "alcatraz" "192.0.2.1")
137*ca7edd0dSR. Christian McDonald
138*ca7edd0dSR. Christian McDonald	if [ -z "${gateway}" ]; then
139*ca7edd0dSR. Christian McDonald		atf_fail "Failed to add interface route."
140*ca7edd0dSR. Christian McDonald	fi
141*ca7edd0dSR. Christian McDonald}
142*ca7edd0dSR. Christian McDonald
143*ca7edd0dSR. Christian McDonaldinterface_route_v4_cleanup()
144*ca7edd0dSR. Christian McDonald{
145*ca7edd0dSR. Christian McDonald	vnet_cleanup
146*ca7edd0dSR. Christian McDonald}
147*ca7edd0dSR. Christian McDonald
148*ca7edd0dSR. Christian McDonaldatf_test_case "interface_route_v6" "cleanup"
149*ca7edd0dSR. Christian McDonaldinterface_route_v6_head()
150*ca7edd0dSR. Christian McDonald{
151*ca7edd0dSR. Christian McDonald	atf_set descr 'add interface route for v6'
152*ca7edd0dSR. Christian McDonald	atf_set require.user root
153*ca7edd0dSR. Christian McDonald	atf_set require.progs jail jq
154*ca7edd0dSR. Christian McDonald}
155*ca7edd0dSR. Christian McDonald
156*ca7edd0dSR. Christian McDonaldinterface_route_v6_body()
157*ca7edd0dSR. Christian McDonald{
158*ca7edd0dSR. Christian McDonald	epair=$(vnet_mkepair)
159*ca7edd0dSR. Christian McDonald	ifconfig ${epair}a up
160*ca7edd0dSR. Christian McDonald	vnet_mkjail alcatraz ${epair}b
161*ca7edd0dSR. Christian McDonald
162*ca7edd0dSR. Christian McDonald	# add interface route
163*ca7edd0dSR. Christian McDonald	jexec alcatraz route add -6 "2001:db8:cc4b::1" -iface ${epair}b
164*ca7edd0dSR. Christian McDonald	gateway=$(check_route "alcatraz" "2001:db8:cc4b::1")
165*ca7edd0dSR. Christian McDonald
166*ca7edd0dSR. Christian McDonald	if [ -z "${gateway}" ]; then
167*ca7edd0dSR. Christian McDonald		atf_fail "Failed to add interface route."
168*ca7edd0dSR. Christian McDonald	fi
169*ca7edd0dSR. Christian McDonald}
170*ca7edd0dSR. Christian McDonald
171*ca7edd0dSR. Christian McDonaldinterface_route_v6_cleanup()
172*ca7edd0dSR. Christian McDonald{
173*ca7edd0dSR. Christian McDonald	vnet_cleanup
174*ca7edd0dSR. Christian McDonald}
175*ca7edd0dSR. Christian McDonald
1763b01bf88STom Jonesatf_init_test_cases()
1773b01bf88STom Jones{
1783b01bf88STom Jones	atf_add_test_case "basic_v4"
1793b01bf88STom Jones	atf_add_test_case "basic_v6"
180*ca7edd0dSR. Christian McDonald	atf_add_test_case "interface_route_v4"
181*ca7edd0dSR. Christian McDonald	atf_add_test_case "interface_route_v6"
1823b01bf88STom Jones}
183