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