]> sjero.net Git - wget/blob - tests/WgetTest.pm.in
Automated merge.
[wget] / tests / WgetTest.pm.in
1 #!/usr/bin/perl -w
2
3 # WARNING!
4 # WgetTest.pm is a generated file! Do not edit! Edit WgetTest.pm.in
5 # instead.
6
7 package WgetTest;
8 $VERSION = 0.01;
9
10 use strict;
11
12 use Cwd;
13 use File::Path;
14
15 our $WGETPATH = "@abs_top_builddir@/src/wget";
16
17 my @unexpected_downloads = ();
18
19 {
20     my %_attr_data = ( # DEFAULT
21         _cmdline      => "",
22         _workdir      => Cwd::getcwd(),
23         _errcode      => 0,
24         _existing     => {},
25         _input        => {},
26         _name         => "",
27         _output       => {},
28     );
29     
30     sub _default_for
31     {
32         my ($self, $attr) = @_;
33         $_attr_data{$attr};
34     }
35
36     sub _standard_keys 
37     {
38         keys %_attr_data;
39     }
40 }
41
42
43 sub new {
44     my ($caller, %args) = @_;
45     my $caller_is_obj = ref($caller);
46     my $class = $caller_is_obj || $caller;
47     #print STDERR "class = ", $class, "\n";
48     #print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
49     my $self = bless {}, $class;
50     foreach my $attrname ($self->_standard_keys()) {
51         #print STDERR "attrname = ", $attrname, " value = ";
52         my ($argname) = ($attrname =~ /^_(.*)/);
53         if (exists $args{$argname}) {
54             #printf STDERR "Setting up $attrname\n";
55             $self->{$attrname} = $args{$argname};
56         } elsif ($caller_is_obj) {
57             #printf STDERR "Copying $attrname\n";
58             $self->{$attrname} = $caller->{$attrname};
59         } else {
60             #printf STDERR "Using default for $attrname\n";
61             $self->{$attrname} = $self->_default_for($attrname);
62         }
63         #print STDERR $attrname, '=', $self->{$attrname}, "\n";
64     }
65     #printf STDERR "_workdir default = ", $self->_default_for("_workdir");
66     return $self;
67 }
68
69
70 sub run {
71     my $self = shift;
72     my $result_message = "Test successful.\n";
73    
74     printf "Running test $self->{_name}\n";
75     
76     # Setup 
77     $self->_setup();
78     chdir ("$self->{_workdir}/$self->{_name}/input");
79     
80     # Launch server
81     my $pid = $self->_fork_and_launch_server();
82     
83     # Call wget
84     chdir ("$self->{_workdir}/$self->{_name}/output");
85     my $cmdline = $self->{_cmdline};
86     $cmdline = $self->_substitute_port($cmdline);
87     print "Calling $cmdline\n";
88     my $errcode = 
89         ($cmdline =~ m{^/.*}) 
90             ? system ($cmdline)
91             : system ("$self->{_workdir}/../src/$cmdline");
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});
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