xref: /freebsd/sbin/route/tests/basic.sh (revision 8db56defa766eacdbaf89a37f25b11a57fd9787a)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2020 Ahsan Barkati
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28
29. $(atf_get_srcdir)/utils.subr
30
31atf_test_case "basic_v4" "cleanup"
32basic_v4_head()
33{
34	atf_set descr 'add/change/delete route test for v4'
35	atf_set require.user root
36	atf_set require.progs jail jq
37}
38
39basic_v4_body()
40{
41	epair=$(vnet_mkepair)
42	ifconfig ${epair}a 192.0.2.2/24 up
43	vnet_mkjail alcatraz ${epair}b
44	jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up
45
46	# add a new route in the jail
47	jexec alcatraz route add 192.0.2.3 192.0.2.2
48	gateway=$(check_route "alcatraz" "192.0.2.3")
49
50	if [ "${gateway}" != "192.0.2.2" ]; then
51		atf_fail "Failed to add new route."
52	fi
53
54	# change the added route
55	jexec alcatraz route change 192.0.2.3 192.0.2.4
56	gateway=$(check_route "alcatraz" "192.0.2.3")
57
58	if [ "${gateway}" != "192.0.2.4" ]; then
59		atf_fail "Failed to change route."
60	fi
61
62	# delete the route
63	jexec alcatraz route delete 192.0.2.3
64	gateway=$(check_route "alcatraz" "192.0.2.3")
65
66	if [ "${gateway}" != "" ]; then
67		atf_fail "Failed to delete route."
68	fi
69}
70
71basic_v4_cleanup()
72{
73	vnet_cleanup
74}
75
76atf_test_case "basic_v6" "cleanup"
77basic_v6_head()
78{
79	atf_set descr 'add/change/delete route test for v6'
80	atf_set require.user root
81	atf_set require.progs jail jq
82}
83
84basic_v6_body()
85{
86	epair=$(vnet_mkepair)
87	ifconfig ${epair}a inet6 2001:db8:cc4b::1/64 up no_dad
88	vnet_mkjail alcatraz ${epair}b
89	jexec alcatraz ifconfig ${epair}b inet6 2001:db8:cc4b::2/64 up no_dad
90
91	# add a new route in the jail
92	jexec alcatraz route add -6 2001:db8:cc4b::3 2001:db8:cc4b::1
93	gateway=$(check_route "alcatraz" "2001:db8:cc4b::3")
94
95	if [ "${gateway}" != "2001:db8:cc4b::1" ]; then
96		atf_fail "Failed to add new route."
97	fi
98
99	# change the added route
100	jexec alcatraz route change -6 2001:db8:cc4b::3 2001:db8:cc4b::4
101	gateway=$(check_route "alcatraz" "2001:db8:cc4b::3")
102	if [ "${gateway}" != "2001:db8:cc4b::4" ]; then
103		atf_fail "Failed to change route."
104	fi
105
106	# delete the route
107	jexec alcatraz route -6 delete 2001:db8:cc4b::3
108	gateway=$(check_route "alcatraz" "2001:db8:cc4b::3")
109
110	if [ "${gateway}" != "" ]; then
111		atf_fail "Failed to delete route."
112	fi
113}
114
115basic_v6_cleanup()
116{
117	vnet_cleanup
118}
119
120atf_test_case "interface_route_v4" "cleanup"
121interface_route_v4_head()
122{
123	atf_set descr 'add interface route for v4'
124	atf_set require.user root
125	atf_set require.progs jail jq
126}
127
128interface_route_v4_body()
129{
130	epair=$(vnet_mkepair)
131	ifconfig ${epair}a up
132	vnet_mkjail alcatraz ${epair}b
133
134	# add interface route
135	jexec alcatraz route add "192.0.2.1" -iface ${epair}b
136	gateway=$(check_route "alcatraz" "192.0.2.1")
137
138	if [ -z "${gateway}" ]; then
139		atf_fail "Failed to add interface route."
140	fi
141}
142
143interface_route_v4_cleanup()
144{
145	vnet_cleanup
146}
147
148atf_test_case "interface_route_v6" "cleanup"
149interface_route_v6_head()
150{
151	atf_set descr 'add interface route for v6'
152	atf_set require.user root
153	atf_set require.progs jail jq
154}
155
156interface_route_v6_body()
157{
158	epair=$(vnet_mkepair)
159	ifconfig ${epair}a up
160	vnet_mkjail alcatraz ${epair}b
161
162	# add interface route
163	jexec alcatraz route add -6 "2001:db8:cc4b::1" -iface ${epair}b
164	gateway=$(check_route "alcatraz" "2001:db8:cc4b::1")
165
166	if [ -z "${gateway}" ]; then
167		atf_fail "Failed to add interface route."
168	fi
169}
170
171interface_route_v6_cleanup()
172{
173	vnet_cleanup
174}
175
176atf_init_test_cases()
177{
178	atf_add_test_case "basic_v4"
179	atf_add_test_case "basic_v6"
180	atf_add_test_case "interface_route_v4"
181	atf_add_test_case "interface_route_v6"
182}
183