1272343Sngie#! /usr/bin/atf-sh
2272343Sngie#	$NetBSD: t_bridge.sh,v 1.1 2014/09/18 15:13:27 ozaki-r Exp $
3272343Sngie#
4272343Sngie# Copyright (c) 2014 The NetBSD Foundation, Inc.
5272343Sngie# All rights reserved.
6272343Sngie#
7272343Sngie# Redistribution and use in source and binary forms, with or without
8272343Sngie# modification, are permitted provided that the following conditions
9272343Sngie# are met:
10272343Sngie# 1. Redistributions of source code must retain the above copyright
11272343Sngie#    notice, this list of conditions and the following disclaimer.
12272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie#    notice, this list of conditions and the following disclaimer in the
14272343Sngie#    documentation and/or other materials provided with the distribution.
15272343Sngie#
16272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie# POSSIBILITY OF SUCH DAMAGE.
27272343Sngie#
28272343Sngie
29272343Sngieinetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_bridge -lrumpnet_shmif"
30272343Sngieinet6server="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_netinet6 -lrumpnet_bridge -lrumpnet_shmif"
31272343Sngie
32272343SngieSOCK1=unix://commsock1
33272343SngieSOCK2=unix://commsock2
34272343SngieSOCK3=unix://commsock3
35272343SngieIP1=10.0.0.1
36272343SngieIP2=10.0.0.2
37272343SngieIP61=fc00::1
38272343SngieIP62=fc00::2
39272343Sngie
40272343Sngieatf_test_case basic cleanup
41272343Sngieatf_test_case basic6 cleanup
42272343Sngie
43272343Sngiebasic_head()
44272343Sngie{
45272343Sngie	atf_set "descr" "Does simple if_bridge tests"
46272343Sngie	atf_set "require.progs" "rump_server"
47272343Sngie}
48272343Sngie
49272343Sngiebasic6_head()
50272343Sngie{
51272343Sngie	atf_set "descr" "Does simple if_bridge tests (IPv6)"
52272343Sngie	atf_set "require.progs" "rump_server"
53272343Sngie}
54272343Sngie
55272343Sngiesetup_endpoint()
56272343Sngie{
57272343Sngie	sock=${1}
58272343Sngie	addr=${2}
59272343Sngie	bus=${3}
60272343Sngie	mode=${4}
61272343Sngie
62272343Sngie	export RUMP_SERVER=${sock}
63272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 create
64272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 linkstr ${bus}
65272343Sngie	if [ $mode = "ipv6" ]; then
66272343Sngie		atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr}
67272343Sngie	else
68272343Sngie		atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00
69272343Sngie	fi
70272343Sngie
71272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 up
72272343Sngie	rump.ifconfig shmif0
73272343Sngie}
74272343Sngie
75272343Sngietest_endpoint()
76272343Sngie{
77272343Sngie	sock=${1}
78272343Sngie	addr=${2}
79272343Sngie	bus=${3}
80272343Sngie	mode=${4}
81272343Sngie
82272343Sngie	export RUMP_SERVER=${sock}
83272343Sngie	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
84272343Sngie	if [ $mode = "ipv6" ]; then
85272343Sngie		export LD_PRELOAD=/usr/lib/librumphijack.so
86272343Sngie		atf_check -s exit:0 -o ignore ping6 -n -c 1 ${addr}
87272343Sngie		unset LD_PRELOAD
88272343Sngie	else
89272343Sngie		atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 ${addr}
90272343Sngie	fi
91272343Sngie}
92272343Sngie
93272343Sngieshow_endpoint()
94272343Sngie{
95272343Sngie	sock=${1}
96272343Sngie
97272343Sngie	export RUMP_SERVER=${sock}
98272343Sngie	rump.ifconfig -v shmif0
99272343Sngie}
100272343Sngie
101272343Sngietest_setup()
102272343Sngie{
103272343Sngie	test_endpoint $SOCK1 $IP1 bus1 ipv4
104272343Sngie	test_endpoint $SOCK3 $IP2 bus2 ipv4
105272343Sngie
106272343Sngie	export RUMP_SERVER=$SOCK2
107272343Sngie	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
108272343Sngie	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
109272343Sngie}
110272343Sngie
111272343Sngietest_setup6()
112272343Sngie{
113272343Sngie	test_endpoint $SOCK1 $IP61 bus1 ipv6
114272343Sngie	test_endpoint $SOCK3 $IP62 bus2 ipv6
115272343Sngie
116272343Sngie	export RUMP_SERVER=$SOCK2
117272343Sngie	atf_check -s exit:0 -o match:shmif0 rump.ifconfig
118272343Sngie	atf_check -s exit:0 -o match:shmif1 rump.ifconfig
119272343Sngie}
120272343Sngie
121272343Sngiesetup_bridge_server()
122272343Sngie{
123272343Sngie	export RUMP_SERVER=$SOCK2
124272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 create
125272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
126272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 up
127272343Sngie
128272343Sngie	atf_check -s exit:0 rump.ifconfig shmif1 create
129272343Sngie	atf_check -s exit:0 rump.ifconfig shmif1 linkstr bus2
130272343Sngie	atf_check -s exit:0 rump.ifconfig shmif1 up
131272343Sngie}
132272343Sngie
133272343Sngiesetup()
134272343Sngie{
135272343Sngie	atf_check -s exit:0 ${inetserver} $SOCK1
136272343Sngie	atf_check -s exit:0 ${inetserver} $SOCK2
137272343Sngie	atf_check -s exit:0 ${inetserver} $SOCK3
138272343Sngie
139272343Sngie	setup_endpoint $SOCK1 $IP1 bus1 ipv4
140272343Sngie	setup_endpoint $SOCK3 $IP2 bus2 ipv4
141272343Sngie	setup_bridge_server
142272343Sngie}
143272343Sngie
144272343Sngiesetup6()
145272343Sngie{
146272343Sngie	atf_check -s exit:0 ${inet6server} $SOCK1
147272343Sngie	atf_check -s exit:0 ${inet6server} $SOCK2
148272343Sngie	atf_check -s exit:0 ${inet6server} $SOCK3
149272343Sngie
150272343Sngie	setup_endpoint $SOCK1 $IP61 bus1 ipv6
151272343Sngie	setup_endpoint $SOCK3 $IP62 bus2 ipv6
152272343Sngie	setup_bridge_server
153272343Sngie}
154272343Sngie
155272343Sngiesetup_bridge()
156272343Sngie{
157272343Sngie	export RUMP_SERVER=$SOCK2
158272343Sngie	atf_check -s exit:0 rump.ifconfig bridge0 create
159272343Sngie	atf_check -s exit:0 rump.ifconfig bridge0 up
160272343Sngie
161272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
162272343Sngie	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif0
163272343Sngie	atf_check -s exit:0 /sbin/brconfig bridge0 add shmif1
164272343Sngie	/sbin/brconfig bridge0
165272343Sngie	unset LD_PRELOAD
166272343Sngie	rump.ifconfig shmif0
167272343Sngie	rump.ifconfig shmif1
168272343Sngie}
169272343Sngie
170272343Sngieteardown_bridge()
171272343Sngie{
172272343Sngie	export RUMP_SERVER=$SOCK2
173272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
174272343Sngie	/sbin/brconfig bridge0
175272343Sngie	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif0
176272343Sngie	atf_check -s exit:0 /sbin/brconfig bridge0 delete shmif1
177272343Sngie	/sbin/brconfig bridge0
178272343Sngie	unset LD_PRELOAD
179272343Sngie	rump.ifconfig shmif0
180272343Sngie	rump.ifconfig shmif1
181272343Sngie}
182272343Sngie
183272343Sngietest_setup_bridge()
184272343Sngie{
185272343Sngie	export RUMP_SERVER=$SOCK2
186272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
187272343Sngie	atf_check -s exit:0 -o match:shmif0 /sbin/brconfig bridge0
188272343Sngie	atf_check -s exit:0 -o match:shmif1 /sbin/brconfig bridge0
189272343Sngie	/sbin/brconfig bridge0
190272343Sngie	unset LD_PRELOAD
191272343Sngie}
192272343Sngie
193272343Sngiecleanup()
194272343Sngie{
195272343Sngie	env RUMP_SERVER=$SOCK1 rump.halt
196272343Sngie	env RUMP_SERVER=$SOCK2 rump.halt
197272343Sngie	env RUMP_SERVER=$SOCK3 rump.halt
198272343Sngie}
199272343Sngie
200272343Sngiedump_bus()
201272343Sngie{
202272343Sngie	/usr/bin/shmif_dumpbus -p - bus1 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
203272343Sngie	/usr/bin/shmif_dumpbus -p - bus2 2>/dev/null| /usr/sbin/tcpdump -n -e -r -
204272343Sngie}
205272343Sngie
206272343Sngiedown_up_interfaces()
207272343Sngie{
208272343Sngie	export RUMP_SERVER=$SOCK1
209272343Sngie	rump.ifconfig shmif0 down
210272343Sngie	rump.ifconfig shmif0 up
211272343Sngie	export RUMP_SERVER=$SOCK3
212272343Sngie	rump.ifconfig shmif0 down
213272343Sngie	rump.ifconfig shmif0 up
214272343Sngie}
215272343Sngie
216272343Sngietest_ping_failure()
217272343Sngie{
218272343Sngie	export RUMP_SERVER=$SOCK1
219272343Sngie	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP2
220272343Sngie	export RUMP_SERVER=$SOCK3
221272343Sngie	atf_check -s not-exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP1
222272343Sngie}
223272343Sngie
224272343Sngietest_ping_success()
225272343Sngie{
226272343Sngie	export RUMP_SERVER=$SOCK1
227272343Sngie	rump.ifconfig -v shmif0
228272343Sngie	atf_check -s exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP2
229272343Sngie	rump.ifconfig -v shmif0
230272343Sngie
231272343Sngie	export RUMP_SERVER=$SOCK3
232272343Sngie	rump.ifconfig -v shmif0
233272343Sngie	atf_check -s exit:0 -o ignore rump.ping -q -n -w 1 -c 1 $IP1
234272343Sngie	rump.ifconfig -v shmif0
235272343Sngie}
236272343Sngie
237272343Sngietest_ping6_failure()
238272343Sngie{
239272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
240272343Sngie	export RUMP_SERVER=$SOCK1
241272343Sngie	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 $IP62
242272343Sngie	export RUMP_SERVER=$SOCK3
243272343Sngie	atf_check -s not-exit:0 -o ignore ping6 -q -n -c 1 $IP61
244272343Sngie	unset LD_PRELOAD
245272343Sngie}
246272343Sngie
247272343Sngietest_ping6_success()
248272343Sngie{
249272343Sngie	export RUMP_SERVER=$SOCK1
250272343Sngie	rump.ifconfig -v shmif0
251272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
252272343Sngie	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 $IP62
253272343Sngie	unset LD_PRELOAD
254272343Sngie	rump.ifconfig -v shmif0
255272343Sngie
256272343Sngie	export RUMP_SERVER=$SOCK3
257272343Sngie	rump.ifconfig -v shmif0
258272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
259272343Sngie	atf_check -s exit:0 -o ignore ping6 -q -n -c 1 $IP61
260272343Sngie	unset LD_PRELOAD
261272343Sngie	rump.ifconfig -v shmif0
262272343Sngie}
263272343Sngie
264272343Sngiebasic_body()
265272343Sngie{
266272343Sngie	setup
267272343Sngie	test_setup
268272343Sngie
269272343Sngie	setup_bridge
270272343Sngie	test_setup_bridge
271272343Sngie
272272343Sngie	test_ping_success
273272343Sngie
274272343Sngie	teardown_bridge
275272343Sngie	test_ping_failure
276272343Sngie}
277272343Sngie
278272343Sngiebasic6_body()
279272343Sngie{
280272343Sngie	setup6
281272343Sngie	test_setup6
282272343Sngie
283272343Sngie	# TODO: enable once ping6 implements timeout feature
284272343Sngie	#test_ping6_failure
285272343Sngie
286272343Sngie	setup_bridge
287272343Sngie	test_setup_bridge
288272343Sngie
289272343Sngie	test_ping6_success
290272343Sngie
291272343Sngie	teardown_bridge
292272343Sngie	# TODO: enable once ping6 implements timeout feature
293272343Sngie	#test_ping6_failure
294272343Sngie}
295272343Sngie
296272343Sngiebasic_cleanup()
297272343Sngie{
298272343Sngie	dump_bus
299272343Sngie	cleanup
300272343Sngie}
301272343Sngie
302272343Sngiebasic6_cleanup()
303272343Sngie{
304272343Sngie	dump_bus
305272343Sngie	cleanup
306272343Sngie}
307272343Sngie
308272343Sngieatf_init_test_cases()
309272343Sngie{
310272343Sngie	atf_add_test_case basic
311272343Sngie	atf_add_test_case basic6
312272343Sngie}
313