routing_test.sh revision 277627
1277627Swill#
2277627Swill#  Copyright (c) 2014 Spectra Logic Corporation
3277627Swill#  All rights reserved.
4277627Swill# 
5277627Swill#  Redistribution and use in source and binary forms, with or without
6277627Swill#  modification, are permitted provided that the following conditions
7277627Swill#  are met:
8277627Swill#  1. Redistributions of source code must retain the above copyright
9277627Swill#     notice, this list of conditions, and the following disclaimer,
10277627Swill#     without modification.
11277627Swill#  2. Redistributions in binary form must reproduce at minimum a disclaimer
12277627Swill#     substantially similar to the "NO WARRANTY" disclaimer below
13277627Swill#     ("Disclaimer") and any redistribution must be conditioned upon
14277627Swill#     including a substantially similar Disclaimer requirement for further
15277627Swill#     binary redistribution.
16277627Swill# 
17277627Swill#  NO WARRANTY
18277627Swill#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19277627Swill#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20277627Swill#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21277627Swill#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22277627Swill#  HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23277627Swill#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24277627Swill#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25277627Swill#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26277627Swill#  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27277627Swill#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28277627Swill#  POSSIBILITY OF SUCH DAMAGES.
29277627Swill# 
30277627Swill#  Authors: Alan Somers         (Spectra Logic Corporation)
31277627Swill#
32277627Swill# $FreeBSD: head/etc/tests/rc.d/routing_test.sh 277627 2015-01-24 00:37:41Z will $
33277627Swill
34277627Swillatf_test_case static_ipv6_loopback_route_for_each_fib cleanup
35277627Swillstatic_ipv6_loopback_route_for_each_fib_head()
36277627Swill{
37277627Swill	atf_set "descr" "Every FIB should have a static IPv6 loopback route"
38277627Swill	atf_set "require.user" "root"
39277627Swill	atf_set "require.config" "fibs"
40277627Swill	atf_set "require.progs" "sysrc"
41277627Swill}
42277627Swillstatic_ipv6_loopback_route_for_each_fib_body()
43277627Swill{
44277627Swill	# Configure the TAP interface to use an RFC5737 nonrouteable address
45277627Swill	# and a non-default fib
46277627Swill	ADDR="192.0.2.2"
47277627Swill	SUBNET="192.0.2.0"
48277627Swill	MASK="24"
49277627Swill
50277627Swill	# Check system configuration
51277627Swill	if [ 0 != `sysctl -n net.add_addr_allfibs` ]; then
52277627Swill		atf_skip "This test requires net.add_addr_allfibs=0"
53277627Swill	fi
54277627Swill
55277627Swill	get_fibs 1
56277627Swill	get_tap
57277627Swill	
58277627Swill	# Configure a TAP interface in /etc/rc.conf.  Register the sysrc
59277627Swill	# variable for cleanup.
60277627Swill	echo "ifconfig_${TAP}" >> "sysrc_vars_to_cleanup"
61277627Swill	sysrc ifconfig_${TAP}="${ADDR}/${MASK} fib ${FIB0}"
62277627Swill
63277627Swill	# Start the interface
64277627Swill	service netif start ${TAP}
65277627Swill	# Check for an IPv6 loopback route
66277627Swill	setfib ${FIB0} netstat -rn -f inet6 | grep -q "^::1.*lo0$"
67277627Swill	if [ 0 -eq $? ]; then
68277627Swill		atf_pass
69277627Swill	else
70277627Swill		setfib ${FIB0} netstat -rn -f inet6
71277627Swill		atf_fail "Did not find an IPv6 loopback route"
72277627Swill	fi
73277627Swill}
74277627Swillstatic_ipv6_loopback_route_for_each_fib_cleanup()
75277627Swill{
76277627Swill	cleanup_sysrc
77277627Swill	cleanup_tap
78277627Swill}
79277627Swill
80277627Swillatf_init_test_cases()
81277627Swill{
82277627Swill	atf_add_test_case static_ipv6_loopback_route_for_each_fib
83277627Swill}
84277627Swill
85277627Swill# Looks up one or more fibs from the configuration data and validates them.
86277627Swill# Returns the results in the env varilables FIB0, FIB1, etc.
87277627Swill# parameter numfibs	The number of fibs to lookup
88277627Swillget_fibs()
89277627Swill{
90277627Swill	NUMFIBS=$1
91277627Swill	net_fibs=`sysctl -n net.fibs`
92277627Swill	i=0
93277627Swill	while [ $i -lt "$NUMFIBS" ]; do
94277627Swill		fib=`atf_config_get "fibs" | \
95277627Swill			awk -v i=$(( i + 1 )) '{print $i}'`
96277627Swill		echo "fib is ${fib}"
97277627Swill		eval FIB${i}=${fib}
98277627Swill		if [ "$fib" -ge "$net_fibs" ]; then
99277627Swill			msg="The ${i}th configured fib is ${fub}, which is "
100277627Swill			msg="$msg not less than net.fibs (${net_fibs})"
101277627Swill			atf_skip "$msg"
102277627Swill		fi
103277627Swill		i=$(( $i + 1 ))
104277627Swill	done
105277627Swill}
106277627Swill
107277627Swill
108277627Swill# Creates a new tap(4) interface, registers it for cleanup, and returns the
109277627Swill# name via the environment variable TAP
110277627Swillget_tap()
111277627Swill{
112277627Swill	local TAPN=0
113277627Swill	while ! ifconfig tap${TAPN} create > /dev/null 2>&1; do
114277627Swill		if [ "$TAPN" -ge 8 ]; then
115277627Swill			atf_skip "Could not create a tap(4) interface"
116277627Swill		else
117277627Swill			TAPN=$(($TAPN + 1))
118277627Swill		fi
119277627Swill	done
120277627Swill	local TAPD=tap${TAPN}
121277627Swill	# Record the TAP device so we can clean it up later
122277627Swill	echo ${TAPD} >> "tap_devices_to_cleanup"
123277627Swill	TAP=${TAPD}
124277627Swill}
125277627Swill
126277627Swillcleanup_sysrc()
127277627Swill{
128277627Swill	for var in `cat "sysrc_vars_to_cleanup"`; do
129277627Swill		sysrc -x $var
130277627Swill	done
131277627Swill}
132277627Swill
133277627Swillcleanup_tap()
134277627Swill{
135277627Swill	for TAPD in `cat "tap_devices_to_cleanup"`; do
136277627Swill		ifconfig ${TAPD} destroy
137277627Swill	done
138277627Swill}
139