2008-06-12 05:25:04 -04:00
|
|
|
# WARNING!
|
|
|
|
# WgetTest.pm is a generated file! Do not edit! Edit WgetTest.pm.in
|
|
|
|
# instead.
|
|
|
|
|
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;
|
|
|
|
|
2007-10-05 18:20:16 -04:00
|
|
|
our $WGETPATH = "@abs_top_builddir@/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 => {},
|
|
|
|
_name => "",
|
|
|
|
_output => {},
|
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";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
printf "Running test $self->{_name}\n";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
|
|
|
# Setup
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->_setup();
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/input");
|
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";
|
2008-11-13 06:29:59 -05:00
|
|
|
my $errcode =
|
|
|
|
($cmdline =~ m{^/.*})
|
2008-06-12 05:18:35 -04:00
|
|
|
? system ($cmdline)
|
|
|
|
: system ("$self->{_workdir}/../src/$cmdline");
|
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";
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
$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");
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->_setup_server();
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub _cleanup {
|
|
|
|
my $self = shift;
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2006-01-24 07:15:58 -05:00
|
|
|
File::Path::rmtree ($self->{_name});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
$content eq $expected_content
|
2006-01-24 07:15:58 -05:00
|
|
|
or return "Test failed: wrong content for file $filename\n";
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
__dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[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;
|
|
|
|
$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
|
|
|
|
|