#!/usr/bin/env atf-sh1#2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright 2022 John-Mark Gurney5# All rights reserved.6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions are9# met:10#11# * Redistributions of source code must retain the above copyright12# notice, this list of conditions and the following disclaimer.13# * Redistributions in binary form must reproduce the above copyright14# notice, this list of conditions and the following disclaimer in the15# documentation and/or other materials provided with the distribution.16#17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829#30# Run the tests:31# make WITH_TESTS=yes -j 4 all install && kyua test -k /usr/tests/Kyuafile sbin/dhclient/pcp32#33# Output last run:34# kyua report --verbose -r $(ls -tr ~/.kyua/store/results.*.db | tail -n 1)3536. $(atf_get_srcdir)/../../sys/common/vnet.subr3738generic_dhcp_cleanup()39{4041# clean up programs42kill $(cat dhclient.test.pid) $(cat dhcpd.pid)4344# clean up files45rm -f dhclient.dhcpd.conf lease.dhclient.test dhclient.test.pid4647vnet_cleanup48}4950atf_test_case normal cleanup51normal_head()52{53atf_set descr 'test dhclient against a server'54atf_set require.user root55}5657normal_body()58{59dhcpd=$(which dhcpd)6061if ! [ -x "$dhcpd" ]; then62atf_skip "ISC dhcp server (isc-dhcp44-server) not installed"63fi6465vnet_init6667epair=$(vnet_mkepair)6869vnet_mkjail dhclient_normal_test ${epair}b7071# Set IP on server iface72ifconfig ${epair}a 192.0.2.2/24 up7374# Create dhcp server config75cat > dhclient.dhcpd.conf << EOF76default-lease-time 36000;77max-lease-time 86400;78authoritative;79subnet 192.0.2.0 netmask 255.255.255.0 {80range 192.0.2.10 192.0.2.10;81option routers 192.0.2.2;82option domain-name-servers 192.0.2.2;83}84EOF8586# Start dhcp server87touch dhcpd.leases.conf88atf_check -e ignore ${dhcpd} -cf ./dhclient.dhcpd.conf -lf ./dhcpd.leases.conf -pf ./dhcpd.pid ${epair}a8990# Expect that we get an IP assigned91atf_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}b9293# And it's the correct one94atf_check -o match:'inet 192.0.2.10' jexec dhclient_normal_test ifconfig ${epair}b9596}9798normal_cleanup()99{100101generic_dhcp_cleanup102}103104atf_test_case pcp cleanup105pcp_head()106{107atf_set descr 'test dhclient on pcp interface'108atf_set require.user root109}110111pcp_body()112{113dhcpd=$(which dhcpd)114115if ! [ -x "$dhcpd" ]; then116atf_skip "ISC dhcp server (isc-dhcp44-server) not installed"117fi118119vnet_init120121epair=$(vnet_mkepair)122123# Server side needs to be up to pass packets124ifconfig ${epair}a up125126# Make sure necessary netgraph modules are loaded127kldstat -q -n ng_ether || kldload ng_ether128kldstat -q -n ng_iface || kldload ng_iface129kldstat -q -n ng_vlan || kldload ng_vlan130131# create vlan, and attach epair to it (has incoming/outgoing vlan132# 0 tagged frames)133ngctl mkpeer ${epair}a: vlan lower downstream134135# create new interface on other side of vlan (untagged/pcp)136ngctl mkpeer ${epair}a:lower. eiface vlan0 ether137138# get the interface created139ngiface=$(ngctl show ${epair}a:lower.vlan0 | head -n 1 | awk '{ print $2}')140141# schedule it for clean up142echo ${ngiface} >> ngctl.shutdown143144# set the filter on it145ngctl msg ${epair}a:lower. 'addfilter { vlan=0 hook="vlan0" }'146147vnet_mkjail dhclient_pcp_test ${epair}b148149# Set IP on server iface150ifconfig ${ngiface} up 192.0.2.2/24151152# Set pcp in jail153jexec dhclient_pcp_test ifconfig ${epair}b pcp 0 up154155# Create dhcp server config156cat > dhclient.dhcpd.conf << EOF157default-lease-time 36000;158max-lease-time 86400;159authoritative;160subnet 192.0.2.0 netmask 255.255.255.0 {161range 192.0.2.10 192.0.2.10;162option routers 192.0.2.2;163option domain-name-servers 192.0.2.2;164}165EOF166167# Start dhcp server168touch dhcpd.leases.conf169atf_check -e ignore ${dhcpd} -cf ./dhclient.dhcpd.conf -lf ./dhcpd.leases.conf -pf ./dhcpd.pid ${ngiface}170171# Expect that we get an IP assigned172atf_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}b173174# And it's the correct one175atf_check -o match:'inet 192.0.2.10' jexec dhclient_pcp_test ifconfig ${epair}b176}177178pcp_cleanup()179{180181generic_dhcp_cleanup182183# Clean up netgraph nodes184for i in $(cat ngctl.shutdown); do185ngctl shutdown ${i}:186done187rm -f ngctl.shutdown188}189190atf_init_test_cases()191{192atf_add_test_case normal193atf_add_test_case pcp194}195196197198