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