added tests for run_hooks & run_hooks_no_respond

This commit is contained in:
Matt Simerson 2014-09-16 22:32:43 -07:00
parent 9a38850f13
commit 1e5d249224
3 changed files with 43 additions and 22 deletions

View File

@ -180,8 +180,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
@ -273,7 +272,7 @@ sub run_hooks_no_respond {
my @r; my @r;
for my $code (@{$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;

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

@ -24,10 +24,12 @@ __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(); __register_hook();
__hook_responder(); __hook_responder();
__run_continuation(); __run_continuation();
done_testing() and exit;
__temp_file(); __temp_file();
__temp_dir(); __temp_dir();
@ -45,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;
@ -52,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");
@ -78,8 +106,6 @@ sub __run_continuation {
ok(!$@, "run_continuation with a continuation doesn't throw exception"); ok(!$@, "run_continuation with a continuation doesn't throw exception");
is($r->[0], 220, "hook_responder, code"); is($r->[0], 220, "hook_responder, code");
ok($r->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $r->[1]); ok($r->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $r->[1]);
#print Data::Dumper::Dumper(@r);
} }
sub __hook_responder { sub __hook_responder {
@ -216,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)");
} }