1#! /usr/bin/atf-sh
2#	$NetBSD: t_bridge.sh,v 1.1 2014/09/18 15:13:27 ozaki-r Exp $
3#
4# Copyright (c) 2014 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_bridge -lrumpnet_shmif"
30inet6server="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_netinet6 -lrumpnet_bridge -lrumpnet_shmif"
31
32SOCK1=unix://commsock1
33SOCK2=unix://commsock2
34SOCK3=unix://commsock3
35IP1=10.0.0.1
36IP2=10.0.0.2
37IP61=fc00::1
38IP62=fc00::2
39
40atf_test_case basic cleanup
41atf_test_case basic6 cleanup
42
43basic_head()
44{
45	atf_set "descr" "Does simple if_bridge tests"
46	atf_set "require.progs" "rump_server"
47}
48
49basic6_head()
50{
51	atf_set "descr" "Does simple if_bridge tests (IPv6)"
52	atf_set "require.progs" "rump_server"
53}
54
55setup_endpoint()
56{
57	sock=${1}
58	addr=${2}
59	bus=${3}
60	mode=${4}
61
62	export RUMP_SERVER=${sock}
63	atf_check -s exit:0 rump.ifconfig shmif0 create
64	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${bus}
65	if [ $mode = "ipv6" ]; then
66		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr}
67	else
68		atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00
69	fi
70
71	atf_check -s exit:0 rump.ifconfig shmif0 up
72	rump.ifconfig shmif0
73}
74
75test_endpoint()
76{
77	sock=${1}
78	addr=${2}
79	bus=${3}
80	mode=${4}
81
82	export RUMP_SERVER=${sock}
83	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
84	if [ $mode = "ipv6" ]; then
85		export LD_PRELOAD=/usr/lib/librumphijack.so
86		atf_check -s exit:0 -o ignore ping6 -n -c 1 ${addr}
87		unset LD_PRELOAD
88	else
89		atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 ${addr}
90	fi
91}
92
93show_endpoint()
94{
95	sock=${1}
96
97	export RUMP_SERVER=${sock}
98	rump.ifconfig -v shmif0
99}
100
101test_setup()
102{
103	test_endpoint $SOCK1 $IP1 bus1 ipv4
104	test_endpoint $SOCK3 $IP2 bus2 ipv4
105
106	export RUMP_SERVER=$SOCK2
107	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
108	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
109}
110
111test_setup6()
112{
113	test_endpoint $SOCK1 $IP61 bus1 ipv6
114	test_endpoint $SOCK3 $IP62 bus2 ipv6
115
116	export RUMP_SERVER=$SOCK2
117	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
118	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
119}
120
121setup_bridge_server()
122{
123	export RUMP_SERVER=$SOCK2
124	atf_check -s exit:0 rump.ifconfig shmif0 create
125	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
126	atf_check -s exit:0 rump.ifconfig shmif0 up
127
128	atf_check -s exit:0 rump.ifconfig shmif1 create
129	atf_check -s exit:0 rump.ifconfig shmif1 linkstr bus2
130	atf_check -s exit:0 rump.ifconfig shmif1 up
131}
132
133setup()
134{
135	atf_check -s exit:0 ${inetserver} $SOCK1
136	atf_check -s exit:0 ${inetserver} $SOCK2
137	atf_check -s exit:0 ${inetserver} $SOCK3
138
139	setup_endpoint $SOCK1 $IP1 bus1 ipv4
140	setup_endpoint $SOCK3 $IP2 bus2 ipv4
141	setup_bridge_server
142}
143
144setup6()
145{
146	atf_check -s exit:0 ${inet6server} $SOCK1
147	atf_check -s exit:0 ${inet6server} $SOCK2
148	atf_check -s exit:0 ${inet6server} $SOCK3
149
150	setup_endpoint $SOCK1 $IP61 bus1 ipv6
151	setup_endpoint $SOCK3 $IP62 bus2 ipv6
152	setup_bridge_server
153}
154
155setup_bridge()
156{
157	export RUMP_SERVER=$SOCK2
158	atf_check -s exit:0 rump.ifconfig bridge0 create
159	atf_check -s exit:0 rump.ifconfig bridge0 up
160
161	export LD_PRELOAD=/usr/lib/librumphijack.so
162	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif0
163	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif1
164	/sbin/brconfig bridge0
165	unset LD_PRELOAD
166	rump.ifconfig shmif0
167	rump.ifconfig shmif1
168}
169
170teardown_bridge()
171{
172	export RUMP_SERVER=$SOCK2
173	export LD_PRELOAD=/usr/lib/librumphijack.so
174	/sbin/brconfig bridge0
175	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif0
176	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif1
177	/sbin/brconfig bridge0
178	unset LD_PRELOAD
179	rump.ifconfig shmif0
180	rump.ifconfig shmif1
181}
182
183test_setup_bridge()
184{
185	export RUMP_SERVER=$SOCK2
186	export LD_PRELOAD=/usr/lib/librumphijack.so
187	atf_check -s exit:0 -o match:shmif0 /sbin/brconfig bridge0
188	atf_check -s exit:0 -o match:shmif1 /sbin/brconfig bridge0
189	/sbin/brconfig bridge0
190	unset LD_PRELOAD
191}
192
193cleanup()
194{
195	env RUMP_SERVER=$SOCK1 rump.halt
196	env RUMP_SERVER=$SOCK2 rump.halt
197	env RUMP_SERVER=$SOCK3 rump.halt
198}
199
200dump_bus()
201{
202	/usr/bin/shmif_dumpbus -p - bus1 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
203	/usr/bin/shmif_dumpbus -p - bus2 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
204}
205
206down_up_interfaces()
207{
208	export RUMP_SERVER=$SOCK1
209	rump.ifconfig shmif0 down
210	rump.ifconfig shmif0 up
211	export RUMP_SERVER=$SOCK3
212	rump.ifconfig shmif0 down
213	rump.ifconfig shmif0 up
214}
215
216test_ping_failure()
217{
218	export RUMP_SERVER=$SOCK1
219	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP2
220	export RUMP_SERVER=$SOCK3
221	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP1
222}
223
224test_ping_success()
225{
226	export RUMP_SERVER=$SOCK1
227	rump.ifconfig -v shmif0
228	atf_check -s exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP2
229	rump.ifconfig -v shmif0
230
231	export RUMP_SERVER=$SOCK3
232	rump.ifconfig -v shmif0
233	atf_check -s exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP1
234	rump.ifconfig -v shmif0
235}
236
237test_ping6_failure()
238{
239	export LD_PRELOAD=/usr/lib/librumphijack.so
240	export RUMP_SERVER=$SOCK1
241	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 $IP62
242	export RUMP_SERVER=$SOCK3
243	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 $IP61
244	unset LD_PRELOAD
245}
246
247test_ping6_success()
248{
249	export RUMP_SERVER=$SOCK1
250	rump.ifconfig -v shmif0
251	export LD_PRELOAD=/usr/lib/librumphijack.so
252	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 $IP62
253	unset LD_PRELOAD
254	rump.ifconfig -v shmif0
255
256	export RUMP_SERVER=$SOCK3
257	rump.ifconfig -v shmif0
258	export LD_PRELOAD=/usr/lib/librumphijack.so
259	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 $IP61
260	unset LD_PRELOAD
261	rump.ifconfig -v shmif0
262}
263
264basic_body()
265{
266	setup
267	test_setup
268
269	setup_bridge
270	test_setup_bridge
271
272	test_ping_success
273
274	teardown_bridge
275	test_ping_failure
276}
277
278basic6_body()
279{
280	setup6
281	test_setup6
282
283	# TODO: enable once ping6 implements timeout feature
284	#test_ping6_failure
285
286	setup_bridge
287	test_setup_bridge
288
289	test_ping6_success
290
291	teardown_bridge
292	# TODO: enable once ping6 implements timeout feature
293	#test_ping6_failure
294}
295
296basic_cleanup()
297{
298	dump_bus
299	cleanup
300}
301
302basic6_cleanup()
303{
304	dump_bus
305	cleanup
306}
307
308atf_init_test_cases()
309{
310	atf_add_test_case basic
311	atf_add_test_case basic6
312}
313