diff --git a/lib/Qpsmtpd/Plugin.pm b/lib/Qpsmtpd/Plugin.pm index dd8ccf4..379031b 100644 --- a/lib/Qpsmtpd/Plugin.pm +++ b/lib/Qpsmtpd/Plugin.pm @@ -349,6 +349,7 @@ sub _register_standard_hooks { sub db_args { my ( $self, %arg ) = @_; + $self->validate_db_args(@_); $self->{db_args} = \%arg if %arg; $self->{db_args}{name} ||= $self->plugin_name; return %{ $self->{db_args} }; @@ -356,7 +357,13 @@ sub db_args { sub db { my ( $self, %arg ) = @_; + $self->validate_db_args(@_); return $self->{db} ||= Qpsmtpd::DB->new( $self->db_args(%arg) ); } +sub validate_db_args { + (my $self, undef, my @args) = @_; + die "Invalid db arguments\n" if @args % 2; +} + 1; diff --git a/t/qpsmtpd-plugin.t b/t/qpsmtpd-plugin.t index a6596ca..2373c0e 100644 --- a/t/qpsmtpd-plugin.t +++ b/t/qpsmtpd-plugin.t @@ -9,12 +9,21 @@ use Test::Qpsmtpd; use_ok('Qpsmtpd::Plugin'); +__validate_db_args(); __db_args(); __db(); __register_hook(); done_testing(); +sub __validate_db_args { + my $plugin = FakePlugin->new; + eval { $plugin->validate_db_args($plugin, testkey => 1) }; + is( $@, '', 'validate_db_args() does not die on valid data' ); + eval { $plugin->validate_db_args($plugin, 'bogus') }; + is( $@, "Invalid db arguments\n", 'validate_db_args() dies on invalid data' ); +} + sub __db_args { my $plugin = FakePlugin->new; is( keyvals($plugin->db_args),