xref: /freebsd/sbin/dhclient/tests/pcp.sh (revision 966e279052f33b1665480e0327c177013cb8205a)
1#!/usr/bin/env atf-sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright 2022 John-Mark Gurney
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are
10# met:
11#
12# * Redistributions of source code must retain the above copyright
13#   notice, this list of conditions and the following disclaimer.
14# * Redistributions in binary form must reproduce the above copyright
15#   notice, this list of conditions and the following disclaimer in the
16#   documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#
31# Run the tests:
32# make WITH_TESTS=yes -j 4 all install && kyua test -k /usr/tests/Kyuafile  sbin/dhclient/pcp
33#
34# Output last run:
35# kyua report --verbose -r $(ls -tr ~/.kyua/store/results.*.db | tail -n 1)
36
37. $(atf_get_srcdir)/../../sys/common/vnet.subr
38
39generic_dhcp_cleanup()
40{
41
42	# clean up programs
43	kill $(cat dhclient.test.pid) $(cat dhcpd.pid)
44
45	# clean up files
46	rm -f dhclient.dhcpd.conf lease.dhclient.test dhclient.test.pid
47
48	vnet_cleanup
49}
50
51atf_test_case normal cleanup
52normal_head()
53{
54	atf_set descr 'test dhclient against a server'
55	atf_set require.user root
56}
57
58normal_body()
59{
60	dhcpd=$(which dhcpd)
61
62	if ! [ -x "$dhcpd" ]; then
63		atf_skip "ISC dhcp server (isc-dhcp44-server) not installed"
64	fi
65
66	vnet_init
67
68	epair=$(vnet_mkepair)
69
70	vnet_mkjail dhclient_normal_test ${epair}b
71
72	# Set IP on server iface
73	ifconfig ${epair}a 192.0.2.2/24 up
74
75	# Create dhcp server config
76	cat > dhclient.dhcpd.conf << EOF
77default-lease-time 36000;
78max-lease-time 86400;
79authoritative;
80subnet 192.0.2.0 netmask 255.255.255.0 {
81	range 192.0.2.10 192.0.2.10;
82	option routers 192.0.2.2;
83	option domain-name-servers 192.0.2.2;
84}
85EOF
86
87	# Start dhcp server
88	touch dhcpd.leases.conf
89	atf_check -e ignore ${dhcpd} -cf ./dhclient.dhcpd.conf -lf ./dhcpd.leases.conf -pf ./dhcpd.pid ${epair}a
90
91	# Expect that we get an IP assigned
92	atf_check -e match:'DHCPACK from 192.0.2.2' jexec dhclient_normal_test dhclient -c /dev/null -l ./lease.dhclient.test -p ./dhclient.test.pid ${epair}b
93
94	# And it's the correct one
95	atf_check -o match:'inet 192.0.2.10' jexec dhclient_normal_test ifconfig ${epair}b
96
97}
98
99normal_cleanup()
100{
101
102	generic_dhcp_cleanup
103}
104
105atf_test_case pcp cleanup
106pcp_head()
107{
108	atf_set descr 'test dhclient on pcp interface'
109	atf_set require.user root
110}
111
112pcp_body()
113{
114	dhcpd=$(which dhcpd)
115
116	if ! [ -x "$dhcpd" ]; then
117		atf_skip "ISC dhcp server (isc-dhcp44-server) not installed"
118	fi
119
120	vnet_init
121
122	epair=$(vnet_mkepair)
123
124	# Server side needs to be up to pass packets
125	ifconfig ${epair}a up
126
127	# Make sure necessary netgraph modules are loaded
128	kldstat -q -n ng_ether || kldload ng_ether
129	kldstat -q -n ng_iface || kldload ng_iface
130	kldstat -q -n ng_vlan || kldload ng_vlan
131
132	# create vlan, and attach epair to it (has incoming/outgoing vlan
133	# 0 tagged frames)
134	ngctl mkpeer ${epair}a: vlan lower downstream
135
136	# create new interface on other side of vlan (untagged/pcp)
137	ngctl mkpeer ${epair}a:lower. eiface vlan0 ether
138
139	# get the interface created
140	ngiface=$(ngctl show ${epair}a:lower.vlan0 | head -n 1 | awk '{ print $2}')
141
142	# schedule it for clean up
143	echo ${ngiface} >> ngctl.shutdown
144
145	# set the filter on it
146	ngctl msg ${epair}a:lower. 'addfilter { vlan=0 hook="vlan0" }'
147
148	vnet_mkjail dhclient_pcp_test ${epair}b
149
150	# Set IP on server iface
151	ifconfig ${ngiface} up 192.0.2.2/24
152
153	# Set pcp in jail
154	jexec dhclient_pcp_test ifconfig ${epair}b pcp 0 up
155
156	# Create dhcp server config
157	cat > dhclient.dhcpd.conf << EOF
158default-lease-time 36000;
159max-lease-time 86400;
160authoritative;
161subnet 192.0.2.0 netmask 255.255.255.0 {
162	range 192.0.2.10 192.0.2.10;
163	option routers 192.0.2.2;
164	option domain-name-servers 192.0.2.2;
165}
166EOF
167
168	# Start dhcp server
169	touch dhcpd.leases.conf
170	atf_check -e ignore ${dhcpd} -cf ./dhclient.dhcpd.conf -lf ./dhcpd.leases.conf -pf ./dhcpd.pid ${ngiface}
171
172	# Expect that we get an IP assigned
173	atf_check -e match:'DHCPACK from 192.0.2.2' jexec dhclient_pcp_test dhclient -c /dev/null -l ./lease.dhclient.test -p ./dhclient.test.pid ${epair}b
174
175	# And it's the correct one
176	atf_check -o match:'inet 192.0.2.10' jexec dhclient_pcp_test ifconfig ${epair}b
177}
178
179pcp_cleanup()
180{
181
182	generic_dhcp_cleanup
183
184	# Clean up netgraph nodes
185	for i in $(cat ngctl.shutdown); do
186		ngctl shutdown ${i}:
187	done
188	rm ngctl.shutdown
189}
190
191atf_init_test_cases()
192{
193	atf_add_test_case normal
194	atf_add_test_case pcp
195}
196
197