improve debug output in case downloadfile fails
authorDavid Kalnischkies <david@kalnischkies.de>
Mon, 10 Mar 2014 13:15:45 +0000 (14:15 +0100)
committerDavid Kalnischkies <david@kalnischkies.de>
Thu, 13 Mar 2014 12:58:46 +0000 (13:58 +0100)
Git-Dch: Ignore

cmdline/apt-helper.cc
test/integration/framework
test/integration/test-apt-https-no-redirect
test/integration/test-partial-file-support

index d66b3ff..37279ec 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <apt-private/acqprogress.h>
 #include <apt-private/private-output.h>
+#include <apt-private/private-download.h>
 #include <apt-private/private-cmndline.h>
 
 #include <iostream>
@@ -27,7 +28,6 @@
 
 #include <apti18n.h>
                                                                        /*}}}*/
-using namespace std;
 
 static bool DoDownloadFile(CommandLine &CmdL)
 {
@@ -43,23 +43,22 @@ static bool DoDownloadFile(CommandLine &CmdL)
    new pkgAcqFile(&Fetcher, download_uri, "", 0, "desc", "short-desc", 
                   "dest-dir-ignored", targetfile);
    Fetcher.Run();
-   if (!FileExists(targetfile))
-   {
-      _error->Error(_("Download Failed"));
-      return false;
-   }
+   bool Failed = false;
+   if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == false ||
+        FileExists(targetfile) == false)
+      return _error->Error(_("Download Failed"));
    return true;
 }
 
 static bool ShowHelp(CommandLine &)
 {
-   ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
+   ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
            COMMON_ARCH,__DATE__,__TIME__);
 
    if (_config->FindB("version") == true)
      return true;
 
-   cout << 
+   std::cout <<
     _("Usage: apt-helper [options] command\n"
       "       apt-helper [options] download-file uri target-path\n"
       "\n"
index bf6fa02..c623e62 100644 (file)
@@ -863,18 +863,16 @@ signreleasefiles() {
 
 webserverconfig() {
        msgtest "Set webserver config option '${1}' to" "$2"
-       downloadfile "http://localhost:8080/_config/set/${1}/${2}" '/dev/null' >/dev/null
-       local DOWNLOG='download-testfile.log'
-       rm -f "$DOWNLOG"
-       local STATUS="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverconfig.status"
-       downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
-       if [ "$(cat "$STATUS")" = '200' ]; then
+       local DOWNLOG='rootdir/tmp/download-testfile.log'
+       local STATUS='rootdir/tmp/webserverconfig.status'
+       rm -f "$STATUS" "$DOWNLOG"
+       if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
                msgpass
        else
-               cat >&2 "$DOWNLOG"
-               msgfail "Statuscode was $(cat "$STATUS")"
+               cat "$DOWNLOG" "$STATUS"
+               msgfail
        fi
-       rm "$STATUS"
+       testwebserverlaststatuscode '200'
 }
 
 rewritesourceslist() {
@@ -952,10 +950,10 @@ acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
 }
 
 downloadfile() {
-        PROTO="$(echo "$1" | cut -d':' -f 1)"
-        apthelper -o Acquire::https::CaInfo=${TESTDIR}/apt.pem \
-                  -o Debug::Acquire::${PROTO}=1 \
-                  download-file "$1" "$2" 2>&1
+       local PROTO="$(echo "$1" | cut -d':' -f 1 )"
+       apthelper -o Acquire::https::CaInfo=${TESTDIR}/apt.pem \
+               -o Debug::Acquire::${PROTO}=1 \
+               download-file "$1" "$2" 2>&1 || true
        # only if the file exists the download was successful
        if [ -e "$2" ]; then
                return 0
@@ -967,8 +965,8 @@ downloadfile() {
 checkdiff() {
        local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
        if [ -n "$DIFFTEXT" ]; then
-               echo
-               echo "$DIFFTEXT"
+               echo >&2
+               echo >&2 "$DIFFTEXT"
                return 1
        else
                return 0
@@ -1017,7 +1015,9 @@ testequalor2() {
        shift 2
        msgtest "Test for equality OR of" "$*"
        $* >$COMPAREAGAINST 2>&1 || true
-       if checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null || checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null; then
+       if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
+               checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
+       then
                msgpass
        else
                echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
@@ -1051,24 +1051,24 @@ testnopackage() {
        msgtest "Test for non-existent packages" "apt-cache show $*"
        local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
        if [ -n "$SHOWPKG" ]; then
-               echo
-               echo "$SHOWPKG"
+               echo >&2
+               echo >&2 "$SHOWPKG"
                msgfail
-               return 1
+       else
+               msgpass
        fi
-       msgpass
 }
 
 testdpkginstalled() {
        msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
        local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
        if [ "$PKGS" != $# ]; then
-               echo $PKGS
-               dpkg -l "$@" | grep '^[a-z]'
+               echo >&2 $PKGS
+               dpkg -l "$@" | grep '^[a-z]' >&2
                msgfail
-               return 1
+       else
+               msgpass
        fi
-       msgpass
 }
 
 testdpkgnotinstalled() {
@@ -1076,11 +1076,11 @@ testdpkgnotinstalled() {
        local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
        if [ "$PKGS" != 0 ]; then
                echo
-               dpkg -l "$@" | grep '^[a-z]'
+               dpkg -l "$@" | grep '^[a-z]' >&2
                msgfail
-               return 1
+       else
+               msgpass
        fi
-       msgpass
 }
 
 testmarkedauto() {
@@ -1105,8 +1105,8 @@ testsuccess() {
        if $@ >${OUTPUT} 2>&1; then
                msgpass
        else
-               echo
-               cat $OUTPUT
+               echo >&2
+               cat >&2 $OUTPUT
                msgfail
        fi
 }
@@ -1119,14 +1119,35 @@ testfailure() {
        fi
        local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
        if $@ >${OUTPUT} 2>&1; then
-               echo
-               cat $OUTPUT
+               echo >&2
+               cat >&2 $OUTPUT
                msgfail
        else
                msgpass
        fi
 }
 
+testwebserverlaststatuscode() {
+       local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
+       local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
+       rm -f "$DOWNLOG" "$STATUS"
+       msgtest 'Test last status code from the webserver was' "$1"
+       downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
+       if [ "$(cat "$STATUS")" = "$1" ]; then
+               msgpass
+       else
+               echo >&2
+               if [ -n "$2" ]; then
+                       shift
+                       echo >&2 '#### Additionally provided output files contain:'
+                       cat >&2 "$@"
+               fi
+               echo >&2 '#### Download log of the status code:'
+               cat >&2 "$DOWNLOG"
+               msgfail "Status was $(cat "$STATUS")"
+       fi
+}
+
 pause() {
        echo "STOPPED execution. Press enter to continue"
        local IGNORE
dissimilarity index 70%
index 0408c68..73352a2 100755 (executable)
@@ -1,24 +1,30 @@
-#!/bin/sh
-set -e
-
-TESTDIR=$(readlink -f $(dirname $0))
-. $TESTDIR/framework
-
-setupenvironment
-configarchitecture "i386"
-
-buildsimplenativepackage 'apt' 'all' '1.0' 'stable'
-setupaptarchive --no-update
-
-changetohttpswebserver  -o 'aptwebserver::redirect::replace::/redirectme/=http://localhost:8080/'
-
-msgtest 'normal http download works'
-downloadfile 'http://localhost:8080/pool/apt_1.0/changelog' changelog2 >/dev/null 2>/dev/null && msgpass || msgfail
-
-msgtest 'normal https download works'
-downloadfile 'https://localhost:4433/pool/apt_1.0/changelog' changelog >/dev/null 2>/dev/null && msgpass || msgfail
-
-msgtest 'redirecting https to http does not work'
-downloadfile 'https://localhost:4433/redirectme/pool/apt_1.0/changelog' changelog3 2>&1 | grep "Protocol http not supported or disabled in libcurl" > /dev/null && msgpass || msgfail
-
-
+#!/bin/sh
+set -e
+
+TESTDIR=$(readlink -f $(dirname $0))
+. $TESTDIR/framework
+
+setupenvironment
+configarchitecture "i386"
+
+insertpackage 'stable' 'apt' 'all' '1'
+setupaptarchive --no-update
+
+echo 'alright' > aptarchive/working
+changetohttpswebserver  -o 'aptwebserver::redirect::replace::/redirectme/=http://localhost:8080/'
+
+msgtest 'download of a file works via' 'http'
+downloadfile 'http://localhost:8080/working' httpfile >/dev/null 2>&1 && msgpass || msgfail
+testfileequal httpfile 'alright'
+
+msgtest 'download of a file works via' 'https'
+downloadfile 'https://localhost:4433/working' httpsfile >/dev/null 2>&1 && msgpass || msgfail
+testfileequal httpsfile 'alright'
+
+msgtest 'download of a file does not work if' 'https redirected to http'
+downloadfile 'https://localhost:4433/redirectme/working' redirectfile >curloutput 2>&1 && msgfail || msgpass
+
+msgtest 'libcurl has forbidden access in last request to' 'http resource'
+grep -q -- 'Protocol http not supported or disabled in libcurl' curloutput && msgpass || msgfail
+
+
index 382789e..5ab326d 100755 (executable)
@@ -13,17 +13,18 @@ copysource() {
        touch -d "$(stat --format '%y' "${TESTFILE}")" "$3"
 }
 
+DOWNLOADLOG='rootdir/tmp/testdownloadfile.log'
+
 testdownloadfile() {
-       local DOWNLOG='download-testfile.log'
-       rm -f "$DOWNLOG"
+       rm -f "$DOWNLOADLOG"
        msgtest "Testing download of file $2 with" "$1"
-       if ! downloadfile "$2" "$3" > "$DOWNLOG"; then
-               cat >&2 "$DOWNLOG"
+       if ! downloadfile "$2" "$3" > "$DOWNLOADLOG"; then
+               cat >&2 "$DOWNLOADLOG"
                msgfail
        else
                msgpass
        fi
-       cat "$DOWNLOG" | while read field hash; do
+       cat "$DOWNLOADLOG" | while read field hash; do
                local EXPECTED
                case "$field" in
                'MD5Sum-Hash:') EXPECTED="$(md5sum "$TESTFILE" | cut -d' ' -f 1)";;
@@ -40,28 +41,12 @@ testdownloadfile() {
                if [ "$EXPECTED" "$4" "$hash" ]; then
                        msgpass
                else
-                       cat >&2 "$DOWNLOG"
+                       cat >&2 "$DOWNLOADLOG"
                        msgfail "expected: $EXPECTED ; got: $hash"
                fi
        done
 }
 
-testwebserverlaststatuscode() {
-       local DOWNLOG='download-testfile.log'
-       rm -f "$DOWNLOG"
-       local STATUS="$(mktemp)"
-       addtrap "rm $STATUS;"
-       msgtest 'Test last status code from the webserver was' "$1"
-       downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
-       if [ "$(cat "$STATUS")" = "$1" ]; then
-               msgpass
-       else
-               cat >&2 "$DOWNLOG"
-               msgfail "Status was $(cat "$STATUS")"
-       fi
-}
-
-
 TESTFILE='aptarchive/testfile'
 cp -a ${TESTDIR}/framework $TESTFILE
 
@@ -70,34 +55,34 @@ testrun() {
 
        copysource $TESTFILE 0 ./testfile
        testdownloadfile 'no data' "${1}/testfile" './testfile' '='
-       testwebserverlaststatuscode '200'
+       testwebserverlaststatuscode '200' "$DOWNLOADLOG"
 
        copysource $TESTFILE 20 ./testfile
        testdownloadfile 'valid partial data' "${1}/testfile" './testfile' '='
-       testwebserverlaststatuscode '206'
+       testwebserverlaststatuscode '206' "$DOWNLOADLOG"
 
        copysource /dev/zero 20 ./testfile
        testdownloadfile 'invalid partial data' "${1}/testfile" './testfile' '!='
-       testwebserverlaststatuscode '206'
+       testwebserverlaststatuscode '206' "$DOWNLOADLOG"
 
        copysource $TESTFILE 1M ./testfile
        testdownloadfile 'completely downloaded file' "${1}/testfile" './testfile' '='
-       testwebserverlaststatuscode '416'
+       testwebserverlaststatuscode '416' "$DOWNLOADLOG"
 
        copysource /dev/zero 1M ./testfile
        testdownloadfile 'too-big partial file' "${1}/testfile" './testfile' '='
-       testwebserverlaststatuscode '200'
+       testwebserverlaststatuscode '200' "$DOWNLOADLOG"
 
        copysource /dev/zero 20 ./testfile
        touch ./testfile
        testdownloadfile 'old data' "${1}/testfile" './testfile' '='
-       testwebserverlaststatuscode '200'
+       testwebserverlaststatuscode '200' "$DOWNLOADLOG"
 
        webserverconfig 'aptwebserver::support::range' 'false'
 
        copysource $TESTFILE 20 ./testfile
        testdownloadfile 'no server support' "${1}/testfile" './testfile' '='
-       testwebserverlaststatuscode '200'
+       testwebserverlaststatuscode '200' "$DOWNLOADLOG"
 }
 
 testrun 'http://localhost:8080'