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