c7e4ac6
#!/bin/bash
c7e4ac6
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
c7e4ac6
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c7e4ac6
#
c7e4ac6
#   runtest.sh of /CoreOS/curl/Sanity/scp-and-sftp-download-test
c7e4ac6
#   Description: downloads test file through scp and sftp
c7e4ac6
#   Author: Karel Srot <ksrot@redhat.com>
c7e4ac6
#
c7e4ac6
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c7e4ac6
#
c7e4ac6
#   Copyright (c) 2012 Red Hat, Inc. All rights reserved.
c7e4ac6
#
c7e4ac6
#   This copyrighted material is made available to anyone wishing
c7e4ac6
#   to use, modify, copy, or redistribute it subject to the terms
c7e4ac6
#   and conditions of the GNU General Public License version 2.
c7e4ac6
#
c7e4ac6
#   This program is distributed in the hope that it will be
c7e4ac6
#   useful, but WITHOUT ANY WARRANTY; without even the implied
c7e4ac6
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
c7e4ac6
#   PURPOSE. See the GNU General Public License for more details.
c7e4ac6
#
c7e4ac6
#   You should have received a copy of the GNU General Public
c7e4ac6
#   License along with this program; if not, write to the Free
c7e4ac6
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
c7e4ac6
#   Boston, MA 02110-1301, USA.
c7e4ac6
#
c7e4ac6
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c7e4ac6
c7e4ac6
# Include Beaker environment
1b982b3
. /usr/share/beakerlib/beakerlib.sh || exit 1
c7e4ac6
c7e4ac6
PACKAGE="curl"
c7e4ac6
c7e4ac6
# GLOBAL/ENVIRONMENT VARIABLE:
c7e4ac6
# PUBKEY_PARAM
c7e4ac6
c7e4ac6
if [ "$PUBKEY_PARAM" == 'none' ]; then
c7e4ac6
  PUBKEY_PARAM=""
c7e4ac6
elif [ "$PUBKEY_PARAM" == 'empty' ]; then
c7e4ac6
  PUBKEY_PARAM="--pubkey ''"
c7e4ac6
else
c7e4ac6
  PUBKEY_PARAM='--pubkey /root/.ssh/id_rsa.pub'
c7e4ac6
fi
c7e4ac6
c7e4ac6
FILESIZE=200 #MB
c7e4ac6
OPTIONS=""
c7e4ac6
rlIsRHEL 7 && OPTIONS="--insecure"
c7e4ac6
c7e4ac6
rlJournalStart
c7e4ac6
    rlPhaseStartSetup
c7e4ac6
        rlAssertRpm $PACKAGE
c7e4ac6
        rlFileBackup --clean  /root/.ssh/known_hosts /root/.ssh
c7e4ac6
        rlFileBackup --clean  /etc/ssh/sshd_config
c7e4ac6
        rlRun "useradd -m curltestuser"
c7e4ac6
c7e4ac6
        # In FIPS-140 we need to explicitly allow one of libssh2-implemented
c7e4ac6
        # Kex algorithms (eg. DH14-SHA1).
c7e4ac6
        rlRun "echo 'KexAlgorithms +diffie-hellman-group14-sha1' >> /etc/ssh/sshd_config" 0
c7e4ac6
        rlServiceStop "sshd"
c7e4ac6
        rlRun "service sshd start && sleep 5" 0
c7e4ac6
c7e4ac6
        # file for download test
c7e4ac6
        rlRun "su - curltestuser -c 'dd if=/dev/zero of=testfile bs=1M count=200'" 0 "Creating $FILESIZE MB large test file"
c7e4ac6
        SUM=`sha256sum /home/curltestuser/testfile | cut -d ' ' -f 1`
c7e4ac6
        rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory"
c7e4ac6
        rlRun "pushd $TmpDir"
c7e4ac6
        rlRun "rm -vf /root/.ssh/*"
c7e4ac6
        rlRun "ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ''" 0 "Generate ssh key"
c7e4ac6
        rlRun "mkdir /home/curltestuser/.ssh && cat /root/.ssh/id_rsa.pub > /home/curltestuser/.ssh/authorized_keys && chown -R curltestuser.curltestuser /home/curltestuser/.ssh/" 0 "Save the key to .ssh/authorized_keys"
c7e4ac6
c7e4ac6
        # this is a workaround as libssh2 is not able to use newer hashes
c7e4ac6
        #rlRun "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/root/.ssh/known_hosts curltestuser@localhost 'exit'" 0 "First ssh login to add localhost to known_hosts"
c7e4ac6
        rlRun "ssh-keyscan localhost >>/root/.ssh/known_hosts"
c7e4ac6
c7e4ac6
        # files for upload test
c7e4ac6
        rlRun "dd if=/dev/zero of=uploadfile1 bs=1M count=50" 0 "Creating 50 MB large test file"
c7e4ac6
        UPSUM1=`sha256sum uploadfile1 | cut -d ' ' -f 1`
c7e4ac6
        rlRun "dd if=/dev/zero of=uploadfile2 bs=1M count=20" 0 "Creating 20 MB large test file"
c7e4ac6
        UPSUM2=`sha256sum uploadfile2 | cut -d ' ' -f 1`
c7e4ac6
    rlPhaseEnd
c7e4ac6
c7e4ac6
    rlPhaseStartTest "scp download test"
c7e4ac6
        rlRun "curl -o ./scp_file -u curltestuser: --key /root/.ssh/id_rsa $PUBKEY_PARAM $OPTIONS scp://localhost/home/curltestuser/testfile" 0 "Initiate curl scp download"
c7e4ac6
        rlAssertExists scp_file
c7e4ac6
        SCPSUM=`sha256sum ./scp_file | cut -d ' ' -f 1`
c7e4ac6
        rlAssertEquals "Checking that whole file was properly downloaded" $SUM $SCPSUM
c7e4ac6
        rm -f ./scp_file
c7e4ac6
    rlPhaseEnd
c7e4ac6
c7e4ac6
    rlPhaseStartTest "sftp download test"
c7e4ac6
        rlRun "curl -o ./sftp_file -u curltestuser: --key /root/.ssh/id_rsa $PUBKEY_PARAM $OPTIONS sftp://localhost/home/curltestuser/testfile" 0 "Initiate curl scp download"
c7e4ac6
        rlAssertExists sftp_file
c7e4ac6
        SFTPSUM=`sha256sum ./sftp_file | cut -d ' ' -f 1`
c7e4ac6
        rlAssertEquals "Checking that whole file was properly downloaded" $SUM $SFTPSUM
c7e4ac6
        rm -f ./sftp_file
c7e4ac6
    rlPhaseEnd
c7e4ac6
c7e4ac6
    rlPhaseStartTest "scp upload test"
c7e4ac6
        rlRun "curl -T '{uploadfile1,uploadfile2}' scp://localhost/home/curltestuser/ -u curltestuser: --key /root/.ssh/id_rsa $PUBKEY_PARAM $OPTIONS" 0 "Initiate curl scp upload"
c7e4ac6
        rlAssertExists /home/curltestuser/uploadfile1
c7e4ac6
        rlAssertExists /home/curltestuser/uploadfile2
c7e4ac6
        SCPUPSUM1=`sha256sum /home/curltestuser/uploadfile1 | cut -d ' ' -f 1`
c7e4ac6
        SCPUPSUM2=`sha256sum /home/curltestuser/uploadfile2 | cut -d ' ' -f 1`
c7e4ac6
        rlAssertEquals "Checking that 1st file was properly uploaded" ${UPSUM1} ${SCPUPSUM1}
c7e4ac6
        rlAssertEquals "Checking that 2nd file was properly uploaded" ${UPSUM2} ${SCPUPSUM2}
c7e4ac6
        rm -f /home/curltestuser/uploadfile1 /home/curltestuser/uploadfile2
c7e4ac6
    rlPhaseEnd
c7e4ac6
c7e4ac6
    rlPhaseStartTest "sftp upload test"
c7e4ac6
        rlRun "curl -T '{uploadfile1,uploadfile2}' sftp://localhost/home/curltestuser/ -u curltestuser: --key /root/.ssh/id_rsa $PUBKEY_PARAM $OPTIONS" 0 "Initiate curl sftp upload"
c7e4ac6
        rlAssertExists /home/curltestuser/uploadfile1
c7e4ac6
        rlAssertExists /home/curltestuser/uploadfile2
c7e4ac6
        SFTPUPSUM1=`sha256sum /home/curltestuser/uploadfile1 | cut -d ' ' -f 1`
c7e4ac6
        SFTPUPSUM2=`sha256sum /home/curltestuser/uploadfile2 | cut -d ' ' -f 1`
c7e4ac6
        rlAssertEquals "Checking that 1st file was properly uploaded" ${UPSUM1} ${SFTPUPSUM1}
c7e4ac6
        rlAssertEquals "Checking that 2nd file was properly uploaded" ${UPSUM2} ${SFTPUPSUM2}
c7e4ac6
        rm -f /home/curltestuser/uploadfile1 /home/curltestuser/uploadfile2
c7e4ac6
    rlPhaseEnd
c7e4ac6
c7e4ac6
c7e4ac6
    rlPhaseStartCleanup
c7e4ac6
        rlRun "userdel -r --force curltestuser"
c7e4ac6
        rlRun "popd"
c7e4ac6
        rlRun "rm -r $TmpDir" 0 "Removing tmp directory"
c7e4ac6
        rlFileRestore
c7e4ac6
        rlServiceRestore "sshd"
c7e4ac6
    rlPhaseEnd
c7e4ac6
rlJournalPrintText
c7e4ac6
rlJournalEnd