1
0
mirror of https://github.com/moparisthebest/wget synced 2024-07-03 16:38:41 -04:00

Stylistic and idiomatic cleanups in Perl tests

This commit is contained in:
Pär Karlsson 2014-11-01 10:06:26 +01:00 committed by Darshit Shah
parent 9f83e0e13c
commit 8078adee7f
7 changed files with 790 additions and 489 deletions

View File

@ -1,3 +1,12 @@
2014-10-31 Pär Karlsson <feinorgh@gmail.com>
* WgetTests.pm: Proper conditional operators, tidied up code, idiomatic
improvements as per modern Perl best practices.
* WgetFeature.pm: Tidied up code, idiomatic improvements for readability
* FTPServer.pm: Tidied up code (perltidy -gnu)
* FTPTest.pm: Likewise
* HTTPServer.pm: Likewise
* HTTPTest.pm: Likewise
2014-10-30 Mike Frysinger <vapier@gentoo.org> 2014-10-30 Mike Frysinger <vapier@gentoo.org>
* WgetFeature.pm: fix skip exit code to 77 * WgetFeature.pm: fix skip exit code to 77

File diff suppressed because it is too large Load Diff

View File

@ -9,9 +9,8 @@ use WgetTests;
our @ISA = qw(WgetTest); our @ISA = qw(WgetTest);
my $VERSION = 0.01; my $VERSION = 0.01;
{ {
my %_attr_data = ( # DEFAULT my %_attr_data = ( # DEFAULT
); );
sub _default_for sub _default_for
@ -28,29 +27,32 @@ my $VERSION = 0.01;
} }
} }
sub _setup_server
sub _setup_server { {
my $self = shift; my $self = shift;
$self->{_server} = FTPServer->new (input => $self->{_input}, $self->{_server} = FTPServer->new(
server_behavior => input => $self->{_input},
$self->{_server_behavior}, server_behavior => $self->{_server_behavior},
LocalAddr => 'localhost', LocalAddr => 'localhost',
ReuseAddr => 1, ReuseAddr => 1,
rootDir => "$self->{_workdir}/$self->{_name}/input") or die "Cannot create server!!!"; rootDir => "$self->{_workdir}/$self->{_name}/input"
)
or die "Cannot create server!!!";
} }
sub _launch_server
sub _launch_server { {
my $self = shift; my $self = shift;
my $synch_func = shift; my $synch_func = shift;
$self->{_server}->run ($synch_func); $self->{_server}->run($synch_func);
} }
sub _substitute_port { sub _substitute_port
{
my $self = shift; my $self = shift;
my $ret = shift; my $ret = shift;
$ret =~ s/{{port}}/$self->{_server}->sockport/eg; $ret =~ s/{{port}}/$self->{_server}->sockport/eg;
return $ret; return $ret;
} }

View File

@ -8,47 +8,58 @@ use HTTP::Status;
use HTTP::Headers; use HTTP::Headers;
use HTTP::Response; use HTTP::Response;
our @ISA=qw(HTTP::Daemon); our @ISA = qw(HTTP::Daemon);
my $VERSION = 0.01; my $VERSION = 0.01;
my $CRLF = "\015\012"; # "\r\n" is not portable my $CRLF = "\015\012"; # "\r\n" is not portable
my $log = undef; my $log = undef;
sub run { sub run
{
my ($self, $urls, $synch_callback) = @_; my ($self, $urls, $synch_callback) = @_;
my $initialized = 0; my $initialized = 0;
while (1) { while (1)
if (!$initialized) { {
if (!$initialized)
{
$synch_callback->(); $synch_callback->();
$initialized = 1; $initialized = 1;
} }
my $con = $self->accept(); my $con = $self->accept();
print STDERR "Accepted a new connection\n" if $log; print STDERR "Accepted a new connection\n" if $log;
while (my $req = $con->get_request) { while (my $req = $con->get_request)
{
#my $url_path = $req->url->path; #my $url_path = $req->url->path;
my $url_path = $req->url->as_string; my $url_path = $req->url->as_string;
if ($url_path =~ m{/$}) { # append 'index.html' if ($url_path =~ m{/$})
{ # append 'index.html'
$url_path .= 'index.html'; $url_path .= 'index.html';
} }
#if ($url_path =~ m{^/}) { # remove trailing '/' #if ($url_path =~ m{^/}) { # remove trailing '/'
# $url_path = substr ($url_path, 1); # $url_path = substr ($url_path, 1);
#} #}
if ($log) { if ($log)
{
print STDERR "Method: ", $req->method, "\n"; print STDERR "Method: ", $req->method, "\n";
print STDERR "Path: ", $url_path, "\n"; print STDERR "Path: ", $url_path, "\n";
print STDERR "Available URLs: ", "\n"; print STDERR "Available URLs: ", "\n";
foreach my $key (keys %$urls) { foreach my $key (keys %$urls)
{
print STDERR $key, "\n"; print STDERR $key, "\n";
} }
} }
if (exists($urls->{$url_path})) { if (exists($urls->{$url_path}))
{
print STDERR "Serving requested URL: ", $url_path, "\n" if $log; print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
next unless ($req->method eq "HEAD" || $req->method eq "GET"); next unless ($req->method eq "HEAD" || $req->method eq "GET");
my $url_rec = $urls->{$url_path}; my $url_rec = $urls->{$url_path};
$self->send_response($req, $url_rec, $con); $self->send_response($req, $url_rec, $con);
} else { }
else
{
print STDERR "Requested wrong URL: ", $url_path, "\n" if $log; print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
$con->send_error($HTTP::Status::RC_FORBIDDEN); $con->send_error($HTTP::Status::RC_FORBIDDEN);
last; last;
@ -59,73 +70,89 @@ sub run {
} }
} }
sub send_response { sub send_response
{
my ($self, $req, $url_rec, $con) = @_; my ($self, $req, $url_rec, $con) = @_;
# create response # create response
my ($code, $msg, $headers); my ($code, $msg, $headers);
my $send_content = ($req->method eq "GET"); my $send_content = ($req->method eq "GET");
if (exists $url_rec->{'auth_method'}) { if (exists $url_rec->{'auth_method'})
{
($send_content, $code, $msg, $headers) = ($send_content, $code, $msg, $headers) =
$self->handle_auth($req, $url_rec); $self->handle_auth($req, $url_rec);
} elsif (!$self->verify_request_headers ($req, $url_rec)) { }
elsif (!$self->verify_request_headers($req, $url_rec))
{
($send_content, $code, $msg, $headers) = ($send_content, $code, $msg, $headers) =
('', 400, 'Mismatch on expected headers', {}); ('', 400, 'Mismatch on expected headers', {});
} else { }
else
{
($code, $msg) = @{$url_rec}{'code', 'msg'}; ($code, $msg) = @{$url_rec}{'code', 'msg'};
$headers = $url_rec->{headers}; $headers = $url_rec->{headers};
} }
my $resp = HTTP::Response->new ($code, $msg); my $resp = HTTP::Response->new($code, $msg);
print STDERR "HTTP::Response: \n", $resp->as_string if $log; print STDERR "HTTP::Response: \n", $resp->as_string if $log;
while (my ($name, $value) = each %{$headers}) { while (my ($name, $value) = each %{$headers})
{
# print STDERR "setting header: $name = $value\n"; # print STDERR "setting header: $name = $value\n";
$resp->header($name => $value); $resp->header($name => $value);
} }
print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log; print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
if ($send_content) { if ($send_content)
{
my $content = $url_rec->{content}; my $content = $url_rec->{content};
if (exists($url_rec->{headers}{"Content-Length"})) { if (exists($url_rec->{headers}{"Content-Length"}))
{
# Content-Length and length($content) don't match # Content-Length and length($content) don't match
# manually prepare the HTTP response # manually prepare the HTTP response
$con->send_basic_header($url_rec->{code}, $resp->message, $resp->protocol); $con->send_basic_header($url_rec->{code}, $resp->message,
$resp->protocol);
print $con $resp->headers_as_string($CRLF); print $con $resp->headers_as_string($CRLF);
print $con $CRLF; print $con $CRLF;
print $con $content; print $con $content;
next; next;
} }
if ($req->header("Range") && !$url_rec->{'force_code'}) { if ($req->header("Range") && !$url_rec->{'force_code'})
{
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/; $req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
my $content_len = length($content); my $content_len = length($content);
my $start = $1 ? $1 : 0; my $start = $1 ? $1 : 0;
my $end = $2 ? $2 : ($content_len - 1); my $end = $2 ? $2 : ($content_len - 1);
my $len = $2 ? ($2 - $start) : ($content_len - $start); my $len = $2 ? ($2 - $start) : ($content_len - $start);
if ($len > 0) { if ($len > 0)
$resp->header("Accept-Ranges" => "bytes"); {
$resp->header("Accept-Ranges" => "bytes");
$resp->header("Content-Length" => $len); $resp->header("Content-Length" => $len);
$resp->header("Content-Range" $resp->header(
=> "bytes $start-$end/$content_len"); "Content-Range" => "bytes $start-$end/$content_len");
$resp->header("Keep-Alive" => "timeout=15, max=100"); $resp->header("Keep-Alive" => "timeout=15, max=100");
$resp->header("Connection" => "Keep-Alive"); $resp->header("Connection" => "Keep-Alive");
$con->send_basic_header(206, $con->send_basic_header(206,
"Partial Content", $resp->protocol); "Partial Content", $resp->protocol);
print $con $resp->headers_as_string($CRLF); print $con $resp->headers_as_string($CRLF);
print $con $CRLF; print $con $CRLF;
print $con substr($content, $start, $len); print $con substr($content, $start, $len);
} else { }
else
{
$con->send_basic_header(416, "Range Not Satisfiable", $con->send_basic_header(416, "Range Not Satisfiable",
$resp->protocol); $resp->protocol);
$resp->header("Keep-Alive" => "timeout=15, max=100"); $resp->header("Keep-Alive" => "timeout=15, max=100");
$resp->header("Connection" => "Keep-Alive"); $resp->header("Connection" => "Keep-Alive");
print $con $CRLF; print $con $CRLF;
} }
next; next;
} }
# fill in content # fill in content
$content = $self->_substitute_port($content) if defined $content; $content = $self->_substitute_port($content) if defined $content;
$resp->content($content); $resp->content($content);
print STDERR "HTTP::Response with content: \n", $resp->as_string if $log; print STDERR "HTTP::Response with content: \n", $resp->as_string
if $log;
} }
$con->send_response($resp); $con->send_response($resp);
@ -134,60 +161,81 @@ sub send_response {
# Generates appropriate response content based on the authentication # Generates appropriate response content based on the authentication
# status of the URL. # status of the URL.
sub handle_auth { sub handle_auth
{
my ($self, $req, $url_rec) = @_; my ($self, $req, $url_rec) = @_;
my ($send_content, $code, $msg, $headers); my ($send_content, $code, $msg, $headers);
# Catch failure to set code, msg: # Catch failure to set code, msg:
$code = 500; $code = 500;
$msg = "Didn't set response code in handle_auth"; $msg = "Didn't set response code in handle_auth";
# Most cases, we don't want to send content. # Most cases, we don't want to send content.
$send_content = 0; $send_content = 0;
# Initialize headers # Initialize headers
$headers = {}; $headers = {};
my $authhdr = $req->header('Authorization'); my $authhdr = $req->header('Authorization');
# Have we sent the challenge yet? # Have we sent the challenge yet?
unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge}) { unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge})
{
# Since we haven't challenged yet, we'd better not # Since we haven't challenged yet, we'd better not
# have received authentication (for our testing purposes). # have received authentication (for our testing purposes).
if ($authhdr) { if ($authhdr)
{
$code = 400; $code = 400;
$msg = "You sent auth before I sent challenge"; $msg = "You sent auth before I sent challenge";
} else { }
else
{
# Send challenge # Send challenge
$code = 401; $code = 401;
$msg = "Authorization Required"; $msg = "Authorization Required";
$headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'} $headers->{'WWW-Authenticate'} =
. " realm=\"wget-test\""; $url_rec->{'auth_method'} . " realm=\"wget-test\"";
$url_rec->{auth_challenged} = 1; $url_rec->{auth_challenged} = 1;
} }
} elsif (!defined($authhdr)) { }
elsif (!defined($authhdr))
{
# We've sent the challenge; we should have received valid # We've sent the challenge; we should have received valid
# authentication with this one. A normal server would just # authentication with this one. A normal server would just
# resend the challenge; but since this is a test, wget just # resend the challenge; but since this is a test, wget just
# failed it. # failed it.
$code = 400; $code = 400;
$msg = "You didn't send auth after I sent challenge"; $msg = "You didn't send auth after I sent challenge";
if ($url_rec->{auth_no_challenge}) { if ($url_rec->{auth_no_challenge})
$msg = "--auth-no-challenge but no auth sent." {
$msg = "--auth-no-challenge but no auth sent.";
} }
} else { }
else
{
my ($sent_method) = ($authhdr =~ /^(\S+)/g); my ($sent_method) = ($authhdr =~ /^(\S+)/g);
unless ($sent_method eq $url_rec->{'auth_method'}) { unless ($sent_method eq $url_rec->{'auth_method'})
{
# Not the authorization type we were expecting. # Not the authorization type we were expecting.
$code = 400; $code = 400;
$msg = "Expected auth type $url_rec->{'auth_method'} but got " $msg = "Expected auth type $url_rec->{'auth_method'} but got "
. "$sent_method"; . "$sent_method";
} elsif (($sent_method eq 'Digest' }
&& &verify_auth_digest($authhdr, $url_rec, \$msg)) elsif (
|| (
($sent_method eq 'Basic' $sent_method eq 'Digest'
&& &verify_auth_basic($authhdr, $url_rec, \$msg))) { && &verify_auth_digest($authhdr, $url_rec, \$msg)
)
|| ( $sent_method eq 'Basic'
&& &verify_auth_basic($authhdr, $url_rec, \$msg))
)
{
# SUCCESSFUL AUTH: send expected message, headers, content. # SUCCESSFUL AUTH: send expected message, headers, content.
($code, $msg) = @{$url_rec}{'code', 'msg'}; ($code, $msg) = @{$url_rec}{'code', 'msg'};
$headers = $url_rec->{headers}; $headers = $url_rec->{headers};
$send_content = 1; $send_content = 1;
} else { }
else
{
$code = 400; $code = 400;
} }
} }
@ -195,43 +243,58 @@ sub handle_auth {
return ($send_content, $code, $msg, $headers); return ($send_content, $code, $msg, $headers);
} }
sub verify_auth_digest { sub verify_auth_digest
return undef; # Not yet implemented. {
return undef; # Not yet implemented.
} }
sub verify_auth_basic { sub verify_auth_basic
{
require MIME::Base64; require MIME::Base64;
my ($authhdr, $url_rec, $msgref) = @_; my ($authhdr, $url_rec, $msgref) = @_;
my $expected = MIME::Base64::encode_base64($url_rec->{'user'} . ':' my $expected =
. $url_rec->{'passwd'}, ''); MIME::Base64::encode_base64(
$url_rec->{'user'} . ':' . $url_rec->{'passwd'},
'');
my ($got) = $authhdr =~ /^Basic (.*)$/; my ($got) = $authhdr =~ /^Basic (.*)$/;
if ($got eq $expected) { if ($got eq $expected)
{
return 1; return 1;
} else { }
else
{
$$msgref = "Wanted ${expected} got ${got}"; $$msgref = "Wanted ${expected} got ${got}";
return undef; return undef;
} }
} }
sub verify_request_headers { sub verify_request_headers
{
my ($self, $req, $url_rec) = @_; my ($self, $req, $url_rec) = @_;
return 1 unless exists $url_rec->{'request_headers'}; return 1 unless exists $url_rec->{'request_headers'};
for my $hdrname (keys %{$url_rec->{'request_headers'}}) { for my $hdrname (keys %{$url_rec->{'request_headers'}})
{
my $must_not_match; my $must_not_match;
my $ehdr = $url_rec->{'request_headers'}{$hdrname}; my $ehdr = $url_rec->{'request_headers'}{$hdrname};
if ($must_not_match = ($hdrname =~ /^!(\w+)/)) { if ($must_not_match = ($hdrname =~ /^!(\w+)/))
{
$hdrname = $1; $hdrname = $1;
} }
my $rhdr = $req->header ($hdrname); my $rhdr = $req->header($hdrname);
if ($must_not_match) { if ($must_not_match)
if (defined $rhdr && $rhdr =~ $ehdr) { {
if (defined $rhdr && $rhdr =~ $ehdr)
{
$rhdr = '' unless defined $rhdr; $rhdr = '' unless defined $rhdr;
print STDERR "\n*** Match forbidden $hdrname: $rhdr =~ $ehdr\n"; print STDERR "\n*** Match forbidden $hdrname: $rhdr =~ $ehdr\n";
return undef; return undef;
} }
} else { }
unless (defined $rhdr && $rhdr =~ $ehdr) { else
{
unless (defined $rhdr && $rhdr =~ $ehdr)
{
$rhdr = '' unless defined $rhdr; $rhdr = '' unless defined $rhdr;
print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n"; print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n";
return undef; return undef;
@ -242,9 +305,10 @@ sub verify_request_headers {
return 1; return 1;
} }
sub _substitute_port { sub _substitute_port
{
my $self = shift; my $self = shift;
my $ret = shift; my $ret = shift;
$ret =~ s/{{port}}/$self->sockport/eg; $ret =~ s/{{port}}/$self->sockport/eg;
return $ret; return $ret;
} }

View File

@ -9,9 +9,8 @@ use WgetTests;
our @ISA = qw(WgetTest); our @ISA = qw(WgetTest);
my $VERSION = 0.01; my $VERSION = 0.01;
{ {
my %_attr_data = ( # DEFAULT my %_attr_data = ( # DEFAULT
); );
sub _default_for sub _default_for
@ -28,25 +27,26 @@ my $VERSION = 0.01;
} }
} }
sub _setup_server
sub _setup_server { {
my $self = shift; my $self = shift;
$self->{_server} = HTTPServer->new (LocalAddr => 'localhost', $self->{_server} = HTTPServer->new(LocalAddr => 'localhost',
ReuseAddr => 1) ReuseAddr => 1)
or die "Cannot create server!!!"; or die "Cannot create server!!!";
} }
sub _launch_server
sub _launch_server { {
my $self = shift; my $self = shift;
my $synch_func = shift; my $synch_func = shift;
$self->{_server}->run ($self->{_input}, $synch_func); $self->{_server}->run($self->{_input}, $synch_func);
} }
sub _substitute_port { sub _substitute_port
{
my $self = shift; my $self = shift;
my $ret = shift; my $ret = shift;
$ret =~ s/{{port}}/$self->{_server}->sockport/eg; $ret =~ s/{{port}}/$self->{_server}->sockport/eg;
return $ret; return $ret;
} }

View File

@ -3,26 +3,41 @@ package WgetFeature;
use strict; use strict;
use warnings; use warnings;
our $VERSION = 0.01;
use Carp;
use English qw(-no_match_vars);
use WgetTests; use WgetTests;
our %skip_messages; our %SKIP_MESSAGES;
require 'WgetFeature.cfg'; {
open my $fh, '<', 'WgetFeature.cfg'
or croak "Cannot open 'WgetFeature.cfg': $ERRNO";
my @lines = <$fh>;
close $fh or carp "Cannot close 'WgetFeature.cfg': $ERRNO";
eval {
@lines;
1;
} or carp "Cannot eval 'WgetFeature.cfg': $ERRNO";
}
sub import sub import
{ {
my ($class, $feature) = @_; my ($class, $feature) = @_;
my $output = `$WgetTest::WGETPATH --version`; my $output = `$WgetTest::WGETPATH --version`;
my ($list) = $output =~ /^([\+\-]\S+(?:\s+[\+\-]\S+)+)/m; my ($list) = $output =~ m/^([+-]\S+(?:\s+[+-]\S+)+)/msx;
my %have_features = map { my %have_features;
my $feature = $_; for my $f (split m/\s+/msx, $list)
$feature =~ s/^.//; {
($feature, /^\+/ ? 1 : 0); my $feat = $f;
} split /\s+/, $list; $feat =~ s/^.//msx;
$have_features{$feat} = $f =~ m/^[+]/msx ? 1 : 0;
unless ($have_features{$feature}) { }
print $skip_messages{$feature}, "\n"; if (!$have_features{$feature})
exit 77; # skip {
print "$SKIP_MESSAGES{$feature}\n";
exit 77; # skip
} }
} }

View File

@ -1,85 +1,103 @@
package WgetTest; package WgetTest;
$VERSION = 0.01;
use strict; use strict;
use warnings; use warnings;
our $VERSION = 0.01;
use Carp;
use Cwd; use Cwd;
use English qw(-no_match_vars);
use File::Path; use File::Path;
use IO::Handle;
use POSIX qw(locale_h); use POSIX qw(locale_h);
use locale; use locale;
our $WGETPATH = "../src/wget"; our $WGETPATH = '../src/wget';
my @unexpected_downloads = (); my @unexpected_downloads = ();
{ {
my %_attr_data = ( # DEFAULT my %_attr_data = ( # DEFAULT
_cmdline => "", _cmdline => q{},
_workdir => Cwd::getcwd(), _workdir => Cwd::getcwd(),
_errcode => 0, _errcode => 0,
_existing => {}, _existing => {},
_input => {}, _input => {},
_name => $0, _name => $PROGRAM_NAME,
_output => {}, _output => {},
_server_behavior => {}, _server_behavior => {},
); );
sub _default_for sub _default_for
{ {
my ($self, $attr) = @_; my ($self, $attr) = @_;
$_attr_data{$attr}; return $_attr_data{$attr};
} }
sub _standard_keys sub _standard_keys
{ {
keys %_attr_data; return keys %_attr_data;
} }
} }
sub new
sub new { {
my ($caller, %args) = @_; my ($caller, %args) = @_;
my $caller_is_obj = ref($caller); my $caller_is_obj = ref $caller;
my $class = $caller_is_obj || $caller; my $class = $caller_is_obj || $caller;
#print STDERR "class = ", $class, "\n"; #print STDERR "class = ", $class, "\n";
#print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n"; #print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
my $self = bless {}, $class; my $self = bless {}, $class;
foreach my $attrname ($self->_standard_keys()) { for my $attrname ($self->_standard_keys())
{
#print STDERR "attrname = ", $attrname, " value = "; #print STDERR "attrname = ", $attrname, " value = ";
my ($argname) = ($attrname =~ /^_(.*)/); my ($argname) = ($attrname =~ m/^_(.*)/msx);
if (exists $args{$argname}) { if (exists $args{$argname})
{
#printf STDERR "Setting up $attrname\n"; #printf STDERR "Setting up $attrname\n";
$self->{$attrname} = $args{$argname}; $self->{$attrname} = $args{$argname};
} elsif ($caller_is_obj) { }
elsif ($caller_is_obj)
{
#printf STDERR "Copying $attrname\n"; #printf STDERR "Copying $attrname\n";
$self->{$attrname} = $caller->{$attrname}; $self->{$attrname} = $caller->{$attrname};
} else { }
else
{
#printf STDERR "Using default for $attrname\n"; #printf STDERR "Using default for $attrname\n";
$self->{$attrname} = $self->_default_for($attrname); $self->{$attrname} = $self->_default_for($attrname);
} }
#print STDERR $attrname, '=', $self->{$attrname}, "\n"; #print STDERR $attrname, '=', $self->{$attrname}, "\n";
} }
#printf STDERR "_workdir default = ", $self->_default_for("_workdir"); #printf STDERR "_workdir default = ", $self->_default_for("_workdir");
return $self; return $self;
} }
sub run
sub run { {
my $self = shift; my $self = shift;
my $result_message = "Test successful.\n"; my $result_message = "Test successful.\n";
my $errcode; my $errcode;
$self->{_name} =~ s{.*/}{}; # remove path $self->{_name} =~ s{.*/}{}msx; # remove path
$self->{_name} =~ s{\.[^.]+$}{}; # remove extension $self->{_name} =~ s{[.][^.]+$}{}msx; # remove extension
printf "Running test $self->{_name}\n"; printf "Running test $self->{_name}\n";
# Setup # Setup
my $new_result = $self->_setup(); my $new_result = $self->_setup();
chdir ("$self->{_workdir}/$self->{_name}/input"); chdir "$self->{_workdir}/$self->{_name}/input"
if (defined $new_result) { or carp "Could not chdir to input directory: $ERRNO";
if (defined $new_result)
{
$result_message = $new_result; $result_message = $new_result;
$errcode = 1; $errcode = 1;
goto cleanup; goto cleanup;
} }
@ -87,140 +105,175 @@ sub run {
my $pid = $self->_fork_and_launch_server(); my $pid = $self->_fork_and_launch_server();
# Call wget # Call wget
chdir ("$self->{_workdir}/$self->{_name}/output"); chdir "$self->{_workdir}/$self->{_name}/output"
or carp "Could not chdir to output directory: $ERRNO";
my $cmdline = $self->{_cmdline}; my $cmdline = $self->{_cmdline};
$cmdline = $self->_substitute_port($cmdline); $cmdline = $self->_substitute_port($cmdline);
$cmdline = ($cmdline =~ m{^/.*}) ? $cmdline : "$self->{_workdir}/$cmdline"; $cmdline =
($cmdline =~ m{^/.*}msx) ? $cmdline : "$self->{_workdir}/$cmdline";
my $valgrind = $ENV{VALGRIND_TESTS}; my $valgrind = $ENV{VALGRIND_TESTS};
if (!defined $valgrind || $valgrind == "" || $valgrind == "0") { if (!defined $valgrind || $valgrind eq q{} || $valgrind == 0)
{
# Valgrind not requested - leave $cmdline as it is # Valgrind not requested - leave $cmdline as it is
} elsif ($valgrind == "1") { }
$cmdline = "valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes " . $cmdline; elsif ($valgrind == 1)
} else { {
$cmdline = $valgrind . " " . $cmdline; $cmdline =
'valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes '
. $cmdline;
}
else
{
$cmdline = "$valgrind $cmdline";
} }
print "Calling $cmdline\n"; print "Calling $cmdline\n";
$errcode = system($cmdline); $errcode = system $cmdline;
$errcode >>= 8; # XXX: should handle abnormal error codes. $errcode >>= 8; # XXX: should handle abnormal error codes.
# Shutdown server # Shutdown server
# if we didn't explicitely kill the server, we would have to call # 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 # waitpid ($pid, 0) here in order to wait for the child process to
# terminate # terminate
kill ('TERM', $pid); kill 'TERM', $pid;
# Verify download # Verify download
unless ($errcode == $self->{_errcode}) { if ($errcode != $self->{_errcode})
$result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n"; {
goto cleanup; $result_message =
"Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
goto CLEANUP;
} }
my $error_str; my $error_str;
if ($error_str = $self->_verify_download()) { if ($error_str = $self->_verify_download())
{
$result_message = $error_str; $result_message = $error_str;
} }
cleanup: CLEANUP:
$self->_cleanup(); $self->_cleanup();
print $result_message; print $result_message;
return $errcode != $self->{_errcode} || ($error_str ? 1 : 0); return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
} }
sub _setup
sub _setup { {
my $self = shift; my $self = shift;
#print $self->{_name}, "\n"; chdir $self->{_workdir}
chdir ($self->{_workdir}); or carp "Could not chdir into $self->{_workdir}: $ERRNO";
# Create temporary directory # Create temporary directory
mkdir ($self->{_name}); mkdir $self->{_name} or carp "Could not mkdir '$self->{_name}': $ERRNO";
chdir ($self->{_name}); chdir $self->{_name}
mkdir ("input"); or carp "Could not chdir into '$self->{_name}': $ERRNO";
mkdir ("output"); mkdir 'input' or carp "Could not mkdir 'input' $ERRNO";
mkdir 'output' or carp "Could not mkdir 'output': $ERRNO";
# Setup existing files # Setup existing files
chdir ("output"); chdir 'output' or carp "Could not chdir into 'output': $ERRNO";
foreach my $filename (keys %{$self->{_existing}}) { for my $filename (keys %{$self->{_existing}})
open (FILE, ">$filename") {
or return "Test failed: cannot open pre-existing file $filename\n"; open my $fh, '>', $filename
or return "Test failed: cannot open pre-existing file $filename\n";
my $file = $self->{_existing}->{$filename}; my $file = $self->{_existing}->{$filename};
print FILE $file->{content} print {$fh} $file->{content}
or return "Test failed: cannot write pre-existing file $filename\n"; or return "Test failed: cannot write pre-existing file $filename\n";
close (FILE); close $fh or carp $ERRNO;
if (exists($file->{timestamp})) { if (exists($file->{timestamp}))
{
utime $file->{timestamp}, $file->{timestamp}, $filename utime $file->{timestamp}, $file->{timestamp}, $filename
or return "Test failed: cannot set timestamp on pre-existing file $filename\n"; or return
"Test failed: cannot set timestamp on pre-existing file $filename\n";
} }
} }
chdir ("../input"); chdir '../input' or carp "Cannot chdir into '../input': $ERRNO";
$self->_setup_server(); $self->_setup_server();
chdir ($self->{_workdir}); chdir $self->{_workdir}
or carp "Cannot chdir into '$self->{_workdir}': $ERRNO";
return; return;
} }
sub _cleanup
sub _cleanup { {
my $self = shift; my $self = shift;
chdir ($self->{_workdir}); chdir $self->{_workdir}
File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP}; 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 # not a method
sub quotechar { sub quotechar
my $c = ord( shift ); {
if ($c >= 0x7 && $c <= 0xD) { my $c = ord shift;
return '\\' . qw(a b t n v f r)[$c - 0x7]; if ($c >= 0x7 && $c <= 0xD)
} else { {
return sprintf('\\x%02x', $c); return q{\\} . qw(a b t n v f r) [$c - 0x7];
}
else
{
return sprintf '\\x%02x', $c;
} }
} }
# not a method # not a method
sub _show_diff { sub _show_diff
{
my ($expected, $actual) = @_;
my $SNIPPET_SIZE = 10; my $SNIPPET_SIZE = 10;
my ($expected, $actual) = @_; my $str = q{};
my $str = '';
my $explen = length $expected; my $explen = length $expected;
my $actlen = length $actual; my $actlen = length $actual;
if ($explen != $actlen) { if ($explen != $actlen)
{
$str .= "Sizes don't match: expected = $explen, actual = $actlen\n"; $str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
} }
my $min = $explen <= $actlen? $explen : $actlen; my $min = $explen <= $actlen ? $explen : $actlen;
my $line = 1; my $line = 1;
my $col = 1; my $col = 1;
my $i; my $i;
for ($i=0; $i != $min; ++$i) {
last if substr($expected, $i, 1) ne substr($actual, $i, 1); # for ($i=0; $i != $min; ++$i) {
if (substr($expected, $i, 1) eq '\n') { for my $i (0 .. $min - 1)
{
last if substr($expected, $i, 1) ne substr $actual, $i, 1;
if (substr($expected, $i, 1) eq q{\n})
{
$line++; $line++;
$col = 0; $col = 0;
} else { }
else
{
$col++; $col++;
} }
} }
my $snip_start = $i - ($SNIPPET_SIZE / 2); my $snip_start = $i - ($SNIPPET_SIZE / 2);
if ($snip_start < 0) { if ($snip_start < 0)
$SNIPPET_SIZE += $snip_start; # Take it from the end. {
$SNIPPET_SIZE += $snip_start; # Take it from the end.
$snip_start = 0; $snip_start = 0;
} }
my $exp_snip = substr($expected, $snip_start, $SNIPPET_SIZE); my $exp_snip = substr $expected, $snip_start, $SNIPPET_SIZE;
my $act_snip = substr($actual, $snip_start, $SNIPPET_SIZE); my $act_snip = substr $actual, $snip_start, $SNIPPET_SIZE;
$exp_snip =~s/[^[:print:]]/ quotechar($&) /ge; $exp_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
$act_snip =~s/[^[:print:]]/ quotechar($&) /ge; $act_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
$str .= "Mismatch at line $line, col $col:\n"; $str .= "Mismatch at line $line, col $col:\n";
$str .= " $exp_snip\n"; $str .= " $exp_snip\n";
$str .= " $act_snip\n"; $str .= " $act_snip\n";
@ -228,102 +281,138 @@ sub _show_diff {
return $str; return $str;
} }
sub _verify_download { sub _verify_download
{
my $self = shift; my $self = shift;
chdir ("$self->{_workdir}/$self->{_name}/output"); chdir "$self->{_workdir}/$self->{_name}/output"
or carp "Could not chdir into output directory: $ERRNO";
# use slurp mode to read file content # use slurp mode to read file content
my $old_input_record_separator = $/; my $old_input_record_separator = $INPUT_RECORD_SEPARATOR;
undef $/; local $INPUT_RECORD_SEPARATOR = undef;
while (my ($filename, $filedata) = each %{$self->{_output}}) { while (my ($filename, $filedata) = each %{$self->{_output}})
open (FILE, $filename) {
or return "Test failed: file $filename not downloaded\n"; open my $fh, '<', $filename
or return "Test failed: file $filename not downloaded\n";
my $content = <$fh>;
close $fh or carp $ERRNO;
my $content = <FILE>;
my $expected_content = $filedata->{'content'}; my $expected_content = $filedata->{'content'};
$expected_content = $self->_substitute_port($expected_content); $expected_content = $self->_substitute_port($expected_content);
unless ($content eq $expected_content) { if ($content ne $expected_content)
{
return "Test failed: wrong content for file $filename\n" return "Test failed: wrong content for file $filename\n"
. _show_diff($expected_content, $content); . _show_diff($expected_content, $content);
} }
if (exists($filedata->{'timestamp'})) { if (exists($filedata->{'timestamp'}))
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, {
$atime, $mtime, $ctime, $blksize, $blocks) = stat FILE; my (
$dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime,
$ctime, $blksize, $blocks
)
= stat $filename;
$mtime == $filedata->{'timestamp'} $mtime == $filedata->{'timestamp'}
or return "Test failed: wrong timestamp for file $filename\n"; or return "Test failed: wrong timestamp for file $filename\n";
} }
close (FILE);
} }
$/ = $old_input_record_separator; local $INPUT_RECORD_SEPARATOR = $old_input_record_separator;
# make sure no unexpected files were downloaded # make sure no unexpected files were downloaded
chdir ("$self->{_workdir}/$self->{_name}/output"); chdir "$self->{_workdir}/$self->{_name}/output"
or carp "Could not change into output directory: $ERRNO";
__dir_walk('.', __dir_walk(
sub { push @unexpected_downloads, q{.},
$_[0] unless (exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}) }, sub {
sub { shift; return @_ } ); if (!(exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}))
if (@unexpected_downloads) { {
return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n"; push @unexpected_downloads, $_[0];
}
},
sub { shift; return @_ }
);
if (@unexpected_downloads)
{
return 'Test failed: unexpected downloaded files [' . join ', ',
@unexpected_downloads . "]\n";
} }
return ""; return q{};
} }
sub __dir_walk
sub __dir_walk { {
my ($top, $filefunc, $dirfunc) = @_; my ($top, $filefunc, $dirfunc) = @_;
my $DIR; my $DIR;
if (-d $top) { if (-d $top)
{
my $file; my $file;
unless (opendir $DIR, $top) { if (!opendir $DIR, $top)
warn "Couldn't open directory $DIR: $!; skipping.\n"; {
warn "Couldn't open directory $DIR: $ERRNO; skipping.\n";
return; return;
} }
my @results; my @results;
while ($file = readdir $DIR) { while ($file = readdir $DIR)
next if $file eq '.' || $file eq '..'; {
my $nextdir = $top eq '.' ? $file : "$top/$file"; next if $file eq q{.} || $file eq q{..};
my $nextdir = $top eq q{.} ? $file : "$top/$file";
push @results, __dir_walk($nextdir, $filefunc, $dirfunc); push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
} }
return $dirfunc ? $dirfunc->($top, @results) : () ; return $dirfunc ? $dirfunc->($top, @results) : ();
} else { }
return $filefunc ? $filefunc->($top) : () ; else
{
return $filefunc ? $filefunc->($top) : ();
} }
} }
sub _fork_and_launch_server sub _fork_and_launch_server
{ {
my $self = shift; my $self = shift;
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!"; pipe FROM_CHILD, TO_PARENT or croak 'Cannot create pipe!';
select((select(TO_PARENT), $| = 1)[0]); TO_PARENT->autoflush();
my $pid = fork;
if ($pid < 0)
{
carp 'Cannot fork';
}
elsif ($pid == 0)
{
my $pid = fork();
if ($pid < 0) {
die "Cannot fork";
} elsif ($pid == 0) {
# child # child
close FROM_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 # FTP Server has to start with english locale due to use of strftime month names in LIST command
setlocale(LC_ALL,"C"); setlocale(LC_ALL, 'C');
$self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT }); $self->_launch_server(
} else { sub {
print {*TO_PARENT} "SYNC\n";
close TO_PARENT or carp $ERRNO;
}
);
}
else
{
# father # father
close TO_PARENT; close TO_PARENT or carp $ERRNO;
chomp(my $line = <FROM_CHILD>); chomp(my $line = <FROM_CHILD>);
close FROM_CHILD; close FROM_CHILD or carp $ERRNO;
} }
return $pid; return $pid;