]> The Tcpdump Group git mirrors - tcpdump/blob - tests/TESTrun
TESTrun: Update Windows executable name to tcpdump.exe
[tcpdump] / tests / TESTrun
1 #!/usr/bin/env perl
2
3 #
4 # Were we told where to find tcpdump?
5 #
6 if (!($TCPDUMP = $ENV{TCPDUMP_BIN})) {
7 #
8 # No. Use the appropriate path.
9 #
10 if ($^O eq 'MSWin32') {
11 #
12 # XXX - assume, for now, a Visual Studio debug build, so that
13 # tcpdump is in the Debug subdirectory.
14 #
15 $TCPDUMP = "Debug\\tcpdump.exe"
16 } else {
17 $TCPDUMP = "./tcpdump"
18 }
19 }
20
21 #
22 # Make true and false work as Booleans.
23 #
24 use constant true => 1;
25 use constant false => 0;
26
27 use File::Basename;
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
31 use File::Spec;
32 use Data::Dumper; # for debugging.
33
34 # these are created in the directory where we are run, which might be
35 # a build directory.
36 my $newdir = "tests/NEW";
37 my $diffdir= "tests/DIFF";
38 mkpath($newdir);
39 mkpath($diffdir);
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';
44
45 #
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
48 # file was generated.
49 #
50 $ENV{'TZ'}='GMT0';
51
52 #
53 # Get the tests directory from $0.
54 #
55 my $testsdir = dirname($0);
56
57 #
58 # Convert it to an absolute path, so it works even after we do a cd.
59 #
60 $testsdir = abs_path($testsdir);
61 print "Running tests from ${testsdir}\n";
62 print "with ${TCPDUMP}, version:\n";
63 system "${TCPDUMP} --version";
64
65 unshift(@INC, $testsdir);
66
67 $passedcount = 0;
68 $failedcount = 0;
69 #
70 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
71
72 # truncate the output file
73 open(FAILUREOUTPUT, ">" . $failureoutput);
74 close(FAILUREOUTPUT);
75
76 $confighhash = undef;
77
78 sub showfile {
79 local($path) = @_;
80
81 #
82 # XXX - just do this directly in Perl?
83 #
84 if ($^O eq 'MSWin32') {
85 my $winpath = File::Spec->canonpath($path);
86 system "type $winpath";
87 } else {
88 system "cat $path";
89 }
90 }
91
92 sub runtest {
93 local($name, $input, $output, $options) = @_;
94 my $r;
95
96 $outputbase = basename($output);
97 my $coredump = false;
98 my $status = 0;
99 my $linecount = 0;
100 my $rawstderrlog = "${newdir}/${outputbase}.raw.stderr";
101 my $stderrlog = "${newdir}/${outputbase}.stderr";
102 my $diffstat = 0;
103 my $errdiffstat = 0;
104
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.
107 #
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 >${newdir}/${outputbase} 2>${rawstderrlog}";
111 if($r != 0) {
112 #
113 # Something other than "tcpdump opened the file, read it, and
114 # dissected all the packets". What happened?
115 #
116 # We write out an exit status after whatever the subprocess
117 # wrote out, so it shows up when we diff the expected output
118 # with it.
119 #
120 open(OUTPUT, ">>"."${newdir}/$outputbase") || die "fail to open $outputbase\n";
121 if($r == -1) {
122 # failed to start due to error.
123 $status = $!;
124 printf OUTPUT "FAILED TO RUN: status: %d\n", $status;
125 } else {
126 if ($^O eq 'MSWin32' or $^O eq 'msys') {
127 #
128 # On Windows, the return value of system is the lower 8
129 # bits of the exit status of the process, shifted left
130 # 8 bits.
131 #
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()
135 # macro.
136 #
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.
145 #
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.
152 #
153 $status = $r >> 8;
154 } else {
155 #
156 # On UN*Xes, the return status is a POSIX as filled in
157 # by wait() or waitpid().
158 #
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.
165 #
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.
175 #
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
179 # use that.
180 #
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
185 # exit status.
186 #
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.)
194 #
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.
202 #
203 if( $r & 128 ) {
204 $coredump = $r & 127;
205 }
206 if( WIFEXITED($r)) {
207 $status = WEXITSTATUS($r);
208 }
209 }
210
211 if($coredump || $status) {
212 printf OUTPUT "EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
213 } else {
214 printf OUTPUT "EXIT CODE %08x\n", $r;
215 }
216 $r = 0;
217 }
218 close(OUTPUT);
219 }
220 if($r == 0) {
221 #
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.
225 #
226 if ($^O eq 'MSWin32') {
227 my $winoutput = File::Spec->canonpath($output);
228 my $winnewdir = File::Spec->canonpath($newdir);
229 my $windiffdir = File::Spec->canonpath($diffdir);
230 $r = system "fc /lb1000 /t /1 $winoutput ${winnewdir}\\$outputbase >${windiffdir}\\$outputbase.diff";
231 $diffstat = $r >> 8;
232 } else {
233 $r = system "diff $diff_flags $output ${newdir}/$outputbase >${diffdir}/$outputbase.diff";
234 $diffstat = WEXITSTATUS($r);
235 }
236 }
237
238 # process the standard error file, sanitize "reading from" line,
239 # and count lines
240 $linecount = 0;
241 open(ERRORRAW, "<" . $rawstderrlog);
242 open(ERROROUT, ">" . $stderrlog);
243 while(<ERRORRAW>) {
244 next if /^$/; # blank lines are boring
245 if(/^(reading from file )(.*)(,.*)$/) {
246 my $filename = basename($2);
247 print ERROROUT "${1}${filename}${3}\n";
248 next;
249 }
250 print ERROROUT;
251 $linecount++;
252 }
253 close(ERROROUT);
254 close(ERRORRAW);
255
256 if ( -f "$output.stderr" ) {
257 #
258 # Compare the standard error with what we think it should be.
259 #
260 if ($^O eq 'MSWin32') {
261 my $winoutput = File::Spec->canonpath($output);
262 my $windiffdir = File::Spec->canonpath($diffdir);
263 my $canonstderrlog = File::Spec->canonpath($stderrlog);
264 $nr = system "fc /lb1000 /t /1 $winoutput.stderr $canonstderrlog >${windiffdir}\\$outputbase.stderr.diff";
265 $errdiffstat = $nr >> 8;
266 } else {
267 $nr = system "diff $output.stderr $stderrlog >${diffdir}/$outputbase.stderr.diff";
268 $errdiffstat = WEXITSTATUS($nr);
269 }
270 if($r == 0) {
271 $r = $nr;
272 }
273 }
274
275 if($r == 0) {
276 if($linecount == 0 && $status == 0) {
277 unlink($stderrlog);
278 } else {
279 $errdiffstat = 1;
280 }
281 }
282
283 #print sprintf("END: %08x\n", $r);
284
285 if($r == 0) {
286 if($linecount == 0) {
287 printf " %-40s: passed\n", $name;
288 } else {
289 printf " %-40s: passed with error messages:\n", $name;
290 showfile($stderrlog);
291 }
292 unlink "${diffdir}/$outputbase.diff";
293 return 0;
294 }
295 # must have failed!
296 printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
297 open FOUT, '>>tests/failure-outputs.txt';
298 printf FOUT "\nFailed test: $name\n\n";
299 close FOUT;
300 if(-f "${diffdir}/$outputbase.diff") {
301 #
302 # XXX - just do this directly in Perl?
303 #
304 if ($^O eq 'MSWin32') {
305 my $windiffdir = File::Spec->canonpath($diffdir);
306 system "type ${windiffdir}\\$outputbase.diff >> tests\\failure-outputs.txt";
307 } else {
308 system "cat ${diffdir}/$outputbase.diff >> tests/failure-outputs.txt";
309 }
310 }
311
312 if($r == -1) {
313 print " (failed to execute: $!)\n";
314 return(30);
315 }
316
317 # this is not working right, $r == 0x8b00 when there is a core dump.
318 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
319 # too.
320 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
321 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
322 if($r & 127 || -f "core") {
323 my $with = ($r & 128) ? 'with' : 'without';
324 if(-f "core") {
325 $with = "with";
326 }
327 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
328 if($linecount == 0) {
329 print "\n";
330 } else {
331 print " with error messages:\n";
332 showfile($stderrlog);
333 }
334 return(($r & 128) ? 10 : 20);
335 }
336 if($linecount == 0) {
337 print "\n";
338 } else {
339 print " with error messages:\n";
340 showfile($stderrlog);
341 }
342 return(5);
343 }
344
345 sub loadconfighash {
346 if(defined($confighhash)) {
347 return $confighhash;
348 }
349
350 $main::confighhash = {};
351
352 # this could be loaded once perhaps.
353 open(CONFIG_H, "config.h") || die "Can not open config.h: $!\n";
354 while(<CONFIG_H>) {
355 chomp;
356 if(/^\#define (.*) 1/) {
357 #print "Setting $1\n";
358 $main::confighhash->{$1} = 1;
359 }
360 }
361 close(CONFIG_H);
362 #print Dumper($main::confighhash);
363
364 # also run tcpdump --fp-type to get the type of floating-point
365 # arithmetic we're doing, setting a HAVE_{fptype} key based
366 # on the value it prints
367 open(FPTYPE_PIPE, "$TCPDUMP --fp-type |") or die("piping tcpdump --fp-type failed\n");
368 my $fptype_val = <FPTYPE_PIPE>;
369 close(FPTYPE_PIPE);
370 my $have_fptype;
371 if($fptype_val == "9877.895") {
372 $have_fptype = "HAVE_FPTYPE1";
373 } else {
374 $have_fptype = "HAVE_FPTYPE2";
375 }
376 $main::confighhash->{$have_fptype} = 1;
377
378 # and check whether this is OpenBSD, as one test fails in OpenBSD
379 # due to the sad hellscape of low-numbered DLT_ values, due to
380 # 12 meaning "OpenBSD loopback" rather than "raw IP" on OpenBSD
381 if($^O eq "openbsd") {
382 $main::confighhash->{"IS_OPENBSD"} = 1;
383 }
384
385 return $main::confighhash;
386 }
387
388
389 sub runOneComplexTest {
390 local($testconfig) = @_;
391
392 my $output = $testconfig->{output};
393 my $input = $testconfig->{input};
394 my $name = $testconfig->{name};
395 my $options= $testconfig->{args};
396 my $foundit = 1;
397 my $unfoundit=1;
398
399 my $configset = $testconfig->{config_set};
400 my $configunset = $testconfig->{config_unset};
401 my $ch = loadconfighash();
402 #print Dumper($ch);
403
404 if(defined($configset)) {
405 $foundit = ($ch->{$configset} == 1);
406 }
407 if(defined($configunset)) {
408 $unfoundit=($ch->{$configunset} != 1);
409 }
410
411 if(!$foundit) {
412 printf " %-40s: skipped (%s not set)\n", $name, $configset;
413 return 0;
414 }
415
416 if(!$unfoundit) {
417 printf " %-40s: skipped (%s set)\n", $name, $configunset;
418 return 0;
419 }
420
421 #use Data::Dumper;
422 #print Dumper($testconfig);
423
424 # EXPAND any occurrences of @TESTDIR@ to $testsdir
425 $options =~ s/\@TESTDIR\@/$testsdir/;
426
427 my $result = runtest($name,
428 $testsdir . "/" . $input,
429 $testsdir . "/" . $output,
430 $options);
431
432 if($result == 0) {
433 $passedcount++;
434 } else {
435 $failedcount++;
436 }
437 }
438
439 # *.tests files are PERL hash definitions. They should create an array of hashes
440 # one per test, and place it into the variable @testlist.
441 sub runComplexTests {
442 my @files = glob( $testsdir . '/*.tests' );
443 foreach $file (@files) {
444 my @testlist = undef;
445 my $definitions;
446 print "FILE: ${file}\n";
447 open(FILE, "<".$file) || die "can not open $file: $!";
448 {
449 local $/ = undef;
450 $definitions = <FILE>;
451 }
452 close(FILE);
453 #print "STUFF: ${definitions}\n";
454 eval $definitions;
455 if(defined($testlist)) {
456 #use Data::Dumper;
457 #print Dumper($testlist);
458 foreach $test (@$testlist) {
459 runOneComplexTest($test);
460 }
461 } else {
462 warn "File: ${file} could not be loaded as PERL: $!";
463 }
464 }
465 }
466
467 sub runSimpleTests {
468
469 local($only)=@_;
470
471 open(TESTLIST, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
472 while(<TESTLIST>) {
473 next if /^\#/;
474 next if /^$/;
475
476 unlink("core");
477 ($name, $input, $output, @options) = split;
478 #print "processing ${only} vs ${name}\n";
479 next if(defined($only) && $only ne $name);
480
481 my $options = join(" ", @options);
482 #print "@{options} becomes ${options}\n";
483
484 my $hash = { name => $name,
485 input=> $input,
486 output=>$output,
487 args => $options };
488
489 runOneComplexTest($hash);
490 }
491 }
492
493 if(scalar(@ARGV) == 0) {
494 runSimpleTests();
495 runComplexTests();
496 } else {
497 runSimpleTests($ARGV[0]);
498 }
499
500 # exit with number of failing tests.
501 print "------------------------------------------------\n";
502 printf("%4u tests failed\n",$failedcount);
503 printf("%4u tests passed\n",$passedcount);
504
505 showfile(${failureoutput});
506 exit $failedcount;