Merge pull request #92 from msimerson/master

tests for Qpsmtpd/SMTP
This commit is contained in:
Jared Johnson 2014-09-11 13:18:03 -05:00
commit bf2cfcf4e3
6 changed files with 69 additions and 40 deletions

View File

@ -207,6 +207,9 @@ t/plugin_tests/sender_permitted_from
t/plugin_tests/spamassassin t/plugin_tests/spamassassin
t/plugin_tests/virus/clamdscan t/plugin_tests/virus/clamdscan
t/qpsmtpd-address.t t/qpsmtpd-address.t
t/qpsmtpd-smtp.t
t/qpsmtpd-utils.t
t/qpsmtpd.t
t/rset.t t/rset.t
t/tempstuff.t t/tempstuff.t
t/Test/Qpsmtpd.pm t/Test/Qpsmtpd.pm

View File

@ -7,7 +7,6 @@ WriteMakefile(
NAME => 'qpsmtpd', NAME => 'qpsmtpd',
VERSION_FROM => 'lib/Qpsmtpd.pm', VERSION_FROM => 'lib/Qpsmtpd.pm',
PREREQ_PM => { PREREQ_PM => {
'Test::Output' => 0,
'Data::Dumper' => 0, 'Data::Dumper' => 0,
'Date::Parse' => 0, 'Date::Parse' => 0,
'File::Temp' => 0, 'File::Temp' => 0,
@ -17,6 +16,9 @@ WriteMakefile(
'Net::IP' => 0, 'Net::IP' => 0,
'Time::HiRes' => 0, 'Time::HiRes' => 0,
'IO::Socket::SSL' => 0, 'IO::Socket::SSL' => 0,
# Dev/Test modules
'Test::More' => 0,
'Test::Output' => 0,
# modules for specific features # modules for specific features
'Mail::DKIM' => 0, 'Mail::DKIM' => 0,
'File::Tail' => 0, # log/summarize, log/watch 'File::Tail' => 0, # log/summarize, log/watch

View File

@ -1,12 +1,15 @@
package Qpsmtpd::SMTP;
use Qpsmtpd;
@ISA = qw(Qpsmtpd);
my %auth_mechanisms = ();
package Qpsmtpd::SMTP; package Qpsmtpd::SMTP;
use strict; 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::Connection;
use Qpsmtpd::Transaction; use Qpsmtpd::Transaction;
use Qpsmtpd::Plugin; use Qpsmtpd::Plugin;
@ -15,11 +18,7 @@ use Qpsmtpd::Auth;
use Qpsmtpd::Address (); use Qpsmtpd::Address ();
use Qpsmtpd::Command; use Qpsmtpd::Command;
use Mail::Header (); my %auth_mechanisms = ();
#use Data::Dumper;
use POSIX qw(strftime);
use Net::DNS;
# this is only good for forkserver # this is only good for forkserver
# can't set these here, cause forkserver resets them # 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; }; #$SIG{ALRM} = sub { warn "Connection Timed Out\n"; exit; };
sub new { sub new {
my $proto = shift; my ($proto, %args) = @_;
my $class = ref($proto) || $proto; my $class = ref($proto) || $proto;
my %args = @_;
my $self = bless({args => \%args}, $class); my $self = bless({args => \%args}, $class);
my (@commands) = 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
my (%commands); $self->{_commands} = {
@commands{@commands} = ('') x @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->SUPER::_restart(%args) if $args{restart}; # calls Qpsmtpd::_restart()
$self; $self;
} }

View File

@ -1,4 +1,4 @@
use Test::More tests => 14; use Test::More tests => 10;
use Test::Output; use Test::Output;
use strict; use strict;
use lib 't'; use lib 't';
@ -6,23 +6,6 @@ use_ok('Test::Qpsmtpd');
ok(my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(), "get new connection"); 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 # vrfy command
is(($smtpd->command('VRFY <foo@bar>'))[0], 252, 'VRFY command'); is(($smtpd->command('VRFY <foo@bar>'))[0], 252, 'VRFY command');

View File

@ -65,11 +65,11 @@ sub test_invalid_localhost {
foreach my $ip ( undef, '', '192.0.99.5' ) { foreach my $ip ( undef, '', '192.0.99.5' ) {
$self->qp->connection->remote_ip(undef); $self->qp->connection->remote_ip(undef);
($err, $why) = $self->invalid_localhost('localhost' ); ($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); $self->qp->connection->remote_ip(undef);
($err, $why) = $self->invalid_localhost('not-localhost'); ($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 / ) { foreach my $ip (qw/ ::1 127.0.0.1 / ) {

45
t/qpsmtpd-smtp.t Normal file
View File

@ -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.*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.*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');
}