]>
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;
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'} || ".";
44 # Force UTC, so time stamps are printed in a standard time zone, and
45 # tests don't have to be run in the time zone in which the output
51 # Get the tests directory from $0.
53 my $testsdir = dirname
($0);
56 # Convert it to an absolute path, so it works even after we do a cd.
58 $testsdir = abs_path
($testsdir);
59 print "Running tests from ${testsdir}\n";
60 print "with ${TCPDUMP}, version:\n";
61 system "${TCPDUMP} --version";
63 unshift(@INC, $testsdir);
68 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
70 # truncate the output file
71 open(FAILUREOUTPUT
, ">" . $failureoutput);
80 # XXX - just do this directly in Perl?
82 if ($^O
eq 'MSWin32') {
83 my $winpath = File
::Spec
->canonpath($path);
84 system "type $winpath";
91 local($name, $input, $output, $options) = @_;
94 $outputbase = basename
($output);
98 my $rawstderrlog = "tests/NEW/${outputbase}.raw.stderr";
99 my $stderrlog = "tests/NEW/${outputbase}.stderr";
103 # we used to do this as a nice pipeline, but the problem is that $r fails to
104 # to be set properly if the tcpdump core dumps.
106 # Furthermore, on Windows, fc can't read the standard input, so we
107 # can't do it as a pipeline in any case.
108 $r = system "$TCPDUMP -# -n -r $input $options >tests/NEW/${outputbase} 2>${rawstderrlog}";
111 # Something other than "tcpdump opened the file, read it, and
112 # dissected all the packets". What happened?
114 # We write out an exit status after whatever the subprocess
115 # wrote out, so it shows up when we diff the expected output
118 open(OUTPUT
, ">>"."tests/NEW/$outputbase") || die "fail to open $outputbase\n";
120 # failed to start due to error.
122 printf OUTPUT
"FAILED TO RUN: status: %d\n", $status;
124 if ($^O
eq 'MSWin32') {
126 # On Windows, the return value of system is the lower 8
127 # bits of the exit status of the process, shifted left
130 # If the process crashed, rather than exiting, the
131 # exit status will be one of the EXCEPTION_ values
132 # listed in the documentation for the GetExceptionCode()
135 # Those are defined as STATUS_ values, which should have
136 # 0xC in the topmost 4 bits (being fatal error
137 # statuses); some of them have a value that fits in
138 # the lower 8 bits. We could, I guess, assume that
139 # any value that 1) isn't returned by tcpdump and 2)
140 # corresponds to the lower 8 bits of a STATUS_ value
141 # used as an EXCEPTION_ value indicates that tcpdump
142 # exited with that exception.
144 # However, as we're running tcpdump with system, which
145 # runs the command through cmd.exe, and as cmd.exe
146 # doesn't map the command's exit code to its own exit
147 # code in any straightforward manner, we can't get
148 # that information in any case, so there's no point
149 # in trying to interpret it in that fashion.
154 # On UN*Xes, the return status is a POSIX as filled in
155 # by wait() or waitpid().
157 # POSIX offers some calls for analyzing it, such as
158 # WIFSIGNALED() to test whether it indicates that the
159 # process was terminated by a signal, WTERMSIG() to
160 # get the signal number from it, WIFEXITED() to test
161 # whether it indicates that the process exited normally,
162 # and WEXITSTATUS() to get the exit status from it.
164 # POSIX doesn't standardize core dumps, so the POSIX
165 # calls can't test whether a core dump occurred.
166 # However, all the UN*Xes we are likely to encounter
167 # follow Research UNIX in this regard, with the exit
168 # status containing either 0 or a signal number in
169 # the lower 7 bits, with 0 meaning "exited rather
170 # than being terminated by a signal", the "core dumped"
171 # flag in the 0x80 bit, and, if the signal number is
172 # 0, the exit status in the next 8 bits up.
174 # This should be cleaned up to use the POSIX calls
175 # from the Perl library - and to define an additional
176 # WCOREDUMP() call to test the "core dumped" bit and
179 # But note also that, as we're running tcpdump with
180 # system, which runs the command through a shell, if
181 # tcpdump crashes, we'll only know that if the shell
182 # maps the signal indication and uses that as its
185 # The good news is that the Bourne shell, and compatible
186 # shells, have traditionally done that. If the process
187 # for which the shell reports the exit status terminates
188 # with a signal, it adds 128 to the signal number and
189 # returns that as its exit status. (This is why the
190 # "this is now working right" behavior described in a
191 # comment below is occurring.)
193 # As tcpdump itself never returns with an exit status
194 # >= 128, we can try checking for an exit status with
195 # the 0x80 bit set and, if we have one, get the signal
196 # number from the lower 7 bits of the exit status. We
197 # can't get the "core dumped" indication from the
198 # shell's exit status; all we can do is check whether
199 # there's a core file.
202 $coredump = $r & 127;
205 $status = WEXITSTATUS
($r);
209 if($coredump || $status) {
210 printf OUTPUT
"EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
212 printf OUTPUT
"EXIT CODE %08x\n", $r;
220 # Compare tcpdump's output with what we think it should be.
221 # If tcpdump failed to produce output, we've produced our own
222 # "output" above, with the exit status.
224 if ($^O
eq 'MSWin32') {
225 my $winoutput = File
::Spec
->canonpath($output);
226 $r = system "fc /lb1000 /t /1 $winoutput tests\\NEW\\$outputbase >tests\\DIFF\\$outputbase.diff";
229 $r = system "diff $output tests/NEW/$outputbase >tests/DIFF/$outputbase.diff";
230 $diffstat = WEXITSTATUS
($r);
234 # process the standard error file, sanitize "reading from" line,
237 open(ERRORRAW
, "<" . $rawstderrlog);
238 open(ERROROUT
, ">" . $stderrlog);
240 next if /^$/; # blank lines are boring
241 if(/^(reading from file )(.*)(,.*)$/) {
242 my $filename = basename
($2);
243 print ERROROUT
"${1}${filename}${3}\n";
252 if ( -f
"$output.stderr" ) {
254 # Compare the standard error with what we think it should be.
256 if ($^O
eq 'MSWin32') {
257 my $winoutput = File
::Spec
->canonpath($output);
258 my $canonstderrlog = File
::Spec
->canonpath($stderrlog);
259 $nr = system "fc /lb1000 /t /1 $winoutput.stderr $canonstderrlog >tests\DIFF\$outputbase.stderr.diff";
260 $errdiffstat = $nr >> 8;
262 $nr = system "diff $output.stderr $stderrlog >tests/DIFF/$outputbase.stderr.diff";
263 $errdiffstat = WEXITSTATUS
($nr);
271 if($linecount == 0 && $status == 0) {
278 #print sprintf("END: %08x\n", $r);
281 if($linecount == 0) {
282 printf " %-40s: passed\n", $name;
284 printf " %-40s: passed with error messages:\n", $name;
285 showfile
($stderrlog);
287 unlink "tests/DIFF/$outputbase.diff";
291 printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
292 open FOUT
, '>>tests/failure-outputs.txt';
293 printf FOUT
"\nFailed test: $name\n\n";
295 if(-f
"tests/DIFF/$outputbase.diff") {
297 # XXX - just do this directly in Perl?
299 if ($^O
eq 'MSWin32') {
300 system "type tests\\DIFF\\$outputbase.diff >> tests\\failure-outputs.txt";
302 system "cat tests/DIFF/$outputbase.diff >> tests/failure-outputs.txt";
307 print " (failed to execute: $!)\n";
311 # this is not working right, $r == 0x8b00 when there is a core dump.
312 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
314 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
315 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
316 if($r & 127 || -f
"core") {
317 my $with = ($r & 128) ?
'with' : 'without';
321 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
322 if($linecount == 0) {
325 print " with error messages:\n";
326 showfile
($stderrlog);
328 return(($r & 128) ?
10 : 20);
330 if($linecount == 0) {
333 print " with error messages:\n";
334 showfile
($stderrlog);
340 if(defined($confighhash)) {
344 $main::confighhash
= {};
346 # this could be loaded once perhaps.
347 open(CONFIG_H
, "config.h") || die "Can not open config.h: $!\n";
350 if(/^\#define (.*) 1/) {
351 #print "Setting $1\n";
352 $main::confighhash
->{$1} = 1;
356 #print Dumper($main::confighhash);
358 # also run tcpdump --fp-type to get the type of floating-point
359 # arithmetic we're doing, setting a HAVE_{fptype} key based
360 # on the value it prints
361 open(FPTYPE_PIPE
, "$TCPDUMP --fp-type |") or die("piping tcpdump --fp-type failed\n");
362 my $fptype_val = <FPTYPE_PIPE
>;
365 if($fptype_val == "9877.895") {
366 $have_fptype = "HAVE_FPTYPE1";
368 $have_fptype = "HAVE_FPTYPE2";
370 $main::confighhash
->{$have_fptype} = 1;
372 return $main::confighhash
;
376 sub runOneComplexTest
{
377 local($testconfig) = @_;
379 my $output = $testconfig->{output
};
380 my $input = $testconfig->{input
};
381 my $name = $testconfig->{name
};
382 my $options= $testconfig->{args
};
386 my $configset = $testconfig->{config_set
};
387 my $configunset = $testconfig->{config_unset
};
388 my $ch = loadconfighash
();
391 if(defined($configset)) {
392 $foundit = ($ch->{$configset} == 1);
394 if(defined($configunset)) {
395 $unfoundit=($ch->{$configunset} != 1);
399 printf " %-40s: skipped (%s not set)\n", $name, $configset;
404 printf " %-40s: skipped (%s set)\n", $name, $configunset;
409 #print Dumper($testconfig);
411 # EXPAND any occurrences of @TESTDIR@ to $testsdir
412 $options =~ s/\@TESTDIR\@/$testsdir/;
414 my $result = runtest
($name,
415 $testsdir . "/" . $input,
416 $testsdir . "/" . $output,
426 # *.tests files are PERL hash definitions. They should create an array of hashes
427 # one per test, and place it into the variable @testlist.
428 sub runComplexTests
{
429 my @files = glob( $testsdir . '/*.tests' );
430 foreach $file (@files) {
431 my @testlist = undef;
433 print "FILE: ${file}\n";
434 open(FILE
, "<".$file) || die "can not open $file: $!";
437 $definitions = <FILE
>;
440 #print "STUFF: ${definitions}\n";
442 if(defined($testlist)) {
444 #print Dumper($testlist);
445 foreach $test (@
$testlist) {
446 runOneComplexTest
($test);
449 warn "File: ${file} could not be loaded as PERL: $!";
458 open(TESTLIST
, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
464 ($name, $input, $output, @options) = split;
465 #print "processing ${only} vs ${name}\n";
466 next if(defined($only) && $only ne $name);
468 my $options = join(" ", @options);
469 #print "@{options} becomes ${options}\n";
471 my $hash = { name
=> $name,
476 runOneComplexTest
($hash);
480 if(scalar(@ARGV) == 0) {
484 runSimpleTests
($ARGV[0]);
487 # exit with number of failing tests.
488 print "------------------------------------------------\n";
489 printf("%4u tests failed\n",$failedcount);
490 printf("%4u tests passed\n",$passedcount);
492 showfile
(${failureoutput
});