Home | History | Annotate | Line # | Download | only in arp
t_dad.sh revision 1.4
      1 #	$NetBSD: t_dad.sh,v 1.4 2015/08/17 07:06:58 ozaki-r 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 
     28 inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
     29 HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK=sysctl=yes"
     30 
     31 SOCKLOCAL=unix://commsock1
     32 SOCKPEER=unix://commsock2
     33 
     34 DEBUG=false
     35 
     36 atf_test_case dad_basic cleanup
     37 atf_test_case dad_duplicated cleanup
     38 
     39 dad_basic_head()
     40 {
     41 	atf_set "descr" "Tests for IPv4 DAD basic behavior"
     42 	atf_set "require.progs" "rump_server"
     43 }
     44 
     45 dad_duplicated_head()
     46 {
     47 	atf_set "descr" "Tests for IPv4 DAD duplicated state"
     48 	atf_set "require.progs" "rump_server"
     49 }
     50 
     51 setup_server()
     52 {
     53 	local sock=$1
     54 	local ip=$2
     55 
     56 	export RUMP_SERVER=$sock
     57 
     58 	atf_check -s exit:0 rump.ifconfig shmif0 create
     59 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
     60 	atf_check -s exit:0 rump.ifconfig shmif0 inet $ip/24
     61 	atf_check -s exit:0 rump.ifconfig shmif0 up
     62 	atf_check -s exit:0 rump.ifconfig -w 10
     63 
     64 	$DEBUG && rump.ifconfig shmif0
     65 }
     66 
     67 make_pkt_str()
     68 {
     69 	local target=$1
     70 	local sender=$2
     71 	pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
     72 	pkt="$pkt Request who-has $target tell $sender, length 28"
     73 	echo $pkt
     74 }
     75 
     76 extract_new_packets()
     77 {
     78 	local old=./old
     79 
     80 	if [ ! -f $old ]; then
     81 		old=/dev/null
     82 	fi
     83 
     84 	shmif_dumpbus -p - bus1 2>/dev/null| \
     85 	    tcpdump -n -e -r - 2>/dev/null > ./new
     86 	diff -u $old ./new |grep '^+' |cut -d '+' -f 2 > ./diff
     87 	mv -f ./new ./old
     88 	cat ./diff
     89 }
     90 
     91 dad_basic_body()
     92 {
     93 	local pkt=
     94 
     95 	atf_check -s exit:0 ${inetserver} $SOCKLOCAL
     96 	export RUMP_SERVER=$SOCKLOCAL
     97 
     98 	atf_check -s exit:0 rump.ifconfig shmif0 create
     99 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
    100 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
    101 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
    102 	$DEBUG && rump.ifconfig shmif0
    103 
    104 	atf_check -s exit:0 rump.ifconfig shmif0 up
    105 	rump.ifconfig shmif0 > ./out
    106 	$DEBUG && cat ./out
    107 
    108 	# The primary address doesn't start with tentative state
    109 	atf_check -s not-exit:0 -x "cat ./out |grep 10.0.0.1 |grep -q tentative"
    110 	# The alias address starts with tentative state
    111 	# XXX we have no stable way to check this, so skip for now
    112 	#atf_check -s exit:0 -x "cat ./out |grep 10.0.0.2 |grep -q tentative"
    113 
    114 	atf_check -s exit:0 sleep 2
    115 	extract_new_packets > ./out
    116 	$DEBUG && cat ./out
    117 
    118 	# Check DAD probe packets
    119 	pkt=$(make_pkt_str 10.0.0.2 0.0.0.0)
    120 	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
    121 	# No DAD for the primary address
    122 	pkt=$(make_pkt_str 10.0.0.1 0.0.0.0)
    123 	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
    124 
    125 	# Waiting for DAD complete
    126 	atf_check -s exit:0 rump.ifconfig -w 10
    127 	extract_new_packets > ./out
    128 	$DEBUG && cat ./out
    129 
    130 	# Check the DAD announce packet
    131 	pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
    132 	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
    133 	# The alias address left tentative
    134 	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.2 |grep -q tentative"
    135 
    136 	#
    137 	# Add a new address on the fly
    138 	#
    139 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24 alias
    140 
    141 	# The new address starts with tentative state
    142 	atf_check -s exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.3 |grep -q tentative"
    143 
    144 	# Check DAD probe packets
    145 	atf_check -s exit:0 sleep 2
    146 	extract_new_packets > ./out
    147 	$DEBUG && cat ./out
    148 	pkt=$(make_pkt_str 10.0.0.3 0.0.0.0)
    149 	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
    150 
    151 	# Waiting for DAD complete
    152 	atf_check -s exit:0 rump.ifconfig -w 10
    153 	extract_new_packets > ./out
    154 	$DEBUG && cat ./out
    155 
    156 	# Check the DAD announce packet
    157 	pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
    158 	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
    159 	# The new address left tentative
    160 	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.3 |grep -q tentative"
    161 }
    162 
    163 dad_duplicated_body()
    164 {
    165 	local localip1=10.0.1.1
    166 	local localip2=10.0.1.11
    167 	local peerip=10.0.1.2
    168 
    169 	atf_check -s exit:0 ${inetserver} $SOCKLOCAL
    170 	atf_check -s exit:0 ${inetserver} $SOCKPEER
    171 
    172 	setup_server $SOCKLOCAL $localip1
    173 	setup_server $SOCKPEER $peerip
    174 
    175 	export RUMP_SERVER=$SOCKLOCAL
    176 
    177 	# The primary address isn't marked as duplicated
    178 	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep $localip1 |grep -q duplicated"
    179 
    180 	#
    181 	# Add a new address duplicated with the peer server
    182 	#
    183 	atf_check -s exit:0 rump.ifconfig shmif0 inet $peerip alias
    184 	atf_check -s exit:0 sleep 1
    185 
    186 	# The new address is marked as duplicated
    187 	atf_check -s exit:0 -x "rump.ifconfig shmif0 |grep $peerip |grep -q duplicated"
    188 
    189 	# A unique address isn't marked as duplicated
    190 	atf_check -s exit:0 rump.ifconfig shmif0 inet $localip2 alias
    191 	atf_check -s exit:0 sleep 1
    192 	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep $localip2 |grep -q duplicated"
    193 }
    194 
    195 cleanup()
    196 {
    197 	env RUMP_SERVER=$SOCKLOCAL rump.halt
    198 	env RUMP_SERVER=$SOCKPEER rump.halt
    199 }
    200 
    201 dump_local()
    202 {
    203 	export RUMP_SERVER=$SOCKLOCAL
    204 	rump.netstat -nr
    205 	rump.arp -n -a
    206 	rump.ifconfig
    207 	$HIJACKING dmesg
    208 }
    209 
    210 dump_peer()
    211 {
    212 	export RUMP_SERVER=$SOCKPEER
    213 	rump.netstat -nr
    214 	rump.arp -n -a
    215 	rump.ifconfig
    216 	$HIJACKING dmesg
    217 }
    218 
    219 dump()
    220 {
    221 	dump_local
    222 	dump_peer
    223 	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
    224 }
    225 
    226 dad_basic_cleanup()
    227 {
    228 	$DEBUG && dump_local
    229 	$DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
    230 	env RUMP_SERVER=$SOCKLOCAL rump.halt
    231 }
    232 
    233 dad_duplicated_cleanup()
    234 {
    235 	$DEBUG && dump
    236 	cleanup
    237 }
    238 
    239 atf_init_test_cases()
    240 {
    241 	atf_add_test_case dad_basic
    242 	atf_add_test_case dad_duplicated
    243 }
    244