From fabbdae9604e562db0e684fb031ce261c45693a4 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Wed, 10 Sep 2014 16:53:28 -0700 Subject: [PATCH 1/3] tests for Qpsmtpd/SMTP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * order the ‘use’ pragmas * compact the new() method * move fault() tests into t/qpsmtpd-smtp.t --- Makefile.PL | 4 +++- lib/Qpsmtpd/SMTP.pm | 34 +++++++++++++++------------------- t/misc.t | 19 +------------------ t/qpsmtpd-smtp.t | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 t/qpsmtpd-smtp.t diff --git a/Makefile.PL b/Makefile.PL index 351c360..7a3298e 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -7,7 +7,6 @@ WriteMakefile( NAME => 'qpsmtpd', VERSION_FROM => 'lib/Qpsmtpd.pm', PREREQ_PM => { - 'Test::Output' => 0, 'Data::Dumper' => 0, 'Date::Parse' => 0, 'File::Temp' => 0, @@ -17,6 +16,9 @@ WriteMakefile( 'Net::IP' => 0, 'Time::HiRes' => 0, 'IO::Socket::SSL' => 0, +# Dev/Test modules + '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/SMTP.pm b/lib/Qpsmtpd/SMTP.pm index 2ce95c2..c6db8a5 100644 --- a/lib/Qpsmtpd/SMTP.pm +++ b/lib/Qpsmtpd/SMTP.pm @@ -1,12 +1,15 @@ -package Qpsmtpd::SMTP; -use Qpsmtpd; -@ISA = qw(Qpsmtpd); -my %auth_mechanisms = (); - package Qpsmtpd::SMTP; use strict; -use Carp; +use base 'Qpsmtpd'; + +use Carp; +#use Data::Dumper; +use POSIX qw(strftime); +use Mail::Header; +use Net::DNS; + +use Qpsmtpd; use Qpsmtpd::Connection; use Qpsmtpd::Transaction; use Qpsmtpd::Plugin; @@ -15,11 +18,7 @@ use Qpsmtpd::Auth; use Qpsmtpd::Address (); use Qpsmtpd::Command; -use Mail::Header (); - -#use Data::Dumper; -use POSIX qw(strftime); -use Net::DNS; +my %auth_mechanisms = (); # this is only good for forkserver # can't set these here, cause forkserver resets them @@ -27,19 +26,16 @@ use Net::DNS; #$SIG{ALRM} = sub { warn "Connection Timed Out\n"; exit; }; sub new { - my $proto = shift; + my ($proto, %args) = @_; my $class = ref($proto) || $proto; - my %args = @_; - my $self = bless({args => \%args}, $class); - my (@commands) = qw(ehlo helo rset mail rcpt data help vrfy noop quit); - my (%commands); - @commands{@commands} = ('') x @commands; + # this list of valid commands should probably be a method or a set of methods + $self->{_commands} = { + map { $_ => '' } qw(ehlo helo rset mail rcpt data help vrfy noop quit) + }; - # this list of valid commands should probably be a method or a set of methods - $self->{_commands} = \%commands; $self->SUPER::_restart(%args) if $args{restart}; # calls Qpsmtpd::_restart() $self; } diff --git a/t/misc.t b/t/misc.t index d538ca1..4143199 100644 --- a/t/misc.t +++ b/t/misc.t @@ -1,4 +1,4 @@ -use Test::More tests => 14; +use Test::More tests => 10; use Test::Output; use strict; use lib 't'; @@ -6,23 +6,6 @@ use_ok('Test::Qpsmtpd'); ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection"); -# fault method -{ - my $fault; - stderr_like { $fault = $smtpd->fault } - qr/program fault - command not performed.*Last system error:/ms, - 'fault outputs proper warning to STDOUT'; - is($fault->[0], 451, 'fault returns 451'); -}; -{ - my $fault; - stderr_like { $fault = $smtpd->fault('test message') } - qr/test message.*Last system error/ms, - 'fault outputs proper custom warning to STDOUT'; - is($fault->[1], 'Internal error - try again later - test message', - 'returns the input message'); -}; - # vrfy command is(($smtpd->command('VRFY '))[0], 252, 'VRFY command'); diff --git a/t/qpsmtpd-smtp.t b/t/qpsmtpd-smtp.t new file mode 100644 index 0000000..d71ec79 --- /dev/null +++ b/t/qpsmtpd-smtp.t @@ -0,0 +1,45 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Data::Dumper; +use Test::More; +use Test::Output; + +use lib 't'; +use lib 'lib'; # test lib/Qpsmtpd/SMTP (vs site_perl) + +use_ok('Test::Qpsmtpd'); +use_ok('Qpsmtpd::SMTP'); + +ok(my $smtp = Qpsmtpd::SMTP->new(), "new smtp"); +ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection"); + +__new(); +__fault(); + +done_testing(); + +sub __new { + isa_ok( $smtp, 'Qpsmtpd::SMTP' ); + + ok( $smtp->{_commands}, "valid commands populated"); + $smtp = Qpsmtpd::SMTP->new( key => 'val' ); + cmp_ok( $smtp->{args}{key}, 'eq', 'val', "new with args"); + +} + +sub __fault { + + my $fault; + stderr_like { $fault = $smtpd->fault } + qr/program fault - command not performed \(/, + 'fault outputs proper warning to STDOUT'; + is($fault->[0], 451, 'fault returns 451'); + + stderr_like { $fault = $smtpd->fault('test message') } + qr/test message \(/, + 'fault outputs proper custom warning to STDOUT'; + is($fault->[1], 'Internal error - try again later - test message', + 'returns the input message'); +} From e7a4ca2b08be64a993ca6343583628e373f78073 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Thu, 11 Sep 2014 09:19:00 -0700 Subject: [PATCH 2/3] update MANIFEST and suppress helo test error --- MANIFEST | 3 +++ t/plugin_tests/helo | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/MANIFEST b/MANIFEST index f7a82fe..0d2740d 100644 --- a/MANIFEST +++ b/MANIFEST @@ -207,6 +207,9 @@ t/plugin_tests/sender_permitted_from t/plugin_tests/spamassassin t/plugin_tests/virus/clamdscan t/qpsmtpd-address.t +t/qpsmtpd-smtp.t +t/qpsmtpd-utils.t +t/qpsmtpd.t t/rset.t t/tempstuff.t t/Test/Qpsmtpd.pm diff --git a/t/plugin_tests/helo b/t/plugin_tests/helo index a1c1a60..eb0ca29 100644 --- a/t/plugin_tests/helo +++ b/t/plugin_tests/helo @@ -65,11 +65,11 @@ sub test_invalid_localhost { foreach my $ip ( undef, '', '192.0.99.5' ) { $self->qp->connection->remote_ip(undef); ($err, $why) = $self->invalid_localhost('localhost' ); - ok(!$err, "host: localhost, remote ip: $ip"); + ok(!$err, "host: localhost, invalid remote ip"); $self->qp->connection->remote_ip(undef); ($err, $why) = $self->invalid_localhost('not-localhost'); - ok($err, "host: not-localhost, remote ip: $ip"); + ok($err, "host: not-localhost, invalid remote ip"); }; foreach my $ip (qw/ ::1 127.0.0.1 / ) { From 847db53e0de7a2352ba23edef023575dbf156256 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Thu, 11 Sep 2014 10:53:09 -0700 Subject: [PATCH 3/3] updated fault test in qpsmtpd-smtp.t --- t/qpsmtpd-smtp.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/qpsmtpd-smtp.t b/t/qpsmtpd-smtp.t index d71ec79..249e951 100644 --- a/t/qpsmtpd-smtp.t +++ b/t/qpsmtpd-smtp.t @@ -33,12 +33,12 @@ sub __fault { my $fault; stderr_like { $fault = $smtpd->fault } - qr/program fault - command not performed \(/, + qr/program fault - command not performed.*Last system error:/ms, 'fault outputs proper warning to STDOUT'; is($fault->[0], 451, 'fault returns 451'); stderr_like { $fault = $smtpd->fault('test message') } - qr/test message \(/, + qr/test message.*Last system error/ms, 'fault outputs proper custom warning to STDOUT'; is($fault->[1], 'Internal error - try again later - test message', 'returns the input message');