=================================================================== RCS file: /cvs/palm/Palm-Keyring/t/keyring.t,v retrieving revision 1.24 retrieving revision 1.27 diff -u -r1.24 -r1.27 --- palm/Palm-Keyring/t/keyring.t 2008/09/19 06:53:08 1.24 +++ palm/Palm-Keyring/t/keyring.t 2011/09/19 04:05:11 1.27 @@ -1,9 +1,9 @@ #!/usr/bin/perl -T -# $RedRiver: keyring.t,v 1.23 2008/09/19 05:39:58 andrew Exp $ +# $RedRiver: keyring.t,v 1.24 2008/09/19 05:53:08 andrew Exp $ use strict; use warnings; -use Test::More tests => 202; +use Test::More tests => 333; use Data::Dumper; BEGIN { @@ -64,6 +64,8 @@ my %unchanging_date = %{ $acct->{3}->{data} }; +my $bad_version = 999; + my $bad_cipher = 999; my %crypt_1_details = ( 'default_iter' => 1000, @@ -111,20 +113,69 @@ is( Palm::Keyring::labels($bad_label_name), undef, "Didn't get label for $bad_label_name" ); +# encrypt/decrypt (v4) +my %acctv4 = ( + account => 'account name', + password => $password, + notes => 'these are notes', + lastchange => undef, +); +my $digestv4; +ok( $digestv4 = Palm::Keyring::_calc_keys( $password ), '_calc_keys' ); + +my $encryptedv4; +ok( $encryptedv4 = Palm::Keyring::_encrypt_v4( \%acctv4, {}, $digestv4 ), + '_encrypt_v4'); + +my $plaintextv4; +ok( $plaintextv4 = Palm::Keyring::_decrypt_v4( $encryptedv4, $digestv4 ), + '_decrypt_v4'); + +$plaintextv4->{lastchange} = undef; +is_deeply( $plaintextv4, \%acctv4, 'Is what we got back, what we sent in?' ); + +my $NULL = chr(0); +$plaintextv4 = join $NULL, $acctv4{account}, $acctv4{password}, $acctv4{notes}; + +ok( $encryptedv4 = Palm::Keyring::_crypt3des( $plaintextv4, $digestv4, 1 ), + 'encrypt without date' ); # 1 is encrypt + +ok( $plaintextv4 = Palm::Keyring::_decrypt_v4( $encryptedv4, $digestv4 ), + '_decrypt_v4'); + +$plaintextv4->{'lastchange'} = undef; +is_deeply( $plaintextv4, \%acctv4, 'Is what we got back, what we sent in?' ); + +# Password + +eval{ Palm::Keyring::_password_verify_v4() }; +like( + $@, + qr/^No \s password \s specified!/xms, + '_password_verify_v4 with no password' +); + +eval{ Palm::Keyring::_password_verify_v4($password) }; +like( + $@, + qr/^No \s encrypted \s password \s in \s file!/xms, + '_password_verify_v4 with no password' +); + my $pdb; eval { $pdb = new Palm::Keyring( -file => 't/Keys-invalid_version.pdb' ) }; like( $@, - qr/^Unsupported \s Version \s 999/xms, + qr/^Unsupported \s Version \s $bad_version/xms, 'Couldn\'t load pdb with invalid version' ); eval { $pdb = new Palm::Keyring( -file => 't/Keys-invalid_cipher.pdb' ) }; like( $@, - qr/^Unknown \s cipher \s 999/xms, + qr/^Unknown \s cipher \s $bad_version/xms, 'Couldn\'t load pdb with Unknown Cipher' ); @@ -135,45 +186,35 @@ 'Couldn\'t load pdb with invalid appinfo' ); -ok( $pdb = new Palm::Keyring( -file => 't/Keys-no_data.pdb', -password => - $new_password ), - 'Loaded Palm::Keyring file with no data' ); +eval{ $pdb = new Palm::Keyring( -file => 't/Keys-no_data.pdb', -password => + $new_password ) }; +like( + $@, + qr/^Incorrect \s Password/xms, + 'Couldn\'t load Palm::Keyring file with no data and wrong password' +); + ok( $pdb = new Palm::Keyring( -file => 't/Keys-no_data.pdb' ), 'Loaded Palm::Keyring file with no data' ); my $record; ok( $record = $pdb->append_Record(), 'Append Record' ); -eval{ $pdb->Encrypt() }; -like( - $@, - qr/^Needed \s parameter \s \[record\] \s not \s passed!/xms, - 'Encrypt account into record without record' -); -eval{ $pdb->Encrypt( $record ) }; -like( - $@, - qr/^password \s not \s set!/xms, - 'Encrypt account into record without password' -); -eval{ $pdb->Encrypt( $record, $password ) }; -like( - $@, - qr/^Needed \s parameter \s \[plaintext\] \s not \s passed!/xms, - 'Encrypt account into record without account' -); -eval{ $pdb->Encrypt( $record, $new_password, $acct ) }; -like( - $@, - qr/^Incorrect \s Password!/xms, - 'Encrypt account into record with wrong password' -); - my $ivec = pack("C*", 1..8); ok( $pdb->Encrypt( $record, $password, $acct, $ivec), 'Encrypt account into record (with custom ivec)' ); -ok( $pdb->Encrypt( $record, $password, $acct), - 'Encrypt account into record (with no changes)'); +$acct->{254} = { + label => 'misc', + label_id => 254, + data => 'This doesn\'t even really existx', + font => 0, +}; +ok( $pdb->Encrypt( $record, $password, $acct ), + 'Encrypt account into record (with custom field)' ); + +delete $acct->{254}; + + delete $record->{plaintext}; ok( $pdb->PackRecord($record), 'Pack Proper Record'); @@ -188,7 +229,44 @@ 'Pack Empty Record' ); -$record2->{encrypted} = q{}; +$pdb->{appinfo}->{cipher} = 'TESTING'; + +eval{ $pdb->Decrypt( $record ) }; +like( + $@, + qr/^Unsupported \s Crypt \s Testing/xms, + 'Couldn\'t Decrypt with unsupported Crypt' +); + +my $encrypted = delete $record->{encrypted}; +eval{ $pdb->Encrypt( $record ) }; +like( + $@, + qr/^Unsupported \s Crypt \s Testing/xms, + 'Couldn\'t Encrypt with unsupported Crypt' +); +$record->{encrypted} = $encrypted; + +$pdb->{appinfo}->{cipher} = $bad_cipher; +eval{ $pdb->Decrypt( $record ) }; +like( + $@, + qr/^Unknown \s cipher \s $bad_cipher/xms, + 'Couldn\'t Decrypt with unsupported cipher' +); + +$encrypted = delete $record->{encrypted}; +eval{ $pdb->Encrypt( $record ) }; +like( + $@, + qr/^Unknown \s cipher \s $bad_cipher/xms, + 'Couldn\'t Encrypt with unsupported cipher' +); +$record->{encrypted} = $encrypted; + + +$record2->{encrypted} = {}; +delete $record2->{ivec}; eval{ $pdb->PackRecord($record2) }; like( $@, @@ -196,49 +274,65 @@ 'Pack Empty Record with encrypted, but no ivec' ); + + $pdb->{version} = 4; -delete $record->{encrypted}; -delete $record->{data}; -eval{ $pdb->PackRecord($record) }; -like( $@, +delete $record2->{encrypted}; +delete $record2->{data}; +eval{ $pdb->PackRecord($record2) }; +like( + $@, qr/^No \s data \s in \s record \s to \s pack/xms, - 'Couldn\'t PackRecord without data' + 'Pack Empty Record with no data' +); + + +$pdb->{version} = $bad_version; +eval{ $pdb->Decrypt( $record ) }; +like( + $@, + qr/^Unsupported \s Version \s $bad_version/xms, + 'Couldn\'t Decrypt with unsupported version' ); -$pdb->{version} = 999; delete $record->{encrypted}; eval{ $pdb->Encrypt( $record, undef, $acct ) }; like( $@, - qr/^Unsupported \s version \s 999/xms, + qr/^Unsupported \s Version \s $bad_version/xms, 'Couldn\'t Encrypt with unsupported version' ); + eval { $pdb->Write($file) }; like( $@, - qr/^Unsupported \s Version \s 999/xms, + qr/^Unsupported \s Version \s $bad_version/xms, 'Couldn\'t Write file with unsupported version' ); eval{ $pdb->PackRecord($record) }; like( $@, - qr/^Unsupported \s Version \s 999/xms, + qr/^Unsupported \s Version \s $bad_version/xms, 'Couldn\'t PackRecord with Invalid Version' ); $record2->{data} = q{nothing}; eval{ $pdb->ParseRecord(%{ $record2 }) }; like( $@, - qr/^Unsupported \s Version \s 999/xms, + qr/^Unsupported \s Version \s $bad_version/xms, 'Couldn\'t ParseRecord with Invalid Version' ); +eval{ $pdb->Password( $new_password ) }; +like( $@, + qr/^Unsupported \s Version \s $bad_version/xms, + 'Couldn\'t Password with Invalid Version' +); $pdb = undef; $record = undef; $record2 = undef; -%{ $acct->{3}->{data} } = %unchanging_date; unlink $file; @@ -248,7 +342,10 @@ my $pdb; my $record; my $decrypted; + %{ $acct->{3}->{data} } = %unchanging_date; + my $rec_num = 0; + my $Num_Tests_Left = 25; SKIP: { if ( defined $options->{cipher} && $options->{cipher} > 0 ) { @@ -302,10 +399,45 @@ #exit if $pdb->{version} == 5; ok( $record = $pdb->append_Record(), 'Append Record' ); + + ok( $pdb->Password(), 'Clear Password' ); + eval{ $pdb->Encrypt() }; + like( + $@, + qr/^Needed \s parameter \s \[record\] \s not \s passed!/xms, + 'Encrypt account into record without record' + ); + eval{ $pdb->Encrypt( $record ) }; + like( + $@, + qr/^Password \s not \s set!/xms, + 'Encrypt account into record without password' + ); + eval{ $pdb->Encrypt( $record, $password ) }; + like( + $@, + qr/^Needed \s parameter \s \[plaintext\] \s not \s passed!/xms, + 'Encrypt account into record without account' + ); + eval{ $pdb->Encrypt( $record, $new_password, $acct ) }; + like( + $@, + qr/^Incorrect \s Password/xms, + 'Encrypt account into record with wrong password' + ); ok( $pdb->Encrypt( $record, $password, $acct ), 'Encrypt account into record' ); + ok( $pdb->Encrypt( $record, $password, $acct ), + 'Encrypt account into record (with no changes)'); + + ok( $decrypted = $pdb->Decrypt( $pdb->{records}->[$rec_num] ), + 'Decrypt record' ); + + + is( $decrypted->{2}->{data}, $password, 'Got password' ); + ok( $pdb->Write($file), 'Write file' ); $pdb = undef; @@ -314,9 +446,43 @@ ok( $pdb->Load($file), 'Load File' ); + eval{ $pdb->Decrypt( ) }; + like( + $@, + qr/^Needed \s parameter \s \[record\] \s not \s passed!/xms, + 'Decrypt with no record' + ); + + eval{ $pdb->Decrypt( $pdb->{records}->[$rec_num] ) }; + like( + $@, + qr/^Password \s not \s set!/xms, + 'Decrypt with no password set' + ); + + eval{ $pdb->Decrypt( $pdb->{records}->[$rec_num], $new_password ) }; + like( + $@, + qr/^Incorrect \s Password/xms, + 'Decypt with invalid password' + ); + + eval{ $pdb->Password($new_password) }; + like( + $@, + qr/^Incorrect \s Password/xms, + 'Verify Incorrect Password' + ); + + eval{ $pdb->Decrypt( {} ) }; + like( + $@, + qr/^No \s encrypted \s content!/xms, + 'Decrypt with empty record' + ); + ok( $pdb->Password($password), 'Verify Password' ); - my $rec_num = 0; ok( $decrypted = $pdb->Decrypt( $pdb->{records}->[$rec_num] ), 'Decrypt record' ); @@ -341,11 +507,13 @@ $pdb->{records}->[$rec_num]->{plaintext} = $decrypted; ok( $pdb->Encrypt( $pdb->{'records'}->[$rec_num] ), - 'Change record' ); + 'Encrypt record (new password)' ); ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ), 'Decrypt new record' ); + is( $decrypted->{2}->{data}, $new_password, 'Got new password' ); + $new_date = $decrypted->{3}->{data}; my $od = join '/', map { $old_date->{$_} } sort keys %{$old_date}; @@ -353,7 +521,35 @@ isnt( $od, $nd, 'Date changed' ); - is( $decrypted->{2}->{data}, $new_password, 'Got new password' ); + %{ $new_date } = %unchanging_date; + $new_date->{year} = 1999; + $decrypted->{3}->{data} = $new_date; + + ok( $pdb->Encrypt( $pdb->{'records'}->[$rec_num], undef, $decrypted ), + 'Encrypt record (new date)' ); + + ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ), + 'Decrypt new record' ); + + $new_date = $decrypted->{3}->{data}; + + $od = $nd; + $nd = join '/', map { $new_date->{$_} } sort keys %{$new_date}; + my $ud = join '/', map { $unchanging_date{$_} } sort keys %unchanging_date; + + isnt( $od, $nd, 'Date changed (from what it used to be)' ); + is( $ud, $nd, 'Date changed (to what we set)' ); + + delete $decrypted->{3}; + ok( $pdb->Encrypt( $pdb->{'records'}->[$rec_num], undef, $decrypted ), + 'Encrypt record (no date)' ); + + ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ), + 'Decrypt new record' ); + + $new_date = $decrypted->{3}->{data}; + + is( ref $new_date, 'HASH', 'Got a hashref date' ); my $last_decrypted = $decrypted;