count_unrecognized_commands

simplified logic in a couple places
consolidated duplicated message
added 4 tests
This commit is contained in:
Matt Simerson 2012-05-20 23:04:37 -04:00 committed by Robert
parent 74125300da
commit 5e76d66c66
2 changed files with 45 additions and 10 deletions

View File

@ -16,20 +16,23 @@ before we disconnect the client. Defaults to 4.
=cut
use strict;
use warnings;
use Qpsmtpd::Constants;
sub register {
my ($self, $qp, @args) = @_;
my ($self, $qp ) = shift, shift;
if (@args > 0) {
$self->{_unrec_cmd_max} = $args[0];
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.") if (@args > 1);
} else {
$self->{_unrec_cmd_max} = 4;
}
$self->{_unrec_cmd_max} = shift || 4;
if ( scalar @_ ) {
$self->log(LOGWARN, "Ignoring additional arguments.");
}
}
sub hook_connect {
my ($self, $transaction) = @_;
my $self = shift;
$self->qp->connection->notes('unrec_cmd_count', 0);
return DECLINED;
@ -46,8 +49,9 @@ sub hook_unrecognized_command {
);
if ($badcmdcount >= $self->{_unrec_cmd_max}) {
$self->log(LOGINFO, "Closing connection. Too many unrecognized commands.");
return (DENY_DISCONNECT, "Closing connection. $badcmdcount unrecognized commands. Perhaps you should read RFC 2821?");
my $msg = "Closing connection, $badcmdcount unrecognized commands.";
$self->log(LOGINFO, "fail: $msg");
return (DENY_DISCONNECT, "$msg Perhaps you should read RFC 2821?");
}
return DECLINED;

View File

@ -0,0 +1,31 @@
#!perl -w
use strict;
use warnings;
use Qpsmtpd::Constants;
sub register_tests {
my $self = shift;
$self->register_test('test_hook_unrecognized_command', 4);
};
sub test_hook_unrecognized_command {
my $self = shift;
$self->{_unrec_cmd_max} = 2;
$self->qp->connection->notes( 'unrec_cmd_count', 0 );
my ($code, $mess) = $self->hook_unrecognized_command(undef,'hiya');
cmp_ok( $code, '==', DECLINED, "good" );
$self->qp->connection->notes( 'unrec_cmd_count', 2 );
($code, $mess) = $self->hook_unrecognized_command(undef,'snookums');
cmp_ok( $code, '==', DENY_DISCONNECT, "limit" );
($code, $mess) = $self->hook_unrecognized_command(undef,'wtf');
cmp_ok( $code, '==', DENY_DISCONNECT, "over limit" );
cmp_ok( $self->qp->connection->notes( 'unrec_cmd_count'), '==', 4, "correct increment" );
};