Home | History | Annotate | Download | only in dhcp
      1 #!/bin/sh
      2 # Copyright (c) 2014-2015 Oracle and/or its affiliates. All Rights Reserved.
      3 #
      4 # This program is free software; you can redistribute it and/or
      5 # modify it under the terms of the GNU General Public License as
      6 # published by the Free Software Foundation; either version 2 of
      7 # the License, or (at your option) any later version.
      8 #
      9 # This program is distributed in the hope that it would be useful,
     10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 # GNU General Public License for more details.
     13 #
     14 # You should have received a copy of the GNU General Public License
     15 # along with this program; if not, write the Free Software Foundation,
     16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     17 #
     18 # Author:       Alexey Kodanev alexey.kodanev (at] oracle.com
     19 
     20 stop_dhcp()
     21 {
     22 	[ "$(pgrep -x $dhcp_name)" ] || return 0
     23 
     24 	tst_resm TINFO "stopping $dhcp_name"
     25 	local count=0
     26 	while [ $count -le 10 ]; do
     27 		pkill -x $dhcp_name
     28 		[ "$(pgrep -x $dhcp_name)" ] || return 0
     29 		tst_sleep 100ms
     30 		count=$((count + 1))
     31 	done
     32 
     33 	pkill -9 -x $dhcp_name
     34 	tst_sleep 100ms
     35 	[ "$(pgrep -x $dhcp_name)" ] && return 1 || return 0
     36 }
     37 
     38 init()
     39 {
     40 	tst_require_root
     41 	tst_check_cmds cat $dhcp_name awk ip pgrep pkill dhclient
     42 
     43 	veth_loaded=
     44 	lsmod | grep -q '^veth ' && veth_loaded=1
     45 
     46 	tst_resm TINFO "create veth interfaces"
     47 	ip li add $iface0 type veth peer name $iface1 || \
     48 		tst_brkm TBROK "failed to add veth $iface0"
     49 
     50 	veth_added=1
     51 	ip li set up $iface0 || tst_brkm TBROK "failed to bring $iface0 up"
     52 	ip li set up $iface1 || tst_brkm TBROK "failed to bring $iface1 up"
     53 
     54 	tst_tmpdir
     55 
     56 	stop_dhcp || tst_brkm TBROK "Failed to stop dhcp server"
     57 
     58 	dhclient_lease="/var/lib/dhclient/dhclient${TST_IPV6}.leases"
     59 	if [ -f $dhclient_lease ]; then
     60 		tst_resm TINFO "backup dhclient${TST_IPV6}.leases"
     61 		mv $dhclient_lease .
     62 	fi
     63 
     64 	tst_resm TINFO "add $ip_addr to $iface0"
     65 	ip addr add $ip_addr dev $iface0 || \
     66 		tst_brkm TBROK "failed to add ip address"
     67 }
     68 
     69 cleanup()
     70 {
     71 	stop_dhcp
     72 
     73 	pkill -f "dhclient -$ipv $iface1"
     74 
     75 	cleanup_dhcp
     76 
     77 	# restore dhclient leases
     78 	[ $dhclient_lease ] && rm -f $dhclient_lease
     79 	[ -f "dhclient${TST_IPV6}.leases" ] && \
     80 		mv dhclient${TST_IPV6}.leases $dhclient_lease
     81 
     82 	[ $veth_added ] && ip li del $iface0
     83 
     84 	if [ -z $veth_loaded ]; then
     85 		lsmod | grep -q '^veth ' && rmmod veth
     86 	fi
     87 
     88 	tst_rmdir
     89 }
     90 
     91 test01()
     92 {
     93 	tst_resm TINFO "starting DHCPv${ipv} server on $iface0"
     94 
     95 	start_dhcp$TST_IPV6
     96 
     97 	sleep 1
     98 
     99 	if [ "$(pgrep '$dhcp_name')" ]; then
    100 		print_dhcp_log
    101 		tst_brkm TBROK "Failed to start $dhcp_name"
    102 	fi
    103 
    104 	tst_resm TINFO "starting dhclient -${ipv} $iface1"
    105 	dhclient -$ipv $iface1 || \
    106 		tst_brkm TBROK "dhclient failed"
    107 
    108 	# check that we get configured ip address
    109 	ip addr show $iface1 | grep $ip_addr_check > /dev/null
    110 	if [ $? -eq 0 ]; then
    111 		tst_resm TPASS "'$ip_addr_check' configured by DHCPv$ipv"
    112 	else
    113 		tst_resm TFAIL "'$ip_addr_check' not configured by DHCPv$ipv"
    114 		print_dhcp_log
    115 	fi
    116 
    117 	stop_dhcp
    118 }
    119 
    120 iface0="ltp_veth0"
    121 iface1="ltp_veth1"
    122 ipv=${TST_IPV6:-"4"}
    123 
    124 if [ $TST_IPV6 ]; then
    125 	ip_addr="fd00:1:1:2::12/64"
    126 	ip_addr_check="fd00:1:1:2::100/64"
    127 else
    128 	ip_addr="10.1.1.12/24"
    129 	ip_addr_check="10.1.1.100/24"
    130 fi
    131 
    132 trap "tst_brkm TBROK 'test interrupted'" INT
    133