]> The Tcpdump Group git mirrors - tcpdump/blob - tests/TESTrun
Revert "added libyaml-perl to list of packages for Linux"
[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 grovel Makefile for some things and pretend they are config options.
72 open(MAKEFILE, "Makefile") || die "can not open Makefile: $!\n";
73 while(<MAKEFILE>) {
74 chomp;
75 #print "Processing $_\n";
76 if(/^CC\s*=.*gcc.*/) {
77 #print "GCC FOUND\n";
78 $main::confighhash->{'USING_GCC'} = 1;
79 }
80 }
81 close(MAKEFILE);
82
83 return $main::confighhash;
84 }
85
86
87 sub runOneComplexTest {
88 local($testconfig) = @_;
89
90 my $output = $testconfig->{output};
91 my $input = $testconfig->{input};
92 my $name = $testconfig->{name};
93 my $options= $testconfig->{args};
94 my $foundit = 1;
95 my $unfoundit=1;
96
97 my $configset = $testconfig->{config_set};
98 my $configunset = $testconfig->{config_unset};
99 my $ch = loadconfighash();
100 #print Dumper($ch);
101
102 if(defined($configset)) {
103 $foundit = ($ch->{$configset} == 1);
104 }
105 if(defined($configunset)) {
106 $unfoundit=($ch->{$configunset} != 1);
107 }
108
109 if(!$foundit) {
110 print "${name} ... skipped, no ${configset}\n";
111 return 0;
112 }
113
114 if(!$unfoundit) {
115 print "${name} ... skipped, ${configunset} is set\n";
116 return 0;
117 }
118
119 #use Data::Dumper;
120 #print Dumper($testconfig);
121
122 # EXPAND any occurances of @TESTDIR@ to $testsdir
123 $options =~ s/\@TESTDIR\@/$testsdir/;
124
125 my $result = runtest($name,
126 $testsdir . "/" . $input,
127 $testsdir . "/" . $output,
128 $options);
129
130 if($result == 0) {
131 $passedcount++;
132 } else {
133 $failedcount++;
134 }
135 }
136
137 # *.tests files are PERL hash definitions. They should create an array of hashes
138 # one per test, and place it into the variable @testlist.
139 sub runComplexTests {
140 my @files = glob( $testsdir . '/*.tests' );
141 foreach $file (@files) {
142 my @testlist = undef;
143 my $definitions;
144 print "FILE: ${file}\n";
145 open(FILE, "<".$file) || die "can not open $file: $!";
146 {
147 local $/ = undef;
148 $definitions = <FILE>;
149 }
150 close(FILE);
151 #print "STUFF: ${definitions}\n";
152 eval $definitions;
153 if(defined($testlist)) {
154 #use Data::Dumper;
155 #print Dumper($testlist);
156 foreach $test (@$testlist) {
157 runOneComplexTest($test);
158 }
159 } else {
160 warn "File: ${file} could not be loaded as PERL: $!";
161 }
162 }
163 }
164
165 sub runSimpleTests {
166
167 local($only)=@_;
168
169 open(TESTLIST, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
170 while(<TESTLIST>) {
171 next if /^\#/;
172 next if /^$/;
173
174 unlink("core");
175 ($name, $input, $output, @options) = split;
176 #print "processing ${only} vs ${name}\n";
177 next if(defined($only) && $only ne $name);
178
179 my $options = join(" ", @options);
180 #print "@{options} becomes ${options}\n";
181
182 my $hash = { name => $name,
183 input=> $input,
184 output=>$output,
185 args => $options };
186
187 runOneComplexTest($hash);
188 }
189 }
190
191 if(scalar(@ARGV) == 0) {
192 runSimpleTests();
193 runComplexTests();
194 } else {
195 runSimpleTests($ARGV[0]);
196 }
197
198 # exit with number of failing tests.
199 print "------------------------------------------------\n";
200 printf("%4u tests failed\n",$failedcount);
201 printf("%4u tests passed\n",$passedcount);
202
203 system("cat ${failureoutput}");
204 exit $failedcount;