]> The Tcpdump Group git mirrors - tcpdump/blob - tests/TESTonce
VRRP: Add a test capture file
[tcpdump] / tests / TESTonce
1 #!/usr/bin/env perl
2
3 $TCPDUMP = "../tcpdump" if (!($TCPDUMP = $ENV{TCPDUMP_BIN}));
4
5 use File::Basename;
6 use POSIX qw( WEXITSTATUS WIFEXITED);
7
8 system("mkdir -p NEW DIFF");
9
10 if(@ARGV != 4) {
11 print "Usage: TESTonce name input output options\n";
12 exit 20;
13 }
14
15 $name=$ARGV[0];
16 $input=$ARGV[1];
17 $output=$ARGV[2];
18 $options=$ARGV[3];
19
20 my $r;
21
22 $outputbase = basename($output);
23 my $coredump = false;
24 my $status = 0;
25 my $linecount = 0;
26 my $rawstderrlog = "NEW/${outputbase}.raw.stderr";
27 my $stderrlog = "NEW/${outputbase}.stderr";
28 my $diffstat = 0;
29 my $errdiffstat = 0;
30
31 if ($^O eq 'MSWin32') {
32 $r = system "..\\windump -# -n -r $input $options 2>NUL | sed 's/\\r//' | tee NEW/$outputbase | diff $output - >DIFF/$outputbase.diff";
33 # need to do same as below for Cygwin.
34 }
35 else {
36 # we used to do this as a nice pipeline, but the problem is that $r fails to
37 # to be set properly if the tcpdump core dumps.
38 $r = system "$TCPDUMP 2>${rawstderrlog} -# -n -r $input $options >NEW/${outputbase}";
39 if($r == -1) {
40 # failed to start due to error.
41 $status = $!;
42 }
43 if($r != 0) {
44 $coredump = false;
45 $status = 0;
46 # this means tcpdump failed.
47 open(OUTPUT, ">>"."NEW/$outputbase") || die "fail to open $outputbase\n";
48 if( $r & 128 ) {
49 $coredump = $r & 127;
50 }
51 if( WIFEXITED($r)) {
52 $status = WEXITSTATUS($r);
53 }
54
55 if($coredump || $status) {
56 printf OUTPUT "EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
57 } else {
58 printf OUTPUT "EXIT CODE %08x\n", $r;
59 }
60 close(OUTPUT);
61 $r = 0;
62 }
63 if($r == 0) {
64 $r = system "cat NEW/$outputbase | diff $output - >DIFF/$outputbase.diff";
65 $diffstat = WEXITSTATUS($r);
66 }
67
68 # process the file, sanitize "reading from" line, and count lines
69 $linecount = 0;
70 open(ERRORRAW, "<" . $rawstderrlog);
71 open(ERROROUT, ">" . $stderrlog);
72 while(<ERRORRAW>) {
73 next if /^$/; # blank lines are boring
74 if(/^(reading from file )(.*)(,.*)$/) {
75 my $filename = basename($2);
76 print ERROROUT "${1}${filename}${3}\n";
77 next;
78 }
79 print ERROROUT;
80 $linecount++;
81 }
82 close(ERROROUT);
83 close(ERRORRAW);
84
85 if ( -f "$output.stderr" ) {
86 $nr = system "cat $stderrlog | diff $output.stderr - >DIFF/$outputbase.stderr.diff";
87 if($r == 0) {
88 $r = $nr;
89 }
90 $errdiffstat = WEXITSTATUS($nr);
91 }
92
93 if($r == 0) {
94 if($linecount == 0 && $status == 0) {
95 unlink($stderrlog);
96 } else {
97 $errdiffstat = 1;
98 }
99 }
100
101 #print sprintf("END: %08x\n", $r);
102 }
103
104 if($r == 0) {
105 if($linecount == 0) {
106 printf " %-35s: passed\n", $name;
107 } else {
108 printf " %-35s: passed with error messages:\n", $name;
109 system "cat $stderrlog";
110 }
111 unlink "DIFF/$outputbase.diff";
112 exit 0;
113 }
114 # must have failed!
115 printf " %-35s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
116 open FOUT, '>>failure-outputs.txt';
117 printf FOUT "\nFailed test: $name\n\n";
118 close FOUT;
119 if(-f "DIFF/$outputbase.diff") {
120 system "cat DIFF/$outputbase.diff >> failure-outputs.txt";
121 }
122
123 if($r == -1) {
124 print " (failed to execute: $!)\n";
125 exit 30;
126 }
127
128 # this is not working right, $r == 0x8b00 when there is a core dump.
129 # clearly, we need some platform specific perl magic to take this apart, so look for "core"
130 # too.
131 # In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
132 # a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
133 if($r & 127 || -f "core") {
134 my $with = ($r & 128) ? 'with' : 'without';
135 if(-f "core") {
136 $with = "with";
137 }
138 printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
139 if($linecount == 0) {
140 print "\n";
141 } else {
142 print " with error messages:\n";
143 system "cat $stderrlog";
144 }
145 exit ($r & 128) ? 10 : 20;
146 }
147 if($linecount == 0) {
148 print "\n";
149 } else {
150 print " with error messages:\n";
151 system "cat $stderrlog";
152 }
153 exit $r >> 8;