Merge pull request #100 from msimerson/qpsmtpd.t

Adds tests for Qpsmtpd and new Qpsmtpd::Config
This commit is contained in:
Jared Johnson 2014-09-15 16:48:25 -05:00
commit bd47c06966
4 changed files with 165 additions and 113 deletions

View File

@ -221,6 +221,15 @@ sub get_qmail_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_config_map($config, $configfile);
};
return $self->_config_from_file($configfile, $config);
}
sub get_qmail_config_map {
my ($self, $config, $configfile) = @_;
unless (-e $configfile . ".cdb") { unless (-e $configfile . ".cdb") {
$_config_cache->{$config} ||= []; $_config_cache->{$config} ||= [];
return +{}; return +{};
@ -244,10 +253,7 @@ sub get_qmail_config {
# the data is in a CDB file in the first place because there's # the data is in a CDB file in the first place because there's
# lots of data and the cache hit ratio would be low. # lots of data and the cache hit ratio would be low.
return \%h; return \%h;
} };
return $self->_config_from_file($configfile, $config);
}
sub _config_from_file { sub _config_from_file {
my ($self, $configfile, $config, $visited) = @_; my ($self, $configfile, $config, $visited) = @_;
@ -257,10 +263,12 @@ sub _config_from_file {
} }
$visited ||= []; $visited ||= [];
push @{$visited}, $configfile; push @$visited, $configfile;
open my $CF, '<', $configfile open my $CF, '<', $configfile or do {
or warn "$$ could not open configfile $configfile: $!" and return; warn "$$ could not open configfile $configfile: $!";
return;
};
my @config = <$CF>; my @config = <$CF>;
chomp @config; chomp @config;
@config = grep { length($_) and $_ !~ m/^\s*#/ and $_ =~ m/\S/ } @config = grep { length($_) and $_ !~ m/^\s*#/ and $_ =~ m/\S/ }

View File

@ -94,8 +94,11 @@ sub get_db_handle {
my $dbuser = $self->qp->config("vpopmail_mysql_user") || "vpopmailuser"; my $dbuser = $self->qp->config("vpopmail_mysql_user") || "vpopmailuser";
my $dbpass = $self->qp->config("vpopmail_mysql_pass") || "vpoppasswd"; my $dbpass = $self->qp->config("vpopmail_mysql_pass") || "vpoppasswd";
my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or do { my $dbh;
$self->log(LOGERROR, "skip: db connection failed"); eval { $dbh = DBI->connect($dsn, $dbuser, $dbpass); };
if (!$dbh) {
$self->log(LOGERROR, "skip: db connection failed: $!");
$self->log(LOGERROR, $@) if $@;
return; return;
}; };
$dbh->{ShowErrorStatement} = 1; $dbh->{ShowErrorStatement} = 1;

View File

@ -1,15 +1,55 @@
#!/usr/bin/perl #!/usr/bin/perl
use strict; use strict;
$^W = 1; use warnings;
use Test::More qw/no_plan/; use Test::More;
use lib 'lib';
BEGIN { BEGIN {
use_ok('Qpsmtpd::Address'); use_ok('Qpsmtpd::Address');
} }
my $as; __new();
my $ao; __parse();
done_testing();
sub __new {
my ($as, $ao);
my @unsorted_list = map { Qpsmtpd::Address->new($_) } qw(
"musa_ibrah@caramail.comandrea.luger"@wifo.ac.at
foo@example.com
ask@perl.org
foo@foo.x.example.com
jpeacock@cpan.org
test@example.com
);
# NOTE that this is sorted by _host_ not by _domain_
my @sorted_list = map { Qpsmtpd::Address->new($_) } qw(
jpeacock@cpan.org
foo@example.com
test@example.com
foo@foo.x.example.com
ask@perl.org
"musa_ibrah@caramail.comandrea.luger"@wifo.ac.at
);
my @test_list = sort @unsorted_list;
is_deeply(\@test_list, \@sorted_list, "sort via overloaded 'cmp' operator");
# RT#38746 - non-RFC compliant address should return undef
$as = '<user@example.com#>';
$ao = Qpsmtpd::Address->new($as);
is($ao, undef, "illegal $as");
}
sub __parse {
my ($as, $ao);
$as = '<>'; $as = '<>';
$ao = Qpsmtpd::Address->parse($as); $ao = Qpsmtpd::Address->parse($as);
@ -77,32 +117,4 @@ $as = 'foo@foo.x.example.com';
ok($ao = Qpsmtpd::Address->parse("<$as>"), "parse <$as>"); ok($ao = Qpsmtpd::Address->parse("<$as>"), "parse <$as>");
is($ao && $ao->address, $as, "address $as"); is($ao && $ao->address, $as, "address $as");
ok($ao eq $as, "overloaded 'cmp' operator"); ok($ao eq $as, "overloaded 'cmp' operator");
};
my @unsorted_list = map { Qpsmtpd::Address->new($_) } qw(
"musa_ibrah@caramail.comandrea.luger"@wifo.ac.at
foo@example.com
ask@perl.org
foo@foo.x.example.com
jpeacock@cpan.org
test@example.com
);
# NOTE that this is sorted by _host_ not by _domain_
my @sorted_list = map { Qpsmtpd::Address->new($_) } qw(
jpeacock@cpan.org
foo@example.com
test@example.com
foo@foo.x.example.com
ask@perl.org
"musa_ibrah@caramail.comandrea.luger"@wifo.ac.at
);
my @test_list = sort @unsorted_list;
is_deeply(\@test_list, \@sorted_list, "sort via overloaded 'cmp' operator");
# RT#38746 - non-RFC compliant address should return undef
$as = '<user@example.com#>';
$ao = Qpsmtpd::Address->new($as);
is($ao, undef, "illegal $as");

View File

@ -9,18 +9,36 @@ use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
BEGIN { use_ok('Qpsmtpd'); } BEGIN { use_ok('Qpsmtpd'); }
BEGIN { use_ok('Qpsmtpd::Constants'); } BEGIN { use_ok('Qpsmtpd::Constants'); }
my $package = 'Qpsmtpd'; my $qp = bless {}, 'Qpsmtpd';
my $qp = bless {}, $package;
ok( $qp->version(), "version, " . $qp->version()); ok( $qp->version(), "version, " . $qp->version());
is_deeply( Qpsmtpd::hooks(), {}, 'hooks, empty'); is_deeply( Qpsmtpd::hooks(), {}, 'hooks, empty');
__authenticated();
__config_dir(); __config_dir();
__get_qmail_config();
__config();
__log(); __log();
__load_logging(); __load_logging();
done_testing(); done_testing();
sub __config {
my @r = $qp->config('badhelo');
ok( $r[0], "config, badhelo, @r");
};
sub __get_qmail_config {
ok( !$qp->get_qmail_config('me'), "get_qmail_config, me");
# TODO: add positive tests.
};
sub __config_from_file {
# $configfile, $config, $visited
};
sub __log { sub __log {
my $warned = ''; my $warned = '';
local $SIG{__WARN__} = sub { local $SIG{__WARN__} = sub {
@ -54,3 +72,14 @@ sub __load_logging {
$Qpsmtpd::hooks->{logging} = undef; # restore $Qpsmtpd::hooks->{logging} = undef; # restore
} }
sub __authenticated {
ok( ! $qp->authenticated(), "authenticated, undef");
$qp->{_auth} = 1;
ok( $qp->authenticated(), "authenticated, true");
$qp->{_auth} = 0;
ok( !$qp->authenticated(), "authenticated, false");
};