t_ipaddress.sh revision 313498
1#	$NetBSD: t_ipaddress.sh,v 1.3 2016/08/10 21:33:52 kre Exp $
2#
3# Copyright (c) 2015 Internet Initiative Japan 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
28SERVER="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpdev"
29SERVER6="$SERVER -lrumpnet_netinet6"
30SOCK_LOCAL=unix://commsock1
31BUS=bus
32
33DEBUG=false
34
35check_entry()
36{
37	local ip=$(echo $1 |sed 's/\./\\./g')
38	local word=$2
39
40	atf_check -s exit:0 -o match:"$word" -e ignore -x \
41	    "rump.netstat -rn | grep ^'$ip'"
42}
43
44check_entry_fail()
45{
46	local ip=$(echo $1 |sed 's/\./\\./g')
47	local flags=$2  # Not used currently
48
49	atf_check -s not-exit:0 -e ignore -x \
50	    "rump.netstat -rn | grep ^'$ip'"
51}
52
53test_same_address()
54{
55	local ip=10.0.0.1
56	local net=10.0.0/24
57
58	atf_check -s exit:0 ${SERVER} ${SOCK_LOCAL}
59	export RUMP_SERVER=$SOCK_LOCAL
60
61	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
62	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
63	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 $ip/24
64	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
65	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
66
67	$DEBUG && rump.netstat -nr -f inet
68
69	check_entry $ip UHl
70	check_entry $ip lo0
71	check_entry $ip 'link#2'
72	check_entry $net U
73	check_entry $net shmif0
74	check_entry $net 'link#2'
75
76	# Delete the address
77	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 $ip delete
78
79	$DEBUG && rump.netstat -nr -f inet
80
81	check_entry_fail $ip
82	check_entry_fail $net
83
84	# Assign the same address again
85	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 $ip/24
86	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
87
88	$DEBUG && rump.netstat -nr -f inet
89
90	check_entry $ip UHl
91	check_entry $ip lo0
92	check_entry $ip 'link#2'
93	check_entry $net U
94	check_entry $net shmif0
95	check_entry $net 'link#2'
96
97	# Delete the address again
98	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 $ip delete
99
100	$DEBUG && rump.netstat -nr -f inet
101
102	check_entry_fail $ip
103	check_entry_fail $net
104}
105
106test_same_address6()
107{
108	local ip=fc00::1
109	local net=fc00::/64
110
111	atf_check -s exit:0 ${SERVER6} ${SOCK_LOCAL}
112	export RUMP_SERVER=$SOCK_LOCAL
113
114	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
115	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
116	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 inet6 $ip
117	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
118	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
119
120	$DEBUG && rump.netstat -nr -f inet6
121
122	check_entry $ip UHl
123	check_entry $ip lo0
124	check_entry $ip 'link#2'
125	check_entry $net U
126	check_entry $net shmif0
127	check_entry $net 'link#2'
128
129	# Delete the address
130	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 inet6 $ip delete
131
132	$DEBUG && rump.netstat -nr -f inet6
133
134	check_entry_fail $ip
135	check_entry_fail $net
136
137	# Assign the same address again
138	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 inet6 $ip
139	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
140
141	$DEBUG && rump.netstat -nr -f inet6
142
143	check_entry $ip UHl
144	check_entry $ip lo0
145	check_entry $ip 'link#2'
146	check_entry $net U
147	check_entry $net shmif0
148	check_entry $net 'link#2'
149
150	# Delete the address again
151	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 inet6 $ip delete
152
153	$DEBUG && rump.netstat -nr -f inet6
154
155	check_entry_fail $ip
156	check_entry_fail $net
157}
158
159cleanup()
160{
161
162	$DEBUG && shmif_dumpbus -p - $BUS 2>/dev/null | tcpdump -n -e -r -
163	env RUMP_SERVER=$SOCK_LOCAL rump.halt
164}
165
166add_test()
167{
168	local name=$1
169	local desc="$2"
170
171	atf_test_case "ipaddr_${name}" cleanup
172	eval "ipaddr_${name}_head() { \
173			atf_set \"descr\" \"${desc}\"; \
174			atf_set \"require.progs\" \"rump_server\"; \
175		}; \
176	    ipaddr_${name}_body() { \
177			test_${name}; \
178		}; \
179	    ipaddr_${name}_cleanup() { \
180			cleanup; \
181		}"
182	atf_add_test_case "ipaddr_${name}"
183}
184
185atf_init_test_cases()
186{
187
188	add_test same_address	"Assigning/deleting an IP address twice"
189	add_test same_address6	"Assigning/deleting an IPv6 address twice"
190}
191