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

1.12      andrew      1: #!/usr/bin/perl -T
1.15    ! andrew      2: # $RedRiver: keyring.t,v 1.14 2007/08/10 04:13:31 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.11      andrew     72:     SKIP: {
                     73:         if (defined $options->{cipher} && $options->{cipher} > 0) {
                     74:             my $crypt = Palm::Keyring::crypts($options->{cipher});
                     75:             skip 'Crypt::CBC not installed', 21 unless
                     76:                 eval "require Crypt::CBC";
                     77:             skip 'Crypt::' . $crypt->{name} . ' not installed', 21 unless
                     78:                 eval "require Crypt::$crypt->{name}";
                     79:         }
1.7       andrew     80:
1.11      andrew     81:         if ($options->{version} == 4) {
                     82:             skip 'Crypt::DES not installed', 21 unless
                     83:                 eval " require Crypt::DES ";
                     84:             skip 'Digest::MD5 not installed', 21 unless
                     85:                 eval " require Digest::MD5 ";
                     86:         } elsif ($options->{version} == 5) {
                     87:             skip 'Digest::HMAC_SHA1 not installed', 21 unless
                     88:                 eval " require Digest::HMAC_SHA1 ";
                     89:         }
1.7       andrew     90:
1.11      andrew     91:         ok( $pdb = new Palm::Keyring($options),
1.14      andrew     92:             'new Palm::Keyring v' . $options->{version});
1.7       andrew     93:
1.11      andrew     94:         ok( $record = $pdb->append_Record(), 'Append Record' );
1.7       andrew     95:
1.15    ! andrew     96:         ok( $pdb->Encrypt($record, $password, $acct),
1.14      andrew     97:             'Encrypt account into record' );
1.7       andrew     98:
1.11      andrew     99:         ok( $pdb->Write($file), 'Write file' );
1.9       andrew    100:
1.11      andrew    101:         $pdb = undef;
                    102:
1.14      andrew    103:         ok( $pdb = new Palm::PDB(), 'new Palm::Keyring' );
1.7       andrew    104:
1.11      andrew    105:         ok( $pdb->Load($file), 'Load File' );
1.7       andrew    106:
1.11      andrew    107:         ok( $pdb->Password($password), 'Verify Password' );
1.7       andrew    108:
1.14      andrew    109:         my $rec_num = 0;
                    110:         ok( $decrypted = $pdb->Decrypt($pdb->{records}->[$rec_num]),
                    111:             'Decrypt record' );
1.7       andrew    112:
1.14      andrew    113:         is( $decrypted->{2}->{data}, $password, 'Got password' );
1.7       andrew    114:
1.11      andrew    115:         is_deeply( $decrypted, $acct, 'Account Matches' );
1.1       andrew    116:
1.14      andrew    117:         my $old_date = $decrypted->{3}->{data};
1.7       andrew    118:
1.11      andrew    119:         ok( $pdb->Password($password, $new_password), 'Change PDB Password' );
1.7       andrew    120:
1.14      andrew    121:         ok( $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]),
                    122:             'Decrypt with new password' );
1.7       andrew    123:
1.14      andrew    124:         my $new_date = $decrypted->{3}->{data};
1.7       andrew    125:
1.11      andrew    126:         is_deeply( $old_date, $new_date, 'Date didn\'t change' );
1.4       andrew    127:
1.14      andrew    128:         $acct->{2}->{data} = $new_password;
1.4       andrew    129:
1.15    ! andrew    130:         $pdb->{records}->[$rec_num]->{plaintext} = $acct;
        !           131:
        !           132:         ok(  $pdb->Encrypt($pdb->{'records'}->[$rec_num]), 'Change record' );
1.7       andrew    133:
1.14      andrew    134:         ok( $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]),
                    135:             'Decrypt new record' );
1.7       andrew    136:
1.14      andrew    137:         $new_date = $decrypted->{3}->{data};
1.4       andrew    138:
1.11      andrew    139:         my $od = join '/', map { $old_date->{$_} } sort keys %{ $old_date };
                    140:         my $nd = join '/', map { $new_date->{$_} } sort keys %{ $new_date };
1.7       andrew    141:
1.11      andrew    142:         isnt( $od, $nd, 'Date changed');
1.7       andrew    143:
1.14      andrew    144:         is( $decrypted->{2}->{data}, $new_password, 'Got new password' );
1.7       andrew    145:
1.15    ! andrew    146:         my $last_decrypted = $decrypted;
        !           147:
1.11      andrew    148:         $decrypted = {};
                    149:         ok( $pdb->Password(), 'Forget password' );
1.7       andrew    150:
1.11      andrew    151:         eval{ $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]) };
                    152:         ok( $@, 'Don\'t decrypt' );
1.7       andrew    153:
1.11      andrew    154:         isnt( $decrypted->{password}, $new_password, 'Didn\'t get new password' );
1.4       andrew    155:
1.15    ! andrew    156:         ok( $pdb->Unlock($new_password), 'Unlock' );
        !           157:
        !           158:         my @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
        !           159:
        !           160:         is_deeply( $plaintext[0], $last_decrypted, 'Account Matches' );
        !           161:
        !           162:         ok( $pdb->Lock(), 'Lock' );
        !           163:
        !           164:         my $cleared_decrypted = {};
        !           165:         $cleared_decrypted->{0}= $last_decrypted->{0};
        !           166:         @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
        !           167:
        !           168:         is_deeply( $plaintext[0], $cleared_decrypted, 'Cleared records' );
        !           169:
1.11      andrew    170:         ok( unlink($file), 'Remove test pdb v' . $options->{version} );
1.15    ! andrew    171:
1.11      andrew    172:     }
1.8       andrew    173: }
1.1       andrew    174:
                    175: 1;

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