Annotation of palm/Palm-Keyring/t/keyring5.t, Revision 1.12
1.5 andrew 1: #!/usr/bin/perl -T
1.12 ! andrew 2: # $RedRiver: keyring5.t,v 1.11 2008/09/18 01:50:25 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.12 ! andrew 17: my $original_accts = [
! 18: { 0 => {
! 19: 'label_id' => 0,
! 20: 'data' => '',
! 21: 'label' => 'name',
! 22: 'font' => 0,
! 23: },
! 24: 2 => {
! 25: 'label_id' => 2,
! 26: 'data' => 'only password is set',
! 27: 'label' => 'password',
! 28: 'font' => 0,
! 29: },
! 30: 3 => {
! 31: 'label_id' => 3,
! 32: 'data' => {
! 33: 'month' => 1,
! 34: 'day' => 1,
! 35: 'year' => 107
1.10 andrew 36: },
1.12 ! andrew 37: 'label' => 'lastchange',
! 38: 'font' => 0,
! 39: }
! 40: },
! 41: { 0 => {
! 42: 'label_id' => 0,
! 43: 'data' => 'test',
! 44: 'label' => 'name',
! 45: 'font' => 0,
! 46: },
! 47: 2 => {
! 48: 'label_id' => 2,
! 49: 'data' => 'abcd1234',
! 50: 'label' => 'password',
! 51: 'font' => 0,
! 52: },
! 53: 3 => {
! 54: 'label_id' => 3,
! 55: 'data' => {
! 56: 'month' => 1,
! 57: 'day' => 11,
! 58: 'year' => 107
1.10 andrew 59: },
1.12 ! andrew 60: 'label' => 'lastchange',
! 61: 'font' => 0,
! 62: },
! 63: 255 => {
! 64: 'label_id' => 255,
! 65: 'data' => 'This is a short note.',
! 66: 'label' => 'notes',
! 67: 'font' => 0,
! 68: }
! 69: },
! 70: { 0 => {
! 71: 'label_id' => 0,
! 72: 'data' => '',
! 73: 'label' => 'name',
! 74: 'font' => 0,
! 75: },
! 76: 2 => {
! 77: 'label_id' => 2,
! 78: 'data' => 'password (date is 2/2/07)',
! 79: 'label' => 'password',
! 80: 'font' => 0,
! 81: },
! 82: 3 => {
! 83: 'label_id' => 3,
! 84: 'data' => {
! 85: 'month' => 1,
! 86: 'day' => 2,
! 87: 'year' => 107
1.10 andrew 88: },
1.12 ! andrew 89: 'label' => 'lastchange',
! 90: 'font' => 0,
1.10 andrew 91: }
1.12 ! andrew 92: }
! 93: ];
1.11 andrew 94:
95: foreach my $cipher ( 0 .. 3 ) {
96:
97: #next unless $cipher == 0;
98: my $pdb;
99: my @recs;
100: my $record;
101: my $decrypted;
102:
103: my $crypt = Palm::Keyring::crypts($cipher);
104:
105: my $options = {
106: version => 5,
107: password => $password,
108: cipher => $cipher,
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>