App-Syslogd
view release on metacpan or search on metacpan
t/edge_cases.t view on Meta::CPAN
# A server writing to a new file, with names off unless asked
sub new_server {
my (%args) = @_;
my $file = delete($args{file}) // new_path();
return (App::Syslogd->new(file => $file, resolve => 0, %args)->reopen_log(), $file);
}
# Collect warnings from $code
sub warnings_from {
my $code = shift;
my @warnings;
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
$code->();
verbose_diag(explain(\@warnings)) if(@warnings);
return \@warnings;
}
# A cache double whose compute() does whatever $behaviour says
{
package HostileCache;
sub new { my ($class, $behaviour) = @_; return bless { behaviour => $behaviour }, $class }
sub compute { my $self = shift; return $self->{behaviour}->(@_) }
}
# A socket double driven by a list of steps (code refs)
{
package ScriptedSocket;
sub new { my ($class, @steps) = @_; return bless { steps => [@steps], closed => 0 }, $class }
sub recv { my $self = $_[0]; my $step = shift(@{$self->{steps}}) or return undef; return $step->(\$_[1]) }
sub close { $_[0]{closed}++; return 1 }
}
# An object that turns itself into a string
{
package StringLike;
use overload q{""} => sub { ${$_[0]} }, fallback => 1;
sub new { my ($class, $text) = @_; return bless \$text, $class }
}
# ===========================================================================
# 1. lib/App/Syslogd.pm
# ===========================================================================
subtest 'new: hostile arguments are refused with the documented errors' => sub {
# Purpose: every wrong type, size or shape is refused by the
# validator, naming the parameter, before anything is opened
my $cyclic = {};
$cyclic->{self} = $cyclic;
my %bad = (
'port as an array' => [{ port => [514] }, qr/Parameter 'port'/],
'port as a hash' => [{ port => {} }, qr/Parameter 'port'/],
'port as code' => [{ port => sub { 514 } }, qr/Parameter 'port'/],
'port as a typeglob' => [{ port => \*STDOUT }, qr/Parameter 'port'/],
'port as a scalar ref' => [{ port => \'514' }, qr/Parameter 'port'/],
'port 65536' => [{ port => $CONFIG{max_port} + 1 }, qr/Parameter 'port' \(65536\) must be no more than 65535/],
'port -1' => [{ port => -1 }, qr/Parameter 'port' \(-1\) must be at least 0/],
'port 1e100' => [{ port => 1e100 }, qr/Parameter 'port' .* must be no more than 65535/],
'port in hex' => [{ port => '0x10' }, qr/Parameter 'port' \(0x10\) must be an integer/],
'port with trailing text' => [{ port => '514; rm -rf /' }, qr/Parameter 'port' .* must be an integer/],
'port NaN' => [{ port => 'NaN' }, qr/Parameter 'port'/],
'port Inf' => [{ port => 'Inf' }, qr/Parameter 'port'/],
'empty file name' => [{ file => '' }, qr/Parameter 'file'/],
'file as a typeglob' => [{ file => \*STDOUT }, qr/Parameter 'file'/],
'file as an array' => [{ file => ['/tmp/x'] }, qr/Parameter 'file'/],
'empty address' => [{ address => '' }, qr/Parameter 'address'/],
'resolve 2' => [{ resolve => 2 }, qr/Parameter 'resolve' \(2\) must be a boolean/],
'resolve as an array' => [{ resolve => [] }, qr/Parameter 'resolve'/],
'empty language' => [{ language => '' }, qr/Parameter 'language'/],
'negative dns_ttl' => [{ dns_ttl => -1 }, qr/Parameter 'dns_ttl'/],
'zero dns_cache_bytes' => [{ dns_cache_bytes => 0 }, qr/Parameter 'dns_cache_bytes'/],
'cache without compute' => [{ cache => bless({}, 'NoCompute') }, qr/Parameter 'cache'/],
'cache as a plain hash' => [{ cache => { compute => 1 } }, qr/Parameter 'cache'/],
'cache as a cyclic plain hash' => [{ cache => $cyclic }, qr/Parameter 'cache'/],
'socket as a typeglob' => [{ socket => \*STDIN }, qr/Parameter 'socket'/],
'socket as code' => [{ socket => sub { } }, qr/Parameter 'socket'/],
'unknown parameter' => [{ evil => 1 }, qr/Unknown parameter 'evil'/],
);
foreach my $case (sort keys %bad) {
my ($args, $error) = @{$bad{$case}};
throws_ok { App::Syslogd->new($args) } $error, $case;
}
# A list with an odd number of items is not "pairs"
dies_ok { App::Syslogd->new('port') } 'an odd-length argument list';
dies_ok { App::Syslogd->new({ port => 1 }, 'extra') } 'a hashref followed by more arguments';
};
subtest 'new: boundaries that are allowed' => sub {
# Purpose: the edges of every range are accepted, undef means
# "default", and duplicate keys follow Perl's last-one-wins rule
is(App::Syslogd->new(port => 0)->port(), 0, 'port 0 (the kernel chooses)');
is(App::Syslogd->new(port => $CONFIG{max_port})->port(), $CONFIG{max_port}, 'port 65535');
is(App::Syslogd->new(port => undef)->port(), 514, 'port undef: the default');
is(App::Syslogd->new(port => 1, port => 2)->port(), 2, 'duplicate keys: the last one wins');
lives_ok { App::Syslogd->new(dns_ttl => 0) } 'dns_ttl 0 (do not remember names)';
lives_ok { App::Syslogd->new(dns_cache_bytes => 1) } 'a one-byte DNS cache';
my $cyclic = HostileCache->new(sub { 'x' });
$cyclic->{self} = $cyclic;
lives_ok { App::Syslogd->new(cache => $cyclic) } 'a cache object that refers to itself';
weaken($cyclic->{self});
};
subtest 'new: hostile settings from the environment are refused' => sub {
# Purpose: Object::Configure lets the environment set options; a
# hostile value there must be refused like an argument
foreach my $value ('514; rm -rf /', '$(reboot)', '70000', '-1', '') {
local $ENV{App__Syslogd__port} = $value;
next if($value eq ''); # an empty variable is ignored by Object::Configure
throws_ok { App::Syslogd->new() } qr/Parameter 'port'/, "App__Syslogd__port='$value'";
}
};
subtest 'security: the language tag cannot load code or files' => sub {
# Purpose: Locale::Maketext turns the tag into a module name and loads
# it; a hostile tag must not run code or reach outside the lexicons
my $sentinel = new_path('pwned');
foreach my $tag (qq{en; system("touch $sentinel")}, '../../../../etc/passwd', 'en::Evil', 'x' x 10_000) {
my $server = App::Syslogd->new(language => $tag);
isa_ok($server->{lh}, 'App::Syslogd::I18N::en', 'falls back to English');
}
( run in 0.755 second using v1.01-cache-2.11-cpan-60884a8df76 )