]> sjero.net Git - wget/blob - tests/WgetTest.pm.in
Improved exit status handling.
[wget] / tests / WgetTest.pm.in
1 # WARNING!
2 # WgetTest.pm is a generated file! Do not edit! Edit WgetTest.pm.in
3 # instead.
4
5 package WgetTest;
6 $VERSION = 0.01;
7
8 use strict;
9 use warnings;
10
11 use Cwd;
12 use File::Path;
13
14 our $WGETPATH = "@abs_top_builddir@/src/wget";
15
16 my @unexpected_downloads = ();
17
18 {
19     my %_attr_data = ( # DEFAULT
20         _cmdline      => "",
21         _workdir      => Cwd::getcwd(),
22         _errcode      => 0,
23         _existing     => {},
24         _input        => {},
25         _name         => "",
26         _output       => {},
27     );
28
29     sub _default_for
30     {
31         my ($self, $attr) = @_;
32         $_attr_data{$attr};
33     }
34
35     sub _standard_keys
36     {
37         keys %_attr_data;
38     }
39 }
40
41
42 sub new {
43     my ($caller, %args) = @_;
44     my $caller_is_obj = ref($caller);
45     my $class = $caller_is_obj || $caller;
46     #print STDERR "class = ", $class, "\n";
47     #print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
48     my $self = bless {}, $class;
49     foreach my $attrname ($self->_standard_keys()) {
50         #print STDERR "attrname = ", $attrname, " value = ";
51         my ($argname) = ($attrname =~ /^_(.*)/);
52         if (exists $args{$argname}) {
53             #printf STDERR "Setting up $attrname\n";
54             $self->{$attrname} = $args{$argname};
55         } elsif ($caller_is_obj) {
56             #printf STDERR "Copying $attrname\n";
57             $self->{$attrname} = $caller->{$attrname};
58         } else {
59             #printf STDERR "Using default for $attrname\n";
60             $self->{$attrname} = $self->_default_for($attrname);
61         }
62         #print STDERR $attrname, '=', $self->{$attrname}, "\n";
63     }
64     #printf STDERR "_workdir default = ", $self->_default_for("_workdir");
65     return $self;
66 }
67
68
69 sub run {
70     my $self = shift;
71     my $result_message = "Test successful.\n";
72
73     printf "Running test $self->{_name}\n";
74
75     # Setup
76     $self->_setup();
77     chdir ("$self->{_workdir}/$self->{_name}/input");
78
79     # Launch server
80     my $pid = $self->_fork_and_launch_server();
81
82     # Call wget
83     chdir ("$self->{_workdir}/$self->{_name}/output");
84     my $cmdline = $self->{_cmdline};
85     $cmdline = $self->_substitute_port($cmdline);
86     print "Calling $cmdline\n";
87     my $errcode =
88         ($cmdline =~ m{^/.*})
89             ? system ($cmdline)
90             : system ("$self->{_workdir}/../src/$cmdline");
91     $errcode >>= 8; # XXX: should handle abnormal error codes.
92
93     # Shutdown server
94     # if we didn't explicitely kill the server, we would have to call
95     # waitpid ($pid, 0) here in order to wait for the child process to
96     # terminate
97     kill ('TERM', $pid);
98
99     # Verify download
100     unless ($errcode == $self->{_errcode}) {
101         $result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
102     }
103     my $error_str;
104     if ($error_str = $self->_verify_download()) {
105         $result_message = $error_str;
106     }
107
108     # Cleanup
109     $self->_cleanup();
110
111     print $result_message;
112     return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
113 }
114
115
116 sub _setup {
117     my $self = shift;
118
119     #print $self->{_name}, "\n";
120     chdir ($self->{_workdir});
121
122     # Create temporary directory
123     mkdir ($self->{_name});
124     chdir ($self->{_name});
125     mkdir ("input");
126     mkdir ("output");
127
128     # Setup existing files
129     chdir ("output");
130     foreach my $filename (keys %{$self->{_existing}}) {
131         open (FILE, ">$filename")
132             or return "Test failed: cannot open pre-existing file $filename\n";
133
134         my $file = $self->{_existing}->{$filename};
135         print FILE $file->{content}
136             or return "Test failed: cannot write pre-existing file $filename\n";
137
138         close (FILE);
139
140         if (exists($file->{timestamp})) {
141             utime $file->{timestamp}, $file->{timestamp}, $filename
142                 or return "Test failed: cannot set timestamp on pre-existing file $filename\n";
143         }
144     }
145
146     chdir ("../input");
147     $self->_setup_server();
148
149     chdir ($self->{_workdir});
150 }
151
152
153 sub _cleanup {
154     my $self = shift;
155
156     chdir ($self->{_workdir});
157     File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
158 }
159
160
161 sub _verify_download {
162     my $self = shift;
163
164     chdir ("$self->{_workdir}/$self->{_name}/output");
165
166     # use slurp mode to read file content
167     my $old_input_record_separator = $/;
168     undef $/;
169
170     while (my ($filename, $filedata) = each %{$self->{_output}}) {
171         open (FILE, $filename)
172             or return "Test failed: file $filename not downloaded\n";
173
174         my $content = <FILE>;
175         my $expected_content = $filedata->{'content'};
176         $expected_content = $self->_substitute_port($expected_content);
177         $content eq $expected_content
178             or return "Test failed: wrong content for file $filename\n";
179
180         if (exists($filedata->{'timestamp'})) {
181             my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
182                 $atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
183
184             $mtime == $filedata->{'timestamp'}
185                 or return "Test failed: wrong timestamp for file $filename\n";
186         }
187
188         close (FILE);
189     }
190
191     $/ = $old_input_record_separator;
192
193     # make sure no unexpected files were downloaded
194     chdir ("$self->{_workdir}/$self->{_name}/output");
195
196     __dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[0]}) }, sub { shift; return @_ } );
197     if (@unexpected_downloads) {
198         return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
199     }
200
201     return "";
202 }
203
204
205 sub __dir_walk {
206     my ($top, $filefunc, $dirfunc) = @_;
207
208     my $DIR;
209
210     if (-d $top) {
211         my $file;
212         unless (opendir $DIR, $top) {
213             warn "Couldn't open directory $DIR: $!; skipping.\n";
214             return;
215         }
216
217         my @results;
218         while ($file = readdir $DIR) {
219             next if $file eq '.' || $file eq '..';
220             my $nextdir = $top eq '.' ? $file : "$top/$file";
221             push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
222         }
223
224         return $dirfunc ? $dirfunc->($top, @results) : () ;
225     } else {
226         return $filefunc ? $filefunc->($top) : () ;
227     }
228 }
229
230
231 sub _fork_and_launch_server
232 {
233     my $self = shift;
234
235     pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
236     select((select(TO_PARENT), $| = 1)[0]);
237
238     my $pid = fork();
239     if ($pid < 0) {
240         die "Cannot fork";
241     } elsif ($pid == 0) {
242         # child
243         close FROM_CHILD;
244         $self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
245     } else {
246         # father
247         close TO_PARENT;
248         chomp(my $line = <FROM_CHILD>);
249         close FROM_CHILD;
250     }
251
252     return $pid;
253 }
254
255 1;
256
257 # vim: et ts=4 sw=4
258