Debian Codegraph @

tests/WgetTests.pm

raw
1 package WgetTest;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = 0.01;
7
8 use Carp;
9 use Cwd;
10 use English qw(-no_match_vars);
11 use File::Path;
12 use IO::Handle;
13 use POSIX qw(locale_h);
14 use locale;
15
16 if (defined $ENV{'WGET_PATH'}) {
17 our $WGETPATH = $ENV{'WGET_PATH'} . ' -d --no-config';
18 } else {
19 our $WGETPATH = '../src/wget -d --no-config';
20 }
21
22
23 our $VALGRIND_SUPP_FILE = Cwd::getcwd();
24 if (defined $ENV{'srcdir'}) {
25 $VALGRIND_SUPP_FILE = $VALGRIND_SUPP_FILE
26 . "/" . $ENV{'srcdir'};
27 }
28 $VALGRIND_SUPP_FILE = $VALGRIND_SUPP_FILE . '/valgrind-suppressions';
29
30 my @unexpected_downloads = ();
31
32 {
33 my %_attr_data = ( # DEFAULT
34 _cmdline => q{},
35 _workdir => Cwd::getcwd(),
36 _errcode => 0,
37 _existing => {},
38 _input => {},
39 _name => $PROGRAM_NAME,
40 _output => {},
41 _server_behavior => {},
42 );
43
44 sub _default_for
45 {
46 my ($self, $attr) = @_;
47 return $_attr_data{$attr};
48 }
49
50 sub _standard_keys
51 {
52 return keys %_attr_data;
53 }
54 }
55
56 sub new
57 {
58 my ($caller, %args) = @_;
59 my $caller_is_obj = ref $caller;
60 my $class = $caller_is_obj || $caller;
61
62 #print STDERR "class = ", $class, "\n";
63 #print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
64 my $self = bless {}, $class;
65 for my $attrname ($self->_standard_keys())
66 {
67
68 #print STDERR "attrname = ", $attrname, " value = ";
69 my ($argname) = ($attrname =~ m/^_(.*)/msx);
70 if (exists $args{$argname})
71 {
72
73 #printf STDERR "Setting up $attrname\n";
74 $self->{$attrname} = $args{$argname};
75 }
76 elsif ($caller_is_obj)
77 {
78
79 #printf STDERR "Copying $attrname\n";
80 $self->{$attrname} = $caller->{$attrname};
81 }
82 else
83 {
84 #printf STDERR "Using default for $attrname\n";
85 $self->{$attrname} = $self->_default_for($attrname);
86 }
87
88 #print STDERR $attrname, '=', $self->{$attrname}, "\n";
89 }
90
91 #printf STDERR "_workdir default = ", $self->_default_for("_workdir");
92 return $self;
93 }
94
95 sub run
96 {
97 my $self = shift;
98 my $result_message = "Test successful.\n";
99 my $errcode;
100
101 $self->{_name} =~ s{.*/}{}msx; # remove path
102 $self->{_name} =~ s{[.][^.]+$}{}msx; # remove extension
103 printf "Running test $self->{_name}\n";
104
105 # Setup
106 my $new_result = $self->_setup();
107 chdir "$self->{_workdir}/$self->{_name}/input"
108 or carp "Could not chdir to input directory: $ERRNO";
109 if (defined $new_result)
110 {
111 $result_message = $new_result;
112 $errcode = 1;
113 goto cleanup;
114 }
115
116 # Launch server
117 my $pid = $self->_fork_and_launch_server();
118
119 # Call wget
120 chdir "$self->{_workdir}/$self->{_name}/output"
121 or carp "Could not chdir to output directory: $ERRNO";
122
123 my $cmdline = $self->{_cmdline};
124 $cmdline = $self->_substitute_port($cmdline);
125 $cmdline =
126 ($cmdline =~ m{^/.*}msx) ? $cmdline : "$self->{_workdir}/$cmdline";
127
128 my $valgrind = $ENV{VALGRIND_TESTS};
129 if (!defined $valgrind)
130 {
131 $valgrind = 0;
132 }
133
134 my $gdb = $ENV{GDB_TESTS};
135 if (!defined $gdb)
136 {
137 $gdb = 0;
138 }
139
140 if ($gdb == 1)
141 {
142 $cmdline = 'gdb --args ' . $cmdline;
143 }
144 elsif ($valgrind eq "1")
145 {
146 $cmdline =
147 'valgrind --suppressions=' . $VALGRIND_SUPP_FILE
148 . ' --error-exitcode=301 --leak-check=full --track-origins=yes --show-leak-kinds=all --gen-suppressions=all '
149 . $cmdline;
150 }
151 elsif ($valgrind ne q{} && $valgrind ne "0")
152 {
153 $cmdline = "$valgrind $cmdline";
154 }
155
156 print "Calling $cmdline\n";
157 $errcode = system $cmdline;
158 $errcode >>= 8; # XXX: should handle abnormal error codes.
159
160 # Shutdown server
161 # if we didn't explicitly kill the server, we would have to call
162 # waitpid ($pid, 0) here in order to wait for the child process to
163 # terminate
164 kill 'TERM', $pid;
165
166 # Verify download
167 if ($errcode != $self->{_errcode})
168 {
169 $result_message =
170 "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
171 goto CLEANUP;
172 }
173 my $error_str;
174 if ($error_str = $self->_verify_download())
175 {
176 $result_message = $error_str;
177 }
178
179 CLEANUP:
180 $self->_cleanup();
181
182 print $result_message;
183 return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
184 }
185
186 sub _setup
187 {
188 my $self = shift;
189
190 chdir $self->{_workdir}
191 or carp "Could not chdir into $self->{_workdir}: $ERRNO";
192
193 # Create temporary directory
194 mkdir $self->{_name} or carp "Could not mkdir '$self->{_name}': $ERRNO";
195 chdir $self->{_name}
196 or carp "Could not chdir into '$self->{_name}': $ERRNO";
197 mkdir 'input' or carp "Could not mkdir 'input' $ERRNO";
198 mkdir 'output' or carp "Could not mkdir 'output': $ERRNO";
199
200 # Setup existing files
201 chdir 'output' or carp "Could not chdir into 'output': $ERRNO";
202 for my $filename (keys %{$self->{_existing}})
203 {
204 open my $fh, '>', $filename
205 or return "Test failed: cannot open pre-existing file $filename\n";
206
207 my $file = $self->{_existing}->{$filename};
208 print {$fh} $file->{content}
209 or return "Test failed: cannot write pre-existing file $filename\n";
210
211 close $fh or carp $ERRNO;
212
213 if (exists($file->{timestamp}))
214 {
215 utime $file->{timestamp}, $file->{timestamp}, $filename
216 or return
217 "Test failed: cannot set timestamp on pre-existing file $filename\n";
218 }
219 }
220
221 chdir '../input' or carp "Cannot chdir into '../input': $ERRNO";
222 $self->_setup_server();
223
224 chdir $self->{_workdir}
225 or carp "Cannot chdir into '$self->{_workdir}': $ERRNO";
226 return;
227 }
228
229 sub _cleanup
230 {
231 my $self = shift;
232
233 chdir $self->{_workdir}
234 or carp "Could not chdir into '$self->{_workdir}': $ERRNO";
235 if (!$ENV{WGET_TEST_NO_CLEANUP})
236 {
237 File::Path::rmtree($self->{_name});
238 }
239 return 1;
240 }
241
242 # not a method
243 sub quotechar
244 {
245 my $c = ord shift;
246 if ($c >= 0x7 && $c <= 0xD)
247 {
248 return q{\\} . qw(a b t n v f r) [$c - 0x7];
249 }
250 else
251 {
252 return sprintf '\\x%02x', $c;
253 }
254 }
255
256 # not a method
257 sub _show_diff
258 {
259 my ($expected, $actual) = @_;
260 my $SNIPPET_SIZE = 10;
261
262 my $str = q{};
263 my $explen = length $expected;
264 my $actlen = length $actual;
265
266 if ($explen != $actlen)
267 {
268 $str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
269 }
270
271 my $min = $explen <= $actlen ? $explen : $actlen;
272 my $line = 1;
273 my $col = 1;
274 my $i = 0;
275
276 while ( $i < $min )
277 {
278 last if substr($expected, $i, 1) ne substr $actual, $i, 1;
279 if (substr($expected, $i, 1) eq "\n")
280 {
281 $line++;
282 $col = 0;
283 }
284 else
285 {
286 $col++;
287 }
288 $i++;
289 }
290 my $snip_start = $i - ($SNIPPET_SIZE / 2);
291 if ($snip_start < 0)
292 {
293 $SNIPPET_SIZE += $snip_start; # Take it from the end.
294 $snip_start = 0;
295 }
296 my $exp_snip = substr $expected, $snip_start, $SNIPPET_SIZE;
297 my $act_snip = substr $actual, $snip_start, $SNIPPET_SIZE;
298 $exp_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
299 $act_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
300 $str .= "Mismatch at line $line, col $col:\n";
301 $str .= " $exp_snip\n";
302 $str .= " $act_snip\n";
303
304 return $str;
305 }
306
307 sub _verify_download
308 {
309 my $self = shift;
310
311 chdir "$self->{_workdir}/$self->{_name}/output"
312 or carp "Could not chdir into output directory: $ERRNO";
313
314 # use slurp mode to read file content
315 my $old_input_record_separator = $INPUT_RECORD_SEPARATOR;
316 local $INPUT_RECORD_SEPARATOR = undef;
317
318 while (my ($filename, $filedata) = each %{$self->{_output}})
319 {
320 open my $fh, '<', $filename
321 or return "Test failed: file $filename not downloaded\n";
322
323 my $content = <$fh>;
324
325 close $fh or carp $ERRNO;
326
327 my $expected_content = $filedata->{'content'};
328 $expected_content = $self->_substitute_port($expected_content);
329 if ($content ne $expected_content)
330 {
331 return "Test failed: wrong content for file $filename\n"
332 . _show_diff($expected_content, $content);
333 }
334
335 if (exists($filedata->{'timestamp'}))
336 {
337 my (
338 $dev, $ino, $mode, $nlink, $uid,
339 $gid, $rdev, $size, $atime, $mtime,
340 $ctime, $blksize, $blocks
341 )
342 = stat $filename;
343
344 $mtime == $filedata->{'timestamp'}
345 or return "Test failed: wrong timestamp for file $filename: expected = $filedata->{'timestamp'}, actual = $mtime\n";
346 }
347
348 }
349
350 local $INPUT_RECORD_SEPARATOR = $old_input_record_separator;
351
352 # make sure no unexpected files were downloaded
353 chdir "$self->{_workdir}/$self->{_name}/output"
354 or carp "Could not change into output directory: $ERRNO";
355
356 __dir_walk(
357 q{.},
358 sub {
359 if (!(exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}))
360 {
361 push @unexpected_downloads, $_[0];
362 }
363 },
364 sub { shift; return @_ }
365 );
366 if (@unexpected_downloads)
367 {
368 return 'Test failed: unexpected downloaded files [' .
369 (join ', ', @unexpected_downloads) . "]\n";
370
371 }
372
373 return q{};
374 }
375
376 sub __dir_walk
377 {
378 my ($top, $filefunc, $dirfunc) = @_;
379
380 my $DIR;
381
382 if (-d $top)
383 {
384 my $file;
385 if (!opendir $DIR, $top)
386 {
387 warn "Couldn't open directory $DIR: $ERRNO; skipping.\n";
388 return;
389 }
390
391 my @results;
392 while ($file = readdir $DIR)
393 {
394 next if $file eq q{.} || $file eq q{..};
395 my $nextdir = $top eq q{.} ? $file : "$top/$file";
396 push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
397 }
398
399 return $dirfunc ? $dirfunc->($top, @results) : ();
400 }
401 else
402 {
403 return $filefunc ? $filefunc->($top) : ();
404 }
405 }
406
407 sub _fork_and_launch_server
408 {
409 my $self = shift;
410
411 pipe FROM_CHILD, TO_PARENT or croak 'Cannot create pipe!';
412 TO_PARENT->autoflush();
413
414 my $pid = fork;
415 if ($pid < 0)
416 {
417 carp 'Cannot fork';
418 }
419 elsif ($pid == 0)
420 {
421
422 # child
423 close FROM_CHILD or carp $ERRNO;
424
425 # FTP Server has to start with english locale due to use of strftime month names in LIST command
426 setlocale(LC_ALL, 'C');
427 $self->_launch_server(
428 sub {
429 print {*TO_PARENT} "SYNC\n";
430 close TO_PARENT or carp $ERRNO;
431 }
432 );
433 }
434 else
435 {
436 # father
437 close TO_PARENT or carp $ERRNO;
438 chomp(my $line = <FROM_CHILD>);
439 close FROM_CHILD or carp $ERRNO;
440 }
441
442 return $pid;
443 }
444
445 1;
446
447 # vim: et ts=4 sw=4