]>
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'} || ".";
42 # Default to unified diff and allow to fall back to basic diff if necessary.
43 my $diff_flags = defined $ENV{'DIFF_FLAGS'} ?
$ENV{'DIFF_FLAGS'} : '-u';
46 # Force UTC, so time stamps are printed in a standard time zone, and
47 # tests don't have to be run in the time zone in which the output
53 # Get the tests directory from $0.
55 my $testsdir = dirname
($0);
58 # Convert it to an absolute path, so it works even after we do a cd.
60 $testsdir = abs_path
($testsdir);
61 print "Running tests from ${testsdir}\n";
62 print "with ${TCPDUMP}, version:\n";
63 system "${TCPDUMP} --version";
65 unshift(@INC, $testsdir);
70 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
72 # truncate the output file
73 open(FAILUREOUTPUT
, ">" . $failureoutput);
82 # XXX - just do this directly in Perl?
84 if ($^O
eq 'MSWin32') {
85 my $winpath = File
::Spec
->canonpath($path);
86 system "type $winpath";
93 local($name, $input, $output, $options) = @_;
96 $outputbase = basename
($output);
100 my $rawstderrlog = "tests/NEW/${outputbase}.raw.stderr";
101 my $stderrlog = "tests/NEW/${outputbase}.stderr";
105 # we used to do this as a nice pipeline, but the problem is that $r fails to
106 # to be set properly if the tcpdump core dumps.
108 # Furthermore, on Windows, fc can't read the standard input, so we
109 # can't do it as a pipeline in any case.
110 $r = system "$TCPDUMP -# -n -r $input $options >tests/NEW/${outputbase} 2>${rawstderrlog}";
113 # Something other than "tcpdump opened the file, read it, and
114 # dissected all the packets". What happened?
116 # We write out an exit status after whatever the subprocess
117 # wrote out, so it shows up when we diff the expected output
120 open(OUTPUT
, ">>"."tests/NEW/$outputbase") || die "fail to open $outputbase\n";
122 # failed to start due to error.
124 printf OUTPUT
"FAILED TO RUN: status: %d\n", $status;
126 if ($^O
eq 'MSWin32' or $^O
eq 'msys') {
128 # On Windows, the return value of system is the lower 8
129 # bits of the exit status of the process, shifted left
132 # If the process crashed, rather than exiting, the
133 # exit status will be one of the EXCEPTION_ values
134 # listed in the documentation for the GetExceptionCode()
137 # Those are defined as STATUS_ values, which should have
138 # 0xC in the topmost 4 bits (being fatal error
139 # statuses); some of them have a value that fits in
140 # the lower 8 bits. We could, I guess, assume that
141 # any value that 1) isn't returned by tcpdump and 2)
142 # corresponds to the lower 8 bits of a STATUS_ value
143 # used as an EXCEPTION_ value indicates that tcpdump
144 # exited with that exception.
146 # However, as we're running tcpdump with system, which
147 # runs the command through cmd.exe, and as cmd.exe
148 # doesn't map the command's exit code to its own exit
149 # code in any straightforward manner, we can't get
150 # that information in any case, so there's no point
151 # in trying to interpret it in that fashion.
156 # On UN*Xes, the return status is a POSIX as filled in
157 # by wait() or waitpid().
159 # POSIX offers some calls for analyzing it, such as
160 # WIFSIGNALED() to test whether it indicates that the
161 # process was terminated by a signal, WTERMSIG() to
162 # get the signal number from it, WIFEXITED() to test
163 # whether it indicates that the process exited normally,
164 # and WEXITSTATUS() to get the exit status from it.
166 # POSIX doesn't standardize core dumps, so the POSIX
167 # calls can't test whether a core dump occurred.
168 # However, all the UN*Xes we are likely to encounter
169 # follow Research UNIX in this regard, with the exit
170 # status containing either 0 or a signal number in
171 # the lower 7 bits, with 0 meaning "exited rather
172 # than being terminated by a signal", the "core dumped"
173 # flag in the 0x80 bit, and, if the signal number is
174 # 0, the exit status in the next 8 bits up.
176 # This should be cleaned up to use the POSIX calls
177 # from the Perl library - and to define an additional
178 # WCOREDUMP() call to test the "core dumped" bit and
181 # But note also that, as we're running tcpdump with
182 # system, which runs the command through a shell, if
183 # tcpdump crashes, we'll only know that if the shell
184 # maps the signal indication and uses that as its
187 # The good news is that the Bourne shell, and compatible
188 # shells, have traditionally done that. If the process
189 # for which the shell reports the exit status terminates
190 # with a signal, it adds 128 to the signal number and
191 # returns that as its exit status. (This is why the
192 # "this is now working right" behavior described in a
193 # comment below is occurring.)
195 # As tcpdump itself never returns with an exit status
196 # >= 128, we can try checking for an exit status with
197 # the 0x80 bit set and, if we have one, get the signal
198 # number from the lower 7 bits of the exit status. We
199 # can't get the "core dumped" indication from the
200 # shell's exit status; all we can do is check whether
201 # there's a core file.
204 $coredump = $r & 127;
207 $status = WEXITSTATUS
($r);
211 if($coredump || $status) {
212 printf OUTPUT
"EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
214 printf OUTPUT
"EXIT CODE %08x\n", $r;
222 # Compare tcpdump's output with what we think it should be.
223 # If tcpdump failed to produce output, we've produced our own
224 # "output" above, with the exit status.
226 if ($^O
eq 'MSWin32') {
227 my $winoutput = File
::Spec
->canonpath($output);
228 $r = system "fc /lb1000 /t /1 $winoutput tests\\NEW\\$outputbase >tests\\DIFF\\$outputbase.diff";
231 $r = system "diff $diff_flags $output tests/NEW/$outputbase >tests/DIFF/$outputbase.diff";
232 $diffstat = WEXITSTATUS
($r);
236 # process the standard error file, sanitize "reading from" line,
239 open(ERRORRAW
, "<" . $rawstderrlog);
240 open(ERROROUT
, ">" . $stderrlog);
242 next if /^$/; # blank lines are boring
243 if(/^(reading from file )(.*)(,.*)$/) {
244 my $filename = basename
($2);
245 print ERROROUT
"${1}${filename}${3}\n";
254 if ( -f
"$output.stderr" ) {
256 # Compare the standard error with what we think it should be.
258 if ($^O
eq 'MSWin32') {
259 my $winoutput = File
::Spec
->canonpath($output);
260 my $canonstderrlog = File
::Spec
->canonpath($stderrlog);
261 $nr = system "fc /lb1000 /t /1 $winoutput.stderr $canonstderrlog >tests\DIFF\$outputbase.stderr.diff";
262 $errdiffstat = $nr >> 8;
264 $nr = system "diff $output.stderr $stderrlog >tests/DIFF/$outputbase.stderr.diff";
265 $errdiffstat = WEXITSTATUS
($nr);
273 if($linecount == 0 && $status == 0) {
280 #print sprintf("END: %08x\n", $r);
283 if($linecount == 0) {
284 printf " %-40s: passed\n", $name;
286 printf " %-40s: passed with error messages:\n", $name;
287 showfile
($stderrlog);
289 unlink "tests/DIFF/$outputbase.diff";
293 printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
294 open FOUT
, '>>tests/failure-outputs.txt';
295 printf FOUT
"\nFailed test: $name\n\n";
297 if(-f
"tests/DIFF/$outputbase.diff") {
299 # XXX - just do this directly in Perl?
301 if ($^O
eq 'MSWin32') {
302 system "type tests\\DIFF\\$outputbase.diff >> tests\\failure-outputs.txt";
304 system "cat tests/DIFF/$outputbase.diff >> tests/failure-outputs.txt";
309 print " (failed to execute: $!)\n";
313 # this is not working right, $r == 0x8b00 when there is a core dump.
314 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
316 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
317 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
318 if($r & 127 || -f
"core") {
319 my $with = ($r & 128) ?
'with' : 'without';
323 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
324 if($linecount == 0) {
327 print " with error messages:\n";
328 showfile
($stderrlog);
330 return(($r & 128) ?
10 : 20);
332 if($linecount == 0) {
335 print " with error messages:\n";
336 showfile
($stderrlog);
342 if(defined($confighhash)) {
346 $main::confighhash
= {};
348 # this could be loaded once perhaps.
349 open(CONFIG_H
, "config.h") || die "Can not open config.h: $!\n";
352 if(/^\#define (.*) 1/) {
353 #print "Setting $1\n";
354 $main::confighhash
->{$1} = 1;
358 #print Dumper($main::confighhash);
360 # also run tcpdump --fp-type to get the type of floating-point
361 # arithmetic we're doing, setting a HAVE_{fptype} key based
362 # on the value it prints
363 open(FPTYPE_PIPE
, "$TCPDUMP --fp-type |") or die("piping tcpdump --fp-type failed\n");
364 my $fptype_val = <FPTYPE_PIPE
>;
367 if($fptype_val == "9877.895") {
368 $have_fptype = "HAVE_FPTYPE1";
370 $have_fptype = "HAVE_FPTYPE2";
372 $main::confighhash
->{$have_fptype} = 1;
374 return $main::confighhash
;
378 sub runOneComplexTest
{
379 local($testconfig) = @_;
381 my $output = $testconfig->{output
};
382 my $input = $testconfig->{input
};
383 my $name = $testconfig->{name
};
384 my $options= $testconfig->{args
};
388 my $configset = $testconfig->{config_set
};
389 my $configunset = $testconfig->{config_unset
};
390 my $ch = loadconfighash
();
393 if(defined($configset)) {
394 $foundit = ($ch->{$configset} == 1);
396 if(defined($configunset)) {
397 $unfoundit=($ch->{$configunset} != 1);
401 printf " %-40s: skipped (%s not set)\n", $name, $configset;
406 printf " %-40s: skipped (%s set)\n", $name, $configunset;
411 #print Dumper($testconfig);
413 # EXPAND any occurrences of @TESTDIR@ to $testsdir
414 $options =~ s/\@TESTDIR\@/$testsdir/;
416 my $result = runtest
($name,
417 $testsdir . "/" . $input,
418 $testsdir . "/" . $output,
428 # *.tests files are PERL hash definitions. They should create an array of hashes
429 # one per test, and place it into the variable @testlist.
430 sub runComplexTests
{
431 my @files = glob( $testsdir . '/*.tests' );
432 foreach $file (@files) {
433 my @testlist = undef;
435 print "FILE: ${file}\n";
436 open(FILE
, "<".$file) || die "can not open $file: $!";
439 $definitions = <FILE
>;
442 #print "STUFF: ${definitions}\n";
444 if(defined($testlist)) {
446 #print Dumper($testlist);
447 foreach $test (@
$testlist) {
448 runOneComplexTest
($test);
451 warn "File: ${file} could not be loaded as PERL: $!";
460 open(TESTLIST
, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
466 ($name, $input, $output, @options) = split;
467 #print "processing ${only} vs ${name}\n";
468 next if(defined($only) && $only ne $name);
470 my $options = join(" ", @options);
471 #print "@{options} becomes ${options}\n";
473 my $hash = { name
=> $name,
478 runOneComplexTest
($hash);
482 if(scalar(@ARGV) == 0) {
486 runSimpleTests
($ARGV[0]);
489 # exit with number of failing tests.
490 print "------------------------------------------------\n";
491 printf("%4u tests failed\n",$failedcount);
492 printf("%4u tests passed\n",$passedcount);
494 showfile
(${failureoutput
});