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

Annotation of palm/Palm-Keyring/t/keyring5.t, Revision 1.5

1.5     ! andrew      1: #!/usr/bin/perl -T
        !             2: # $RedRiver: keyring5.t,v 1.4 2007/02/22 04:57:37 andrew Exp $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5:
1.3       andrew      6: use Test::More tests => 126;
1.1       andrew      7: use YAML;
                      8:
                      9: BEGIN {
                     10:     use_ok( 'Palm::PDB' );
                     11:     use_ok( 'Palm::Keyring' );
                     12: }
                     13:
                     14: my $file = 'Keys-test.pdb';
                     15: my $password = '12345';
                     16: my $new_password = '54321';
                     17:
1.2       andrew     18: foreach my $cipher (0..3) {
1.1       andrew     19:     my $pdb;
1.2       andrew     20:     my @recs;
1.1       andrew     21:     my $record;
                     22:     my $decrypted;
                     23:
1.4       andrew     24:     my $crypt = Palm::Keyring::crypts($cipher);
                     25:
1.2       andrew     26:     my $options = {
                     27:         version  => 5,
                     28:         password => $password,
                     29:         cipher   => $cipher,
1.1       andrew     30:     };
                     31:
1.3       andrew     32:     my $rec1_name = 'test';
                     33:
1.2       andrew     34:     my $original_accts = [
                     35:     [
                     36:     {
                     37:         'label_id' => 2,
                     38:         'data' => 'only password is set',
                     39:         'label' => 'password',
1.3       andrew     40:         'font' => 0,
1.2       andrew     41:     },
                     42:     {
                     43:         'label_id' => 3,
                     44:         'data' => {
                     45:             'month' => 1,
                     46:             'day' => 1,
                     47:             'year' => 107
                     48:         },
                     49:         'label' => 'lastchange',
1.3       andrew     50:         'font' => 0,
1.2       andrew     51:     }
                     52:     ],
                     53:     [
                     54:     {
                     55:         'label_id' => 2,
                     56:         'data' => 'abcd1234',
                     57:         'label' => 'password',
1.3       andrew     58:         'font' => 0,
1.2       andrew     59:     },
                     60:     {
                     61:         'label_id' => 3,
                     62:         'data' => {
                     63:             'month' => 1,
                     64:             'day' => 11,
                     65:             'year' => 107
                     66:         },
                     67:         'label' => 'lastchange',
1.3       andrew     68:         'font' => 0,
1.2       andrew     69:     },
                     70:     {
                     71:         'label_id' => 255,
                     72:         'data' => 'This is a short note.',
                     73:         'label' => 'notes',
1.3       andrew     74:         'font' => 0,
1.2       andrew     75:     }
                     76:     ],
                     77:     [
                     78:     {
                     79:         'label_id' => 2,
                     80:         'data' => 'password (date is 2/2/07)',
                     81:         'label' => 'password',
1.3       andrew     82:         'font' => 0,
1.2       andrew     83:     },
                     84:     {
                     85:         'label_id' => 3,
                     86:         'data' => {
                     87:             'month' => 1,
                     88:             'day' => 2,
                     89:             'year' => 107
                     90:         },
                     91:         'label' => 'lastchange',
1.3       andrew     92:         'font' => 0,
1.2       andrew     93:     }
                     94:     ]
                     95:     ];
1.1       andrew     96:
1.4       andrew     97:     SKIP: {
                     98:         if ($cipher > 0) {
                     99:             skip 'Crypt::CBC not installed', 31 unless
                    100:                 eval "require Crypt::CBC";
                    101:             skip 'Crypt::' . $crypt->{name} . ' not installed', 31 unless
                    102:                 eval "require Crypt::$crypt->{name}";
                    103:         }
                    104:         skip 'Digest::HMAC_SHA1 not installed', 31 unless
                    105:             eval " require Digest::HMAC_SHA1 ";
                    106:
                    107:         ok( $pdb = new Palm::Keyring($options), 'New Palm::Keyring v'
                    108:             . $options->{version}
                    109:             . ' Cipher '
                    110:             . $options->{cipher}
                    111:         );
                    112:
                    113:         my $rec_id = 0;
                    114:         foreach my $acct (@{ $original_accts} ) {
                    115:             ok( $record = $pdb->append_Record(), 'Append Record' );
                    116:             if ($rec_id == 1) {
                    117:                 ok( $record->{name} = $rec1_name, 'Setting record name' );
                    118:             }
                    119:             ok( $pdb->Encrypt($record, $acct, $password), 'Encrypt account into record' );
                    120:             $rec_id++;
1.3       andrew    121:         }
1.1       andrew    122:
1.4       andrew    123:         ok( $pdb->Write($file), 'Write file' );
1.1       andrew    124:
1.4       andrew    125:         $pdb = undef;
1.1       andrew    126:
1.4       andrew    127:         ok( $pdb = new Palm::PDB(), 'New Palm::PDB' );
1.1       andrew    128:
1.4       andrew    129:         ok( $pdb->Load($file), 'Load File' );
1.1       andrew    130:
1.4       andrew    131:         ok( $pdb->Password($password), 'Verify Password' );
1.1       andrew    132:
1.4       andrew    133:         $rec_id = 0;
                    134:         foreach my $rec (@{ $pdb->{records} }) {
1.2       andrew    135:         ok( $decrypted = $pdb->Decrypt($rec), 'Decrypt record' );
1.3       andrew    136:         if ($rec_id == 1) {
1.4       andrew    137:         is( $rec->{name}, $rec1_name, 'Checking record name' );
1.3       andrew    138:         }
1.2       andrew    139:         push @recs, $decrypted;
1.3       andrew    140:         $rec_id++;
1.4       andrew    141:         }
1.1       andrew    142:
1.4       andrew    143:         is_deeply( \@recs, $original_accts, 'Account Matches' );
1.1       andrew    144:
1.4       andrew    145:         @recs = ();
                    146:         my $rec_num = 1;
1.1       andrew    147:
1.4       andrew    148:         ok( $pdb->Password($password, $new_password), 'Change PDB Password' );
1.1       andrew    149:
1.4       andrew    150:         foreach my $rec (@{ $pdb->{records} }) {
1.2       andrew    151:         ok( $decrypted = $pdb->Decrypt($rec), 'Decrypt record' );
                    152:         push @recs, $decrypted;
1.4       andrew    153:         }
1.1       andrew    154:
1.4       andrew    155:         is_deeply( \@recs, $original_accts, 'Account Matches' );
1.1       andrew    156:
1.4       andrew    157:         my $acct;
                    158:         ok( $acct = $pdb->Decrypt( $pdb->{records}->[$rec_num]), 'decrypt record ' . $rec_num);
1.1       andrew    159:
1.4       andrew    160:         foreach my $field (@{ $acct }) {
1.2       andrew    161:         next unless $field->{label} eq 'password';
                    162:         ok($field->{data} = $new_password, 'Change password');
1.4       andrew    163:         }
1.1       andrew    164:
1.4       andrew    165:         ok(  $pdb->Encrypt($pdb->{'records'}->[$rec_num], $acct), 'Change record' );
1.1       andrew    166:
1.4       andrew    167:         ok( $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]), 'Decrypt changed record' );
1.1       andrew    168:
1.4       andrew    169:         is_deeply($acct, $decrypted, 'Compare changed record');
1.1       andrew    170:
1.4       andrew    171:         $decrypted = [];
                    172:         ok( $pdb->Password(), 'Forget password' );
1.1       andrew    173:
1.4       andrew    174:         eval{ $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]) };
                    175:         ok($@, 'Don\'t decrypt');
1.1       andrew    176:
1.4       andrew    177:         my $got_password = 'Got nothing';
                    178:         if ($decrypted) {
1.2       andrew    179:         foreach my $field (@{ $decrypted }) {
1.4       andrew    180:         next unless $field->{label} eq 'password';
                    181:         $got_password = $field->{data};
                    182:         }
1.2       andrew    183:         }
1.1       andrew    184:
1.4       andrew    185:         isnt( $got_password, $new_password, 'Didn\'t get new password' );
1.1       andrew    186:
1.4       andrew    187:         ok( unlink($file), 'Remove test pdb v' . $options->{version} );
                    188:         }
1.1       andrew    189: }
                    190:
                    191: 1;

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