simplified logic in a couple places
consolidated duplicated message
added 4 tests
---
 plugins/count_unrecognized_commands        |   24 ++++++++++++---------
 t/plugin_tests/count_unrecognized_commands |   31 ++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 10 deletions(-)
 create mode 100644 t/plugin_tests/count_unrecognized_commands

diff --git a/plugins/count_unrecognized_commands 
b/plugins/count_unrecognized_commands
index 5330a99..40a0e1c 100644
--- a/plugins/count_unrecognized_commands
+++ b/plugins/count_unrecognized_commands
@@ -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;
diff --git a/t/plugin_tests/count_unrecognized_commands 
b/t/plugin_tests/count_unrecognized_commands
new file mode 100644
index 0000000..b92afef
--- /dev/null
+++ b/t/plugin_tests/count_unrecognized_commands
@@ -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" );
+};
-- 
1.7.9.6

Reply via email to