[BACK]Return to keyring.t CVS log [TXT][DIR] Up to [local] / palm / Palm-Keyring / t

Annotation of palm/Palm-Keyring/t/keyring.t, Revision 1.19

1.12      andrew      1: #!/usr/bin/perl -T
1.19    ! andrew      2: # $RedRiver: keyring.t,v 1.18 2008/09/18 01:53:27 andrew Exp $
1.7       andrew      3: use strict;
                      4: use warnings;
1.1       andrew      5:
1.15      andrew      6: use Test::More tests => 52;
                      7: use Data::Dumper;
1.1       andrew      8:
1.18      andrew      9: BEGIN {
                     10:     use_ok('Palm::PDB');
                     11:     use_ok('Palm::Keyring');
1.9       andrew     12: }
1.1       andrew     13:
1.18      andrew     14: my $file         = 'Keys-test.pdb';
                     15: my $password     = '12345';
1.3       andrew     16: my $new_password = '54321';
                     17:
1.8       andrew     18: my @o = (
1.18      andrew     19:     {   version  => 4,
1.8       andrew     20:         password => $password,
                     21:     },
1.18      andrew     22:     {   version  => 5,
                     23:         password => $password,
                     24:         cipher   => 1,
1.8       andrew     25:     },
                     26: );
                     27:
1.19    ! andrew     28: my $acct = {
        !            29:     0 => {
        !            30:         label    => 'name',
        !            31:         label_id => 0,
        !            32:         data     => 'test3',
        !            33:         font     => 0,
        !            34:     },
        !            35:     1 => {
        !            36:         label    => 'account',
        !            37:         label_id => 1,
        !            38:         data     => 'atestaccount',
        !            39:         font     => 0,
        !            40:     },
        !            41:     2 => {
        !            42:         label    => 'password',
        !            43:         label_id => 2,
        !            44:         data     => $password,
        !            45:         font     => 0,
        !            46:     },
        !            47:     3 => {
        !            48:         label    => 'lastchange',
        !            49:         label_id => 3,
        !            50:         data     => {
        !            51:             day   => 2,
        !            52:             month => 2,
        !            53:             year  => 99,
1.14      andrew     54:         },
1.19    ! andrew     55:         font => 0,
        !            56:     },
        !            57:     255 => {
        !            58:         label    => 'notes',
        !            59:         label_id => 255,
        !            60:         data     => 'now that really roxorZ!',
        !            61:         font     => 0,
        !            62:     },
        !            63: };
1.9       andrew     64:
1.18      andrew     65: foreach my $options (@o) {
                     66:     my $pdb;
                     67:     my $record;
                     68:     my $decrypted;
                     69:
1.17      andrew     70:     my $Num_Tests_Left = 25;
1.18      andrew     71: SKIP: {
                     72:         if ( defined $options->{cipher} && $options->{cipher} > 0 ) {
                     73:             my $crypt = Palm::Keyring::crypts( $options->{cipher} );
                     74:             skip 'Crypt::CBC not installed', $Num_Tests_Left
1.17      andrew     75:                 unless eval "require Crypt::CBC";
1.18      andrew     76:             skip 'Crypt::' . $crypt->{name} . ' not installed',
                     77:                 $Num_Tests_Left
1.17      andrew     78:                 unless eval "require Crypt::$crypt->{name}";
1.11      andrew     79:         }
1.7       andrew     80:
1.18      andrew     81:         if ( $options->{version} == 4 ) {
1.17      andrew     82:             skip 'Crypt::DES not installed', $Num_Tests_Left
                     83:                 unless eval "require Crypt::DES ";
1.18      andrew     84:             skip 'Digest::MD5 not installed', $Num_Tests_Left
1.17      andrew     85:                 unless eval "require Digest::MD5 ";
1.18      andrew     86:         }
                     87:         elsif ( $options->{version} == 5 ) {
1.17      andrew     88:             skip 'Digest::HMAC_SHA1 not installed', $Num_Tests_Left
                     89:                 unless eval "require Digest::HMAC_SHA1 ";
1.11      andrew     90:         }
1.7       andrew     91:
1.18      andrew     92:         ok( $pdb = new Palm::Keyring($options),
                     93:             'new Palm::Keyring v' . $options->{version}
                     94:         );
1.7       andrew     95:
1.11      andrew     96:         ok( $record = $pdb->append_Record(), 'Append Record' );
1.7       andrew     97:
1.18      andrew     98:         ok( $pdb->Encrypt( $record, $password, $acct ),
1.14      andrew     99:             'Encrypt account into record' );
1.7       andrew    100:
1.11      andrew    101:         ok( $pdb->Write($file), 'Write file' );
1.9       andrew    102:
1.11      andrew    103:         $pdb = undef;
                    104:
1.19    ! andrew    105:         ok( $pdb = new Palm::Keyring(), 'new Palm::Keyring' );
1.7       andrew    106:
1.11      andrew    107:         ok( $pdb->Load($file), 'Load File' );
1.7       andrew    108:
1.11      andrew    109:         ok( $pdb->Password($password), 'Verify Password' );
1.7       andrew    110:
1.14      andrew    111:         my $rec_num = 0;
1.18      andrew    112:         ok( $decrypted = $pdb->Decrypt( $pdb->{records}->[$rec_num] ),
1.14      andrew    113:             'Decrypt record' );
1.7       andrew    114:
1.14      andrew    115:         is( $decrypted->{2}->{data}, $password, 'Got password' );
1.7       andrew    116:
1.11      andrew    117:         is_deeply( $decrypted, $acct, 'Account Matches' );
1.1       andrew    118:
1.14      andrew    119:         my $old_date = $decrypted->{3}->{data};
1.7       andrew    120:
1.18      andrew    121:         ok( $pdb->Password( $password, $new_password ),
                    122:             'Change PDB Password' );
1.7       andrew    123:
1.18      andrew    124:         ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ),
1.14      andrew    125:             'Decrypt with new password' );
1.7       andrew    126:
1.14      andrew    127:         my $new_date = $decrypted->{3}->{data};
1.7       andrew    128:
1.11      andrew    129:         is_deeply( $old_date, $new_date, 'Date didn\'t change' );
1.4       andrew    130:
1.19    ! andrew    131:         $decrypted->{2}->{data} = $new_password;
1.4       andrew    132:
1.19    ! andrew    133:         $pdb->{records}->[$rec_num]->{plaintext} = $decrypted;
1.15      andrew    134:
1.18      andrew    135:         ok( $pdb->Encrypt( $pdb->{'records'}->[$rec_num] ), 'Change record' );
1.7       andrew    136:
1.18      andrew    137:         ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ),
1.14      andrew    138:             'Decrypt new record' );
1.7       andrew    139:
1.14      andrew    140:         $new_date = $decrypted->{3}->{data};
1.4       andrew    141:
1.18      andrew    142:         my $od = join '/', map { $old_date->{$_} } sort keys %{$old_date};
                    143:         my $nd = join '/', map { $new_date->{$_} } sort keys %{$new_date};
1.7       andrew    144:
1.18      andrew    145:         isnt( $od, $nd, 'Date changed' );
1.7       andrew    146:
1.18      andrew    147:         is( $decrypted->{2}->{data}, $new_password, 'Got new password' );
1.7       andrew    148:
1.15      andrew    149:         my $last_decrypted = $decrypted;
                    150:
1.11      andrew    151:         $decrypted = {};
                    152:         ok( $pdb->Password(), 'Forget password' );
1.7       andrew    153:
1.18      andrew    154:         eval { $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ) };
1.11      andrew    155:         ok( $@, 'Don\'t decrypt' );
1.7       andrew    156:
1.18      andrew    157:         isnt( $decrypted->{password},
                    158:             $new_password, 'Didn\'t get new password' );
1.4       andrew    159:
1.15      andrew    160:         ok( $pdb->Unlock($new_password), 'Unlock' );
                    161:
                    162:         my @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
                    163:
                    164:         is_deeply( $plaintext[0], $last_decrypted, 'Account Matches' );
                    165:
                    166:         ok( $pdb->Lock(), 'Lock' );
                    167:
                    168:         my $cleared_decrypted = {};
1.18      andrew    169:         $cleared_decrypted->{0} = $last_decrypted->{0};
1.15      andrew    170:         @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
                    171:
                    172:         is_deeply( $plaintext[0], $cleared_decrypted, 'Cleared records' );
                    173:
1.11      andrew    174:         ok( unlink($file), 'Remove test pdb v' . $options->{version} );
1.15      andrew    175:
1.11      andrew    176:     }
1.8       andrew    177: }
1.1       andrew    178:
                    179: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>