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

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

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