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