]> The Tcpdump Group git mirrors - tcpdump/blob - tests/TESTrun
Do the floating-point tests closer to what we need.
[tcpdump] / tests / TESTrun
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 use Cwd qw(abs_path getcwd);
8 use File::Path qw(mkpath); # mkpath works with ancient perl, as well as newer perl
9 use Data::Dumper; # for debugging.
10
11 # these are created in the directory where we are run, which might be
12 # a build directory.
13 my $newdir = "tests/NEW";
14 my $diffdir= "tests/DIFF";
15 mkpath($newdir);
16 mkpath($diffdir);
17 my $origdir = getcwd();
18 my $srcdir = $ENV{'srcdir'} || ".";
19
20 #
21 # Force UTC, so time stamps are printed in a standard time zone, and
22 # tests don't have to be run in the time zone in which the output
23 # file was generated.
24 #
25 $ENV{'TZ'}='GMT0';
26
27 #
28 # Get the tests directory from $0.
29 #
30 my $testsdir = dirname($0);
31
32 #
33 # Convert it to an absolute path, so it works even after we do a cd.
34 #
35 $testsdir = abs_path($testsdir);
36 print "Running tests from ${testsdir}\n";
37
38 unshift(@INC, $testsdir);
39 require 'testfuncs.pm';
40
41 $passedcount = 0;
42 $failedcount = 0;
43 #
44 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
45
46 # truncate the output file
47 open(FAILUREOUTPUT, ">" . $failureoutput);
48 close(FAILUREOUTPUT);
49
50 $confighhash = undef;
51
52 sub loadconfighash {
53 if(defined($confighhash)) {
54 return $confighhash;
55 }
56
57 $main::confighhash = {};
58
59 # this could be loaded once perhaps.
60 open(CONFIG_H, "config.h") || die "Can not open config.h: $!\n";
61 while(<CONFIG_H>) {
62 chomp;
63 if(/^\#define (.*) 1/) {
64 #print "Setting $1\n";
65 $main::confighhash->{$1} = 1;
66 }
67 }
68 close(CONFIG_H);
69 #print Dumper($main::confighhash);
70
71 # also run tcpdump --fp-type to get the type of floating-point
72 # arithmetic we're doing, setting a HAVE_{fptype} key based
73 # on the value it prints
74 open(FPTYPE_PIPE, "./tcpdump --fp-type |") or die("piping tcpdump --fp-type failed\n");
75 my $fptype_val = <FPTYPE_PIPE>;
76 close(FPTYPE_PIPE);
77 my $have_fptype;
78 if($fptype_val == "9877.895") {
79 $have_fptype = "HAVE_FPTYPE1";
80 } else {
81 $have_fptype = "HAVE_FPTYPE2";
82 }
83 $main::confighhash->{$have_fptype} = 1;
84
85 return $main::confighhash;
86 }
87
88
89 sub runOneComplexTest {
90 local($testconfig) = @_;
91
92 my $output = $testconfig->{output};
93 my $input = $testconfig->{input};
94 my $name = $testconfig->{name};
95 my $options= $testconfig->{args};
96 my $foundit = 1;
97 my $unfoundit=1;
98
99 my $configset = $testconfig->{config_set};
100 my $configunset = $testconfig->{config_unset};
101 my $ch = loadconfighash();
102 #print Dumper($ch);
103
104 if(defined($configset)) {
105 $foundit = ($ch->{$configset} == 1);
106 }
107 if(defined($configunset)) {
108 $unfoundit=($ch->{$configunset} != 1);
109 }
110
111 if(!$foundit) {
112 printf " %-40s: skipped (%s not set)\n", $name, $configset;
113 return 0;
114 }
115
116 if(!$unfoundit) {
117 printf " %-40s: skipped (%s set)\n", $name, $configunset;
118 return 0;
119 }
120
121 #use Data::Dumper;
122 #print Dumper($testconfig);
123
124 # EXPAND any occurances of @TESTDIR@ to $testsdir
125 $options =~ s/\@TESTDIR\@/$testsdir/;
126
127 my $result = runtest($name,
128 $testsdir . "/" . $input,
129 $testsdir . "/" . $output,
130 $options);
131
132 if($result == 0) {
133 $passedcount++;
134 } else {
135 $failedcount++;
136 }
137 }
138
139 # *.tests files are PERL hash definitions. They should create an array of hashes
140 # one per test, and place it into the variable @testlist.
141 sub runComplexTests {
142 my @files = glob( $testsdir . '/*.tests' );
143 foreach $file (@files) {
144 my @testlist = undef;
145 my $definitions;
146 print "FILE: ${file}\n";
147 open(FILE, "<".$file) || die "can not open $file: $!";
148 {
149 local $/ = undef;
150 $definitions = <FILE>;
151 }
152 close(FILE);
153 #print "STUFF: ${definitions}\n";
154 eval $definitions;
155 if(defined($testlist)) {
156 #use Data::Dumper;
157 #print Dumper($testlist);
158 foreach $test (@$testlist) {
159 runOneComplexTest($test);
160 }
161 } else {
162 warn "File: ${file} could not be loaded as PERL: $!";
163 }
164 }
165 }
166
167 sub runSimpleTests {
168
169 local($only)=@_;
170
171 open(TESTLIST, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
172 while(<TESTLIST>) {
173 next if /^\#/;
174 next if /^$/;
175
176 unlink("core");
177 ($name, $input, $output, @options) = split;
178 #print "processing ${only} vs ${name}\n";
179 next if(defined($only) && $only ne $name);
180
181 my $options = join(" ", @options);
182 #print "@{options} becomes ${options}\n";
183
184 my $hash = { name => $name,
185 input=> $input,
186 output=>$output,
187 args => $options };
188
189 runOneComplexTest($hash);
190 }
191 }
192
193 if(scalar(@ARGV) == 0) {
194 runSimpleTests();
195 runComplexTests();
196 } else {
197 runSimpleTests($ARGV[0]);
198 }
199
200 # exit with number of failing tests.
201 print "------------------------------------------------\n";
202 printf("%4u tests failed\n",$failedcount);
203 printf("%4u tests passed\n",$passedcount);
204
205 system("cat ${failureoutput}");
206 exit $failedcount;