Merge pull request #112 from msimerson/qpsmtpd.t

Qpsmtpd.t
This commit is contained in:
Jared Johnson 2014-09-17 18:04:55 -05:00
commit 26b1d0bb62
5 changed files with 166 additions and 128 deletions

View File

@ -6,7 +6,7 @@ our $VERSION = '0.95';
use vars qw($TraceLevel $Spool_dir $Size_threshold); use vars qw($TraceLevel $Spool_dir $Size_threshold);
use lib 'lib'; use lib 'lib';
use base 'Qpsmtpd::Base'; use parent 'Qpsmtpd::Base';
use Qpsmtpd::Address; use Qpsmtpd::Address;
use Qpsmtpd::Config; use Qpsmtpd::Config;
use Qpsmtpd::Constants; use Qpsmtpd::Constants;
@ -30,7 +30,7 @@ sub _restart {
} }
} }
sub version { $VERSION . ($git ? "/$git" : "") } sub version { $VERSION . ($git ? "/$git" : '') }
sub git_version { sub git_version {
return if !-e '.git'; return if !-e '.git';
@ -56,35 +56,24 @@ sub hooks {
sub load_logging { sub load_logging {
my $self = shift; my $self = shift;
# avoid triggering log activity return if $LOGGING_LOADED; # already done
return if ($LOGGING_LOADED || $hooks->{'logging'}); return if $hooks->{'logging'}; # avoid triggering log activity
my $configdir = $self->config_dir("logging"); my @plugin_dirs = $self->conf->from_file('plugin_dirs');
my $configfile = "$configdir/logging"; if (!@plugin_dirs) {
my @loggers = $self->conf->from_file($configfile, 'logging');
$configdir = $self->config_dir('plugin_dirs');
$configfile = "$configdir/plugin_dirs";
my @plugin_dirs = $self->conf->from_file($configfile, 'plugin_dirs');
unless (@plugin_dirs) {
my ($name) = ($0 =~ m!(.*?)/([^/]+)$!); my ($name) = ($0 =~ m!(.*?)/([^/]+)$!);
@plugin_dirs = ("$name/plugins"); @plugin_dirs = ("$name/plugins");
} }
my @loaded; my @loggers = $self->conf->from_file('logging');
for my $logger (@loggers) { for my $logger (@loggers) {
push @loaded, $self->_load_plugin($logger, @plugin_dirs); $self->_load_plugin($logger, @plugin_dirs);
}
foreach my $logger (@loaded) {
$self->log(LOGINFO, "Loaded $logger"); $self->log(LOGINFO, "Loaded $logger");
} }
$configdir = $self->config_dir("loglevel"); $TraceLevel = $self->conf->from_file('loglevel');
$configfile = "$configdir/loglevel";
$TraceLevel = $self->conf->from_file($configfile, 'loglevel');
unless (defined($TraceLevel) and $TraceLevel =~ /^\d+$/) { unless (defined($TraceLevel) && $TraceLevel =~ /^\d+$/) {
$TraceLevel = LOGWARN; # Default if no loglevel file found. $TraceLevel = LOGWARN; # Default if no loglevel file found.
} }
@ -180,8 +169,7 @@ sub load_plugins {
} }
sub _load_plugin { sub _load_plugin {
my $self = shift; my ($self, $plugin_line, @plugin_dirs) = @_;
my ($plugin_line, @plugin_dirs) = @_;
# untaint the config data before passing it to plugins # untaint the config data before passing it to plugins
my ($safe_line) = $plugin_line =~ /^([ -~]+)$/ # all ascii printable my ($safe_line) = $plugin_line =~ /^([ -~]+)$/ # all ascii printable
@ -204,7 +192,7 @@ sub _load_plugin {
(/+) # directory (/+) # directory
(\d?) # package's first character (\d?) # package's first character
}[ }[
"::" . (length $2 ? sprintf("_%2x",unpack("C",$2)) : "") "::" . (length $2 ? sprintf("_%2x",unpack("C",$2)) : '')
]egx; ]egx;
my $package = "Qpsmtpd::Plugin::$plugin_name"; my $package = "Qpsmtpd::Plugin::$plugin_name";
@ -269,111 +257,96 @@ sub run_hooks {
sub run_hooks_no_respond { sub run_hooks_no_respond {
my ($self, $hook) = (shift, shift); my ($self, $hook) = (shift, shift);
if ($hooks->{$hook}) { return (0, '') if !$hooks->{$hook};
my @r; my @r;
for my $code ($self->hooks($hook)) { for my $code (@{$hooks->{$hook}}) {
eval { (@r) = $code->{code}->($self, $self->transaction, @_); }; eval { @r = $code->{code}->($self, $self->transaction, @_); };
if ($@) { if ($@) {
warn("FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@); warn("FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@);
next; next;
} }
last unless $r[0] == DECLINED; last if $r[0] != DECLINED;
} }
$r[0] = DECLINED if not defined $r[0]; $r[0] = DECLINED if not defined $r[0];
return @r; return @r;
} }
return (0, '');
}
sub run_continuation { sub run_continuation {
my $self = shift; my $self = shift;
die "No continuation in progress" unless $self->{_continuation}; die "No continuation in progress\n" if !$self->{_continuation};
my $todo = $self->{_continuation}; my $todo = $self->{_continuation};
$self->{_continuation} = undef; $self->{_continuation} = undef;
my $hook = shift @$todo || die "No hook in the continuation"; my $hook = shift @$todo or die "No hook in the continuation";
my $args = shift @$todo || die "No hook args in the continuation"; my $args = shift @$todo or die "No hook args in the continuation";
my @r; my @r;
while (@$todo) { while (@$todo) {
my $code = shift @$todo; my $code = shift @$todo;
my $name = $code->{name};
$self->varlog(LOGDEBUG, $hook, $code->{name}); $self->varlog(LOGDEBUG, $hook, $name);
my $tran = $self->transaction; my $tran = $self->transaction;
eval { (@r) = $code->{code}->($self, $tran, @$args); }; eval { @r = $code->{code}->($self, $tran, @$args); };
if ($@) { if ($@) {
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $self->log(LOGCRIT, "FATAL PLUGIN ERROR [$name]: ", $@);
$@);
next; next;
} }
!defined $r[0] my $log_msg = "Plugin $name, hook $hook returned ";
and $self->log(LOGERROR, if (!defined $r[0]) {
"plugin " $self->log(LOGERROR, $log_msg . "undef!");
. $code->{name} next;
. " running the $hook hook returned undef!" }
)
and next;
# note this is wrong as $tran is always true in the
# current code...
if ($tran) { if ($tran) {
my $tnotes = $tran->notes($code->{name}); my $tnotes = $tran->notes($name);
$tnotes->{"hook_$hook"}->{'return'} = $r[0] if (!defined $tnotes || ref $tnotes eq 'HASH') {
if (!defined $tnotes || ref $tnotes eq "HASH"); $tnotes->{"hook_$hook"}{return} = $r[0];
};
} }
else { else {
my $cnotes = $self->connection->notes($code->{name}); my $cnotes = $self->connection->notes($name);
$cnotes->{"hook_$hook"}->{'return'} = $r[0] if (!defined $cnotes || ref $cnotes eq 'HASH') {
if (!defined $cnotes || ref $cnotes eq "HASH"); $cnotes->{"hook_$hook"}{return} = $r[0];
};
} }
if ( $r[0] == DENY if ( $r[0] == DENY
or $r[0] == DENYSOFT || $r[0] == DENYSOFT
or $r[0] == DENY_DISCONNECT || $r[0] == DENY_DISCONNECT
or $r[0] == DENYSOFT_DISCONNECT) || $r[0] == DENYSOFT_DISCONNECT)
{ {
$r[1] = "" if not defined $r[1]; $r[1] = '' if !defined $r[1];
$self->log(LOGDEBUG, $self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
"Plugin " if ($hook ne 'deny') {
. $code->{name} $self->run_hooks_no_respond('deny', $name, $r[0], $r[1]);
. ", hook $hook returned " };
. return_code($r[0])
. ", $r[1]"
);
$self->run_hooks_no_respond("deny", $code->{name}, $r[0], $r[1])
unless ($hook eq "deny");
} }
else { else {
$r[1] = "" if not defined $r[1]; $r[1] = '' if not defined $r[1];
$self->log(LOGDEBUG, $self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
"Plugin " $self->run_hooks_no_respond('ok', $name, $r[0], $r[1]) if $hook ne 'ok';
. $code->{name}
. ", hook $hook returned "
. return_code($r[0])
. ", $r[1]"
);
$self->run_hooks_no_respond("ok", $code->{name}, $r[0], $r[1])
unless ($hook eq "ok");
} }
last unless $r[0] == DECLINED; last if $r[0] != DECLINED;
} }
$r[0] = DECLINED if not defined $r[0]; $r[0] = DECLINED if ! defined $r[0];
# hook_*_parse() may return a CODE ref.. # hook_*_parse() may return a CODE ref..
# ... which breaks when splitting as string: # ... which breaks when splitting as string:
@r = map { split /\n/ } @r unless (ref($r[1]) eq "CODE"); if ('CODE' ne ref $r[1]) {
@r = map { split /\n/ } @r;
};
return $self->hook_responder($hook, \@r, $args); return $self->hook_responder($hook, \@r, $args);
} }
sub hook_responder { sub hook_responder {
my ($self, $hook, $msg, $args) = @_; my ($self, $hook, $msg, $args) = @_;
my $code = shift @$msg; my $code = shift @$msg;
my $responder = $hook . '_respond'; if (my $meth = $self->can($hook . '_respond')) {
if (my $meth = $self->can($responder)) {
return $meth->($self, $code, $msg, $args); return $meth->($self, $code, $msg, $args);
} }
return $code, @$msg; return $code, @$msg;
@ -452,17 +425,17 @@ sub size_threshold {
sub authenticated { sub authenticated {
my $self = shift; my $self = shift;
return (defined $self->{_auth} ? $self->{_auth} : ""); return (defined $self->{_auth} ? $self->{_auth} : '');
} }
sub auth_user { sub auth_user {
my $self = shift; my $self = shift;
return (defined $self->{_auth_user} ? $self->{_auth_user} : ""); return (defined $self->{_auth_user} ? $self->{_auth_user} : '');
} }
sub auth_mechanism { sub auth_mechanism {
my $self = shift; my $self = shift;
return (defined $self->{_auth_mechanism} ? $self->{_auth_mechanism} : ""); return (defined $self->{_auth_mechanism} ? $self->{_auth_mechanism} : '');
} }
sub address { sub address {

View File

@ -90,38 +90,35 @@ sub default {
sub get_qmail { sub get_qmail {
my ($self, $config, $type) = @_; my ($self, $config, $type) = @_;
$self->log(LOGDEBUG, "trying to get config for $config"); $self->log(LOGDEBUG, "trying to get config for $config");
my $configdir = $self->config_dir($config);
my $configfile = "$configdir/$config";
# CDB config support really should be moved to a plugin # CDB config support really should be moved to a plugin
if ($type and $type eq "map") { if ($type and $type eq "map") {
return $self->get_qmail_map($config, $configfile); return $self->get_qmail_map($config);
} }
return $self->from_file($configfile, $config); return $self->from_file($config);
} }
sub get_qmail_map { sub get_qmail_map {
my ($self, $config, $configfile) = @_; my ($self, $config, $file) = @_;
if (!-e $configfile . ".cdb") { $file ||= $self->config_dir($config) . "/$config.cdb";
$self->log(LOGDEBUG, "File $configfile.cdb does not exist");
if (!-e $file) {
$self->log(LOGDEBUG, "File $file does not exist");
$config_cache{$config} ||= []; $config_cache{$config} ||= [];
return +{}; return +{};
} }
eval { require CDB_File }; eval { require CDB_File };
if ($@) { if ($@) {
$self->log(LOGERROR, $self->log(LOGERROR, "No CDB Support! Did NOT read $file, could not load CDB_File: $@");
"No CDB Support! Did NOT read $configfile.cdb, could not load CDB_File module: $@"
);
return +{}; return +{};
} }
my %h; my %h;
unless (tie(%h, 'CDB_File', "$configfile.cdb")) { unless (tie(%h, 'CDB_File', $file)) {
$self->log(LOGERROR, "tie of $configfile.cdb failed: $!"); $self->log(LOGERROR, "tie of $file failed: $!");
return +{}; return +{};
} }
@ -132,17 +129,19 @@ sub get_qmail_map {
} }
sub from_file { sub from_file {
my ($self, $configfile, $config, $visited) = @_; my ($self, $config, $file, $visited) = @_;
if (!-e $configfile) { $file ||= $self->config_dir($config) . "/$config";
if (!-e $file) {
$config_cache{$config} ||= []; $config_cache{$config} ||= [];
return; return;
} }
$visited ||= []; $visited ||= [];
push @$visited, $configfile; push @$visited, $file;
open my $CF, '<', $configfile or do { open my $CF, '<', $file or do {
warn "$$ could not open configfile $configfile: $!"; warn "$$ could not open configfile $file: $!";
return; return;
}; };
my @config = <$CF>; my @config = <$CF>;
@ -180,8 +179,8 @@ sub from_file {
} }
push @{$visited}, $inclusion; push @{$visited}, $inclusion;
for my $inc ($self->expand_inclusion($inclusion, $configfile)) { for my $inc ($self->expand_inclusion($inclusion, $file)) {
my @insertion = $self->from_file($inc, $config, $visited); my @insertion = $self->from_file($config, $inc, $visited);
splice @config, $pos, 0, @insertion; # insert the inclusion splice @config, $pos, 0, @insertion; # insert the inclusion
$pos += @insertion; $pos += @insertion;
} }

View File

@ -99,7 +99,7 @@ sub adjust_log_level {
sub transaction { sub transaction {
# not sure if this will work in a non-forking or a threaded daemon # does this work in a non-forking or a threaded daemon?
shift->qp->transaction; shift->qp->transaction;
} }
@ -137,7 +137,7 @@ sub temp_dir {
# usage: # usage:
# sub init { # sub init {
# my $self = shift; # my $self = shift;
# $self->isa_plugin("rhsbl"); # $self->isa_plugin('rhsbl');
# $self->SUPER::register(@_); # $self->SUPER::register(@_);
# } # }
sub isa_plugin { sub isa_plugin {
@ -164,30 +164,29 @@ sub isa_plugin {
$self->compile($self->plugin_name . "_isa_$cleanParent", $self->compile($self->plugin_name . "_isa_$cleanParent",
$newPackage, "$parent_dir/$parent"); $newPackage, "$parent_dir/$parent");
warn "---- $newPackage\n"; warn "---- $newPackage\n";
no strict 'refs'; no strict 'refs'; ## no critic (strict)
push @{"${currentPackage}::ISA"}, $newPackage; push @{"${currentPackage}::ISA"}, $newPackage;
} }
# why isn't compile private? it's only called from Plugin and Qpsmtpd.
sub compile { sub compile {
my ($class, $plugin, $package, $file, $test_mode, $orig_name) = @_; my ($class, $plugin, $package, $file, $test_mode, $orig_name) = @_;
my $sub; my $sub;
open F, $file or die "could not open $file: $!"; open my $F, '<', $file or die "could not open $file: $!";
{ {
local $/ = undef; local $/ = undef;
$sub = <F>; $sub = <$F>;
} }
close F; close $F;
my $line = "\n#line 0 $file\n"; my $line = "\n#line 0 $file\n";
if ($test_mode) { if ($test_mode) {
if (open(F, "t/plugin_tests/$orig_name")) { if (open(my $F, '<', "t/plugin_tests/$orig_name")) {
local $/ = undef; local $/ = undef;
$sub .= "#line 1 t/plugin_tests/$orig_name\n"; $sub .= "#line 1 t/plugin_tests/$orig_name\n";
$sub .= <F>; $sub .= <$F>;
close F; close $F;
} }
} }
@ -206,12 +205,10 @@ sub compile {
"\n", # last line comment without newline? "\n", # last line comment without newline?
); );
#warn "eval: $eval";
$eval =~ m/(.*)/s; $eval =~ m/(.*)/s;
$eval = $1; $eval = $1;
eval $eval; eval $eval; ## no critic (Eval)
die "eval $@" if $@; die "eval $@" if $@;
} }
@ -355,8 +352,8 @@ sub _register_standard_hooks {
for my $hook (@hooks) { for my $hook (@hooks) {
my $hooksub = "hook_$hook"; my $hooksub = "hook_$hook";
$hooksub =~ s/\W/_/g; $hooksub =~ s/\W/_/g;
next if !$plugin->can($hooksub);
$plugin->register_hook($hook, $hooksub) $plugin->register_hook($hook, $hooksub)
if ($plugin->can($hooksub));
} }
} }

View File

@ -94,7 +94,7 @@ sub __get_qmail {
sub __get_qmail_map { sub __get_qmail_map {
eval "require CDB_File"; ## no critic (StringyEval) eval "require CDB_File"; ## no critic (StringyEval)
if (!$@) { if (!$@) {
my $r = $config->get_qmail_map('users', 't/config/users'); my $r = $config->get_qmail_map('users', 't/config/users.cdb');
ok(keys %$r, 'get_qmail_map("users.cdb")'); ok(keys %$r, 'get_qmail_map("users.cdb")');
ok($r->{'!example.com-'}, "get_qmail_map, known entry"); ok($r->{'!example.com-'}, "get_qmail_map, known entry");
}; };
@ -102,7 +102,7 @@ sub __get_qmail_map {
sub __from_file { sub __from_file {
my $test_file = 't/config/test_config_file'; my $test_file = 't/config/test_config_file';
my @r = $config->from_file($test_file, 'test_config_file'); my @r = $config->from_file('test_config_file', $test_file);
ok( @r, "from_file, $test_file"); ok( @r, "from_file, $test_file");
cmp_ok('1st line with content', 'eq', $r[0], "from_file string compare"); cmp_ok('1st line with content', 'eq', $r[0], "from_file string compare");
ok( !$r[1], "from_file"); ok( !$r[1], "from_file");

View File

@ -24,6 +24,13 @@ __hooks_none();
ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection"); ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection");
__hooks(); __hooks();
__run_hooks_no_respond();
__run_hooks();
__register_hook();
__hook_responder();
__run_continuation();
__temp_file(); __temp_file();
__temp_dir(); __temp_dir();
__size_threshold(); __size_threshold();
@ -40,6 +47,33 @@ __config();
done_testing(); done_testing();
sub __run_hooks {
my @r = $qp->run_hooks('nope');
is($r[0], 0, "run_hooks, invalid hook");
@r = $smtpd->run_hooks('nope');
is($r[0], 0, "run_hooks, invalid hook");
foreach my $hook (qw/ connect helo rset /) {
my $r = $smtpd->run_hooks('connect');
is($r->[0], 220, "run_hooks, $hook code");
ok($r->[1] =~ /ready/, "run_hooks, $hook result");
}
}
sub __run_hooks_no_respond {
my @r = $qp->run_hooks_no_respond('nope');
is($r[0], 0, "run_hooks_no_respond, invalid hook");
@r = $smtpd->run_hooks_no_respond('nope');
is($r[0], 0, "run_hooks_no_respond, invalid hook");
foreach my $hook (qw/ connect helo rset /) {
@r = $smtpd->run_hooks_no_respond('connect');
is($r[0], 909, "run_hooks_no_respond, $hook hook");
}
}
sub __hooks { sub __hooks {
ok(Qpsmtpd::hooks(), "hooks, populated"); ok(Qpsmtpd::hooks(), "hooks, populated");
my $r = $qp->hooks; my $r = $qp->hooks;
@ -47,7 +81,6 @@ sub __hooks {
$r = $qp->hooks('connect'); $r = $qp->hooks('connect');
ok(@$r, "hooks, populated, connect"); ok(@$r, "hooks, populated, connect");
#warn Data::Dumper::Dumper($r);
my @r = $qp->hooks('connect'); my @r = $qp->hooks('connect');
ok(@r, "hooks, populated, connect, wants array"); ok(@r, "hooks, populated, connect, wants array");
@ -61,6 +94,43 @@ sub __hooks_none {
is_deeply($r, [], 'hooks, empty, specified'); is_deeply($r, [], 'hooks, empty, specified');
} }
sub __run_continuation {
my $r;
eval { $smtpd->run_continuation };
ok($@, "run_continuation w/o continuation: " . $@);
my @local_hooks = @{$Qpsmtpd::hooks->{'connect'}};
$smtpd->{_continuation} = ['connect', [DECLINED, "test mess"], @local_hooks];
eval { $r = $smtpd->run_continuation };
ok(!$@, "run_continuation with a continuation doesn't throw exception");
is($r->[0], 220, "hook_responder, code");
ok($r->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $r->[1]);
}
sub __hook_responder {
my ($code, $msg) = $qp->hook_responder('test-hook', ['test code','test mesg']);
is($code, 'test code', "hook_responder, code");
is($msg, 'test mesg', "hook_responder, test msg");
($code, $msg) = $smtpd->hook_responder('connect', ['test code','test mesg']);
is($code->[0], 220, "hook_responder, code");
ok($code->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $code->[1]);
my $rej_msg = 'Your father smells of elderberries';
($code, $msg) = $smtpd->hook_responder('connect', [DENY, $rej_msg]);
is($code, undef, "hook_responder, disconnected yields undef code");
is($msg, undef, "hook_responder, disconnected yields undef msg");
}
sub __register_hook {
my $hook = 'test';
is( $Qpsmtpd::hooks->{'test'}, undef, "_register_hook, test hook is undefined");
$smtpd->_register_hook('test', 'fake-code-ref');
is_deeply( $Qpsmtpd::hooks->{'test'}, ['fake-code-ref'], "test hook is registered");
}
sub __log { sub __log {
my $warned = ''; my $warned = '';
local $SIG{__WARN__} = sub { local $SIG{__WARN__} = sub {
@ -172,7 +242,6 @@ sub __config_dir {
my $dir = $qp->config_dir('logging'); my $dir = $qp->config_dir('logging');
ok($dir, "config_dir, $dir"); ok($dir, "config_dir, $dir");
#warn Data::Dumper::Dumper($Qpsmtpd::config_dir_memo{logging});
$dir = $Qpsmtpd::Config::dir_memo{logging}; $dir = $Qpsmtpd::Config::dir_memo{logging};
ok($dir, "config_dir, $dir (memo)"); ok($dir, "config_dir, $dir (memo)");
} }