Home | History | Annotate | Line # | Download | only in route
t_flags.sh revision 1.2
      1 #	$NetBSD: t_flags.sh,v 1.2 2015/05/20 01:30:42 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 netserver=\
     29 "rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
     30 SOCK_LOCAL=unix://commsock1
     31 SOCK_PEER=unix://commsock2
     32 SOCK_GW=unix://commsock3
     33 BUS=bus1
     34 BUS2=bus2
     35 
     36 DEBUG=false
     37 
     38 setup_local()
     39 {
     40 
     41 	atf_check -s exit:0 ${netserver} ${SOCK_LOCAL}
     42 
     43 	export RUMP_SERVER=$SOCK_LOCAL
     44 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
     45 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
     46 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.2/24
     47 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
     48 
     49 	$DEBUG && rump.ifconfig
     50 	$DEBUG && rump.netstat -rn -f inet
     51 }
     52 
     53 setup_peer()
     54 {
     55 
     56 	atf_check -s exit:0 ${netserver} ${SOCK_PEER}
     57 
     58 	export RUMP_SERVER=$SOCK_PEER
     59 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
     60 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
     61 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.1/24
     62 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
     63 
     64 	$DEBUG && rump.ifconfig
     65 	$DEBUG && rump.netstat -rn -f inet
     66 }
     67 
     68 setup_gw()
     69 {
     70 
     71 	atf_check -s exit:0 ${netserver} ${SOCK_GW}
     72 
     73 	export RUMP_SERVER=$SOCK_GW
     74 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 create
     75 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 linkstr ${BUS}
     76 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 10.0.0.254/24
     77 	atf_check -s exit:0 -o ignore rump.ifconfig shmif0 up
     78 
     79 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 create
     80 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 linkstr ${BUS2}
     81 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 10.0.2.1/24
     82 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 alias 10.0.2.2/24
     83 	atf_check -s exit:0 -o ignore rump.ifconfig shmif1 up
     84 
     85 	# Wait until DAD completes (10 sec at most)
     86 	atf_check -s exit:0 -o ignore rump.ifconfig -w 10
     87 	atf_check -s not-exit:0 -x "rump.ifconfig shmif1 |grep -q tentative"
     88 
     89 	$DEBUG && rump.ifconfig
     90 	$DEBUG && rump.netstat -rn -f inet
     91 }
     92 
     93 teardown_gw()
     94 {
     95 
     96 	env RUMP_SERVER=$SOCK_GW rump.halt
     97 }
     98 
     99 check_entry_flags()
    100 {
    101 	local ip=$(echo $1 |sed 's/\./\\./g')
    102 	local flags=$2
    103 
    104 	atf_check -s exit:0 -o match:" $flags " -e ignore -x \
    105 	    "rump.netstat -rn -f inet | grep ^'$ip'"
    106 }
    107 
    108 check_entry_gw()
    109 {
    110 	local ip=$(echo $1 |sed 's/\./\\./g')
    111 	local gw=$2
    112 
    113 	atf_check -s exit:0 -o match:" $gw " -e ignore -x \
    114 	    "rump.netstat -rn -f inet | grep ^'$ip'"
    115 }
    116 
    117 check_entry_fail()
    118 {
    119 	ip=$(echo $1 |sed 's/\./\\./g')
    120 	flags=$2  # Not used currently
    121 
    122 	atf_check -s not-exit:0 -e ignore -x \
    123 	    "rump.netstat -rn -f inet | grep ^'$ip'"
    124 }
    125 
    126 test_lo()
    127 {
    128 
    129 	export RUMP_SERVER=$SOCK_LOCAL
    130 
    131 	# Up, Host, local
    132 	check_entry_flags 127.0.0.1 UHl
    133 }
    134 
    135 test_connected()
    136 {
    137 
    138 	export RUMP_SERVER=$SOCK_LOCAL
    139 
    140 	# Up, Host, LLINFO, local
    141 	check_entry_flags 10.0.0.2 UHLl
    142 
    143 	# Up, Cloning
    144 	check_entry_flags 10.0.0/24 UC
    145 }
    146 
    147 test_cloned()
    148 {
    149 
    150 	export RUMP_SERVER=$SOCK_LOCAL
    151 
    152 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.0.1
    153 	$DEBUG && rump.netstat -rn -f inet
    154 
    155 	# Up, Host, LLINFO, cloned
    156 	check_entry_flags 10.0.0.1 UHLc
    157 }
    158 
    159 test_default_gateway()
    160 {
    161 
    162 	export RUMP_SERVER=$SOCK_LOCAL
    163 
    164 	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
    165 	$DEBUG && rump.netstat -rn -f inet
    166 
    167 	# Up, Gateway, Static
    168 	check_entry_flags default UGS
    169 }
    170 
    171 test_static()
    172 {
    173 
    174 	export RUMP_SERVER=$SOCK_LOCAL
    175 
    176 	# Static route to host
    177 	atf_check -s exit:0 -o ignore rump.route add 10.0.1.1 10.0.0.1
    178 	$DEBUG && rump.netstat -rn -f inet
    179 
    180 	# Up, Gateway, Host, Static
    181 	check_entry_flags 10.0.1.1 UGHS
    182 
    183 	# Static route to network
    184 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.1
    185 	$DEBUG && rump.netstat -rn -f inet
    186 
    187 	# Up, Gateway, Static
    188 	check_entry_flags 10.0.2/24 UGS
    189 }
    190 
    191 test_route_flush()
    192 {
    193 
    194 	export RUMP_SERVER=$SOCK_LOCAL
    195 
    196 	# Reusing other tests to create routes
    197 	test_cloned_route
    198 	test_default_gateway
    199 	test_static_route
    200 
    201 	atf_check -s exit:0 -o ignore rump.route flush
    202 	$DEBUG && rump.netstat -rn -f inet
    203 
    204 	# Should remain
    205 	check_entry_flags 127.0.0.1 UHl
    206 	check_entry_flags 10.0.0/24 UC
    207 
    208 	# Shouldn't remain
    209 	check_entry_fail default UGS
    210 	check_entry_fail 10.0.1.1 UGHS
    211 	check_entry_fail 10.0.2/24 UGS
    212 	# Should it remain?
    213 	check_entry_fail 10.0.0.2 UHLl
    214 }
    215 
    216 test_blackhole()
    217 {
    218 
    219 	export RUMP_SERVER=$SOCK_LOCAL
    220 
    221 	# Delete an existing route first
    222 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    223 
    224 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -blackhole
    225 	$DEBUG && rump.netstat -rn -f inet
    226 
    227 	# Up, Gateway, Blackhole, Static
    228 	check_entry_flags 10.0.0/24 UGBS
    229 
    230 	atf_check -s not-exit:0 -o ignore -e match:'No route to host' \
    231 	    rump.ping -n -w 1 -c 1 10.0.0.1
    232 	$DEBUG && rump.netstat -rn -f inet
    233 
    234 	# Shouldn't be created
    235 	check_entry_fail 10.0.0.1 UHLc
    236 }
    237 
    238 test_reject()
    239 {
    240 
    241 	export RUMP_SERVER=$SOCK_LOCAL
    242 
    243 	# Delete an existing route first
    244 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    245 
    246 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -reject
    247 	$DEBUG && rump.netstat -rn -f inet
    248 
    249 	# Up, Gateway, Reject, Static
    250 	check_entry_flags 10.0.0/24 UGRS
    251 
    252 	atf_check -s not-exit:0 -o ignore -e match:'No route to host' \
    253 	    rump.ping -n -w 1 -c 1 10.0.0.1
    254 	$DEBUG && rump.netstat -rn -f inet
    255 
    256 	# Shouldn't be created
    257 	check_entry_fail 10.0.0.1 UHLc
    258 }
    259 
    260 test_icmp_redirect()
    261 {
    262 
    263 	### Testing Dynamic flag ###
    264 
    265 	#
    266 	# Setup a gateway 10.0.0.254. 10.0.2.1 is behind it.
    267 	#
    268 	setup_gw
    269 
    270 	#
    271 	# Teach the peer that 10.0.2.* is behind 10.0.0.254
    272 	#
    273 	export RUMP_SERVER=$SOCK_PEER
    274 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.2.0/24 10.0.0.254
    275 	# Up, Gateway, Static
    276 	check_entry_flags 10.0.2/24 UGS
    277 
    278 	#
    279 	# Setup the default gateway to the peer, 10.0.0.1
    280 	#
    281 	export RUMP_SERVER=$SOCK_LOCAL
    282 	atf_check -s exit:0 -o ignore rump.route add default 10.0.0.1
    283 	# Up, Gateway, Static
    284 	check_entry_flags default UGS
    285 
    286 	# Try ping 10.0.2.1
    287 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.1
    288 	$DEBUG && rump.netstat -rn -f inet
    289 
    290 	# Up, Gateway, Host, Dynamic
    291 	check_entry_flags 10.0.2.1 UGHD
    292 	check_entry_gw 10.0.2.1 10.0.0.254
    293 
    294 	export RUMP_SERVER=$SOCK_PEER
    295 	$DEBUG && rump.netstat -rn -f inet
    296 
    297 	### Testing Modified flag ###
    298 
    299 	#
    300 	# Teach a wrong route to 10.0.2.2
    301 	#
    302 	export RUMP_SERVER=$SOCK_LOCAL
    303 	atf_check -s exit:0 -o ignore rump.route add 10.0.2.2 10.0.0.1
    304 	# Up, Gateway, Host, Static
    305 	check_entry_flags 10.0.2.2 UGHS
    306 	check_entry_gw 10.0.2.2 10.0.0.1
    307 
    308 	# Try ping 10.0.2.2
    309 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 10.0.2.2
    310 	$DEBUG && rump.netstat -rn -f inet
    311 
    312 	# Up, Gateway, Host, Modified, Static
    313 	check_entry_flags 10.0.2.2 UGHMS
    314 	check_entry_gw 10.0.2.2 10.0.0.254
    315 
    316 	teardown_gw
    317 }
    318 
    319 test_announce()
    320 {
    321 	export RUMP_SERVER=$SOCK_LOCAL
    322 
    323 	# Delete an existing route first
    324 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    325 
    326 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.1 -proxy
    327 	$DEBUG && rump.netstat -rn -f inet
    328 
    329 	# Up, Gateway, Static, proxy
    330 	check_entry_flags 10.0.0/24 UGSp
    331 
    332 	# TODO test its behavior
    333 }
    334 
    335 test_xresolve_rtm()
    336 {
    337 	local ip=$1
    338 	local rtm=$2
    339 	local pid=
    340 
    341 	rump.route -n monitor > ./mon.log &
    342 	pid=$!
    343 
    344 	atf_check -s exit:0 -o ignore rump.ping -n -w 1 -c 1 $ip
    345 	$DEBUG && rump.netstat -rn -f inet
    346 	$DEBUG && cat ./mon.log
    347 
    348 	atf_check -s exit:0 grep -q $rtm ./mon.log
    349 
    350 	kill $pid
    351 }
    352 
    353 test_xresolve()
    354 {
    355 
    356 	export RUMP_SERVER=$SOCK_LOCAL
    357 
    358 	# For a normal route, a RTM_ADD message is emitted on a route cloning
    359 	test_xresolve_rtm 10.0.0.1 RTM_ADD
    360 	# Up, Host, LLINFO, cloned
    361 	check_entry_flags 10.0.0.1 UHLc
    362 
    363 	# Delete an existing route first
    364 	atf_check -s exit:0 -o ignore rump.route delete -net 10.0.0.0/24
    365 	# Create a connected route with XRESOLVE flag for the interface
    366 	atf_check -s exit:0 -o ignore rump.route add -net 10.0.0.0/24 10.0.0.2 \
    367 	    -interface -xresolve
    368 	$DEBUG && rump.netstat -rn -f inet
    369 
    370 	# Up, Cloning, Xresolve, Static
    371 	check_entry_flags 10.0.0/24 UCXS
    372 
    373 	# If XRESOLVE flag is set, a RTM_RESOLVE message is emitted
    374 	test_xresolve_rtm 10.0.0.1 RTM_RESOLVE
    375 	# Up, Host, Xresolve, LLINFO, cloned
    376 	check_entry_flags 10.0.0.1 UHXLc
    377 }
    378 
    379 cleanup()
    380 {
    381 	$DEBUG && /usr/bin/shmif_dumpbus -p - $BUS 2>/dev/null | \
    382 	    /usr/sbin/tcpdump -n -e -r -
    383 	env RUMP_SERVER=$SOCK_LOCAL rump.halt
    384 	env RUMP_SERVER=$SOCK_PEER rump.halt
    385 }
    386 
    387 add_test()
    388 {
    389 	local name=$1
    390 	local desc="$2"
    391 
    392 	atf_test_case "route_flags_${name}" cleanup
    393 	eval "route_flags_${name}_head() { \
    394 			atf_set \"descr\" \"${desc}\"; \
    395 			atf_set \"require.progs\" \"rump_server\"; \
    396 		}; \
    397 	    route_flags_${name}_body() { \
    398 			setup_local; \
    399 			setup_peer; \
    400 			test_${name}; \
    401 		}; \
    402 	    route_flags_${name}_cleanup() { \
    403 			cleanup; \
    404 		}"
    405 	atf_add_test_case "route_flags_${name}"
    406 }
    407 
    408 atf_init_test_cases()
    409 {
    410 
    411 	add_test lo              "Tests route flags: loop back interface"
    412 	add_test connected       "Tests route flags: connected route"
    413 	add_test cloned          "Tests route flags: cloned route"
    414 	add_test default_gateway "Tests route flags: default gateway"
    415 	add_test static          "Tests route flags: static route"
    416 	add_test blackhole       "Tests route flags: blackhole route"
    417 	add_test reject          "Tests route flags: reject route"
    418 	add_test icmp_redirect   "Tests route flags: icmp redirect"
    419 	add_test announce        "Tests route flags: announce flag"
    420 	add_test xresolve        "Tests route flags: xresolve flag"
    421 }
    422