]>
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.exe"
17 $TCPDUMP = "./tcpdump"
22 # Make true and false work as Booleans.
24 use constant true
=> 1;
25 use constant false
=> 0;
28 use POSIX
qw( WEXITSTATUS WIFEXITED);
29 use Cwd
qw(abs_path getcwd);
30 use File
::Path
qw(mkpath); # mkpath works with ancient perl, as well as newer perl
32 use Data
::Dumper
; # for debugging.
34 # these are created in the directory where we are run, which might be
36 my $newdir = "tests/NEW";
37 my $diffdir= "tests/DIFF";
40 my $origdir = getcwd
();
41 my $srcdir = $ENV{'srcdir'} || ".";
42 # Default to unified context diff (on HP-UX diff does not support it, so
43 # default to the closest alternative) and allow to fall back to another diff
44 # format if necessary.
45 my $diff_flags = defined $ENV{'DIFF_FLAGS'} ?
$ENV{'DIFF_FLAGS'} :
46 $^O
eq 'hpux' ?
'-c' :
50 # Force UTC, so time stamps are printed in a standard time zone, and
51 # tests don't have to be run in the time zone in which the output
57 # Get the tests directory from $0.
59 my $testsdir = dirname
($0);
62 # Convert it to an absolute path, so it works even after we do a cd.
64 $testsdir = abs_path
($testsdir);
65 print "Running tests from ${testsdir}\n";
66 print "with ${TCPDUMP}, version:\n";
67 system "${TCPDUMP} --version";
69 unshift(@INC, $testsdir);
75 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
77 # truncate the output file
78 open(FAILUREOUTPUT
, ">" . $failureoutput);
87 # XXX - just do this directly in Perl?
89 if ($^O
eq 'MSWin32') {
90 my $winpath = File
::Spec
->canonpath($path);
91 system "type $winpath";
98 local($name, $input, $output, $options) = @_;
101 $outputbase = basename
($output);
102 my $coredump = false
;
105 my $rawstderrlog = "${newdir}/${outputbase}.raw.stderr";
106 my $stderrlog = "${newdir}/${outputbase}.stderr";
110 # we used to do this as a nice pipeline, but the problem is that $r fails to
111 # to be set properly if the tcpdump core dumps.
113 # Furthermore, on Windows, fc can't read the standard input, so we
114 # can't do it as a pipeline in any case.
115 if (index($options, "SPECIAL_t") != -1) {
116 # Hack to keep specific time options for tcp-handshake-micro-t, etc.
118 $options =~ s/ SPECIAL_t//;
120 # No specific time option, use -tttt
121 $options .= " -tttt";
123 $r = system "$TCPDUMP -# -n -r $input $options >${newdir}/${outputbase} 2>${rawstderrlog}";
127 # Something other than "tcpdump opened the file, read it, and
128 # dissected all the packets". What happened?
130 # We write out an exit status after whatever the subprocess
131 # wrote out, so it shows up when we diff the expected output
134 open(OUTPUT
, ">>"."${newdir}/$outputbase") || die "fail to open $outputbase\n";
136 # failed to start due to error.
138 printf OUTPUT
"FAILED TO RUN: status: %d\n", $status;
140 if ($^O
eq 'MSWin32' or $^O
eq 'msys') {
142 # On Windows, the return value of system is the lower 8
143 # bits of the exit status of the process, shifted left
146 # If the process crashed, rather than exiting, the
147 # exit status will be one of the EXCEPTION_ values
148 # listed in the documentation for the GetExceptionCode()
151 # Those are defined as STATUS_ values, which should have
152 # 0xC in the topmost 4 bits (being fatal error
153 # statuses); some of them have a value that fits in
154 # the lower 8 bits. We could, I guess, assume that
155 # any value that 1) isn't returned by tcpdump and 2)
156 # corresponds to the lower 8 bits of a STATUS_ value
157 # used as an EXCEPTION_ value indicates that tcpdump
158 # exited with that exception.
160 # However, as we're running tcpdump with system, which
161 # runs the command through cmd.exe, and as cmd.exe
162 # doesn't map the command's exit code to its own exit
163 # code in any straightforward manner, we can't get
164 # that information in any case, so there's no point
165 # in trying to interpret it in that fashion.
170 # On UN*Xes, the return status is a POSIX as filled in
171 # by wait() or waitpid().
173 # POSIX offers some calls for analyzing it, such as
174 # WIFSIGNALED() to test whether it indicates that the
175 # process was terminated by a signal, WTERMSIG() to
176 # get the signal number from it, WIFEXITED() to test
177 # whether it indicates that the process exited normally,
178 # and WEXITSTATUS() to get the exit status from it.
180 # POSIX doesn't standardize core dumps, so the POSIX
181 # calls can't test whether a core dump occurred.
182 # However, all the UN*Xes we are likely to encounter
183 # follow Research UNIX in this regard, with the exit
184 # status containing either 0 or a signal number in
185 # the lower 7 bits, with 0 meaning "exited rather
186 # than being terminated by a signal", the "core dumped"
187 # flag in the 0x80 bit, and, if the signal number is
188 # 0, the exit status in the next 8 bits up.
190 # This should be cleaned up to use the POSIX calls
191 # from the Perl library - and to define an additional
192 # WCOREDUMP() call to test the "core dumped" bit and
195 # But note also that, as we're running tcpdump with
196 # system, which runs the command through a shell, if
197 # tcpdump crashes, we'll only know that if the shell
198 # maps the signal indication and uses that as its
201 # The good news is that the Bourne shell, and compatible
202 # shells, have traditionally done that. If the process
203 # for which the shell reports the exit status terminates
204 # with a signal, it adds 128 to the signal number and
205 # returns that as its exit status. (This is why the
206 # "this is now working right" behavior described in a
207 # comment below is occurring.)
209 # As tcpdump itself never returns with an exit status
210 # >= 128, we can try checking for an exit status with
211 # the 0x80 bit set and, if we have one, get the signal
212 # number from the lower 7 bits of the exit status. We
213 # can't get the "core dumped" indication from the
214 # shell's exit status; all we can do is check whether
215 # there's a core file.
218 $coredump = $r & 127;
221 $status = WEXITSTATUS
($r);
225 if($coredump || $status) {
226 printf OUTPUT
"EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
228 printf OUTPUT
"EXIT CODE %08x\n", $r;
236 # Compare tcpdump's output with what we think it should be.
237 # If tcpdump failed to produce output, we've produced our own
238 # "output" above, with the exit status.
240 if ($^O
eq 'MSWin32') {
241 my $winoutput = File
::Spec
->canonpath($output);
242 my $winnewdir = File
::Spec
->canonpath($newdir);
243 my $windiffdir = File
::Spec
->canonpath($diffdir);
244 $r = system "fc /lb1000 /t /1 $winoutput ${winnewdir}\\$outputbase >${windiffdir}\\$outputbase.diff";
247 $r = system "diff $diff_flags $output ${newdir}/$outputbase >${diffdir}/$outputbase.diff";
248 $diffstat = WEXITSTATUS
($r);
252 # process the standard error file, sanitize "reading from" line,
255 open(ERRORRAW
, "<" . $rawstderrlog);
256 open(ERROROUT
, ">" . $stderrlog);
258 next if /^$/; # blank lines are boring
259 if(/^(reading from file )(.*)(,.*)$/) {
260 my $filename = basename
($2);
261 print ERROROUT
"${1}${filename}${3}\n";
270 if ( -f
"$output.stderr" ) {
272 # Compare the standard error with what we think it should be.
274 if ($^O
eq 'MSWin32') {
275 my $winoutput = File
::Spec
->canonpath($output);
276 my $windiffdir = File
::Spec
->canonpath($diffdir);
277 my $canonstderrlog = File
::Spec
->canonpath($stderrlog);
278 $nr = system "fc /lb1000 /t /1 $winoutput.stderr $canonstderrlog >${windiffdir}\\$outputbase.stderr.diff";
279 $errdiffstat = $nr >> 8;
281 $nr = system "diff $output.stderr $stderrlog >${diffdir}/$outputbase.stderr.diff";
282 $errdiffstat = WEXITSTATUS
($nr);
290 if($linecount == 0 && $status == 0) {
297 #print sprintf("END: %08x\n", $r);
300 if($linecount == 0) {
301 printf " %-40s: passed\n", $name;
303 printf " %-40s: passed with error messages:\n", $name;
304 showfile
($stderrlog);
306 unlink "${diffdir}/$outputbase.diff";
310 printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
311 open FOUT
, '>>tests/failure-outputs.txt';
312 printf FOUT
"\nFailed test: $name\n\n";
314 if(-f
"${diffdir}/$outputbase.diff") {
316 # XXX - just do this directly in Perl?
318 if ($^O
eq 'MSWin32') {
319 my $windiffdir = File
::Spec
->canonpath($diffdir);
320 system "type ${windiffdir}\\$outputbase.diff >> tests\\failure-outputs.txt";
322 system "cat ${diffdir}/$outputbase.diff >> tests/failure-outputs.txt";
327 print " (failed to execute: $!)\n";
331 # this is not working right, $r == 0x8b00 when there is a core dump.
332 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
334 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
335 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
336 if($r & 127 || -f
"core") {
337 my $with = ($r & 128) ?
'with' : 'without';
341 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
342 if($linecount == 0) {
345 print " with error messages:\n";
346 showfile
($stderrlog);
348 return(($r & 128) ?
10 : 20);
350 if($linecount == 0) {
353 print " with error messages:\n";
354 showfile
($stderrlog);
360 if(defined($confighhash)) {
364 $main::confighhash
= {};
366 # this could be loaded once perhaps.
367 open(CONFIG_H
, "config.h") || die "Can not open config.h: $!\n";
370 if(/^\#define (.*) 1/) {
371 #print "Setting $1\n";
372 $main::confighhash
->{$1} = 1;
376 #print Dumper($main::confighhash);
378 # also run tcpdump --fp-type to get the type of floating-point
379 # arithmetic we're doing, setting a HAVE_{fptype} key based
380 # on the value it prints
381 open(FPTYPE_PIPE
, "$TCPDUMP --fp-type |") or die("piping tcpdump --fp-type failed\n");
382 my $fptype_val = <FPTYPE_PIPE
>;
385 if($fptype_val == "9877.895") {
386 $have_fptype = "HAVE_FPTYPE1";
388 $have_fptype = "HAVE_FPTYPE2";
390 printf "$TCPDUMP --fp-type => %s\n", $have_fptype;
391 $main::confighhash
->{$have_fptype} = 1;
393 # and check whether this is OpenBSD, as one test fails in OpenBSD
394 # due to the sad hellscape of low-numbered DLT_ values, due to
395 # 12 meaning "OpenBSD loopback" rather than "raw IP" on OpenBSD
396 if($^O
eq "openbsd") {
397 $main::confighhash
->{"IS_OPENBSD"} = 1;
400 return $main::confighhash
;
404 sub runOneComplexTest
{
405 local($testconfig) = @_;
407 my $output = $testconfig->{output
};
408 my $input = $testconfig->{input
};
409 my $name = $testconfig->{name
};
410 my $options= $testconfig->{args
};
414 my $configset = $testconfig->{config_set
};
415 my $configunset = $testconfig->{config_unset
};
416 my $ch = loadconfighash
();
419 if(defined($configset)) {
420 $foundit = ($ch->{$configset} == 1);
422 if(defined($configunset)) {
423 $unfoundit=($ch->{$configunset} != 1);
427 printf " %-40s: skipped (%s not set)\n", $name, $configset;
433 printf " %-40s: skipped (%s set)\n", $name, $configunset;
439 #print Dumper($testconfig);
441 # EXPAND any occurrences of @TESTDIR@ to $testsdir
442 $options =~ s/\@TESTDIR\@/$testsdir/;
444 my $result = runtest
($name,
445 $testsdir . "/" . $input,
446 $testsdir . "/" . $output,
456 # *.tests files are PERL hash definitions. They should create an array of hashes
457 # one per test, and place it into the variable @testlist.
458 sub runComplexTests
{
459 my @files = glob( $testsdir . '/*.tests' );
460 foreach $file (@files) {
461 my @testlist = undef;
463 print "FILE: ${file}\n";
464 open(FILE
, "<".$file) || die "can not open $file: $!";
467 $definitions = <FILE
>;
470 #print "STUFF: ${definitions}\n";
472 if(defined($testlist)) {
474 #print Dumper($testlist);
475 foreach $test (@
$testlist) {
476 runOneComplexTest
($test);
479 warn "File: ${file} could not be loaded as PERL: $!";
488 open(TESTLIST
, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
494 ($name, $input, $output, @options) = split;
495 #print "processing ${only} vs ${name}\n";
496 next if(defined($only) && $only ne $name);
498 my $options = join(" ", @options);
499 #print "@{options} becomes ${options}\n";
501 my $hash = { name
=> $name,
506 runOneComplexTest
($hash);
510 if(scalar(@ARGV) == 0) {
514 runSimpleTests
($ARGV[0]);
517 # exit with number of failing tests.
518 print "------------------------------------------------\n";
519 printf("%4u tests skipped\n",$skippedcount);
520 printf("%4u tests failed\n",$failedcount);
521 printf("%4u tests passed\n",$passedcount);
523 showfile
(${failureoutput
});