2 # WgetTest.pm is a generated file! Do not edit! Edit WgetTest.pm.in
14 our $WGETPATH = "@abs_top_builddir@/src/wget";
16 my @unexpected_downloads = ();
19 my %_attr_data = ( # DEFAULT
21 _workdir => Cwd::getcwd(),
27 _server_behavior => {},
32 my ($self, $attr) = @_;
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};
60 #printf STDERR "Using default for $attrname\n";
61 $self->{$attrname} = $self->_default_for($attrname);
63 #print STDERR $attrname, '=', $self->{$attrname}, "\n";
65 #printf STDERR "_workdir default = ", $self->_default_for("_workdir");
72 my $result_message = "Test successful.\n";
75 printf "Running test $self->{_name}\n";
78 my $new_result = $self->_setup();
79 chdir ("$self->{_workdir}/$self->{_name}/input");
80 if (defined $new_result) {
81 $result_message = $new_result;
87 my $pid = $self->_fork_and_launch_server();
90 chdir ("$self->{_workdir}/$self->{_name}/output");
91 my $cmdline = $self->{_cmdline};
92 $cmdline = $self->_substitute_port($cmdline);
93 print "Calling $cmdline\n";
97 : system ("$self->{_workdir}/../src/$cmdline");
98 $errcode >>= 8; # XXX: should handle abnormal error codes.
101 # if we didn't explicitely kill the server, we would have to call
102 # waitpid ($pid, 0) here in order to wait for the child process to
107 unless ($errcode == $self->{_errcode}) {
108 $result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
112 if ($error_str = $self->_verify_download()) {
113 $result_message = $error_str;
119 print $result_message;
120 return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
127 #print $self->{_name}, "\n";
128 chdir ($self->{_workdir});
130 # Create temporary directory
131 mkdir ($self->{_name});
132 chdir ($self->{_name});
136 # Setup existing files
138 foreach my $filename (keys %{$self->{_existing}}) {
139 open (FILE, ">$filename")
140 or return "Test failed: cannot open pre-existing file $filename\n";
142 my $file = $self->{_existing}->{$filename};
143 print FILE $file->{content}
144 or return "Test failed: cannot write pre-existing file $filename\n";
148 if (exists($file->{timestamp})) {
149 utime $file->{timestamp}, $file->{timestamp}, $filename
150 or return "Test failed: cannot set timestamp on pre-existing file $filename\n";
155 $self->_setup_server();
157 chdir ($self->{_workdir});
165 chdir ($self->{_workdir});
166 File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
171 my $c = ord( shift );
172 if ($c >= 0x7 && $c <= 0xD) {
173 return '\\' . qw(a b t n v f r)[$c - 0x7];
175 return sprintf('\\x%02x', $c);
181 my $SNIPPET_SIZE = 10;
183 my ($expected, $actual) = @_;
186 my $explen = length $expected;
187 my $actlen = length $actual;
189 if ($explen != $actlen) {
190 $str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
193 my $min = $explen <= $actlen? $explen : $actlen;
197 for ($i=0; $i != $min; ++$i) {
198 last if substr($expected, $i, 1) ne substr($actual, $i, 1);
199 if (substr($expected, $i, 1) eq '\n') {
206 my $snip_start = $i - ($SNIPPET_SIZE / 2);
207 if ($snip_start < 0) {
208 $SNIPPET_SIZE += $snip_start; # Take it from the end.
211 my $exp_snip = substr($expected, $snip_start, $SNIPPET_SIZE);
212 my $act_snip = substr($actual, $snip_start, $SNIPPET_SIZE);
213 $exp_snip =~s/[^[:print:]]/ quotechar($&) /ge;
214 $act_snip =~s/[^[:print:]]/ quotechar($&) /ge;
215 $str .= "Mismatch at line $line, col $col:\n";
216 $str .= " $exp_snip\n";
217 $str .= " $act_snip\n";
222 sub _verify_download {
225 chdir ("$self->{_workdir}/$self->{_name}/output");
227 # use slurp mode to read file content
228 my $old_input_record_separator = $/;
231 while (my ($filename, $filedata) = each %{$self->{_output}}) {
232 open (FILE, $filename)
233 or return "Test failed: file $filename not downloaded\n";
235 my $content = <FILE>;
236 my $expected_content = $filedata->{'content'};
237 $expected_content = $self->_substitute_port($expected_content);
238 unless ($content eq $expected_content) {
239 return "Test failed: wrong content for file $filename\n"
240 . _show_diff($expected_content, $content);
243 if (exists($filedata->{'timestamp'})) {
244 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
245 $atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
247 $mtime == $filedata->{'timestamp'}
248 or return "Test failed: wrong timestamp for file $filename\n";
254 $/ = $old_input_record_separator;
256 # make sure no unexpected files were downloaded
257 chdir ("$self->{_workdir}/$self->{_name}/output");
259 __dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[0]}) }, sub { shift; return @_ } );
260 if (@unexpected_downloads) {
261 return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
269 my ($top, $filefunc, $dirfunc) = @_;
275 unless (opendir $DIR, $top) {
276 warn "Couldn't open directory $DIR: $!; skipping.\n";
281 while ($file = readdir $DIR) {
282 next if $file eq '.' || $file eq '..';
283 my $nextdir = $top eq '.' ? $file : "$top/$file";
284 push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
287 return $dirfunc ? $dirfunc->($top, @results) : () ;
289 return $filefunc ? $filefunc->($top) : () ;
294 sub _fork_and_launch_server
298 pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
299 select((select(TO_PARENT), $| = 1)[0]);
304 } elsif ($pid == 0) {
307 $self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
311 chomp(my $line = <FROM_CHILD>);