aboutsummaryrefslogtreecommitdiffstats
path: root/tests/TESTonce
blob: 425abaab36d16f65f79bc30a74dba600e9de7845 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env perl

$TCPDUMP = "./tcpdump";
if ($^O eq 'MSWin32') {
    $TCPDUMP = "windump";
}
if($ENV{TCPDUMP_BIN}) {
    $TCPDUMP = $ENV{TCPDUMP_BIN};
}

use File::Basename;
use POSIX qw( WEXITSTATUS WIFEXITED);

system("mkdir -p tests/NEW tests/DIFF");

if(@ARGV != 4) {
  print "Usage: TESTonce name input output options\n";
  exit 20;
}

$name=$ARGV[0];
$input=$ARGV[1];
$output=$ARGV[2];
$options=$ARGV[3];

my $r;
my $debug = 0; # change to suit.

$outputbase = basename($output);
my $coredump = false;
my $status = 0;
my $linecount = 0;
my $rawstderrlog = "tests/NEW/${outputbase}.raw.stderr";
my $stderrlog = "tests/NEW/${outputbase}.stderr";
my $diffstat = 0;
my $errdiffstat = 0;
my $cmd;

if ($^O eq 'MSWin32') {
    $r = system ".\\${TCPDUMP} -t -n -r $input $options 2>NUL | sed 's/\\r//' | tee tests/NEW/${outputbase} | diff $output - >tests/DIFF/${outputbase}.diff";
    # need to do same as below for Cygwin.
}
else {
    # we used to do this as a nice pipeline, but the problem is that $r fails to
    # to be set properly if the tcpdump core dumps.
    $cmd = "$TCPDUMP 2>${rawstderrlog} -t -n -r $input $options >tests/NEW/${outputbase}";
    print "CMD: $cmd\n" if $debug;
    $r = system $cmd;
    if($r == -1) {
        # failed to start due to error.
        $status = $!;
    }
    if($r != 0) {
        $coredump = false;
        $status = 0;
        # this means tcpdump failed, which however, might be expected.
        open(OUTPUT, ">>"."tests/NEW/${outputbase}") || die "fail to open ${outputbase}\n";
        if( $r & 128 ) {
            $coredump = $r & 127;
        }
        if( WIFEXITED($r)) {
            $status = WEXITSTATUS($r);
        }

        if($coredump || $status) {
            printf OUTPUT "\nEXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
        } else {
            printf OUTPUT "\nEXIT CODE %08x\n", $r;
        }
        close(OUTPUT);
        $r = 0;  # clear the error so that the diff will occur.
    }
    print "RUNNING DIFF after ${r}\n" if $debug;

    # always run diff.
    $cmd = "cat tests/NEW/${outputbase} | diff $output - >tests/DIFF/${outputbase}.diff";
    print "RUNNING: $cmd\n" if $debug;
    $r = system $cmd;
    if(WIFEXITED($r)) {
        $diffstat = WEXITSTATUS($r);
    }

    #system("/bin/sh");

    # process the file, sanitize "reading from" line, and count lines
    $linecount = 0;
    open(ERRORRAW, "<" . $rawstderrlog);
    open(ERROROUT, ">" . $stderrlog);
    while(<ERRORRAW>) {
        next if /^\s*$/;  # blank lines are boring
        if(/^(reading from file )(.*)(,.*)$/) {
            my $filename = basename($2);
            print ERROROUT "${1}${filename}${3}\n";
            next;
        }
        print ERROROUT;
        $linecount++;
    }
    close(ERROROUT);
    close(ERRORRAW);

    if ( -f "$output.stderr" ) {
        $nr = system "cat $stderrlog | diff $output.stderr - >tests/DIFF/$outputbase.stderr.diff";
        if($r == 0) {
            $r = $nr;
        }
        $errdiffstat = WEXITSTATUS($nr);
    } else {
        $errdiffstat = "-"
    }

    if($r == 0) {
        if($linecount == 0 && $status == 0) {
            print "UNLINK: ${stderrlog}\n" if $debug;
            unlink($stderrlog) unless $debug;
        } else {
            $errdiffstat = "+";
        }
    }

    print sprintf("END: %08x\n", $r) if $debug;
}

if($r == 0) {
    my $stderrlog="";
    if($linecount > 0 && $errdiffstat != "-") {
        $stderrlog=sprintf("-- %d lines extra in stderr", $linecount);
    }
    if(!defined($ENV{"SKIPPASSED"})) {
        printf "    %-35s: passed%s\n", $name, $stderrlog;
    }
    unlink "tests/DIFF/$outputbase.diff" unless $debug;
    exit 0;
}
# must have failed!
printf "    %-35s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
open FOUT, '>>tests/failure-outputs.txt';
printf FOUT "\nFailed test: $name\n\n";
close FOUT;
if(-f "tests/DIFF/$outputbase.diff") {
    system "cat tests/DIFF/$outputbase.diff >> tests/failure-outputs.txt";
}

if($r == -1) {
  print " (failed to execute: $!)\n";
  exit 30;
}

# this is not working right, $r == 0x8b00 when there is a core dump.
# clearly, we need some platform specific perl magic to take this apart, so look for "core"
# too.
# In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
# a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
if($r & 127 || -f "core") {
    my $with = ($r & 128) ? 'with' : 'without';
    if(-f "core") {
        $with = "with";
    }
    printf " (terminated with signal %u, %s coredump)\n", ($r & 127), $with;
    exit ($r & 128) ? 10 : 20;
}
print "\n";
exit $r >> 8;