From 3f2a49b697a6eb5ff8db78c05c65070b46bb75a6 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 3 Apr 2011 16:54:40 +0200 Subject: Use confess instead of return undef, Test::Exception for App::Raps2::Password test --- lib/App/Raps2.pm | 11 +++++------ lib/App/Raps2/Password.pm | 12 +++++------- lib/App/Raps2/UI.pm | 3 ++- t/20-app-raps2-password.t | 34 +++++++++++++++++----------------- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm index 4746e5a..8078653 100644 --- a/lib/App/Raps2.pm +++ b/lib/App/Raps2.pm @@ -12,6 +12,7 @@ use base 'Exporter'; use App::Raps2::Password; use App::Raps2::UI; +use Carp q(confess); use File::Path qw(make_path); use File::Slurp qw(slurp write_file); @@ -82,9 +83,7 @@ sub get_master_password { passphrase => $pass, ); - if (not $self->{'pass'}->verify($self->{'master_hash'})) { - return undef; - } + $self->{'pass'}->verify($self->{'master_hash'}); } sub create_config { @@ -122,7 +121,7 @@ sub cmd_add { my $ui = $self->{'ui'}; if (-e $pwfile) { - return undef; + confess('Password file already exists'); } $self->get_master_password(); @@ -157,7 +156,7 @@ sub cmd_dump { my $pwfile = $self->{'xdg_data'} . "/${name}"; if (not -e $pwfile) { - return undef; + confess('Password file does not exist'); } my %key = file_to_hash($pwfile); @@ -182,7 +181,7 @@ sub cmd_info { my $pwfile = $self->{'xdg_data'} . "/${name}"; if (not -e $pwfile) { - return undef; + confess('Password file does not exist'); } my %key = file_to_hash($pwfile); diff --git a/lib/App/Raps2/Password.pm b/lib/App/Raps2/Password.pm index 2ac1a51..ca7874b 100644 --- a/lib/App/Raps2/Password.pm +++ b/lib/App/Raps2/Password.pm @@ -1,8 +1,5 @@ package App::Raps2::Password; - - - use strict; use warnings; use autodie; @@ -10,6 +7,7 @@ use 5.010; use base 'Exporter'; +use Carp 'confess'; use Crypt::CBC; use Crypt::Eksblowfish; use Crypt::Eksblowfish::Bcrypt qw(bcrypt_hash en_base64 de_base64); @@ -23,11 +21,11 @@ sub new { $conf{'cost'} //= 12; if (not (defined $conf{'salt'} and length($conf{'salt'}) == 16)) { - return undef; + confess('incorrect salt length'); } if (not (defined $conf{'passphrase'} and length $conf{'passphrase'})) { - return undef; + confess('no passphrase given'); } my $ref = \%conf; @@ -39,7 +37,7 @@ sub salt { my ($self, $salt) = @_; if (not (defined $salt and length($salt) == 16)) { - return undef; + confess('incorrect salt length'); } $self->{'salt'} = $salt; @@ -92,7 +90,7 @@ sub verify { if ($testhash eq $myhash) { return 1; } - return undef; + confess('Passwords did not match'); } 1; diff --git a/lib/App/Raps2/UI.pm b/lib/App/Raps2/UI.pm index 43abf29..427270c 100644 --- a/lib/App/Raps2/UI.pm +++ b/lib/App/Raps2/UI.pm @@ -7,6 +7,7 @@ use 5.010; use base 'Exporter'; +use Carp qw(confess); use POSIX; our @EXPORT_OK = (); @@ -63,7 +64,7 @@ sub read_pw { $term->setattr(0, POSIX::TCSANOW); if ($verify and $in1 ne $in2) { - return undef; + confess('Input lines did not match'); } chomp $in1; diff --git a/t/20-app-raps2-password.t b/t/20-app-raps2-password.t index 7e36358..6a5f080 100644 --- a/t/20-app-raps2-password.t +++ b/t/20-app-raps2-password.t @@ -4,6 +4,7 @@ use warnings; use 5.010; use Test::More tests => 13; +use Test::Exception; my $pw; my $salt = 'abcdefghijklmnop'; @@ -11,26 +12,24 @@ my $pass = 'something'; use_ok('App::Raps2::Password'); -$pw = App::Raps2::Password->new(); -is($pw, undef, 'new() missing salt and passphrase'); +throws_ok { App::Raps2::Password->new() } qr{incorrect salt length}, + 'new() missing salt and passphrase'; -$pw = App::Raps2::Password->new(salt => $salt); -is($pw, undef, 'new() missing passphrase'); +throws_ok { App::Raps2::Password->new(salt => $salt) } qr{no passphrase given}, + 'new() missing passphrase'; -$pw = App::Raps2::Password->new(passphrase => $pass); -is($pw, undef, 'new() missing salt'); +throws_ok { App::Raps2::Password->new(passphrase => $pass) } qr{incorrect salt length}, + 'new() missing salt'; -$pw = App::Raps2::Password->new( - passphrase => $pass, - salt => 'abcdefghijklmno', -); -is($pw, undef, 'new() salt one too short'); +throws_ok { App::Raps2::Password->new( + passphrase => $pass, + salt => 'abcdefghijklmno', + ) } qr{incorrect salt length}, 'new() salt one too short'; -$pw = App::Raps2::Password->new( - passphrase => $pass, - salt => $salt . 'z', -); -is($pw, undef, 'new() salt one too long'); +throws_ok { App::Raps2::Password->new( + passphrase => $pass, + salt => $salt . 'z', + ) } qr{incorrect salt length}, 'new() salt one too long'; $pw = App::Raps2::Password->new( passphrase => $pass, @@ -53,6 +52,7 @@ is($pw->decrypt($pw->encrypt('foo')), 'foo', 'encrypt->decrypt okay'); ok($pw->verify('3lJRlaRuOGWv/z3g1DAOlcH.u9vS8Wm'), 'verify: verifies correct hash'); -ok(!$pw->verify('3lJRlaRuOGWv/z3g1DAOlcH.u9vS8WM'), 'verify: does not verify invalid hash'); +throws_ok { $pw->verify('3lJRlaRuOGWv/z3g1DAOlcH.u9vS8WM') } qr{Passwords did not match}, +'verify: does not verify invalid hash'; ok($pw->verify($pw->crypt('truth')), 'crypt->verify okay') -- cgit v1.2.3