]> sjero.net Git - wget/blob - tests/WgetTest.pm.in
Error-checking improvements to WgetTest.pm.
[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     my $errcode;
73
74     printf "Running test $self->{_name}\n";
75
76     # Setup
77     my $new_result = $self->_setup();
78     chdir ("$self->{_workdir}/$self->{_name}/input");
79     if (defined $new_result) {
80         $result_message = $new_result;
81         $errcode = 1;
82         goto cleanup;
83     }
84
85     # Launch server
86     my $pid = $self->_fork_and_launch_server();
87
88     # Call wget
89     chdir ("$self->{_workdir}/$self->{_name}/output");
90     my $cmdline = $self->{_cmdline};
91     $cmdline = $self->_substitute_port($cmdline);
92     print "Calling $cmdline\n";
93     $errcode =
94         ($cmdline =~ m{^/.*})
95             ? system ($cmdline)
96             : system ("$self->{_workdir}/../src/$cmdline");
97     $errcode >>= 8; # XXX: should handle abnormal error codes.
98
99     # Shutdown server
100     # if we didn't explicitely kill the server, we would have to call
101     # waitpid ($pid, 0) here in order to wait for the child process to
102     # terminate
103     kill ('TERM', $pid);
104
105     # Verify download
106     unless ($errcode == $self->{_errcode}) {
107         $result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
108         goto cleanup;
109     }
110     my $error_str;
111     if ($error_str = $self->_verify_download()) {
112         $result_message = $error_str;
113     }
114
115   cleanup:
116     $self->_cleanup();
117
118     print $result_message;
119     return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
120 }
121
122
123 sub _setup {
124     my $self = shift;
125
126     #print $self->{_name}, "\n";
127     chdir ($self->{_workdir});
128
129     # Create temporary directory
130     mkdir ($self->{_name});
131     chdir ($self->{_name});
132     mkdir ("input");
133     mkdir ("output");
134
135     # Setup existing files
136     chdir ("output");
137     foreach my $filename (keys %{$self->{_existing}}) {
138         open (FILE, ">$filename")
139             or return "Test failed: cannot open pre-existing file $filename\n";
140
141         my $file = $self->{_existing}->{$filename};
142         print FILE $file->{content}
143             or return "Test failed: cannot write pre-existing file $filename\n";
144
145         close (FILE);
146
147         if (exists($file->{timestamp})) {
148             utime $file->{timestamp}, $file->{timestamp}, $filename
149                 or return "Test failed: cannot set timestamp on pre-existing file $filename\n";
150         }
151     }
152
153     chdir ("../input");
154     my $ret = $self->_setup_server();
155
156     chdir ($self->{_workdir});
157     return $ret;
158 }
159
160
161 sub _cleanup {
162     my $self = shift;
163
164     chdir ($self->{_workdir});
165     File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
166 }
167
168
169 sub _verify_download {
170     my $self = shift;
171
172     chdir ("$self->{_workdir}/$self->{_name}/output");
173
174     # use slurp mode to read file content
175     my $old_input_record_separator = $/;
176     undef $/;
177
178     while (my ($filename, $filedata) = each %{$self->{_output}}) {
179         open (FILE, $filename)
180             or return "Test failed: file $filename not downloaded\n";
181
182         my $content = <FILE>;
183         my $expected_content = $filedata->{'content'};
184         $expected_content = $self->_substitute_port($expected_content);
185         $content eq $expected_content
186             or return "Test failed: wrong content for file $filename\n";
187
188         if (exists($filedata->{'timestamp'})) {
189             my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
190                 $atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
191
192             $mtime == $filedata->{'timestamp'}
193                 or return "Test failed: wrong timestamp for file $filename\n";
194         }
195
196         close (FILE);
197     }
198
199     $/ = $old_input_record_separator;
200
201     # make sure no unexpected files were downloaded
202     chdir ("$self->{_workdir}/$self->{_name}/output");
203
204     __dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[0]}) }, sub { shift; return @_ } );
205     if (@unexpected_downloads) {
206         return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
207     }
208
209     return "";
210 }
211
212
213 sub __dir_walk {
214     my ($top, $filefunc, $dirfunc) = @_;
215
216     my $DIR;
217
218     if (-d $top) {
219         my $file;
220         unless (opendir $DIR, $top) {
221             warn "Couldn't open directory $DIR: $!; skipping.\n";
222             return;
223         }
224
225         my @results;
226         while ($file = readdir $DIR) {
227             next if $file eq '.' || $file eq '..';
228             my $nextdir = $top eq '.' ? $file : "$top/$file";
229             push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
230         }
231
232         return $dirfunc ? $dirfunc->($top, @results) : () ;
233     } else {
234         return $filefunc ? $filefunc->($top) : () ;
235     }
236 }
237
238
239 sub _fork_and_launch_server
240 {
241     my $self = shift;
242
243     pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
244     select((select(TO_PARENT), $| = 1)[0]);
245
246     my $pid = fork();
247     if ($pid < 0) {
248         die "Cannot fork";
249     } elsif ($pid == 0) {
250         # child
251         close FROM_CHILD;
252         $self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
253     } else {
254         # father
255         close TO_PARENT;
256         chomp(my $line = <FROM_CHILD>);
257         close FROM_CHILD;
258     }
259
260     return $pid;
261 }
262
263 1;
264
265 # vim: et ts=4 sw=4
266