2014-09-09 07:58:27 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2014-09-16 08:58:55 +02:00
|
|
|
use Cwd;
|
2014-09-09 07:58:27 +02:00
|
|
|
use Data::Dumper;
|
2014-09-16 01:58:21 +02:00
|
|
|
use File::Path;
|
2014-09-09 07:58:27 +02:00
|
|
|
use Test::More;
|
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
|
2014-09-16 08:41:31 +02:00
|
|
|
use lib 't';
|
2014-09-16 00:57:12 +02:00
|
|
|
|
2014-09-15 16:24:13 +02:00
|
|
|
BEGIN {
|
|
|
|
use_ok('Qpsmtpd');
|
|
|
|
use_ok('Qpsmtpd::Constants');
|
2014-09-16 08:41:31 +02:00
|
|
|
use_ok('Test::Qpsmtpd');
|
|
|
|
}
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-15 20:53:28 +02:00
|
|
|
my $qp = bless {}, 'Qpsmtpd';
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
ok($qp->version(), "version, " . $qp->version());
|
2014-09-17 06:22:36 +02:00
|
|
|
__hooks_none();
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-16 08:41:31 +02:00
|
|
|
ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection");
|
2014-09-17 06:22:36 +02:00
|
|
|
__hooks();
|
2014-09-16 08:41:31 +02:00
|
|
|
|
2014-09-17 07:32:43 +02:00
|
|
|
__run_hooks_no_respond();
|
|
|
|
__run_hooks();
|
|
|
|
|
2014-09-16 23:13:44 +02:00
|
|
|
__register_hook();
|
|
|
|
__hook_responder();
|
2014-09-17 05:30:36 +02:00
|
|
|
__run_continuation();
|
2014-09-16 23:13:44 +02:00
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
__temp_file();
|
2014-09-16 01:58:21 +02:00
|
|
|
__temp_dir();
|
|
|
|
__size_threshold();
|
2014-09-15 20:53:28 +02:00
|
|
|
__authenticated();
|
2014-09-16 01:58:21 +02:00
|
|
|
__auth_user();
|
|
|
|
__auth_mechanism();
|
2014-09-16 03:50:48 +02:00
|
|
|
__spool_dir();
|
2014-09-16 01:58:21 +02:00
|
|
|
|
|
|
|
__log();
|
|
|
|
__load_logging();
|
|
|
|
|
2014-09-09 07:58:27 +02:00
|
|
|
__config_dir();
|
2014-09-15 20:53:28 +02:00
|
|
|
__config();
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-12-16 03:07:45 +01:00
|
|
|
__warn_level();
|
|
|
|
|
2014-09-09 07:58:27 +02:00
|
|
|
done_testing();
|
|
|
|
|
2014-09-17 07:32:43 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 06:22:36 +02:00
|
|
|
sub __hooks {
|
|
|
|
ok(Qpsmtpd::hooks(), "hooks, populated");
|
|
|
|
my $r = $qp->hooks;
|
|
|
|
ok(%$r, "hooks, populated returns a hashref");
|
|
|
|
|
|
|
|
$r = $qp->hooks('connect');
|
|
|
|
ok(@$r, "hooks, populated, connect");
|
|
|
|
|
|
|
|
my @r = $qp->hooks('connect');
|
|
|
|
ok(@r, "hooks, populated, connect, wants array");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub __hooks_none {
|
|
|
|
is_deeply(Qpsmtpd::hooks(), {}, 'hooks, empty');
|
|
|
|
is_deeply($qp->hooks, {}, 'hooks, empty');
|
|
|
|
|
|
|
|
my $r = $qp->hooks('connect');
|
|
|
|
is_deeply($r, [], 'hooks, empty, specified');
|
|
|
|
}
|
|
|
|
|
2014-09-17 05:30:36 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:13:44 +02:00
|
|
|
sub __hook_responder {
|
2014-09-18 04:08:34 +02:00
|
|
|
my ($code, $msg) = $qp->hook_responder('test-hook', [OK,'test mesg']);
|
|
|
|
is($code, OK, "hook_responder, code");
|
2014-09-16 23:13:44 +02:00
|
|
|
is($msg, 'test mesg', "hook_responder, test msg");
|
|
|
|
|
2014-09-18 04:08:34 +02:00
|
|
|
($code, $msg) = $smtpd->hook_responder('connect', [OK,'test mesg']);
|
2014-09-16 23:13:44 +02:00
|
|
|
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]);
|
2014-09-17 02:54:26 +02:00
|
|
|
is($code, undef, "hook_responder, disconnected yields undef code");
|
2014-09-16 23:13:44 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-09-09 07:58:27 +02:00
|
|
|
sub __log {
|
2014-09-12 08:23:09 +02:00
|
|
|
my $warned = '';
|
|
|
|
local $SIG{__WARN__} = sub {
|
2014-09-16 00:57:12 +02:00
|
|
|
if ($_[0] eq "$$ test log message\n") {
|
2014-09-12 08:23:09 +02:00
|
|
|
$warned = join ' ', @_;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warn @_;
|
|
|
|
}
|
|
|
|
};
|
2014-09-16 00:57:12 +02:00
|
|
|
ok($qp->log(LOGWARN, "test log message"), 'log');
|
|
|
|
is($warned, "$$ test log message\n", 'LOGWARN emitted correct warning');
|
2014-09-09 07:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub __load_logging {
|
|
|
|
$Qpsmtpd::LOGGING_LOADED = 1;
|
2014-09-16 00:57:12 +02:00
|
|
|
ok(!$qp->load_logging(), "load_logging, loaded");
|
2014-09-09 07:58:27 +02:00
|
|
|
|
|
|
|
$Qpsmtpd::LOGGING_LOADED = 0;
|
|
|
|
$Qpsmtpd::hooks->{logging} = 1;
|
2014-09-16 00:57:12 +02:00
|
|
|
ok(!$qp->load_logging(), "load_logging, logging hook");
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
$Qpsmtpd::hooks->{logging} = undef; # restore
|
2014-09-09 07:58:27 +02:00
|
|
|
}
|
2014-09-15 20:53:28 +02:00
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
sub __spool_dir {
|
|
|
|
my $dir = $qp->spool_dir();
|
2014-09-16 08:58:55 +02:00
|
|
|
ok($dir, "spool_dir is at $dir");
|
2014-09-16 08:41:31 +02:00
|
|
|
|
2014-09-16 08:58:55 +02:00
|
|
|
my $cwd = getcwd;
|
|
|
|
chomp $cwd;
|
|
|
|
open my $SD, '>', "./config.sample/spool_dir";
|
|
|
|
print $SD "$cwd/t/tmp";
|
|
|
|
close $SD;
|
2014-09-16 08:41:31 +02:00
|
|
|
|
|
|
|
my $spool_dir = $smtpd->spool_dir();
|
2014-09-16 08:58:55 +02:00
|
|
|
ok($spool_dir =~ m!/tmp/$!, "Located the spool directory")
|
|
|
|
or diag ("spool_dir: $spool_dir instead of tmp");
|
2014-09-16 08:41:31 +02:00
|
|
|
|
|
|
|
my $tempfile = $smtpd->temp_file();
|
|
|
|
my $tempdir = $smtpd->temp_dir();
|
|
|
|
|
|
|
|
ok($tempfile =~ /^$spool_dir/, "Temporary filename");
|
|
|
|
ok($tempdir =~ /^$spool_dir/, "Temporary directory");
|
|
|
|
ok(-d $tempdir, "And that directory exists");
|
|
|
|
|
|
|
|
unlink "./config.sample/spool_dir";
|
|
|
|
rmtree($spool_dir);
|
2014-09-16 03:50:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub __temp_file {
|
|
|
|
my $r = $qp->temp_file();
|
|
|
|
ok( $r, "temp_file at $r");
|
|
|
|
if ($r && -f $r) {
|
|
|
|
unlink $r;
|
|
|
|
ok( unlink $r, "cleaned up temp file $r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub __temp_dir {
|
|
|
|
my $r = $qp->temp_dir();
|
|
|
|
ok( $r, "temp_dir at $r");
|
|
|
|
if ($r && -d $r) { File::Path::rmtree($r); }
|
|
|
|
|
|
|
|
$r = $qp->temp_dir('0775');
|
|
|
|
ok( $r, "temp_dir with mask, $r");
|
|
|
|
if ($r && -d $r) { File::Path::rmtree($r); }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub __size_threshold {
|
2014-09-16 08:41:31 +02:00
|
|
|
is( $qp->size_threshold(), 10000, "size_threshold from t/config is 1000")
|
2014-09-16 03:50:48 +02:00
|
|
|
or warn "size_threshold: " . $qp->size_threshold;
|
|
|
|
|
|
|
|
$Qpsmtpd::Size_threshold = 5;
|
|
|
|
cmp_ok( 5, '==', $qp->size_threshold(), "size_threshold equals 5");
|
|
|
|
|
|
|
|
$Qpsmtpd::Size_threshold = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub __authenticated {
|
|
|
|
ok( ! $qp->authenticated(), "authenticated is undefined");
|
|
|
|
|
|
|
|
$qp->{_auth} = 1;
|
|
|
|
ok($qp->authenticated(), "authenticated is true");
|
|
|
|
|
|
|
|
$qp->{_auth} = 0;
|
|
|
|
ok(! $qp->authenticated(), "authenticated is false");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub __auth_user {
|
|
|
|
ok( ! $qp->auth_user(), "auth_user is undefined");
|
|
|
|
|
|
|
|
$qp->{_auth_user} = 'matt';
|
|
|
|
cmp_ok('matt', 'eq', $qp->auth_user(), "auth_user set");
|
|
|
|
|
|
|
|
$qp->{_auth_user} = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub __auth_mechanism {
|
|
|
|
ok( ! $qp->auth_mechanism(), "auth_mechanism is undefined");
|
|
|
|
|
|
|
|
$qp->{_auth_mechanism} = 'MD5';
|
|
|
|
cmp_ok('MD5', 'eq', $qp->auth_mechanism(), "auth_mechanism set");
|
|
|
|
|
|
|
|
$qp->{_auth_mechanism} = undef;
|
|
|
|
}
|
|
|
|
|
2014-09-16 08:41:31 +02:00
|
|
|
sub __config_dir {
|
|
|
|
my $dir = $qp->config_dir('logging');
|
|
|
|
ok($dir, "config_dir, $dir");
|
|
|
|
|
|
|
|
$dir = $Qpsmtpd::Config::dir_memo{logging};
|
|
|
|
ok($dir, "config_dir, $dir (memo)");
|
|
|
|
}
|
|
|
|
|
2014-09-15 16:24:13 +02:00
|
|
|
sub __config {
|
|
|
|
my @r = $qp->config('badhelo');
|
2014-09-16 00:57:12 +02:00
|
|
|
ok($r[0], "config, badhelo, @r");
|
2014-09-16 08:41:31 +02:00
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
my $a = FakeAddress->new(test => 'test value');
|
|
|
|
ok(my ($qp, $cxn) = Test::Qpsmtpd->new_conn(), "get new connection");
|
2014-09-15 16:24:13 +02:00
|
|
|
my @test_data = (
|
|
|
|
{
|
2014-09-16 00:57:12 +02:00
|
|
|
pref => 'size_threshold',
|
|
|
|
hooks => {
|
2014-12-18 21:16:23 +01:00
|
|
|
user_config => undef,
|
|
|
|
config => undef,
|
2014-09-16 00:57:12 +02:00
|
|
|
},
|
|
|
|
expected => {
|
|
|
|
user => 10000,
|
|
|
|
global => 10000,
|
|
|
|
},
|
|
|
|
descr => 'no user or global config hooks, fall back to config file',
|
2014-09-15 16:24:13 +02:00
|
|
|
},
|
|
|
|
{
|
2014-09-16 00:57:12 +02:00
|
|
|
pref => 'timeout',
|
|
|
|
hooks => {
|
2014-12-18 21:16:23 +01:00
|
|
|
user_config => undef,
|
|
|
|
config => undef,
|
2014-09-16 00:57:12 +02:00
|
|
|
},
|
|
|
|
expected => {
|
|
|
|
user => 1200,
|
|
|
|
global => 1200,
|
|
|
|
},
|
|
|
|
descr => 'no user or global config hooks, fall back to defaults',
|
2014-09-15 16:24:13 +02:00
|
|
|
},
|
|
|
|
{
|
2014-09-16 00:57:12 +02:00
|
|
|
pref => 'timeout',
|
|
|
|
hooks => {
|
|
|
|
user_config => [DECLINED],
|
|
|
|
config => [DECLINED],
|
|
|
|
},
|
|
|
|
expected => {
|
|
|
|
user => 1200,
|
|
|
|
global => 1200,
|
|
|
|
},
|
|
|
|
descr => 'user and global config hooks decline, fall back to defaults',
|
2014-09-15 16:24:13 +02:00
|
|
|
},
|
|
|
|
{
|
2014-09-16 00:57:12 +02:00
|
|
|
pref => 'timeout',
|
|
|
|
hooks => {
|
|
|
|
user_config => [DECLINED],
|
|
|
|
config => [OK, 1000],
|
|
|
|
},
|
|
|
|
expected => {
|
|
|
|
user => 1000,
|
|
|
|
global => 1000,
|
|
|
|
},
|
|
|
|
descr => 'user hook declines, global hook returns',
|
2014-09-15 16:24:13 +02:00
|
|
|
},
|
|
|
|
{
|
2014-09-16 00:57:12 +02:00
|
|
|
pref => 'timeout',
|
|
|
|
hooks => {
|
|
|
|
user_config => [OK, 500],
|
|
|
|
config => [OK, undef],
|
|
|
|
},
|
|
|
|
expected => {
|
|
|
|
user => 500,
|
|
|
|
global => undef,
|
|
|
|
},
|
|
|
|
descr => 'user hook returns int, global hook returns undef',
|
2014-09-15 16:24:13 +02:00
|
|
|
},
|
|
|
|
{
|
2014-09-16 00:57:12 +02:00
|
|
|
pref => 'timeout',
|
|
|
|
hooks => {
|
|
|
|
user_config => [OK, undef],
|
|
|
|
config => [OK, 1000],
|
|
|
|
},
|
|
|
|
expected => {
|
|
|
|
user => undef,
|
|
|
|
global => 1000,
|
|
|
|
},
|
|
|
|
descr => 'user hook returns undef, global hook returns int',
|
2014-09-15 16:24:13 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
for my $t (@test_data) {
|
2014-12-18 21:16:23 +01:00
|
|
|
for my $hook ( grep { $t->{hooks}{$_} } qw( config user_config ) ) {
|
|
|
|
$qp->fake_hook( $hook, sub { return @{ $t->{hooks}{$hook} } } );
|
2014-09-15 16:24:13 +02:00
|
|
|
}
|
2014-09-16 00:57:12 +02:00
|
|
|
is(
|
|
|
|
$qp->config($t->{pref}, $a),
|
|
|
|
$t->{expected}{user},
|
|
|
|
"User config: $t->{descr}"
|
|
|
|
);
|
|
|
|
is($qp->config($t->{pref}),
|
|
|
|
$t->{expected}{global},
|
|
|
|
"Global config: $t->{descr}");
|
2014-09-15 16:24:13 +02:00
|
|
|
}
|
2014-12-18 21:16:23 +01:00
|
|
|
$qp->unfake_hook($_) for qw( config user_config );
|
2014-09-15 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
2014-12-16 03:07:45 +01:00
|
|
|
sub __warn_level {
|
|
|
|
my ( $level, $msg ) = $qp->warn_level('qwerty');
|
|
|
|
is( log_level($level), 'LOGWARN', 'Correct log level with no prefix' );
|
|
|
|
is( $msg, 'qwerty', 'Correct message with no prefix' );
|
|
|
|
|
2014-12-17 23:11:12 +01:00
|
|
|
my $s = 'abc123';
|
|
|
|
$s =~ /(abc)/;
|
|
|
|
( $level, $msg ) = $qp->warn_level('dvorak');
|
|
|
|
is( log_level($level), 'LOGWARN', 'Correct level after $1 assignment' );
|
|
|
|
is( $msg, 'dvorak', 'Correct message with no prefix after $1 assignment' );
|
|
|
|
|
2014-12-16 03:07:45 +01:00
|
|
|
( $level, $msg ) = $qp->warn_level('NOTICE: asdf');
|
|
|
|
is( log_level($level), 'LOGNOTICE', 'Correct level with NOTICE prefix' );
|
|
|
|
is( $msg, 'asdf', 'Correct message with NOTICE prefix' );
|
|
|
|
|
|
|
|
( $level, $msg ) = $qp->warn_level('EMERGENCY:foo');
|
|
|
|
is( log_level($level), 'LOGEMERG', 'Correct level with EMERGENCY prefix' );
|
|
|
|
is( $msg, 'foo', 'Correct message with EMERGENCY prefix' );
|
|
|
|
|
|
|
|
( $level, $msg ) = $qp->warn_level('NOTICE != LOGNOTICE');
|
|
|
|
is( log_level($level), 'LOGWARN', 'Correct level with no colon' );
|
|
|
|
is( $msg, 'NOTICE != LOGNOTICE', 'Correct message with no colon' );
|
|
|
|
}
|
|
|
|
|
2014-09-16 08:41:31 +02:00
|
|
|
1;
|
|
|
|
|
2014-09-15 16:24:13 +02:00
|
|
|
package FakeAddress;
|
|
|
|
|
|
|
|
sub new {
|
2014-09-16 01:58:21 +02:00
|
|
|
my $class = shift;
|
|
|
|
return bless {@_}, $class;
|
2014-09-15 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
sub address { } # pass the can('address') conditional
|
2014-09-16 08:41:31 +02:00
|
|
|
|
|
|
|
1;
|