2007-07-12 02:10:48 -04:00
|
|
|
package WgetTest;
|
2005-12-05 08:35:07 -05:00
|
|
|
$VERSION = 0.01;
|
|
|
|
|
|
|
|
use strict;
|
2008-11-25 13:59:36 -05:00
|
|
|
use warnings;
|
2005-12-05 08:35:07 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
use Cwd;
|
|
|
|
use File::Path;
|
2014-09-24 10:40:03 -04:00
|
|
|
use POSIX qw(locale_h);
|
|
|
|
use locale;
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2014-09-24 10:40:03 -04:00
|
|
|
our $WGETPATH = "../src/wget";
|
2007-07-12 02:10:48 -04:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
my @unexpected_downloads = ();
|
2005-12-05 08:35:07 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
my %_attr_data = ( # DEFAULT
|
2006-01-24 07:15:58 -05:00
|
|
|
_cmdline => "",
|
2006-04-27 09:27:17 -04:00
|
|
|
_workdir => Cwd::getcwd(),
|
2006-01-24 07:15:58 -05:00
|
|
|
_errcode => 0,
|
2006-04-27 09:27:17 -04:00
|
|
|
_existing => {},
|
2006-01-24 07:15:58 -05:00
|
|
|
_input => {},
|
2014-10-02 05:26:59 -04:00
|
|
|
_name => $0,
|
2006-01-24 07:15:58 -05:00
|
|
|
_output => {},
|
2009-09-06 16:04:22 -04:00
|
|
|
_server_behavior => {},
|
2005-12-05 08:35:07 -05:00
|
|
|
);
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-12-27 04:00:12 -05:00
|
|
|
sub _default_for
|
|
|
|
{
|
|
|
|
my ($self, $attr) = @_;
|
|
|
|
$_attr_data{$attr};
|
|
|
|
}
|
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
sub _standard_keys
|
2006-12-27 04:00:12 -05:00
|
|
|
{
|
|
|
|
keys %_attr_data;
|
|
|
|
}
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
sub new {
|
|
|
|
my ($caller, %args) = @_;
|
|
|
|
my $caller_is_obj = ref($caller);
|
|
|
|
my $class = $caller_is_obj || $caller;
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR "class = ", $class, "\n";
|
2007-07-12 02:10:48 -04:00
|
|
|
#print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
|
2005-12-05 08:35:07 -05:00
|
|
|
my $self = bless {}, $class;
|
|
|
|
foreach my $attrname ($self->_standard_keys()) {
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR "attrname = ", $attrname, " value = ";
|
2005-12-05 08:35:07 -05:00
|
|
|
my ($argname) = ($attrname =~ /^_(.*)/);
|
|
|
|
if (exists $args{$argname}) {
|
|
|
|
#printf STDERR "Setting up $attrname\n";
|
|
|
|
$self->{$attrname} = $args{$argname};
|
|
|
|
} elsif ($caller_is_obj) {
|
|
|
|
#printf STDERR "Copying $attrname\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->{$attrname} = $caller->{$attrname};
|
2005-12-05 08:35:07 -05:00
|
|
|
} else {
|
|
|
|
#printf STDERR "Using default for $attrname\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->{$attrname} = $self->_default_for($attrname);
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR $attrname, '=', $self->{$attrname}, "\n";
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2006-04-27 09:27:17 -04:00
|
|
|
#printf STDERR "_workdir default = ", $self->_default_for("_workdir");
|
2005-12-05 08:35:07 -05:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
sub run {
|
|
|
|
my $self = shift;
|
|
|
|
my $result_message = "Test successful.\n";
|
2009-09-04 21:32:52 -04:00
|
|
|
my $errcode;
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2014-10-02 05:26:59 -04:00
|
|
|
$self->{_name} =~ s{.*/}{}; # remove path
|
|
|
|
$self->{_name} =~ s{\.[^.]+$}{}; # remove extension
|
2006-01-24 07:15:58 -05:00
|
|
|
printf "Running test $self->{_name}\n";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
|
|
|
# Setup
|
2009-09-04 21:32:52 -04:00
|
|
|
my $new_result = $self->_setup();
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/input");
|
2009-09-04 21:32:52 -04:00
|
|
|
if (defined $new_result) {
|
|
|
|
$result_message = $new_result;
|
|
|
|
$errcode = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
# Launch server
|
2006-05-29 05:15:06 -04:00
|
|
|
my $pid = $self->_fork_and_launch_server();
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
# Call wget
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/output");
|
2008-06-12 05:18:35 -04:00
|
|
|
my $cmdline = $self->{_cmdline};
|
|
|
|
$cmdline = $self->_substitute_port($cmdline);
|
|
|
|
print "Calling $cmdline\n";
|
2009-09-04 21:32:52 -04:00
|
|
|
$errcode =
|
2008-11-13 06:29:59 -05:00
|
|
|
($cmdline =~ m{^/.*})
|
2008-06-12 05:18:35 -04:00
|
|
|
? system ($cmdline)
|
2014-09-24 10:40:03 -04:00
|
|
|
: system ("$self->{_workdir}/$cmdline");
|
2009-08-28 02:08:58 -04:00
|
|
|
$errcode >>= 8; # XXX: should handle abnormal error codes.
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Shutdown server
|
2008-11-13 06:29:59 -05:00
|
|
|
# if we didn't explicitely kill the server, we would have to call
|
|
|
|
# waitpid ($pid, 0) here in order to wait for the child process to
|
2006-05-29 05:15:06 -04:00
|
|
|
# terminate
|
2006-01-24 07:15:58 -05:00
|
|
|
kill ('TERM', $pid);
|
|
|
|
|
|
|
|
# Verify download
|
|
|
|
unless ($errcode == $self->{_errcode}) {
|
|
|
|
$result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
|
2009-09-04 21:32:52 -04:00
|
|
|
goto cleanup;
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
2007-07-12 02:10:48 -04:00
|
|
|
my $error_str;
|
|
|
|
if ($error_str = $self->_verify_download()) {
|
2006-01-24 07:15:58 -05:00
|
|
|
$result_message = $error_str;
|
|
|
|
}
|
|
|
|
|
2009-09-04 21:32:52 -04:00
|
|
|
cleanup:
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->_cleanup();
|
|
|
|
|
|
|
|
print $result_message;
|
2007-07-12 02:10:48 -04:00
|
|
|
return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub _setup {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
#print $self->{_name}, "\n";
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Create temporary directory
|
|
|
|
mkdir ($self->{_name});
|
|
|
|
chdir ($self->{_name});
|
|
|
|
mkdir ("input");
|
|
|
|
mkdir ("output");
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
# Setup existing files
|
|
|
|
chdir ("output");
|
|
|
|
foreach my $filename (keys %{$self->{_existing}}) {
|
2008-11-13 06:29:59 -05:00
|
|
|
open (FILE, ">$filename")
|
2006-04-27 09:27:17 -04:00
|
|
|
or return "Test failed: cannot open pre-existing file $filename\n";
|
2007-08-22 10:05:18 -04:00
|
|
|
|
|
|
|
my $file = $self->{_existing}->{$filename};
|
|
|
|
print FILE $file->{content}
|
2006-04-27 09:27:17 -04:00
|
|
|
or return "Test failed: cannot write pre-existing file $filename\n";
|
2007-08-22 10:05:18 -04:00
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
close (FILE);
|
2007-08-22 10:05:18 -04:00
|
|
|
|
|
|
|
if (exists($file->{timestamp})) {
|
|
|
|
utime $file->{timestamp}, $file->{timestamp}, $filename
|
|
|
|
or return "Test failed: cannot set timestamp on pre-existing file $filename\n";
|
|
|
|
}
|
2008-11-13 06:29:59 -05:00
|
|
|
}
|
|
|
|
|
2007-06-14 11:43:25 -04:00
|
|
|
chdir ("../input");
|
2009-09-06 16:04:22 -04:00
|
|
|
$self->_setup_server();
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2009-09-06 16:04:22 -04:00
|
|
|
return;
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub _cleanup {
|
|
|
|
my $self = shift;
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2009-06-29 21:53:13 -04:00
|
|
|
File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
2009-09-06 16:04:22 -04:00
|
|
|
# not a method
|
|
|
|
sub quotechar {
|
|
|
|
my $c = ord( shift );
|
|
|
|
if ($c >= 0x7 && $c <= 0xD) {
|
|
|
|
return '\\' . qw(a b t n v f r)[$c - 0x7];
|
|
|
|
} else {
|
|
|
|
return sprintf('\\x%02x', $c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# not a method
|
|
|
|
sub _show_diff {
|
|
|
|
my $SNIPPET_SIZE = 10;
|
|
|
|
|
|
|
|
my ($expected, $actual) = @_;
|
|
|
|
|
|
|
|
my $str = '';
|
|
|
|
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;
|
|
|
|
for ($i=0; $i != $min; ++$i) {
|
|
|
|
last if substr($expected, $i, 1) ne substr($actual, $i, 1);
|
|
|
|
if (substr($expected, $i, 1) eq '\n') {
|
|
|
|
$line++;
|
|
|
|
$col = 0;
|
|
|
|
} else {
|
|
|
|
$col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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($&) /ge;
|
|
|
|
$act_snip =~s/[^[:print:]]/ quotechar($&) /ge;
|
|
|
|
$str .= "Mismatch at line $line, col $col:\n";
|
|
|
|
$str .= " $exp_snip\n";
|
|
|
|
$str .= " $act_snip\n";
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
sub _verify_download {
|
|
|
|
my $self = shift;
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/output");
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
# use slurp mode to read file content
|
|
|
|
my $old_input_record_separator = $/;
|
|
|
|
undef $/;
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
while (my ($filename, $filedata) = each %{$self->{_output}}) {
|
2008-11-13 06:29:59 -05:00
|
|
|
open (FILE, $filename)
|
2006-01-24 07:15:58 -05:00
|
|
|
or return "Test failed: file $filename not downloaded\n";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
my $content = <FILE>;
|
2008-06-12 05:18:35 -04:00
|
|
|
my $expected_content = $filedata->{'content'};
|
|
|
|
$expected_content = $self->_substitute_port($expected_content);
|
2009-09-06 16:04:22 -04:00
|
|
|
unless ($content eq $expected_content) {
|
|
|
|
return "Test failed: wrong content for file $filename\n"
|
|
|
|
. _show_diff($expected_content, $content);
|
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
if (exists($filedata->{'timestamp'})) {
|
|
|
|
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
|
|
|
|
$atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
|
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
$mtime == $filedata->{'timestamp'}
|
2006-01-24 07:15:58 -05:00
|
|
|
or return "Test failed: wrong timestamp for file $filename\n";
|
|
|
|
}
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
close (FILE);
|
2008-11-13 06:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$/ = $old_input_record_separator;
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# make sure no unexpected files were downloaded
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/output");
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2014-03-19 11:42:06 -04:00
|
|
|
__dir_walk('.',
|
|
|
|
sub { push @unexpected_downloads,
|
|
|
|
$_[0] unless (exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}) },
|
|
|
|
sub { shift; return @_ } );
|
2008-11-13 06:29:59 -05:00
|
|
|
if (@unexpected_downloads) {
|
2006-01-24 07:15:58 -05:00
|
|
|
return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub __dir_walk {
|
|
|
|
my ($top, $filefunc, $dirfunc) = @_;
|
|
|
|
|
|
|
|
my $DIR;
|
|
|
|
|
|
|
|
if (-d $top) {
|
|
|
|
my $file;
|
|
|
|
unless (opendir $DIR, $top) {
|
|
|
|
warn "Couldn't open directory $DIR: $!; skipping.\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @results;
|
|
|
|
while ($file = readdir $DIR) {
|
|
|
|
next if $file eq '.' || $file eq '..';
|
|
|
|
my $nextdir = $top eq '.' ? $file : "$top/$file";
|
|
|
|
push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dirfunc ? $dirfunc->($top, @results) : () ;
|
|
|
|
} else {
|
|
|
|
return $filefunc ? $filefunc->($top) : () ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-29 05:15:06 -04:00
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
sub _fork_and_launch_server
|
2006-05-29 05:15:06 -04:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
|
|
|
|
select((select(TO_PARENT), $| = 1)[0]);
|
|
|
|
|
|
|
|
my $pid = fork();
|
|
|
|
if ($pid < 0) {
|
|
|
|
die "Cannot fork";
|
|
|
|
} elsif ($pid == 0) {
|
2008-11-13 06:29:59 -05:00
|
|
|
# child
|
2006-05-29 05:15:06 -04:00
|
|
|
close FROM_CHILD;
|
2014-09-24 10:40:03 -04:00
|
|
|
# FTP Server has to start with english locale due to use of strftime month names in LIST command
|
|
|
|
setlocale(LC_ALL,"C");
|
2006-05-29 05:15:06 -04:00
|
|
|
$self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
|
|
|
|
} else {
|
|
|
|
# father
|
|
|
|
close TO_PARENT;
|
|
|
|
chomp(my $line = <FROM_CHILD>);
|
|
|
|
close FROM_CHILD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pid;
|
|
|
|
}
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
1;
|
|
|
|
|
|
|
|
# vim: et ts=4 sw=4
|