2014-09-09 07:58:27 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Data::Dumper;
|
|
|
|
use Test::More;
|
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
|
|
|
|
|
2014-09-15 16:24:13 +02:00
|
|
|
BEGIN {
|
|
|
|
use_ok('Qpsmtpd');
|
|
|
|
use_ok('Qpsmtpd::Constants');
|
|
|
|
}
|
|
|
|
|
|
|
|
use lib 't';
|
|
|
|
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());
|
|
|
|
is_deeply(Qpsmtpd::hooks(), {}, 'hooks, empty');
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-15 20:53:28 +02:00
|
|
|
__authenticated();
|
2014-09-09 07:58:27 +02:00
|
|
|
__config_dir();
|
2014-09-15 20:53:28 +02:00
|
|
|
__get_qmail_config();
|
|
|
|
__config();
|
2014-09-09 07:58:27 +02:00
|
|
|
__log();
|
|
|
|
__load_logging();
|
|
|
|
|
|
|
|
done_testing();
|
|
|
|
|
2014-09-15 20:53:28 +02:00
|
|
|
sub __get_qmail_config {
|
2014-09-16 00:57:12 +02:00
|
|
|
ok(!$qp->get_qmail_config('me'), "get_qmail_config, me");
|
2014-09-15 20:53:28 +02:00
|
|
|
|
|
|
|
# TODO: add positive tests.
|
2014-09-16 00:57:12 +02:00
|
|
|
}
|
2014-09-15 20:53:28 +02:00
|
|
|
|
|
|
|
sub __config_from_file {
|
2014-09-16 00:57:12 +02:00
|
|
|
|
2014-09-15 20:53:28 +02:00
|
|
|
# $configfile, $config, $visited
|
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
}
|
2014-09-15 20:53:28 +02:00
|
|
|
|
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 __config_dir {
|
|
|
|
my $dir = $qp->config_dir('logging');
|
2014-09-16 00:57:12 +02:00
|
|
|
ok($dir, "config_dir, $dir");
|
2014-09-09 07:58:27 +02:00
|
|
|
|
|
|
|
#warn Data::Dumper::Dumper($Qpsmtpd::config_dir_memo{logging});
|
|
|
|
$dir = $Qpsmtpd::config_dir_memo{logging};
|
2014-09-16 00:57:12 +02:00
|
|
|
ok($dir, "config_dir, $dir (memo)");
|
|
|
|
}
|
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
|
|
|
|
|
|
|
sub __authenticated {
|
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
ok(!$qp->authenticated(), "authenticated, undef");
|
2014-09-15 20:53:28 +02:00
|
|
|
|
|
|
|
$qp->{_auth} = 1;
|
2014-09-16 00:57:12 +02:00
|
|
|
ok($qp->authenticated(), "authenticated, true");
|
2014-09-15 20:53:28 +02:00
|
|
|
|
|
|
|
$qp->{_auth} = 0;
|
2014-09-16 00:57:12 +02:00
|
|
|
ok(!$qp->authenticated(), "authenticated, false");
|
|
|
|
}
|
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");
|
|
|
|
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 => {
|
|
|
|
user_config => [],
|
|
|
|
config => [],
|
|
|
|
},
|
|
|
|
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 => {
|
|
|
|
user_config => [],
|
|
|
|
config => [],
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
for my $hook (qw( config user_config )) {
|
2014-09-16 00:57:12 +02:00
|
|
|
$qp->hooks->{$hook} = @{$t->{hooks}{$hook}}
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
name => 'test hook',
|
|
|
|
code => sub { return @{$t->{hooks}{$hook}} }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
: undef;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
package FakeAddress;
|
|
|
|
|
|
|
|
sub new {
|
2014-09-16 00:57:12 +02:00
|
|
|
shift;
|
|
|
|
return bless {@_};
|
2014-09-15 16:24:13 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
sub address { } # pass the can('address') conditional
|