From 1ae8ed206fda170743e39a5620e0ff481a7eebbf Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Tue, 24 Feb 2015 15:19:47 -0600 Subject: [PATCH] More useful exception on invalid db_dir --- lib/Qpsmtpd/DB/File/DBM.pm | 14 +++++++++----- t/qpsmtpd-db-file-dbm.t | 23 ++++++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/Qpsmtpd/DB/File/DBM.pm b/lib/Qpsmtpd/DB/File/DBM.pm index 7a30c5a..a95e867 100644 --- a/lib/Qpsmtpd/DB/File/DBM.pm +++ b/lib/Qpsmtpd/DB/File/DBM.pm @@ -182,12 +182,16 @@ sub flush { sub dir { my ( $self, $dir ) = @_; if ( $dir ) { - die "Cannot use DB directory '$dir'\n" if !$self->validate_dir($dir); + $self->validate_dir($dir); return $self->{dir} = $dir; } return $self->{dir} if $self->{dir}; for my $d ( $self->candidate_dirs ) { - next if ! $self->validate_dir($d); + # Ignore invalid directories for static default directories + my $is_valid; + eval { $is_valid = $self->validate_dir($d); }; + next if $@; + next if !$is_valid; return $self->{dir} = $d; # first match wins } } @@ -203,9 +207,9 @@ sub candidate_dirs { sub validate_dir { my ( $self, $d ) = @_; - return 0 if ! $d; - return 0 if ! -d $d; - return 0 if ! -w $d; + die "Empty DB directory supplied\n" if ! $d; + die "DB directory '$d' does not exist\n" if ! -d $d; + die "DB directory '$d' is not writeable\n" if ! -w $d; return 1; } diff --git a/t/qpsmtpd-db-file-dbm.t b/t/qpsmtpd-db-file-dbm.t index 65e1eb0..2217d21 100644 --- a/t/qpsmtpd-db-file-dbm.t +++ b/t/qpsmtpd-db-file-dbm.t @@ -119,15 +119,24 @@ sub __candidate_dirs { } sub __validate_dir { - is( $db->validate_dir(), 0, 'validate_dir(): false on no input' ); - is( $db->validate_dir(undef), 0, 'validate_dir(): false on undef' ); - is( $db->validate_dir('invalid'), 0, - 'validate_dir(): false for non-existent directory' ); + eval { $db->validate_dir(); }; + is( $@, "Empty DB directory supplied\n", + 'validate_dir(): die on no input' ); + eval { $db->validate_dir(undef); }; + is( $@, "Empty DB directory supplied\n", + 'validate_dir(): die on undef' ); + eval { $db->validate_dir(''); }; + is( $@, "Empty DB directory supplied\n", + 'validate_dir(): die on empty string' ); + eval { $db->validate_dir('invalid'); }; + is( $@, "DB directory 'invalid' does not exist\n", + 'validate_dir(): die on non-existent directory' ); is( $db->validate_dir('t/tmp'), 1, 'validate_dir(): true for real directory' ); mkdir 't/tmp/wtest', 0555; - is( $db->validate_dir('t/tmp/wtest'), 0, - 'validate_dir(): false for non-writeable directory' ); + eval { $db->validate_dir('t/tmp/wtest') }; + is( $@, "DB directory 't/tmp/wtest' is not writeable\n", + 'validate_dir(): die on non-writeable directory' ); chmod 0777, 't/tmp/wtest'; is( $db->validate_dir('t/tmp/wtest'), 1, 'validate_dir(): true for writeable directory' ); @@ -147,7 +156,7 @@ sub __dir { $db2->candidate_dirs('_invalid'); is( $db2->dir, 't/config', 'invalid candidate dirs reverts to default' ); eval { $db2->dir('_invalid'); }; - is( $@, "Cannot use DB directory '_invalid'\n", 'die on invalid dir' ); + is( $@, "DB directory '_invalid' does not exist\n", 'die on invalid dir' ); } sub __untie_gotcha {