[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.3

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

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