]>
The Tcpdump Group git mirrors - tcpdump/blob - tests/TESTrun
4 # Were we told where to find tcpdump?
6 if (!($TCPDUMP = $ENV{TCPDUMP_BIN
})) {
8 # No. Use the appropriate path.
10 if ($^O
eq 'MSWin32') {
12 # XXX - assume, for now, a Visual Studio debug build, so that
13 # tcpdump is in the Debug subdirectory.
15 $TCPDUMP = "Debug\\tcpdump"
17 $TCPDUMP = "./tcpdump"
22 use POSIX
qw( WEXITSTATUS WIFEXITED);
23 use Cwd
qw(abs_path getcwd);
24 use File
::Path
qw(mkpath); # mkpath works with ancient perl, as well as newer perl
26 use Data
::Dumper
; # for debugging.
28 # these are created in the directory where we are run, which might be
30 my $newdir = "tests/NEW";
31 my $diffdir= "tests/DIFF";
34 my $origdir = getcwd
();
35 my $srcdir = $ENV{'srcdir'} || ".";
38 # Force UTC, so time stamps are printed in a standard time zone, and
39 # tests don't have to be run in the time zone in which the output
45 # Get the tests directory from $0.
47 my $testsdir = dirname
($0);
50 # Convert it to an absolute path, so it works even after we do a cd.
52 $testsdir = abs_path
($testsdir);
53 print "Running tests from ${testsdir}\n";
55 unshift(@INC, $testsdir);
60 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
62 # truncate the output file
63 open(FAILUREOUTPUT
, ">" . $failureoutput);
72 # XXX - just do this directly in Perl?
74 if ($^O
eq 'MSWin32') {
75 my $winpath = File
::Spec
->canonpath($path);
76 system "type $winpath";
83 local($name, $input, $output, $options) = @_;
86 $outputbase = basename
($output);
90 my $rawstderrlog = "tests/NEW/${outputbase}.raw.stderr";
91 my $stderrlog = "tests/NEW/${outputbase}.stderr";
95 # we used to do this as a nice pipeline, but the problem is that $r fails to
96 # to be set properly if the tcpdump core dumps.
98 # Furthermore, on Windows, fc can't read the standard input, so we
99 # can't do it as a pipeline in any case.
100 $r = system "$TCPDUMP -# -n -r $input $options >tests/NEW/${outputbase} 2>${rawstderrlog}";
103 # Something other than "tcpdump opened the file, read it, and
104 # dissected all the packets". What happened?
106 # We write out an exit status after whatever the subprocess
107 # wrote out, so it shows up when we diff the expected output
110 open(OUTPUT
, ">>"."tests/NEW/$outputbase") || die "fail to open $outputbase\n";
112 # failed to start due to error.
114 printf OUTPUT
"FAILED TO RUN: status: %d\n", $status;
116 if ($^O
eq 'MSWin32') {
118 # On Windows, the return value of system is the lower 8
119 # bits of the exit status of the process, shifted left
122 # If the process crashed, rather than exiting, the
123 # exit status will be one of the EXCEPTION_ values
124 # listed in the documentation for the GetExceptionCode()
127 # Those are defined as STATUS_ values, which should have
128 # 0xC in the topmost 4 bits (being fatal error
129 # statuses); some of them have a value that fits in
130 # the lower 8 bits. We could, I guess, assume that
131 # any value that 1) isn't returned by tcpdump and 2)
132 # corresponds to the lower 8 bits of a STATUS_ value
133 # used as an EXCEPTION_ value indicates that tcpdump
134 # exited with that exception.
136 # However, as we're running tcpdump with system, which
137 # runs the command through cmd.exe, and as cmd.exe
138 # doesn't map the command's exit code to its own exit
139 # code in any straightforward manner, we can't get
140 # that information in any case, so there's no point
141 # in trying to interpret it in that fashion.
146 # On UN*Xes, the return status is a POSIX as filled in
147 # by wait() or waitpid().
149 # POSIX offers some calls for analyzing it, such as
150 # WIFSIGNALED() to test whether it indicates that the
151 # process was terminated by a signal, WTERMSIG() to
152 # get the signal number from it, WIFEXITED() to test
153 # whether it indicates that the process exited normally,
154 # and WEXITSTATUS() to get the exit status from it.
156 # POSIX doesn't standardize core dumps, so the POSIX
157 # calls can't test whether a core dump occurred.
158 # However, all the UN*Xes we are likely to encounter
159 # follow Research UNIX in this regard, with the exit
160 # status containing either 0 or a signal number in
161 # the lower 7 bits, with 0 meaning "exited rather
162 # than being terminated by a signal", the "core dumped"
163 # flag in the 0x80 bit, and, if the signal number is
164 # 0, the exit status in the next 8 bits up.
166 # This should be cleaned up to use the POSIX calls
167 # from the Perl library - and to define an additional
168 # WCOREDUMP() call to test the "core dumped" bit and
171 # But note also that, as we're running tcpdump with
172 # system, which runs the command through a shell, if
173 # tcpdump crashes, we'll only know that if the shell
174 # maps the signal indication and uses that as its
177 # The good news is that the Bourne shell, and compatible
178 # shells, have traditionally done that. If the process
179 # for which the shell reports the exit status terminates
180 # with a signal, it adds 128 to the signal number and
181 # returns that as its exit status. (This is why the
182 # "this is now working right" behavior described in a
183 # comment below is occurring.)
185 # As tcpdump itself never returns with an exit status
186 # >= 128, we can try checking for an exit status with
187 # the 0x80 bit set and, if we have one, get the signal
188 # number from the lower 7 bits of the exit status. We
189 # can't get the "core dumped" indication from the
190 # shell's exit status; all we can do is check whether
191 # there's a core file.
194 $coredump = $r & 127;
197 $status = WEXITSTATUS
($r);
201 if($coredump || $status) {
202 printf OUTPUT
"EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
204 printf OUTPUT
"EXIT CODE %08x\n", $r;
212 # Compare tcpdump's output with what we think it should be.
213 # If tcpdump failed to produce output, we've produced our own
214 # "output" above, with the exit status.
216 if ($^O
eq 'MSWin32') {
217 my $winoutput = File
::Spec
->canonpath($output);
218 $r = system "fc /lb1000 /t /1 $winoutput tests\\NEW\\$outputbase >tests\\DIFF\\$outputbase.diff";
221 $r = system "diff $output tests/NEW/$outputbase >tests/DIFF/$outputbase.diff";
222 $diffstat = WEXITSTATUS
($r);
226 # process the standard error file, sanitize "reading from" line,
229 open(ERRORRAW
, "<" . $rawstderrlog);
230 open(ERROROUT
, ">" . $stderrlog);
232 next if /^$/; # blank lines are boring
233 if(/^(reading from file )(.*)(,.*)$/) {
234 my $filename = basename
($2);
235 print ERROROUT
"${1}${filename}${3}\n";
244 if ( -f
"$output.stderr" ) {
246 # Compare the standard error with what we think it should be.
248 if ($^O
eq 'MSWin32') {
249 my $winoutput = File
::Spec
->canonpath($output);
250 my $canonstderrlog = File
::Spec
->canonpath($stderrlog);
251 $nr = system "fc /lb1000 /t /1 $winoutput.stderr $canonstderrlog >tests\DIFF\$outputbase.stderr.diff";
252 $errdiffstat = $nr >> 8;
254 $nr = system "diff $output.stderr $stderrlog >tests/DIFF/$outputbase.stderr.diff";
255 $errdiffstat = WEXITSTATUS
($nr);
263 if($linecount == 0 && $status == 0) {
270 #print sprintf("END: %08x\n", $r);
273 if($linecount == 0) {
274 printf " %-40s: passed\n", $name;
276 printf " %-40s: passed with error messages:\n", $name;
277 showfile
($stderrlog);
279 unlink "tests/DIFF/$outputbase.diff";
283 printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
284 open FOUT
, '>>tests/failure-outputs.txt';
285 printf FOUT
"\nFailed test: $name\n\n";
287 if(-f
"tests/DIFF/$outputbase.diff") {
289 # XXX - just do this directly in Perl?
291 if ($^O
eq 'MSWin32') {
292 system "type tests\\DIFF\\$outputbase.diff >> tests\\failure-outputs.txt";
294 system "cat tests/DIFF/$outputbase.diff >> tests/failure-outputs.txt";
299 print " (failed to execute: $!)\n";
303 # this is not working right, $r == 0x8b00 when there is a core dump.
304 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
306 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
307 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
308 if($r & 127 || -f
"core") {
309 my $with = ($r & 128) ?
'with' : 'without';
313 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
314 if($linecount == 0) {
317 print " with error messages:\n";
318 showfile
($stderrlog);
320 return(($r & 128) ?
10 : 20);
322 if($linecount == 0) {
325 print " with error messages:\n";
326 showfile
($stderrlog);
332 if(defined($confighhash)) {
336 $main::confighhash
= {};
338 # this could be loaded once perhaps.
339 open(CONFIG_H
, "config.h") || die "Can not open config.h: $!\n";
342 if(/^\#define (.*) 1/) {
343 #print "Setting $1\n";
344 $main::confighhash
->{$1} = 1;
348 #print Dumper($main::confighhash);
350 # also run tcpdump --fp-type to get the type of floating-point
351 # arithmetic we're doing, setting a HAVE_{fptype} key based
352 # on the value it prints
353 open(FPTYPE_PIPE
, "$TCPDUMP --fp-type |") or die("piping tcpdump --fp-type failed\n");
354 my $fptype_val = <FPTYPE_PIPE
>;
357 if($fptype_val == "9877.895") {
358 $have_fptype = "HAVE_FPTYPE1";
360 $have_fptype = "HAVE_FPTYPE2";
362 $main::confighhash
->{$have_fptype} = 1;
364 return $main::confighhash
;
368 sub runOneComplexTest
{
369 local($testconfig) = @_;
371 my $output = $testconfig->{output
};
372 my $input = $testconfig->{input
};
373 my $name = $testconfig->{name
};
374 my $options= $testconfig->{args
};
378 my $configset = $testconfig->{config_set
};
379 my $configunset = $testconfig->{config_unset
};
380 my $ch = loadconfighash
();
383 if(defined($configset)) {
384 $foundit = ($ch->{$configset} == 1);
386 if(defined($configunset)) {
387 $unfoundit=($ch->{$configunset} != 1);
391 printf " %-40s: skipped (%s not set)\n", $name, $configset;
396 printf " %-40s: skipped (%s set)\n", $name, $configunset;
401 #print Dumper($testconfig);
403 # EXPAND any occurances of @TESTDIR@ to $testsdir
404 $options =~ s/\@TESTDIR\@/$testsdir/;
406 my $result = runtest
($name,
407 $testsdir . "/" . $input,
408 $testsdir . "/" . $output,
418 # *.tests files are PERL hash definitions. They should create an array of hashes
419 # one per test, and place it into the variable @testlist.
420 sub runComplexTests
{
421 my @files = glob( $testsdir . '/*.tests' );
422 foreach $file (@files) {
423 my @testlist = undef;
425 print "FILE: ${file}\n";
426 open(FILE
, "<".$file) || die "can not open $file: $!";
429 $definitions = <FILE
>;
432 #print "STUFF: ${definitions}\n";
434 if(defined($testlist)) {
436 #print Dumper($testlist);
437 foreach $test (@
$testlist) {
438 runOneComplexTest
($test);
441 warn "File: ${file} could not be loaded as PERL: $!";
450 open(TESTLIST
, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
456 ($name, $input, $output, @options) = split;
457 #print "processing ${only} vs ${name}\n";
458 next if(defined($only) && $only ne $name);
460 my $options = join(" ", @options);
461 #print "@{options} becomes ${options}\n";
463 my $hash = { name
=> $name,
468 runOneComplexTest
($hash);
472 if(scalar(@ARGV) == 0) {
476 runSimpleTests
($ARGV[0]);
479 # exit with number of failing tests.
480 print "------------------------------------------------\n";
481 printf("%4u tests failed\n",$failedcount);
482 printf("%4u tests passed\n",$passedcount);
484 showfile
(${failureoutput
});