=================================================================== RCS file: /cvs/palm/Palm-Keyring/t/keyring.t,v retrieving revision 1.21 retrieving revision 1.26 diff -u -r1.21 -r1.26 --- palm/Palm-Keyring/t/keyring.t 2008/09/19 03:08:01 1.21 +++ palm/Palm-Keyring/t/keyring.t 2011/09/18 00:45:33 1.26 @@ -1,9 +1,9 @@ #!/usr/bin/perl -T -# $RedRiver: keyring.t,v 1.20 2008/09/18 06:06:11 andrew Exp $ +# $RedRiver: keyring.t,v 1.24 2008/09/19 05:53:08 andrew Exp $ use strict; use warnings; -use Test::More tests => 194; +use Test::More tests => 321; use Data::Dumper; BEGIN { @@ -62,6 +62,10 @@ }, }; +my %unchanging_date = %{ $acct->{3}->{data} }; + +my $bad_version = 999; + my $bad_cipher = 999; my %crypt_1_details = ( 'default_iter' => 1000, @@ -109,64 +113,227 @@ 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' ); +eval { $pdb = new Palm::Keyring( -file => 't/Keys-invalid_appinfo.pdb' ) }; +like( + $@, + qr/^Corrupt \s appinfo\? \s no \s {other}/xms, + 'Couldn\'t load pdb with invalid appinfo' +); + +eval{ $pdb = new Palm::Keyring( -file => 't/Keys-no_data.pdb', -password => + $new_password ) }; +like( + $@, + qr/^Invalid \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' ); -ok( $pdb->Password($password), 'Entering Password' ); - my $record; ok( $record = $pdb->append_Record(), 'Append Record' ); +my $ivec = pack("C*", 1..8); +ok( $pdb->Encrypt( $record, $password, $acct, $ivec), + 'Encrypt account into record (with custom ivec)' ); + +$acct->{254} = { + label => 'misc', + label_id => 254, + data => 'This doesn\'t even really exist', + font => 0, +}; ok( $pdb->Encrypt( $record, $password, $acct ), - 'Encrypt account into record' ); + 'Encrypt account into record (with custom field)' ); + +delete $acct->{254}; + + +delete $record->{plaintext}; + ok( $pdb->PackRecord($record), 'Pack Proper Record'); ok( $record = $pdb->ParseRecord(%{ $record }), 'Parse Proper Packed'); my $record2; ok( $record2 = $pdb->append_Record(), 'Append Record' ); -ok( $pdb->PackRecord($record2), 'Pack Empty Record'); -ok( $record = $pdb->ParseRecord(%{ $record2 }), 'Parse Empty Packed'); +eval{ $pdb->PackRecord($record2) }; +like( + $@, + qr/^No \s encrypted \s data \s in \s record/xms, + 'Pack Empty Record' +); +$pdb->{appinfo}->{cipher} = 'TESTING'; -$pdb->{version} = 999; +eval{ $pdb->Decrypt( $record ) }; +like( + $@, + qr/^Unsupported \s Crypt \s Testing \s in \s decrypt/xms, + 'Couldn\'t Decrypt with unsupported Crypt' +); + +my $encrypted = delete $record->{encrypted}; +eval{ $pdb->Encrypt( $record ) }; +like( + $@, + qr/^Unsupported \s Crypt \s Testing \s in \s encrypt/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 \s in \s decrypt/xms, + 'Couldn\'t Decrypt with unsupported cipher' +); + +$encrypted = delete $record->{encrypted}; +eval{ $pdb->Encrypt( $record ) }; +like( + $@, + qr/^Unknown \s cipher \s $bad_cipher \s in \s encrypt/xms, + 'Couldn\'t Encrypt with unsupported cipher' +); +$record->{encrypted} = $encrypted; + + +$record2->{encrypted} = {}; +delete $record2->{ivec}; +eval{ $pdb->PackRecord($record2) }; +like( + $@, + qr/^No \s ivec/xms, + 'Pack Empty Record with encrypted, but no ivec' +); + + + +$pdb->{version} = 4; +delete $record2->{encrypted}; +delete $record2->{data}; +eval{ $pdb->PackRecord($record2) }; +like( + $@, + qr/^No \s data \s to \s PackRecord/xms, + 'Pack Empty Record with no data' +); + + +$pdb->{version} = $bad_version; +eval{ $pdb->Decrypt( $record ) }; +like( + $@, + qr/^Unsupported \s version \s $bad_version \s in \s Decrypt/xms, + 'Couldn\'t Decrypt with unsupported version' +); + +delete $record->{encrypted}; +eval{ $pdb->Encrypt( $record, undef, $acct ) }; +like( + $@, + qr/^Unsupported \s version \s $bad_version \s in \s Encrypt/xms, + 'Couldn\'t Encrypt with unsupported version' +); + + +eval { $pdb->Write($file) }; +like( + $@, + 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->Write($file) }; -like( - $@, - qr/^Unsupported \s Version \s 999/xms, - 'Couldn\'t Write file with unsupported version' +eval{ $pdb->Password( $password ) }; +like( $@, + qr/^Unsupported \s Version \s $bad_version/xms, + 'Couldn\'t Password with Invalid Version' ); -$pdb = undef; +$pdb = undef; +$record = undef; +$record2 = undef; unlink $file; @@ -176,6 +343,7 @@ my $pdb; my $record; my $decrypted; + %{ $acct->{3}->{data} } = %unchanging_date; my $Num_Tests_Left = 25; SKIP: { @@ -227,13 +395,45 @@ ); ok( $pdb->Write($file), 'Write "empty" file' ); + #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/^Invalid \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( $pdb->Write($file), 'Write file' ); + #print Dumper $pdb, $acct, $record; $pdb = undef; @@ -241,9 +441,45 @@ ok( $pdb->Load($file), 'Load File' ); + my $rec_num = 0; + + 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/^Invalid \s Password/xms, + 'Decypt with invalid password' + ); + ok( $pdb->Password($password), 'Verify Password' ); - my $rec_num = 0; + eval{ $pdb->Password($new_password) }; + like( + $@, + qr/^Invalid \s Password/xms, + 'Verify Incorrect Password' + ); + + eval{ $pdb->Decrypt( {} ) }; + like( + $@, + qr/^No \s encrypted \s content!/xms, + 'Decrypt with empty record' + ); + ok( $decrypted = $pdb->Decrypt( $pdb->{records}->[$rec_num] ), 'Decrypt record' ); @@ -268,11 +504,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}; @@ -280,7 +518,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;