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>
* 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);
my $VERSION = 0.01;
{
my %_attr_data = ( # DEFAULT
my %_attr_data = ( # DEFAULT
);
sub _default_for
@ -28,29 +27,32 @@ my $VERSION = 0.01;
}
}
sub _setup_server {
sub _setup_server
{
my $self = shift;
$self->{_server} = FTPServer->new (input => $self->{_input},
server_behavior =>
$self->{_server_behavior},
LocalAddr => 'localhost',
ReuseAddr => 1,
rootDir => "$self->{_workdir}/$self->{_name}/input") or die "Cannot create server!!!";
$self->{_server} = FTPServer->new(
input => $self->{_input},
server_behavior => $self->{_server_behavior},
LocalAddr => 'localhost',
ReuseAddr => 1,
rootDir => "$self->{_workdir}/$self->{_name}/input"
)
or die "Cannot create server!!!";
}
sub _launch_server {
my $self = shift;
sub _launch_server
{
my $self = 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 $ret = shift;
my $ret = shift;
$ret =~ s/{{port}}/$self->{_server}->sockport/eg;
return $ret;
}

View File

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

View File

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

View File

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

View File

@ -1,85 +1,103 @@
package WgetTest;
$VERSION = 0.01;
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;
our $WGETPATH = "../src/wget";
our $WGETPATH = '../src/wget';
my @unexpected_downloads = ();
{
my %_attr_data = ( # DEFAULT
_cmdline => "",
_workdir => Cwd::getcwd(),
_errcode => 0,
_existing => {},
_input => {},
_name => $0,
_output => {},
_server_behavior => {},
);
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) = @_;
$_attr_data{$attr};
return $_attr_data{$attr};
}
sub _standard_keys
{
keys %_attr_data;
return keys %_attr_data;
}
}
sub new {
sub new
{
my ($caller, %args) = @_;
my $caller_is_obj = ref($caller);
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;
foreach my $attrname ($self->_standard_keys()) {
for my $attrname ($self->_standard_keys())
{
#print STDERR "attrname = ", $attrname, " value = ";
my ($argname) = ($attrname =~ /^_(.*)/);
if (exists $args{$argname}) {
my ($argname) = ($attrname =~ m/^_(.*)/msx);
if (exists $args{$argname})
{
#printf STDERR "Setting up $attrname\n";
$self->{$attrname} = $args{$argname};
} elsif ($caller_is_obj) {
}
elsif ($caller_is_obj)
{
#printf STDERR "Copying $attrname\n";
$self->{$attrname} = $caller->{$attrname};
} else {
}
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;
sub run
{
my $self = shift;
my $result_message = "Test successful.\n";
my $errcode;
$self->{_name} =~ s{.*/}{}; # remove path
$self->{_name} =~ s{\.[^.]+$}{}; # remove extension
$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");
if (defined $new_result) {
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;
$errcode = 1;
goto cleanup;
}
@ -87,140 +105,175 @@ sub run {
my $pid = $self->_fork_and_launch_server();
# 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};
$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};
if (!defined $valgrind || $valgrind == "" || $valgrind == "0") {
if (!defined $valgrind || $valgrind eq q{} || $valgrind == 0)
{
# Valgrind not requested - leave $cmdline as it is
} elsif ($valgrind == "1") {
$cmdline = "valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes " . $cmdline;
} else {
$cmdline = $valgrind . " " . $cmdline;
}
elsif ($valgrind == 1)
{
$cmdline =
'valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes '
. $cmdline;
}
else
{
$cmdline = "$valgrind $cmdline";
}
print "Calling $cmdline\n";
$errcode = system($cmdline);
$errcode >>= 8; # XXX: should handle abnormal error codes.
$errcode = system $cmdline;
$errcode >>= 8; # XXX: should handle abnormal error codes.
# Shutdown server
# 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
# terminate
kill ('TERM', $pid);
kill 'TERM', $pid;
# Verify download
unless ($errcode == $self->{_errcode}) {
$result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
goto cleanup;
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()) {
if ($error_str = $self->_verify_download())
{
$result_message = $error_str;
}
cleanup:
CLEANUP:
$self->_cleanup();
print $result_message;
return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
}
sub _setup {
sub _setup
{
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
mkdir ($self->{_name});
chdir ($self->{_name});
mkdir ("input");
mkdir ("output");
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");
foreach my $filename (keys %{$self->{_existing}}) {
open (FILE, ">$filename")
or return "Test failed: cannot open pre-existing file $filename\n";
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 FILE $file->{content}
or return "Test failed: cannot write pre-existing file $filename\n";
print {$fh} $file->{content}
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
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();
chdir ($self->{_workdir});
chdir $self->{_workdir}
or carp "Cannot chdir into '$self->{_workdir}': $ERRNO";
return;
}
sub _cleanup {
sub _cleanup
{
my $self = shift;
chdir ($self->{_workdir});
File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
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 '\\' . qw(a b t n v f r)[$c - 0x7];
} else {
return sprintf('\\x%02x', $c);
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 {
sub _show_diff
{
my ($expected, $actual) = @_;
my $SNIPPET_SIZE = 10;
my ($expected, $actual) = @_;
my $str = '';
my $str = q{};
my $explen = length $expected;
my $actlen = length $actual;
if ($explen != $actlen) {
if ($explen != $actlen)
{
$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 $col = 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') {
# for ($i=0; $i != $min; ++$i) {
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++;
$col = 0;
} else {
}
else
{
$col++;
}
}
my $snip_start = $i - ($SNIPPET_SIZE / 2);
if ($snip_start < 0) {
$SNIPPET_SIZE += $snip_start; # Take it from the end.
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;
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";
@ -228,102 +281,138 @@ sub _show_diff {
return $str;
}
sub _verify_download {
sub _verify_download
{
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
my $old_input_record_separator = $/;
undef $/;
my $old_input_record_separator = $INPUT_RECORD_SEPARATOR;
local $INPUT_RECORD_SEPARATOR = undef;
while (my ($filename, $filedata) = each %{$self->{_output}}) {
open (FILE, $filename)
or return "Test failed: file $filename not downloaded\n";
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 $content = <FILE>;
my $expected_content = $filedata->{'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"
. _show_diff($expected_content, $content);
. _show_diff($expected_content, $content);
}
if (exists($filedata->{'timestamp'})) {
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
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\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
chdir ("$self->{_workdir}/$self->{_name}/output");
chdir "$self->{_workdir}/$self->{_name}/output"
or carp "Could not change into output directory: $ERRNO";
__dir_walk('.',
sub { push @unexpected_downloads,
$_[0] unless (exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}) },
sub { shift; return @_ } );
if (@unexpected_downloads) {
return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
__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 "";
return q{};
}
sub __dir_walk {
sub __dir_walk
{
my ($top, $filefunc, $dirfunc) = @_;
my $DIR;
if (-d $top) {
if (-d $top)
{
my $file;
unless (opendir $DIR, $top) {
warn "Couldn't open directory $DIR: $!; skipping.\n";
if (!opendir $DIR, $top)
{
warn "Couldn't open directory $DIR: $ERRNO; skipping.\n";
return;
}
my @results;
while ($file = readdir $DIR) {
next if $file eq '.' || $file eq '..';
my $nextdir = $top eq '.' ? $file : "$top/$file";
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) : () ;
return $dirfunc ? $dirfunc->($top, @results) : ();
}
else
{
return $filefunc ? $filefunc->($top) : ();
}
}
sub _fork_and_launch_server
{
my $self = shift;
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
select((select(TO_PARENT), $| = 1)[0]);
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)
{
my $pid = fork();
if ($pid < 0) {
die "Cannot fork";
} elsif ($pid == 0) {
# 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
setlocale(LC_ALL,"C");
$self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
} else {
setlocale(LC_ALL, 'C');
$self->_launch_server(
sub {
print {*TO_PARENT} "SYNC\n";
close TO_PARENT or carp $ERRNO;
}
);
}
else
{
# father
close TO_PARENT;
close TO_PARENT or carp $ERRNO;
chomp(my $line = <FROM_CHILD>);
close FROM_CHILD;
close FROM_CHILD or carp $ERRNO;
}
return $pid;