t_dad.sh revision 305358
1#	$NetBSD: t_dad.sh,v 1.7 2016/08/10 22:05:07 kre Exp $
2#
3# Copyright (c) 2015 The NetBSD Foundation, Inc.
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
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
29inetserver="${inetserver} -lrumpdev"
30HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK=sysctl=yes"
31
32SOCKLOCAL=unix://commsock1
33SOCKPEER=unix://commsock2
34
35DEBUG=false
36
37atf_test_case dad_basic cleanup
38atf_test_case dad_duplicated cleanup
39
40dad_basic_head()
41{
42	atf_set "descr" "Tests for IPv4 DAD basic behavior"
43	atf_set "require.progs" "rump_server"
44}
45
46dad_duplicated_head()
47{
48	atf_set "descr" "Tests for IPv4 DAD duplicated state"
49	atf_set "require.progs" "rump_server"
50}
51
52setup_server()
53{
54	local sock=$1
55	local ip=$2
56
57	export RUMP_SERVER=$sock
58
59	atf_check -s exit:0 rump.ifconfig shmif0 create
60	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
61	atf_check -s exit:0 rump.ifconfig shmif0 inet $ip/24
62	atf_check -s exit:0 rump.ifconfig shmif0 up
63	atf_check -s exit:0 rump.ifconfig -w 10
64
65	$DEBUG && rump.ifconfig shmif0
66}
67
68make_pkt_str()
69{
70	local target=$1
71	local sender=$2
72	pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
73	pkt="$pkt Request who-has $target tell $sender, length 28"
74	echo $pkt
75}
76
77extract_new_packets()
78{
79	local old=./old
80
81	if [ ! -f $old ]; then
82		old=/dev/null
83	fi
84
85	shmif_dumpbus -p - bus1 2>/dev/null| \
86	    tcpdump -n -e -r - 2>/dev/null > ./new
87	diff -u $old ./new |grep '^+' |cut -d '+' -f 2 > ./diff
88	mv -f ./new ./old
89	cat ./diff
90}
91
92dad_basic_body()
93{
94	local pkt=
95
96	atf_check -s exit:0 ${inetserver} $SOCKLOCAL
97	export RUMP_SERVER=$SOCKLOCAL
98
99	atf_check -s exit:0 rump.ifconfig shmif0 create
100	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
101	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
102	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
103	$DEBUG && rump.ifconfig shmif0
104
105	atf_check -s exit:0 rump.ifconfig shmif0 up
106	rump.ifconfig shmif0 > ./out
107	$DEBUG && cat ./out
108
109	# The primary address doesn't start with tentative state
110	atf_check -s not-exit:0 -x "cat ./out |grep 10.0.0.1 |grep -q tentative"
111	# The alias address starts with tentative state
112	# XXX we have no stable way to check this, so skip for now
113	#atf_check -s exit:0 -x "cat ./out |grep 10.0.0.2 |grep -q tentative"
114
115	atf_check -s exit:0 sleep 2
116	extract_new_packets > ./out
117	$DEBUG && cat ./out
118
119	# Check DAD probe packets
120	pkt=$(make_pkt_str 10.0.0.2 0.0.0.0)
121	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
122	# No DAD for the primary address
123	pkt=$(make_pkt_str 10.0.0.1 0.0.0.0)
124	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
125
126	# Waiting for DAD complete
127	atf_check -s exit:0 rump.ifconfig -w 10
128	# Give a chance to send a DAD announce packet
129	atf_check -s exit:0 sleep 1
130	extract_new_packets > ./out
131	$DEBUG && cat ./out
132
133	# Check the DAD announce packet
134	pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
135	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
136	# The alias address left tentative
137	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.2 |grep -q tentative"
138
139	#
140	# Add a new address on the fly
141	#
142	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24 alias
143
144	# The new address starts with tentative state
145	# XXX we have no stable way to check this, so skip for now
146	#atf_check -s exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.3 |grep -q tentative"
147
148	# Check DAD probe packets
149	atf_check -s exit:0 sleep 2
150	extract_new_packets > ./out
151	$DEBUG && cat ./out
152	pkt=$(make_pkt_str 10.0.0.3 0.0.0.0)
153	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
154
155	# Waiting for DAD complete
156	atf_check -s exit:0 rump.ifconfig -w 10
157	# Give a chance to send a DAD announce packet
158	atf_check -s exit:0 sleep 1
159	extract_new_packets > ./out
160	$DEBUG && cat ./out
161
162	# Check the DAD announce packet
163	pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
164	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
165	# The new address left tentative
166	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.3 |grep -q tentative"
167}
168
169dad_duplicated_body()
170{
171	local localip1=10.0.1.1
172	local localip2=10.0.1.11
173	local peerip=10.0.1.2
174
175	atf_check -s exit:0 ${inetserver} $SOCKLOCAL
176	atf_check -s exit:0 ${inetserver} $SOCKPEER
177
178	setup_server $SOCKLOCAL $localip1
179	setup_server $SOCKPEER $peerip
180
181	export RUMP_SERVER=$SOCKLOCAL
182
183	# The primary address isn't marked as duplicated
184	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep $localip1 |grep -q duplicated"
185
186	#
187	# Add a new address duplicated with the peer server
188	#
189	atf_check -s exit:0 rump.ifconfig shmif0 inet $peerip alias
190	atf_check -s exit:0 sleep 1
191
192	# The new address is marked as duplicated
193	atf_check -s exit:0 -x "rump.ifconfig shmif0 |grep $peerip |grep -q duplicated"
194
195	# A unique address isn't marked as duplicated
196	atf_check -s exit:0 rump.ifconfig shmif0 inet $localip2 alias
197	atf_check -s exit:0 sleep 1
198	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep $localip2 |grep -q duplicated"
199}
200
201cleanup()
202{
203	env RUMP_SERVER=$SOCKLOCAL rump.halt
204	env RUMP_SERVER=$SOCKPEER rump.halt
205}
206
207dump_local()
208{
209	export RUMP_SERVER=$SOCKLOCAL
210	rump.netstat -nr
211	rump.arp -n -a
212	rump.ifconfig
213	$HIJACKING dmesg
214}
215
216dump_peer()
217{
218	export RUMP_SERVER=$SOCKPEER
219	rump.netstat -nr
220	rump.arp -n -a
221	rump.ifconfig
222	$HIJACKING dmesg
223}
224
225dump()
226{
227	dump_local
228	dump_peer
229	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
230}
231
232dad_basic_cleanup()
233{
234	$DEBUG && dump_local
235	$DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
236	env RUMP_SERVER=$SOCKLOCAL rump.halt
237}
238
239dad_duplicated_cleanup()
240{
241	$DEBUG && dump
242	cleanup
243}
244
245atf_init_test_cases()
246{
247	atf_add_test_case dad_basic
248	atf_add_test_case dad_duplicated
249}
250