#!/bin/bash
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/lftp/Regression/bz732863-IPv6-to-IPv4-fallback
# Description: Test if lftp tries to connect over IPv4 when IPv6 not available
# Author: Martin Frodl <mfrodl@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2015 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1
PACKAGES=${PACKAGES:-"lftp"}
REQUIRES=${REQUIRES:-"httpd"}
TESTDIR='bz732863'
IP4='3.14.15.92'
IP6='3:14:15:92:65:35:89:79'
rlJournalStart
rlPhaseStartSetup
rlRun "rlImport httpd/http" 0 "Importing httpd library" || rlDie
rlAssertRpm --all
rlRun "ip addr add ${IP4} dev lo" 0 "Adding IP ${IP4} to lo interface"
rlRun "ip route add ${IP4} dev lo" 0 "${IP4} will be routed via lo"
rlRun "ip route add ${IP6} dev lo" 0 "${IP6} will be routed via lo"
HOSTS="/etc/hosts"
rlRun "rlFileBackup ${HOSTS}"
rlRun "echo '${IP4} italmas' >> ${HOSTS}" 0 "Adding IPv4 entry to hosts"
rlRun "echo '${IP6} italmas' >> ${HOSTS}" 0 "Adding IPv6 entry to hosts"
rlRun "rlFileBackup --clean ${httpROOTDIR}"
rlRun "mkdir ${httpROOTDIR}/${TESTDIR}" 0 "Creating test directory"
rlServiceStop "httpd"
sleep 10
rlRun "httpStart" 0 "Starting httpd"
rlPhaseEnd
rlPhaseStartTest
# Make sure that 3.14.15.92 is reachable via loopback interface, whereas
# 3:14:15:92:65:35:89:79 is not
rlRun "ping -w 5 ${IP4}" 0 "Trying to contact local server over IPv4"
rlRun "ping6 -w 5 ${IP6}" 1,2 "Trying to contact local server over IPv6"
URL="http://italmas/${TESTDIR}/"
rlRun -s "lftp -de exit ${URL}" 0 "Connecting to localhost with lftp"
rlAssertGrep 'Network is unreachable' ${rlRun_LOG}
rlAssertGrep "200 OK" ${rlRun_LOG}
rlIsRHEL 6 && rlAssertGrep "cd ok, cwd=/${TESTDIR}" ${rlRun_LOG}
rlPhaseEnd
rlPhaseStartCleanup
rlServiceStop "httpd"
rlRun "httpStop" 0 "Stopping httpd"
rlRun "rlFileRestore" 0 "Restoring original files"
rlRun "ip addr del ${IP4} dev lo" 0 \
"Removing IP ${IP4} from lo interace"
rlRun "ip route del ${IP4}" 0 "Deleting routing entry for ${IP4}"
rlRun "ip route del ${IP6}" 0 "Deleting routing entry for ${IP6}"
rlRun "rlServiceRestore ${httpHTTPD}" 0 "Restoring httpd service"
rlPhaseEnd
rlJournalPrintText
rlJournalEnd