]>
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 # Make true and false work as Booleans.
24 use constant
{ true
=> 1, false
=> 0 };
27 use POSIX
qw( WEXITSTATUS WIFEXITED);
28 use Cwd
qw(abs_path getcwd);
29 use File
::Path
qw(mkpath); # mkpath works with ancient perl, as well as newer perl
31 use Data
::Dumper
; # for debugging.
33 # these are created in the directory where we are run, which might be
35 my $newdir = "tests/NEW";
36 my $diffdir= "tests/DIFF";
39 my $origdir = getcwd
();
40 my $srcdir = $ENV{'srcdir'} || ".";
43 # Force UTC, so time stamps are printed in a standard time zone, and
44 # tests don't have to be run in the time zone in which the output
50 # Get the tests directory from $0.
52 my $testsdir = dirname
($0);
55 # Convert it to an absolute path, so it works even after we do a cd.
57 $testsdir = abs_path
($testsdir);
58 print "Running tests from ${testsdir}\n";
59 print "with ${TCPDUMP}, version:\n";
60 system "${TCPDUMP} --version";
62 unshift(@INC, $testsdir);
67 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
69 # truncate the output file
70 open(FAILUREOUTPUT
, ">" . $failureoutput);
79 # XXX - just do this directly in Perl?
81 if ($^O
eq 'MSWin32') {
82 my $winpath = File
::Spec
->canonpath($path);
83 system "type $winpath";
90 local($name, $input, $output, $options) = @_;
93 $outputbase = basename
($output);
97 my $rawstderrlog = "tests/NEW/${outputbase}.raw.stderr";
98 my $stderrlog = "tests/NEW/${outputbase}.stderr";
102 # we used to do this as a nice pipeline, but the problem is that $r fails to
103 # to be set properly if the tcpdump core dumps.
105 # Furthermore, on Windows, fc can't read the standard input, so we
106 # can't do it as a pipeline in any case.
107 $r = system "$TCPDUMP -# -n -r $input $options >tests/NEW/${outputbase} 2>${rawstderrlog}";
110 # Something other than "tcpdump opened the file, read it, and
111 # dissected all the packets". What happened?
113 # We write out an exit status after whatever the subprocess
114 # wrote out, so it shows up when we diff the expected output
117 open(OUTPUT
, ">>"."tests/NEW/$outputbase") || die "fail to open $outputbase\n";
119 # failed to start due to error.
121 printf OUTPUT
"FAILED TO RUN: status: %d\n", $status;
123 if ($^O
eq 'MSWin32') {
125 # On Windows, the return value of system is the lower 8
126 # bits of the exit status of the process, shifted left
129 # If the process crashed, rather than exiting, the
130 # exit status will be one of the EXCEPTION_ values
131 # listed in the documentation for the GetExceptionCode()
134 # Those are defined as STATUS_ values, which should have
135 # 0xC in the topmost 4 bits (being fatal error
136 # statuses); some of them have a value that fits in
137 # the lower 8 bits. We could, I guess, assume that
138 # any value that 1) isn't returned by tcpdump and 2)
139 # corresponds to the lower 8 bits of a STATUS_ value
140 # used as an EXCEPTION_ value indicates that tcpdump
141 # exited with that exception.
143 # However, as we're running tcpdump with system, which
144 # runs the command through cmd.exe, and as cmd.exe
145 # doesn't map the command's exit code to its own exit
146 # code in any straightforward manner, we can't get
147 # that information in any case, so there's no point
148 # in trying to interpret it in that fashion.
153 # On UN*Xes, the return status is a POSIX as filled in
154 # by wait() or waitpid().
156 # POSIX offers some calls for analyzing it, such as
157 # WIFSIGNALED() to test whether it indicates that the
158 # process was terminated by a signal, WTERMSIG() to
159 # get the signal number from it, WIFEXITED() to test
160 # whether it indicates that the process exited normally,
161 # and WEXITSTATUS() to get the exit status from it.
163 # POSIX doesn't standardize core dumps, so the POSIX
164 # calls can't test whether a core dump occurred.
165 # However, all the UN*Xes we are likely to encounter
166 # follow Research UNIX in this regard, with the exit
167 # status containing either 0 or a signal number in
168 # the lower 7 bits, with 0 meaning "exited rather
169 # than being terminated by a signal", the "core dumped"
170 # flag in the 0x80 bit, and, if the signal number is
171 # 0, the exit status in the next 8 bits up.
173 # This should be cleaned up to use the POSIX calls
174 # from the Perl library - and to define an additional
175 # WCOREDUMP() call to test the "core dumped" bit and
178 # But note also that, as we're running tcpdump with
179 # system, which runs the command through a shell, if
180 # tcpdump crashes, we'll only know that if the shell
181 # maps the signal indication and uses that as its
184 # The good news is that the Bourne shell, and compatible
185 # shells, have traditionally done that. If the process
186 # for which the shell reports the exit status terminates
187 # with a signal, it adds 128 to the signal number and
188 # returns that as its exit status. (This is why the
189 # "this is now working right" behavior described in a
190 # comment below is occurring.)
192 # As tcpdump itself never returns with an exit status
193 # >= 128, we can try checking for an exit status with
194 # the 0x80 bit set and, if we have one, get the signal
195 # number from the lower 7 bits of the exit status. We
196 # can't get the "core dumped" indication from the
197 # shell's exit status; all we can do is check whether
198 # there's a core file.
201 $coredump = $r & 127;
204 $status = WEXITSTATUS
($r);
208 if($coredump || $status) {
209 printf OUTPUT
"EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
211 printf OUTPUT
"EXIT CODE %08x\n", $r;
219 # Compare tcpdump's output with what we think it should be.
220 # If tcpdump failed to produce output, we've produced our own
221 # "output" above, with the exit status.
223 if ($^O
eq 'MSWin32') {
224 my $winoutput = File
::Spec
->canonpath($output);
225 $r = system "fc /lb1000 /t /1 $winoutput tests\\NEW\\$outputbase >tests\\DIFF\\$outputbase.diff";
228 $r = system "diff $output tests/NEW/$outputbase >tests/DIFF/$outputbase.diff";
229 $diffstat = WEXITSTATUS
($r);
233 # process the standard error file, sanitize "reading from" line,
236 open(ERRORRAW
, "<" . $rawstderrlog);
237 open(ERROROUT
, ">" . $stderrlog);
239 next if /^$/; # blank lines are boring
240 if(/^(reading from file )(.*)(,.*)$/) {
241 my $filename = basename
($2);
242 print ERROROUT
"${1}${filename}${3}\n";
251 if ( -f
"$output.stderr" ) {
253 # Compare the standard error with what we think it should be.
255 if ($^O
eq 'MSWin32') {
256 my $winoutput = File
::Spec
->canonpath($output);
257 my $canonstderrlog = File
::Spec
->canonpath($stderrlog);
258 $nr = system "fc /lb1000 /t /1 $winoutput.stderr $canonstderrlog >tests\DIFF\$outputbase.stderr.diff";
259 $errdiffstat = $nr >> 8;
261 $nr = system "diff $output.stderr $stderrlog >tests/DIFF/$outputbase.stderr.diff";
262 $errdiffstat = WEXITSTATUS
($nr);
270 if($linecount == 0 && $status == 0) {
277 #print sprintf("END: %08x\n", $r);
280 if($linecount == 0) {
281 printf " %-40s: passed\n", $name;
283 printf " %-40s: passed with error messages:\n", $name;
284 showfile
($stderrlog);
286 unlink "tests/DIFF/$outputbase.diff";
290 printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
291 open FOUT
, '>>tests/failure-outputs.txt';
292 printf FOUT
"\nFailed test: $name\n\n";
294 if(-f
"tests/DIFF/$outputbase.diff") {
296 # XXX - just do this directly in Perl?
298 if ($^O
eq 'MSWin32') {
299 system "type tests\\DIFF\\$outputbase.diff >> tests\\failure-outputs.txt";
301 system "cat tests/DIFF/$outputbase.diff >> tests/failure-outputs.txt";
306 print " (failed to execute: $!)\n";
310 # this is not working right, $r == 0x8b00 when there is a core dump.
311 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
313 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
314 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
315 if($r & 127 || -f
"core") {
316 my $with = ($r & 128) ?
'with' : 'without';
320 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
321 if($linecount == 0) {
324 print " with error messages:\n";
325 showfile
($stderrlog);
327 return(($r & 128) ?
10 : 20);
329 if($linecount == 0) {
332 print " with error messages:\n";
333 showfile
($stderrlog);
339 if(defined($confighhash)) {
343 $main::confighhash
= {};
345 # this could be loaded once perhaps.
346 open(CONFIG_H
, "config.h") || die "Can not open config.h: $!\n";
349 if(/^\#define (.*) 1/) {
350 #print "Setting $1\n";
351 $main::confighhash
->{$1} = 1;
355 #print Dumper($main::confighhash);
357 # also run tcpdump --fp-type to get the type of floating-point
358 # arithmetic we're doing, setting a HAVE_{fptype} key based
359 # on the value it prints
360 open(FPTYPE_PIPE
, "$TCPDUMP --fp-type |") or die("piping tcpdump --fp-type failed\n");
361 my $fptype_val = <FPTYPE_PIPE
>;
364 if($fptype_val == "9877.895") {
365 $have_fptype = "HAVE_FPTYPE1";
367 $have_fptype = "HAVE_FPTYPE2";
369 $main::confighhash
->{$have_fptype} = 1;
371 return $main::confighhash
;
375 sub runOneComplexTest
{
376 local($testconfig) = @_;
378 my $output = $testconfig->{output
};
379 my $input = $testconfig->{input
};
380 my $name = $testconfig->{name
};
381 my $options= $testconfig->{args
};
385 my $configset = $testconfig->{config_set
};
386 my $configunset = $testconfig->{config_unset
};
387 my $ch = loadconfighash
();
390 if(defined($configset)) {
391 $foundit = ($ch->{$configset} == 1);
393 if(defined($configunset)) {
394 $unfoundit=($ch->{$configunset} != 1);
398 printf " %-40s: skipped (%s not set)\n", $name, $configset;
403 printf " %-40s: skipped (%s set)\n", $name, $configunset;
408 #print Dumper($testconfig);
410 # EXPAND any occurrences of @TESTDIR@ to $testsdir
411 $options =~ s/\@TESTDIR\@/$testsdir/;
413 my $result = runtest
($name,
414 $testsdir . "/" . $input,
415 $testsdir . "/" . $output,
425 # *.tests files are PERL hash definitions. They should create an array of hashes
426 # one per test, and place it into the variable @testlist.
427 sub runComplexTests
{
428 my @files = glob( $testsdir . '/*.tests' );
429 foreach $file (@files) {
430 my @testlist = undef;
432 print "FILE: ${file}\n";
433 open(FILE
, "<".$file) || die "can not open $file: $!";
436 $definitions = <FILE
>;
439 #print "STUFF: ${definitions}\n";
441 if(defined($testlist)) {
443 #print Dumper($testlist);
444 foreach $test (@
$testlist) {
445 runOneComplexTest
($test);
448 warn "File: ${file} could not be loaded as PERL: $!";
457 open(TESTLIST
, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
463 ($name, $input, $output, @options) = split;
464 #print "processing ${only} vs ${name}\n";
465 next if(defined($only) && $only ne $name);
467 my $options = join(" ", @options);
468 #print "@{options} becomes ${options}\n";
470 my $hash = { name
=> $name,
475 runOneComplexTest
($hash);
479 if(scalar(@ARGV) == 0) {
483 runSimpleTests
($ARGV[0]);
486 # exit with number of failing tests.
487 print "------------------------------------------------\n";
488 printf("%4u tests failed\n",$failedcount);
489 printf("%4u tests passed\n",$passedcount);
491 showfile
(${failureoutput
});