• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2014-2018 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) 2018 Petr Vorel <pvorel@suse.cz>
5# Author:       Alexey Kodanev alexey.kodanev@oracle.com
6
7TST_SETUP="dhcp_lib_setup"
8TST_CLEANUP="dhcp_lib_cleanup"
9TST_TESTFUNC="test01"
10TST_NEEDS_TMPDIR=1
11TST_NEEDS_ROOT=1
12TST_NEEDS_CMDS="cat $dhcp_name awk ip pgrep pkill dhclient"
13
14. tst_net.sh
15. daemonlib.sh
16
17iface0="ltp_veth0"
18iface1="ltp_veth1"
19
20stop_dhcp()
21{
22	[ "$(pgrep -x $dhcp_name)" ] || return 0
23
24	tst_res 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
38dhcp_lib_setup()
39{
40	[ -z "$log" ] && log="$PWD/$(basename $0 '.sh').log"
41
42	if [ $TST_IPV6 ]; then
43		ip_addr="fd00:1:1:2::12/64"
44		ip_addr_check_noprefix="fd00:1:1:2::100"
45		ip_addr_check="$ip_addr_check_noprefix/128"
46	else
47		ip_addr="10.1.1.12/24"
48		ip_addr_check_noprefix="10.1.1.100"
49		ip_addr_check="$ip_addr_check_noprefix/24"
50	fi
51
52	lsmod | grep -q '^veth ' && veth_loaded=yes || veth_loaded=no
53
54	tst_res TINFO "create veth interfaces"
55	ip li add $iface0 type veth peer name $iface1 || \
56		tst_brk TBROK "failed to add veth $iface0"
57
58	veth_added=1
59	ip li set up $iface0 || tst_brk TBROK "failed to bring $iface0 up"
60	ip li set up $iface1 || tst_brk TBROK "failed to bring $iface1 up"
61
62	stop_dhcp || tst_brk TBROK "Failed to stop dhcp server"
63
64	dhclient_lease="/var/lib/dhclient/dhclient${TST_IPV6}.leases"
65	[ -f $dhclient_lease ] || dhclient_lease="/var/lib/dhcp/dhclient${TST_IPV6}.leases"
66	if [ -f $dhclient_lease ]; then
67		tst_res TINFO "backup dhclient${TST_IPV6}.leases"
68		mv $dhclient_lease .
69	fi
70
71	tst_res TINFO "add $ip_addr to $iface0"
72	ip addr add $ip_addr dev $iface0 || \
73		tst_brk TBROK "failed to add ip address"
74
75	if [ ! -d "$lease_dir" ]; then
76		mkdir -p $lease_dir
77		lease_dir_added=1
78	fi
79}
80
81dhcp_lib_cleanup()
82{
83	[ -z "$veth_loaded" ] && return
84
85	[ "$lease_dir_added" = 1 ] && rm -rf $lease_dir
86	rm -f $lease_file
87
88	stop_dhcp
89
90	pkill -f "dhclient -$TST_IPVER $iface1"
91
92	cleanup_dhcp
93
94	# restore dhclient leases
95	[ $dhclient_lease ] && rm -f $dhclient_lease
96	[ -f "dhclient${TST_IPV6}.leases" ] && \
97		mv dhclient${TST_IPV6}.leases $dhclient_lease
98
99	[ $veth_added ] && ip li del $iface0
100
101	[ "$veth_loaded" = "no" ] && lsmod | grep -q '^veth ' && rmmod veth
102}
103
104print_dhcp_log()
105{
106	[ -f "$log" ] && cat $log
107}
108
109test01()
110{
111	local wicked_cfg="/etc/sysconfig/network/ifcfg-$iface1"
112	local wicked_cleanup
113
114	tst_res TINFO "testing DHCP server $dhcp_name: $(print_dhcp_version)"
115	tst_res TINFO "using DHCP client: $(dhclient --version 2>&1)"
116
117	tst_res TINFO "starting DHCPv$TST_IPVER server on $iface0"
118
119	start_dhcp$TST_IPV6
120	if [ $? -ne 0 ]; then
121		print_dhcp_log
122		tst_brk TBROK "Failed to start $dhcp_name"
123	fi
124
125	sleep 1
126
127	if [ "$(pgrep '$dhcp_name')" ]; then
128		print_dhcp_log
129		tst_brk TBROK "Failed to start $dhcp_name"
130	fi
131
132	if [ $HAVE_SYSTEMCTL -eq 1 ] && \
133		systemctl --no-pager -p Id show network.service | grep -q Id=wicked.service; then
134		tst_res TINFO "wicked is running, don't start dhclient"
135		if [ ! -f "$wicked_cfg" ]; then
136			cat <<EOF > $wicked_cfg
137BOOTPROTO='dhcp'
138NAME='LTP card'
139STARTMODE='auto'
140USERCONTROL='no'
141EOF
142			wicked_cleanup=1
143		else
144			tst_res TINFO "wicked config file $wicked_cfg already exist"
145		fi
146
147		tst_res TINFO "restarting wicked"
148		systemctl restart wicked
149	else
150		tst_res TINFO "starting dhclient -$TST_IPVER $iface1"
151		dhclient -$TST_IPVER $iface1 || tst_brk TBROK "dhclient failed"
152	fi
153
154	# check that we get configured ip address
155	ip addr show $iface1 | grep -q $ip_addr_check
156	if [ $? -eq 0 ]; then
157		tst_res TPASS "'$ip_addr_check' configured by DHCPv$TST_IPVER"
158	else
159		if ip addr show $iface1 | grep -q $ip_addr_check_noprefix; then
160			tst_res TFAIL "'$ip_addr_check_noprefix' configured but has wrong prefix, expect '$ip_addr_check'"
161			ip addr show $iface1
162		else
163			tst_res TFAIL "'$ip_addr_check' not configured by DHCPv$TST_IPVER"
164			print_dhcp_log
165		fi
166	fi
167
168	[ "$wicked_cleanup" ] && rm -f $wicked_cfg
169
170	stop_dhcp
171}
172