Skip to content
runtests.pl 47.2 KiB
Newer Older
Daniel Stenberg's avatar
Daniel Stenberg committed
    if($listonly) {
        return 0; # look successful
    }

    my @codepieces = getpart("client", "tool");

    my $tool="";
    if(@codepieces) {
        $tool = $codepieces[0];
        chomp $tool;
    }

Daniel Stenberg's avatar
Daniel Stenberg committed
    # remove previous server output logfile
    unlink($SERVERIN);

    if(@ftpservercmd) {
        # write the instructions to file
        writearray($FTPDCMD, \@ftpservercmd);
    }

    my (@setenv)= getpart("client", "setenv");
    my @envs;

    my $s;
    for $s (@setenv) {
        chomp $s; # cut off the newline

        subVariables \$s;

        if($s =~ /([^=]*)=(.*)/) {
            my ($var, $content)=($1, $2);
            $ENV{$var}=$content;
            # remember which, so that we can clear them afterwards!
            push @envs, $var;
        }
    }

    # get the command line options to use
    my ($cmd, @blaha)= getpart("client", "command");

    # make some nice replace operations
    $cmd =~ s/\n//g; # no newlines please
Daniel Stenberg's avatar
Daniel Stenberg committed
    # substitute variables in the command line
    my @inputfile=getpart("client", "file");
    if(@inputfile) {
        # we need to generate a file before this test is invoked
        my %hash = getpartattr("client", "file");

        my $filename=$hash{'name'};
        if(!$filename) {
            print "ERROR: section client=>file has no name attribute!\n";
            exit;
        }
        my $fileContent = join('', @inputfile);
        subVariables \$fileContent;
#        print "DEBUG: writing file " . $filename . "\n";
        open OUTFILE, ">$filename";
        binmode OUTFILE; # for crapage systems, use binary
    my %cmdhash = getpartattr("client", "command");

Daniel Stenberg's avatar
Daniel Stenberg committed
    if($cmdhash{'option'} !~ /no-output/) {
        #We may slap on --output!
        if (!@validstdout) {
            $out=" --output $CURLOUT ";
    my $cmdargs;
    if(!$tool) {
        # run curl, add -v for debug information output
        $cmdargs ="$out --include -v $cmd";
    }
    else {
        $cmdargs = " $cmd"; # $cmd is the command line for the test file
        $CURLOUT = $STDOUT; # sends received data to stdout
    }
    my @stdintest = getpart("client", "stdin");

    if(@stdintest) {
        my $stdinfile="$LOGDIR/stdin-for-$testnum";
        writearray($stdinfile, \@stdintest);

        $cmdargs .= " <$stdinfile";
    if(!$tool) {
        $CMDLINE="$CURL";
    else {
        $CMDLINE="$LIBDIR/$tool";
        $CMDLINE = "valgrind ".$valgrind_tool."--leak-check=yes --logfile=log/valgrind$testnum -q $CMDLINE";
Daniel Stenberg's avatar
Daniel Stenberg committed
    $CMDLINE .= "$cmdargs >>$STDOUT 2>>$STDERR";
        print "$CMDLINE\n";
    my @precommand= getpart("client", "precommand");
    if($precommand[0]) {
        # this is pure perl to eval!
        my $code = join("", @precommand);
        eval $code;
        if($@) {
            print "perl: $code\n";
            print "precommand: $@";
            exit;
        }
    }
    if($gdbthis) {
        open(GDBCMD, ">log/gdbcmd");
        print GDBCMD "set args $cmdargs\n";
        print GDBCMD "show args\n";
        close(GDBCMD);
    }
    # run the command line we built
    if ($torture) {
        return torture($CMDLINE,
                       "gdb --directory libtest $DBGCURL -x log/gdbcmd");
        system("gdb --directory libtest $DBGCURL -x log/gdbcmd");
        $cmdres=0; # makes it always continue after a debugged run
        $cmdres = system("$CMDLINE");
        my $signal_num  = $cmdres & 127;

        if(!$anyway && ($signal_num || $dumped_core)) {
    if(!$dumped_core) {
        if(-r "core") {
            # there's core file present now!
            $dumped_core = 1;
        }
    }

    if($dumped_core) {
        print "core dumped!\n";
Daniel Stenberg's avatar
Daniel Stenberg committed
        if(0 && $gdb) {
            print "running gdb for post-mortem analysis:\n";
            open(GDBCMD, ">log/gdbcmd2");
            print GDBCMD "bt\n";
            close(GDBCMD);
            system("gdb --directory libtest -x log/gdbcmd2 -batch $DBGCURL core ");
     #       unlink("log/gdbcmd2");
        }
    }
    # remove the special FTP command file after each test!
    unlink($FTPDCMD);

    my @err = getpart("verify", "errorcode");
    my $errorcode = $err[0];
    if (@validstdout) {
        # verify redirected stdout
        my @actual = loadarray($STDOUT);
        $res = compare("stdout", \@actual, \@validstdout);
        if(!$short) {
            print " stdout OK";
    my %replyattr = getpartattr("reply", "data");
    if(!$replyattr{'nocheck'} && (@reply || $replyattr{'sendzero'})) {
        # verify the received data
        my @out = loadarray($CURLOUT);
        $res = compare("data", \@out, \@reply);
        if(!$short) {
            print " data OK";
    if(@upload) {
        # verify uploaded data
        my @out = loadarray("$LOGDIR/upload.$testnum");
        $res = compare("upload", \@out, \@upload);
        if ($res) {
            return 1;
        if(!$short) {
            print " upload OK";
    if(@protocol) {
        my @out;
        my $retry = 5;

        # Verify the sent request. Sometimes, like in test 513 on some hosts,
        # curl will return back faster than the server writes down the request
        # to its file, so we might need to wait here for a while to see if the
        # file gets written a bit later.

        while($retry--) {
            @out = loadarray($SERVERIN);

            if(!$out[0]) {
                # nothing there yet, wait a while and try again
                sleep(1);
            }
        }
        # what to cut off from the live protocol sent by curl
        my @strip = getpart("verify", "strip");
Daniel Stenberg's avatar
Daniel Stenberg committed
        my @protstrip=@protocol;

        # check if there's any attributes on the verify/protocol section
        my %hash = getpartattr("verify", "protocol");

        if($hash{'nonewline'}) {
            # Yes, we must cut off the final newline from the final line
            # of the protocol data
            chomp($protstrip[$#protstrip]);
        }

Daniel Stenberg's avatar
Daniel Stenberg committed
        for(@strip) {
            # strip off all lines that match the patterns from both arrays
Daniel Stenberg's avatar
Daniel Stenberg committed
            @out = striparray( $_, \@out);
            @protstrip= striparray( $_, \@protstrip);
        }
Daniel Stenberg's avatar
Daniel Stenberg committed
        # what parts to cut off from the protocol
        my @strippart = getpart("verify", "strippart");
        my $strip;
        for $strip (@strippart) {
            chomp $strip;
            for(@out) {
                eval $strip;
            }
        }

        $res = compare("protocol", \@out, \@protstrip);
        if($res) {
            return 1;
        }
        if(!$short) {
            print " protocol OK";
    my @outfile=getpart("verify", "file");
    if(@outfile) {
        # we're supposed to verify a dynamicly generated file!
        my %hash = getpartattr("verify", "file");

        my $filename=$hash{'name'};
        if(!$filename) {
            print "ERROR: section verify=>file has no name attribute!\n";
            exit;
        }
        my @generated=loadarray($filename);

        $res = compare("output", \@generated, \@outfile);
        if($res) {
            return 1;
        }
        if(!$short) {
            print " output OK";
    if($errorcode == $cmdres) {
        $errorcode =~ s/\n//;
        if($verbose) {
            print " received exitcode $errorcode OK";
            print "\ncurl returned $cmdres, ".(0+$errorcode)." was expected\n";
    # the test succeeded, remove all log files
        cleardir($LOGDIR);

    unlink($FTPDCMD); # remove the instructions for this test

    @what = getpart("client", "killserver");
    for(@what) {
        my $serv = $_;
        chomp $serv;
        if($run{$serv}) {
            stopserver($run{$serv}); # the pid file is in the hash table
        }
        else {
            print STDERR "RUN: The $serv server is not running\n";
        }
    }

        if(! -f $memdump) {
            print "\n** ALERT! memory debuggin without any output file?\n";
        }
        else {
            my @memdata=`$memanalyze $memdump`;
                if($_ ne "") {
                    # well it could be other memory problems as well, but
                    # we call it leak for short here
                print "\n** MEMORY FAILURE\n";
    }
    if($valgrind) {
        opendir(DIR, "log") ||
            return 0; # can't open log dir
        my @files = readdir(DIR);
        closedir DIR;
        my $f;
        my $l;
        foreach $f (@files) {
            if($f =~ /^valgrind$testnum/) {
                $l = $f;
                last;
            }
        }
        my $leak;
        open(VAL, "<$l");
        while(<VAL>) {
            if($_ =~ /definitely lost: (\d*) bytes/) {
                $leak = $1;
                last;
            }
        }
        close(VAL);
        if($leak) {
            print " valgrind ERROR ";
        }
        elsif(!$short) {
            print " valgrind OK";
        }

Daniel Stenberg's avatar
Daniel Stenberg committed
    if($short) {
        print "OK";
    }
#######################################################################
# Stop all running test servers
sub stopservers {
    print "Shutting down test suite servers:\n" if ($verbose);
        printf ("* kill pid for %-5s => %-5d\n", $_, $run{$_}) if($verbose);
        stopserver($run{$_}); # the pid file is in the hash table
#######################################################################
# startservers() starts all the named servers
#
sub startservers {
    my @what = @_;
    my $pid;
    for(@what) {
        my $what = lc($_);
        $what =~ s/[^a-z]//g;
        if($what eq "ftp") {
            if(!$run{'ftp'}) {
                $pid = runftpserver($verbose);
                if($pid <= 0) {
                printf ("* pid ftp => %-5d\n", $pid) if($verbose);
            }
        }
        elsif($what eq "http") {
            if(!$run{'http'}) {
                $pid = runhttpserver($verbose);
                if($pid <= 0) {
                printf ("* pid http => %-5d\n", $pid) if($verbose);
            }
        }
        elsif($what eq "ftps") {
                # we can't run ftps tests without stunnel
                # or if libcurl is SSL-less
                $pid = runftpserver($verbose);
                if($pid <= 0) {
                printf ("* pid ftp => %-5d\n", $pid) if($verbose);
            if(!$run{'ftps'}) {
                $pid = runftpsserver($verbose);
                if($pid <= 0) {
                    return 2;
                }
                printf ("* pid ftps => %-5d\n", $pid) if($verbose);
        elsif($what eq "file") {
            # we support it but have no server!
        }
        elsif($what eq "https") {
                # we can't run https tests without stunnel
                # or if libcurl is SSL-less
                return 1;
            }
                $pid = runhttpserver($verbose);
                if($pid <= 0) {
                printf ("* pid http => %-5d\n", $pid) if($verbose);
            if(!$run{'https'}) {
                $pid = runhttpsserver($verbose);
                if($pid <= 0) {
                    return 2;
                }
                printf ("* pid https => %-5d\n", $pid) if($verbose);
Daniel Stenberg's avatar
Daniel Stenberg committed
        elsif($what eq "none") {
            print "* starts no server\n" if ($verbose);
        else {
            warn "we don't support a server for $what";
    return 0;
}

##############################################################################
# This function makes sure the right set of server is running for the
# specified test case. This is a useful design when we run single tests as not
# all servers need to run then!
#
# Returns:
# 100 if this is not a test case
# 99  if this test case has no servers specified
# 3   if this test is skipped due to no FTPS server
# 2   if one of the required servers couldn't be started
# 1   if this test is skipped due to no HTTPS server

sub serverfortest {
    my ($testnum)=@_;

    # load the test case file definition
    if(loadtest("${TESTDIR}/test${testnum}")) {
        if($verbose) {
            # this is not a test
            print "$testnum doesn't look like a test case!\n";
        }
        return 100;
    }

    my @what = getpart("client", "server");

    if(!$what[0]) {
        warn "Test case $testnum has no server(s) specified!";
        return 99;
    }

    return &startservers(@what);

#######################################################################
# Check options to this test program
#

do {
    if ($ARGV[0] eq "-v") {
        # verbose output
        $verbose=1;
    }
        # use this path to curl instead of default
        # have the servers display protocol output
    elsif ($ARGV[0] eq "-g") {
        # run this test with gdb
        $gdbthis=1;
    }
    elsif($ARGV[0] eq "-s") {
        # short output
        $short=1;
    }
    elsif($ARGV[0] eq "-n") {
        # no valgrind
        undef $valgrind;
    }
    elsif($ARGV[0] =~ /^-t(.*)/) {
        # torture
        $torture=1;
        my $xtra = $1;
        if($xtra =~ s/(\d+)$//) {
            $tortalloc = $1;
        # we undef valgrind to make this fly in comparison
        undef $valgrind;
Daniel Stenberg's avatar
Daniel Stenberg committed
    elsif($ARGV[0] eq "-a") {
        # continue anyway, even if a test fail
        $anyway=1;
    }
Daniel Stenberg's avatar
Daniel Stenberg committed
    elsif($ARGV[0] eq "-l") {
        # lists the test case names only
        $listonly=1;
    }
    elsif($ARGV[0] eq "-k") {
        # keep stdout and stderr files after tests
        $keepoutfiles=1;
    }
Daniel Stenberg's avatar
Daniel Stenberg committed
    elsif($ARGV[0] eq "-h") {
        # show help text
        print <<EOHELP
Usage: runtests.pl [options]
Daniel Stenberg's avatar
Daniel Stenberg committed
  -a       continue even if a test fails
  -g       run the test case with gdb
Daniel Stenberg's avatar
Daniel Stenberg committed
  -h       this help text
  -k       keep stdout and stderr files present after tests
Daniel Stenberg's avatar
Daniel Stenberg committed
  -l       list all test case names/descriptions
  -n       No valgrind
  -p       Print log file contents when a test fails
Daniel Stenberg's avatar
Daniel Stenberg committed
  -s       short output
Daniel Stenberg's avatar
Daniel Stenberg committed
  -v       verbose output
  [num]    like "5 6 9" or " 5 to 22 " to run those tests only
        $number = $1;
        if($fromnum >= 0) {
            for($fromnum .. $number) {
                push @testthis, $_;
            }
            $fromnum = -1;
        }
        else {
            push @testthis, $1;
        }
    }
    elsif($ARGV[0] =~ /^to$/i) {
        $fromnum = $number+1;
if($testthis[0] ne "") {
    $TESTCASES=join(" ", @testthis);
}

if($valgrind) {
    # we have found valgrind on the host, use it

    # verify that we can invoke it fine
    my $code = system("valgrind >/dev/null 2>&1");

    if(($code>>8) != 1) {
        #print "Valgrind failure, disable it\n";
        undef $valgrind;
    }
}

$HTTPPORT =  $base + 0; # HTTP server port
$HTTPSPORT = $base + 1; # HTTPS server port
$FTPPORT =   $base + 2; # FTP server port
$FTPSPORT =  $base + 3; # FTPS server port

#######################################################################
# Output curl version and host info being tested
#

if(!$listonly) {

#######################################################################
# clear and create logging directory:
cleardir($LOGDIR);
mkdir($LOGDIR, 0777);

#######################################################################
# If 'all' tests are requested, find out all test numbers
#

if ( $TESTCASES eq "all") {
    # Get all commands and find out their test numbers
    opendir(DIR, $TESTDIR) || die "can't opendir $TESTDIR: $!";
    my @cmds = grep { /^test([0-9]+)$/ && -f "$TESTDIR/$_" } readdir(DIR);
    $TESTCASES=""; # start with no test cases

    # cut off everything but the digits
    }
    # the the numbers from low to high
    for(sort { $a <=> $b } @cmds) {
#######################################################################
# Start the command line log
#
open(CMDLOG, ">$CURLLOG") ||
    print "can't log command lines to $CURLLOG\n";

#######################################################################

sub displaylogcontent {
    my ($file)=@_;
    open(SINGLE, "<$file");
    while(<SINGLE>) {
        print " $_";
    }
    close(SINGLE);
}

sub displaylogs {
    opendir(DIR, "$LOGDIR") ||
        die "can't open dir: $!";
    my @logs = readdir(DIR);
    closedir DIR;
    my $log;
    print "== Contents of files in the log/ dir after test $testnum\n";
        # the log file is not "." or ".." and contains more than zero bytes
        if(($log !~ /\.(\.|)$/) && -s "$LOGDIR/$log") {
            displaylogcontent("$LOGDIR/$log");
#######################################################################
# The main test-loop
#

Daniel Stenberg's avatar
Daniel Stenberg committed
my $testnum;
foreach $testnum (split(" ", $TESTCASES)) {

    $lasttest = $testnum if($testnum > $lasttest);

        $failed.= "$testnum ";
        if($postmortem) {
            # display all files in log/ in a nice way
        if(!$anyway) {
            # a test failed, abort
            print "\n - abort tests\n";
            last;
        }
#######################################################################
# Close command log
#
close(CMDLOG);

# Tests done, stop the servers
stopservers();
    printf("TESTDONE: $ok tests out of $total reported OK: %d%%\n",
        print "TESTFAIL: These test cases failed: $failed\n";
    print "TESTFAIL: No tests were performed!\n";

if($all) {
    print "TESTDONE: $all tests were considered.\n";
}

if($skipped) {
    my $s=0;
    print "TESTINFO: $skipped tests were skipped due to these restraints:\n";

    for(keys %skipped) {
        my $r = $_;
        printf "TESTINFO: \"%s\" %d times (", $r, $skipped{$_};

        # now show all test case numbers that had this reason for being
        # skipped
        my $c=0;
        for(0 .. $lasttest) {
            my $t = $_;
            if($teststat[$_] eq $r) {
                print ", " if($c);
                print $_;
                $c++;
            }
        }
        print ")\n";
    }
if($total && ($ok != $total)) {
    exit 1;
}