From b153c0ab99f0bd504616e6d6398cd9c589527e43 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 16 Sep 2014 00:54:47 -0700 Subject: [PATCH] more tests for Qpsmtpd::Config * fix for cache clearing that didn't work * imported a sample .cdb file for testing --- Makefile.PL | 21 +++++++++--------- lib/Qpsmtpd/Config.pm | 15 +++++++------ t/config/users.cdb | Bin 0 -> 2170 bytes t/qpsmtpd-config.t | 50 +++++++++++++++++++++++++++++++++++------- 4 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 t/config/users.cdb diff --git a/Makefile.PL b/Makefile.PL index 7a3298e..7d455d3 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -7,18 +7,19 @@ WriteMakefile( NAME => 'qpsmtpd', VERSION_FROM => 'lib/Qpsmtpd.pm', PREREQ_PM => { - 'Data::Dumper' => 0, - 'Date::Parse' => 0, - 'File::Temp' => 0, - 'Mail::Header' => 0, - 'MIME::Base64' => 0, - 'Net::DNS' => 0.39, - 'Net::IP' => 0, - 'Time::HiRes' => 0, + 'CDB_File' => 0, + 'Data::Dumper' => 0, + 'Date::Parse' => 0, + 'File::Temp' => 0, + 'Mail::Header' => 0, + 'MIME::Base64' => 0, + 'Net::DNS' => 0.39, + 'Net::IP' => 0, + 'Time::HiRes' => 0, 'IO::Socket::SSL' => 0, # Dev/Test modules - 'Test::More' => 0, - 'Test::Output' => 0, + 'Test::More' => 0, + 'Test::Output' => 0, # modules for specific features 'Mail::DKIM' => 0, 'File::Tail' => 0, # log/summarize, log/watch diff --git a/lib/Qpsmtpd/Config.pm b/lib/Qpsmtpd/Config.pm index 1055c84..021315c 100644 --- a/lib/Qpsmtpd/Config.pm +++ b/lib/Qpsmtpd/Config.pm @@ -8,7 +8,7 @@ use lib 'lib'; use parent 'Qpsmtpd::Base'; use Qpsmtpd::Constants; -our $_config_cache = {}; +our %config_cache = (); our %dir_memo; our %defaults = ( me => hostname, @@ -43,7 +43,7 @@ sub config_dir { } sub clear_cache { - $_config_cache = {}; + %config_cache = (); %dir_memo = (); } @@ -72,7 +72,8 @@ sub get_qmail_map { my ($self, $config, $configfile) = @_; if (!-e $configfile . ".cdb") { - $_config_cache->{$config} ||= []; + $self->log(LOGERROR, "File $configfile.cdb does not exist"); + $config_cache{$config} ||= []; return +{}; } eval { require CDB_File }; @@ -99,7 +100,7 @@ sub get_qmail_map { sub from_file { my ($self, $configfile, $config, $visited) = @_; if (!-e $configfile) { - $_config_cache->{$config} ||= []; + $config_cache{$config} ||= []; return; } @@ -145,7 +146,7 @@ sub from_file { } push @{$visited}, $inclusion; - for my $inc ($self->expand_inclusion_($inclusion, $configfile)) { + for my $inc ($self->expand_inclusion($inclusion, $configfile)) { my @insertion = $self->from_file($inc, $config, $visited); splice @config, $pos, 0, @insertion; # insert the inclusion $pos += @insertion; @@ -156,12 +157,12 @@ sub from_file { } } - $_config_cache->{$config} = \@config; + $config_cache{$config} = \@config; return wantarray ? @config : $config[0]; } -sub expand_inclusion_ { +sub expand_inclusion { my $self = shift; my $inclusion = shift; my $context = shift; diff --git a/t/config/users.cdb b/t/config/users.cdb new file mode 100644 index 0000000000000000000000000000000000000000..507dbf94ee0a6df962b6918ea52e7ae4d571b771 GIT binary patch literal 2170 zcma#zU;u+CB4{R{$}Fh5Q5q6%EY9gL;}`sD*& zU|`z-v0`dPVs1fBs$Oz_t}YUr!NL-R^h=A2^mFo)6La*-3i1nb6Ek!4Q}TgSUa>y1 fYF%*ZWdxEC7LdUz%D@l^t?fXvdJnI~F#!1hH5Z!o literal 0 HcmV?d00001 diff --git a/t/qpsmtpd-config.t b/t/qpsmtpd-config.t index fc99c4c..a547888 100644 --- a/t/qpsmtpd-config.t +++ b/t/qpsmtpd-config.t @@ -4,6 +4,7 @@ use warnings; use Data::Dumper; use File::Path; use Test::More; +use Sys::Hostname; use lib 'lib'; # test lib/Qpsmtpd (vs site_perl) use lib 't'; @@ -30,8 +31,12 @@ isa_ok($config, 'Qpsmtpd::Config'); __log(); __config_dir(); +__clear_cache(); +__default(); __from_file(); __get_qmail(); +__get_qmail_map(); +__expand_inclusion(); __config_via_smtpd(); foreach my $f (@mes) { unlink $f; } @@ -52,26 +57,55 @@ sub __log { is($warned, "$$ test log message\n", 'LOGWARN emitted correct warning'); } +sub __config_dir { + my $dir = $config->config_dir('logging'); + ok($dir, "config_dir, $dir"); + + #warn Data::Dumper::Dumper($Qpsmtpd::config_dir_memo{logging}); + $dir = $Qpsmtpd::Config::dir_memo{logging}; + ok($dir, "config_dir, $dir (memo)"); +} + +sub __clear_cache { + $Qpsmtpd::Config::config_cache{foo} = 2; + $Qpsmtpd::Config::dir_memo{dir1} = 'some/path'; + + $config->clear_cache(); + ok(! $Qpsmtpd::Config::config_cache{foo}, "clear_cache, config_cache") + or diag Data::Dumper::Dumper($Qpsmtpd::Config::config_cache{foo}); + ok(! $Qpsmtpd::Config::dir_memo{dir1}, "clear_cache, dir_memo") +}; + +sub __default { + is($config->default('me'), hostname, "default, my hostname"); + is($config->default('timeout'), 1200, "default timeout is 1200"); + ok(!$config->default('undefined-test'), "default, undefined"); +} + sub __get_qmail { is($config->get_qmail('me'), 'host.example.org', 'get_qmail("me")'); ok(!$config->get_qmail('not-me'), 'get_qmail("not-me")'); } +sub __get_qmail_map { + eval "require CDB_File"; ## no critic (StringyEval) + if (!$@) { + my $r = $config->get_qmail_map('users', 't/config/users'); + ok(keys %$r, 'get_qmail_map("users.cdb")'); + ok($r->{'!example.com-'}, "get_qmail_map, known entry"); + }; +} + sub __from_file { my $test_file = 't/config/test_config_file'; my @r = $config->from_file($test_file, 'test_config_file'); ok( @r, "from_file, $test_file"); cmp_ok('1st line with content', 'eq', $r[0], "from_file string compare"); ok( !$r[1], "from_file"); -}; +} -sub __config_dir { - my $dir = $config->config_dir('logging'); - ok($dir, "config_dir, $dir"); - - #warn Data::Dumper::Dumper($Qpsmtpd::config_dir_memo{logging}); - $dir = $Qpsmtpd::Config::dir_memo{logging}; - ok($dir, "config_dir, $dir (memo)"); +sub __expand_inclusion { + # TODO } sub __config_via_smtpd {