Add some validation for passed db args

This commit is contained in:
Jared Johnson 2015-02-23 14:13:39 -06:00
parent 51ca3fcda4
commit 42c551944e
2 changed files with 16 additions and 0 deletions

View File

@ -349,6 +349,7 @@ sub _register_standard_hooks {
sub db_args { sub db_args {
my ( $self, %arg ) = @_; my ( $self, %arg ) = @_;
$self->validate_db_args(@_);
$self->{db_args} = \%arg if %arg; $self->{db_args} = \%arg if %arg;
$self->{db_args}{name} ||= $self->plugin_name; $self->{db_args}{name} ||= $self->plugin_name;
return %{ $self->{db_args} }; return %{ $self->{db_args} };
@ -356,7 +357,13 @@ sub db_args {
sub db { sub db {
my ( $self, %arg ) = @_; my ( $self, %arg ) = @_;
$self->validate_db_args(@_);
return $self->{db} ||= Qpsmtpd::DB->new( $self->db_args(%arg) ); 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; 1;

View File

@ -9,12 +9,21 @@ use Test::Qpsmtpd;
use_ok('Qpsmtpd::Plugin'); use_ok('Qpsmtpd::Plugin');
__validate_db_args();
__db_args(); __db_args();
__db(); __db();
__register_hook(); __register_hook();
done_testing(); 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 { sub __db_args {
my $plugin = FakePlugin->new; my $plugin = FakePlugin->new;
is( keyvals($plugin->db_args), is( keyvals($plugin->db_args),