Skip to content

Commit 900f485

Browse files
committed
move test invocation code into subroutine
1 parent 77328e8 commit 900f485

File tree

1 file changed

+116
-115
lines changed

1 file changed

+116
-115
lines changed

tests/TESTonce

Lines changed: 116 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -12,142 +12,143 @@ if(@ARGV != 4) {
1212
exit 20;
1313
}
1414

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 = $!;
15+
sub runtest {
16+
local($name, $input, $output, $options) = @_;
17+
my $r;
18+
19+
$outputbase = basename($output);
20+
my $coredump = false;
21+
my $status = 0;
22+
my $linecount = 0;
23+
my $rawstderrlog = "NEW/${outputbase}.raw.stderr";
24+
my $stderrlog = "NEW/${outputbase}.stderr";
25+
my $diffstat = 0;
26+
my $errdiffstat = 0;
27+
28+
if ($^O eq 'MSWin32') {
29+
$r = system "..\\windump -# -n -r $input $options 2>NUL | sed 's/\\r//' | tee NEW/$outputbase | diff $output - >DIFF/$outputbase.diff";
30+
# need to do same as below for Cygwin.
4231
}
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;
32+
else {
33+
# we used to do this as a nice pipeline, but the problem is that $r fails to
34+
# to be set properly if the tcpdump core dumps.
35+
$r = system "$TCPDUMP 2>${rawstderrlog} -# -n -r $input $options >NEW/${outputbase}";
36+
if($r == -1) {
37+
# failed to start due to error.
38+
$status = $!;
5039
}
51-
if( WIFEXITED($r)) {
52-
$status = WEXITSTATUS($r);
40+
if($r != 0) {
41+
$coredump = false;
42+
$status = 0;
43+
# this means tcpdump failed.
44+
open(OUTPUT, ">>"."NEW/$outputbase") || die "fail to open $outputbase\n";
45+
if( $r & 128 ) {
46+
$coredump = $r & 127;
47+
}
48+
if( WIFEXITED($r)) {
49+
$status = WEXITSTATUS($r);
50+
}
51+
52+
if($coredump || $status) {
53+
printf OUTPUT "EXIT CODE %08x: dump:%d code: %d\n", $r, $coredump, $status;
54+
} else {
55+
printf OUTPUT "EXIT CODE %08x\n", $r;
56+
}
57+
close(OUTPUT);
58+
$r = 0;
5359
}
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;
60+
if($r == 0) {
61+
$r = system "cat NEW/$outputbase | diff $output - >DIFF/$outputbase.diff";
62+
$diffstat = WEXITSTATUS($r);
5963
}
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;
64+
65+
# process the file, sanitize "reading from" line, and count lines
66+
$linecount = 0;
67+
open(ERRORRAW, "<" . $rawstderrlog);
68+
open(ERROROUT, ">" . $stderrlog);
69+
while(<ERRORRAW>) {
70+
next if /^$/; # blank lines are boring
71+
if(/^(reading from file )(.*)(,.*)$/) {
72+
my $filename = basename($2);
73+
print ERROROUT "${1}${filename}${3}\n";
74+
next;
75+
}
76+
print ERROROUT;
77+
$linecount++;
7878
}
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";
79+
close(ERROROUT);
80+
close(ERRORRAW);
81+
82+
if ( -f "$output.stderr" ) {
83+
$nr = system "cat $stderrlog | diff $output.stderr - >DIFF/$outputbase.stderr.diff";
84+
if($r == 0) {
85+
$r = $nr;
86+
}
87+
$errdiffstat = WEXITSTATUS($nr);
88+
}
89+
8790
if($r == 0) {
88-
$r = $nr;
91+
if($linecount == 0 && $status == 0) {
92+
unlink($stderrlog);
93+
} else {
94+
$errdiffstat = 1;
95+
}
8996
}
90-
$errdiffstat = WEXITSTATUS($nr);
97+
98+
#print sprintf("END: %08x\n", $r);
9199
}
92100

93101
if($r == 0) {
94-
if($linecount == 0 && $status == 0) {
95-
unlink($stderrlog);
102+
if($linecount == 0) {
103+
printf " %-40s: passed\n", $name;
96104
} else {
97-
$errdiffstat = 1;
105+
printf " %-40s: passed with error messages:\n", $name;
106+
system "cat $stderrlog";
98107
}
108+
unlink "DIFF/$outputbase.diff";
109+
exit 0;
99110
}
100-
101-
#print sprintf("END: %08x\n", $r);
102-
}
103-
104-
if($r == 0) {
105-
if($linecount == 0) {
106-
printf " %-40s: passed\n", $name;
107-
} else {
108-
printf " %-40s: passed with error messages:\n", $name;
109-
system "cat $stderrlog";
111+
# must have failed!
112+
printf " %-40s: TEST FAILED(exit core=%d/diffstat=%d,%d/r=%d)", $name, $coredump, $diffstat, $errdiffstat, $r;
113+
open FOUT, '>>failure-outputs.txt';
114+
printf FOUT "\nFailed test: $name\n\n";
115+
close FOUT;
116+
if(-f "DIFF/$outputbase.diff") {
117+
system "cat DIFF/$outputbase.diff >> failure-outputs.txt";
118+
}
119+
120+
if($r == -1) {
121+
print " (failed to execute: $!)\n";
122+
exit 30;
110123
}
111-
unlink "DIFF/$outputbase.diff";
112-
exit 0;
113-
}
114-
# must have failed!
115-
printf " %-40s: 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-
}
127124

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";
125+
# this is not working right, $r == 0x8b00 when there is a core dump.
126+
# clearly, we need some platform specific perl magic to take this apart, so look for "core"
127+
# too.
128+
# In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
129+
# a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
130+
if($r & 127 || -f "core") {
131+
my $with = ($r & 128) ? 'with' : 'without';
132+
if(-f "core") {
133+
$with = "with";
134+
}
135+
printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
136+
if($linecount == 0) {
137+
print "\n";
138+
} else {
139+
print " with error messages:\n";
140+
system "cat $stderrlog";
141+
}
142+
exit ($r & 128) ? 10 : 20;
137143
}
138-
printf " (terminated with signal %u, %s coredump)", ($r & 127), $with;
139144
if($linecount == 0) {
140145
print "\n";
141146
} else {
142147
print " with error messages:\n";
143148
system "cat $stderrlog";
144149
}
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";
152150
}
151+
152+
$r = runtest($ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3]);
153153
exit $r >> 8;
154+

0 commit comments

Comments
 (0)