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