summaryrefslogtreecommitdiff
path: root/tests/TESTrun
blob: b668ac83ef3e52a8c19d5ac8fc14d07f6dd2e265 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env perl

use File::Basename;
use POSIX qw( WEXITSTATUS WIFEXITED);
use Cwd qw(abs_path getcwd);
use File::Path qw(mkpath);   # mkpath works with ancient perl, as well as newer perl
use Data::Dumper;            # for debugging.

# these are created in the directory where we are run, which might be
# a build directory.
my $newdir = "tests/NEW";
my $diffdir= "tests/DIFF";
mkpath($newdir);
mkpath($diffdir);
my $origdir = getcwd();
my $srcdir  = $ENV{'srcdir'} || ".";

#
# Force UTC, so time stamps are printed in a standard time zone, and
# tests don't have to be run in the time zone in which the output
# file was generated.
#
$ENV{'TZ'}='GMT0';

#
# Get the tests directory from $0.
#
my $testsdir = dirname($0);

#
# Convert it to an absolute path, so it works even after we do a cd.
#
$testsdir = abs_path($testsdir);
print "Running tests from ${testsdir}\n";

unshift(@INC, $testsdir);

$passedcount = 0;
$failedcount = 0;
#
my $failureoutput=$origdir . "/tests/failure-outputs.txt";

# truncate the output file
open(FAILUREOUTPUT, ">" . $failureoutput);
close(FAILUREOUTPUT);

$confighhash = undef;

sub runtest {
    my ($opt) = @_;
    my $r;

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

    #print Dumper($opt);
    my $testdir=$opt->{'testdir'};
    my $name = $opt->{'name'};
    my $output=$testdir ."/".$opt->{'output'};
    my $args = $opt->{'args'};

    if(defined($opt->{'program'})) {
        $program= $opt->{'program'};
    } else {
        # setup default arguments for tcpdump
        $args = $args . " -# -n"
    }

    if(defined($opt->{'input'})) {
        $args = $args . " -r ".$testdir."/".$opt->{'input'};
    }

    #print Dumper($args);
    my $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;

    if ($^O eq 'MSWin32') {
        $r = system "${program} $args 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.
        $r = system "${program} 2>${rawstderrlog} $args >tests/NEW/${outputbase}";
        if($r == -1) {
            # failed to start due to error.
            $status = $!;
        }
        if($r != 0) {
            $coredump = false;
            $status = 0;
            # this means tcpdump failed.
            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 "EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
            } else {
                printf OUTPUT "EXIT CODE %08x\n", $r;
            }
            close(OUTPUT);
            $r = 0;
        }
        if($r == 0) {
            $r = system "cat tests/NEW/$outputbase | diff $output - >tests/DIFF/$outputbase.diff";
            $diffstat = WEXITSTATUS($r);
        }

        # process the file, sanitize "reading from" line, and count lines
        $linecount = 0;
        open(ERRORRAW, "<" . $rawstderrlog);
        open(ERROROUT, ">" . $stderrlog);
        while(<ERRORRAW>) {
            next if /^$/;  # 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);
        }

        if($r == 0) {
            if($linecount == 0 && $status == 0) {
                unlink($stderrlog);
            } else {
                $errdiffstat = 1;
            }
        }

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

    if($r == 0) {
        if($linecount == 0) {
            printf "    %-40s: passed\n", $name;
        } else {
            printf "    %-40s: passed with error messages:\n", $name;
            system "cat $stderrlog";
        }
        unlink "tests/DIFF/$outputbase.diff";
        return 0;
    }
    # must have failed!
    printf "    %-40s: 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";
        return(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)", ($r & 127), $with;
        if($linecount == 0) {
            print "\n";
        } else {
            print " with error messages:\n";
            system "cat $stderrlog";
        }
        return(($r & 128) ? 10 : 20);
    }
    if($linecount == 0) {
        print "\n";
    } else {
        print " with error messages:\n";
        system "cat $stderrlog";
    }
}

sub loadconfighash {
    if(defined($confighhash)) {
        return $confighhash;
    }

    $main::confighhash = {};

    # this could be loaded once perhaps.
    open(CONFIG_H, "config.h") || die "Can not open config.h: $!\n";
    while(<CONFIG_H>) {
        chomp;
        if(/^\#define (.*) 1/) {
            #print "Setting $1\n";
            $main::confighhash->{$1} = 1;
        }
    }
    close(CONFIG_H);
    #print Dumper($main::confighhash);

    # also run tcpdump --fp-type to get the type of floating-point
    # arithmetic we're doing, setting a HAVE_{fptype} key based
    # on the value it prints
    open(FPTYPE_PIPE, "./tcpdump --fp-type |") or die("piping tcpdump --fp-type failed\n");
    my $fptype_val = <FPTYPE_PIPE>;
    close(FPTYPE_PIPE);
    my $have_fptype;
    if($fptype_val == "9877.895") {
        $have_fptype = "HAVE_FPTYPE1";
    } else {
        $have_fptype = "HAVE_FPTYPE2";
    }
    $main::confighhash->{$have_fptype} = 1;

    return $main::confighhash;
}


sub runOneComplexTest {
    local($testconfig) = @_;

    my $output = $testconfig->{output};
    my $input  = $testconfig->{input};
    my $name   = $testconfig->{name};
    my $args   = $testconfig->{args};
    my $foundit = 1;
    my $unfoundit=1;

    my $configset = $testconfig->{config_set};
    my $configunset = $testconfig->{config_unset};
    my $ch = loadconfighash();
    #print Dumper($ch);

    if(defined($configset)) {
        $foundit = ($ch->{$configset} == 1);
    }
    if(defined($configunset)) {
        $unfoundit=($ch->{$configunset} != 1);
    }

    if(!$foundit) {
        printf "    %-40s: skipped (%s not set)\n", $name, $configset;
        return 0;
    }

    if(!$unfoundit) {
        printf "    %-40s: skipped (%s set)\n", $name, $configunset;
        return 0;
    }

    #use Data::Dumper;
    #print Dumper($testconfig);

    # EXPAND any occurances of @TESTDIR@ to $testsdir
    $args =~ s/\@TESTDIR\@/$testsdir/;

    my $opt = {name => $name,
               testdir=>$testsdir,
               input=> $input,
               output=>$output,
               args => $args};

    if(defined($testconfig->{program})) {
        $opt->{'program'} = $testconfig->{program};
    }

    my $result = runtest($opt);

    if($result == 0) {
        $passedcount++;
    } else {
        $failedcount++;
    }
}

# *.tests files are PERL hash definitions.  They should create an array of hashes
# one per test, and place it into the variable @testlist.
sub runComplexTests {
    my @files = glob( $testsdir . '/*.tests' );
    foreach $file (@files) {
        my @testlist = undef;
        my $definitions;
        print "FILE: ${file}\n";
        open(FILE, "<".$file) || die "can not open $file: $!";
        {
            local $/ = undef;
            $definitions = <FILE>;
        }
        close(FILE);
        #print "STUFF: ${definitions}\n";
        eval $definitions;
        if(defined($testlist)) {
            #use Data::Dumper;
            #print Dumper($testlist);
            foreach $test (@$testlist) {
                runOneComplexTest($test);
            }
        } else {
            warn "File: ${file} could not be loaded as PERL: $!";
        }
    }
}

sub runSimpleTests {

    local($only)=@_;

    open(TESTLIST, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
    while(<TESTLIST>) {
        next if /^\#/;
        next if /^$/;

        unlink("core");
        ($name, $input, $output, @options) = split;
        #print "processing ${only} vs ${name}\n";
        next if(defined($only) && $only ne $name);

        my $options = join(" ", @options);
        #print "@{options} becomes ${options}\n";

        my $hash = { name => $name,
                     input=> $input,
                     output=>$output,
                     args => $options };

        runOneComplexTest($hash);
    }
}

if(scalar(@ARGV) == 0) {
    runSimpleTests();
    runComplexTests();
} else {
    runSimpleTests($ARGV[0]);
}

# exit with number of failing tests.
print "------------------------------------------------\n";
printf("%4u tests failed\n",$failedcount);
printf("%4u tests passed\n",$passedcount);

system("cat ${failureoutput}");
exit $failedcount;