]> The Tcpdump Group git mirrors - tcpdump/blob - tests/TESTrun
use perl hashes for configuration, rather than YAML things
[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
10 # these are created in the directory where we are run, which might be
11 # a build directory.
12 my $newdir = "tests/NEW";
13 my $diffdir= "tests/DIFF";
14 mkpath($newdir);
15 mkpath($diffdir);
16 my $origdir = getcwd();
17 my $srcdir = $ENV{'srcdir'} || ".";
18
19 #
20 # Force UTC, so time stamps are printed in a standard time zone, and
21 # tests don't have to be run in the time zone in which the output
22 # file was generated.
23 #
24 $ENV{'TZ'}='GMT0';
25
26 #
27 # Get the tests directory from $0.
28 #
29 my $testsdir = dirname($0);
30
31 #
32 # Convert it to an absolute path, so it works even after we do a cd.
33 #
34 $testsdir = abs_path($testsdir);
35 print "Running tests from ${testsdir}\n";
36
37 unshift(@INC, $testsdir);
38 require 'testfuncs.pm';
39
40 $passedcount = 0;
41 $failedcount = 0;
42 #
43 # XXX - create files used by shell scripts; we can remove this once we
44 # no longer use shell scripts in tests.
45 #
46 open(PASSEDCOUNT, ">", "tests/.passed");
47 print PASSEDCOUNT "${passedcount}\n";
48 close(PASSEDCOUNT);
49 open(FAILEDCOUNT, ">", "tests/.failed");
50 print FAILEDCOUNT "${failedcount}\n";
51 close(FAILEDCOUNT);
52 my $failureoutput=$origdir . "/tests/failure-outputs.txt";
53
54 # truncate the output file
55 open(FAILUREOUTPUT, ">" . $failureoutput);
56 close(FAILUREOUTPUT);
57
58 sub runShellTests {
59
60 my @files = glob( $testsdir . '/*.sh' );
61 foreach $file (@files) {
62 if($file =~ /TEST.*\.sh/) {
63 print "File $file, skipped\n";
64 next;
65 }
66
67 print "Running $file\n";
68 system("cd tests && sh $file ${srcdir}")
69 }
70
71 # have to update passed/failed here
72 }
73
74 sub runOneComplexTest {
75 local($testconfig) = @_;
76
77 my $output = $testconfig->{output};
78 my $input = $testconfig->{input};
79 my $name = $testconfig->{name};
80 my $options= $testconfig->{args};
81
82 #use Data::Dumper;
83 #print Dumper($testconfig);
84
85 my $result = runtest($name,
86 $testsdir . "/" . $input,
87 $testsdir . "/" . $output,
88 $options);
89
90 if($result == 0) {
91 $passedcount++;
92 } else {
93 $failedcount++;
94 }
95 }
96
97 # *.tests files are PERL hash definitions. They should create an array of hashes
98 # one per test, and place it into the variable @testlist.
99 sub runComplexTests {
100 my @files = glob( $testsdir . '/*.tests' );
101 foreach $file (@files) {
102 my @testlist = undef;
103 print "FILE: ${file}\n";
104 open(FILE, "<".$file) || die "can not open $file: $!";
105 local $/ = undef;
106 $definitions = <FILE>;
107 close(FILE);
108 #print "STUFF: ${definitions}\n";
109 eval $definitions;
110 if(defined($testlist)) {
111 #use Data::Dumper;
112 #print Dumper($testlist);
113 foreach $test (@$testlist) {
114 runOneComplexTest($test);
115 }
116 } else {
117 warn "File: ${file} could not be loaded as PERL: $!";
118 }
119 }
120 }
121
122 sub runSimpleTests {
123
124 local($only)=@_;
125
126 open(TESTLIST, "<" . "${testsdir}/TESTLIST") || die "no ${testsdir}/TESTFILE: $!\n";
127 while(<TESTLIST>) {
128 next if /^\#/;
129 next if /^$/;
130
131 unlink("core");
132 ($name, $input, $output, @options) = split;
133 #print "processing ${only} vs ${name}\n";
134 next if(defined($only) && $only ne $name);
135
136 my $options = join(" ", @options);
137 #print "@{options} becomes ${options}\n";
138
139 my $hash = { name => $name,
140 input=> $input,
141 output=>$output,
142 args => $options };
143
144 runOneComplexTest($hash);
145 }
146 }
147
148 if(scalar(@ARGV) == 0) {
149 runShellTests();
150 runSimpleTests();
151 runComplexTests();
152 } else {
153 runSimpleTests($ARGV[0]);
154 }
155
156 #
157 # XXX - read files used by shell scripts, and add the passed and failed
158 # counts reported therein to our own passed and failed counts; we can
159 # remove this once we no longer use shell scripts in tests.
160 #
161 open(PASSEDCOUNT, "<", "tests/.passed");
162 $passedcount += <PASSEDCOUNT>;
163 close(PASSEDCOUNT);
164 open(FAILEDCOUNT, ">", "tests/.failed");
165 $failedcount += <FAILEDCOUNT>;
166 close(FAILEDCOUNT);
167
168 # exit with number of failing tests.
169 print "------------------------------------------------\n";
170 printf("%4u tests failed\n",$failedcount);
171 printf("%4u tests passed\n",$passedcount);
172
173 system("cat ${failureoutput}");
174 exit $failedcount;