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

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

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