Annotation of palm/Palm-Keyring/t/keyring5.t, Revision 1.9
1.5 andrew 1: #!/usr/bin/perl -T
1.9 ! andrew 2: # $RedRiver: keyring5.t,v 1.8 2007/09/12 02:44:36 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.9 ! andrew 113: my $Num_Tests_Left = 34;
1.4 andrew 114: SKIP: {
115: if ($cipher > 0) {
1.9 ! andrew 116: skip 'Crypt::CBC not installed', $Num_Tests_Left
! 117: unless eval "require Crypt::CBC";
! 118: skip 'Crypt::' . $crypt->{name} . ' not installed', $Num_Tests_Left
! 119: unless eval "require Crypt::$crypt->{name}";
1.4 andrew 120: }
1.9 ! andrew 121: skip 'Digest::HMAC_SHA1 not installed', $Num_Tests_Left
! 122: unless eval "require Digest::HMAC_SHA1";
1.4 andrew 123:
124: ok( $pdb = new Palm::Keyring($options), 'New Palm::Keyring v'
125: . $options->{version}
126: . ' Cipher '
127: . $options->{cipher}
128: );
129:
130: foreach my $acct (@{ $original_accts} ) {
131: ok( $record = $pdb->append_Record(), 'Append Record' );
1.8 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.4 andrew 147: foreach my $rec (@{ $pdb->{records} }) {
1.7 andrew 148: ok( $decrypted = $pdb->Decrypt($rec), 'Decrypt record' );
149: if ($rec_id == 1) {
150: is( $decrypted->{0}->{data}, $original_accts->[1]->{0}->{data},
151: 'Checking record name' );
152: }
153: push @recs, $decrypted;
154: $rec_id++;
1.4 andrew 155: }
1.1 andrew 156:
1.4 andrew 157: is_deeply( \@recs, $original_accts, 'Account Matches' );
1.1 andrew 158:
1.4 andrew 159: @recs = ();
160: my $rec_num = 1;
1.1 andrew 161:
1.4 andrew 162: ok( $pdb->Password($password, $new_password), 'Change PDB Password' );
1.1 andrew 163:
1.4 andrew 164: foreach my $rec (@{ $pdb->{records} }) {
1.2 andrew 165: ok( $decrypted = $pdb->Decrypt($rec), 'Decrypt record' );
166: push @recs, $decrypted;
1.4 andrew 167: }
1.1 andrew 168:
1.4 andrew 169: is_deeply( \@recs, $original_accts, 'Account Matches' );
1.1 andrew 170:
1.4 andrew 171: my $acct;
172: ok( $acct = $pdb->Decrypt( $pdb->{records}->[$rec_num]), 'decrypt record ' . $rec_num);
1.1 andrew 173:
1.7 andrew 174: ok($acct->{2}->{data} = $new_password, 'Change password');
1.1 andrew 175:
1.8 andrew 176: $pdb->{records}->[$rec_num]->{plaintext} = $acct;
177: $recs[$rec_num] = $acct;
178:
179: ok( $pdb->Encrypt($pdb->{'records'}->[$rec_num]), 'Change record');
1.1 andrew 180:
1.7 andrew 181: ok( $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]),
182: 'Decrypt changed record' );
1.1 andrew 183:
1.4 andrew 184: is_deeply($acct, $decrypted, 'Compare changed record');
1.1 andrew 185:
1.8 andrew 186: my $last_decrypted = $decrypted;
1.7 andrew 187: $decrypted = {};
1.4 andrew 188: ok( $pdb->Password(), 'Forget password' );
1.1 andrew 189:
1.4 andrew 190: eval{ $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]) };
191: ok($@, 'Don\'t decrypt');
1.1 andrew 192:
1.4 andrew 193: my $got_password = 'Got nothing';
194: if ($decrypted) {
1.7 andrew 195: $got_password = $decrypted->{2}->{data};
1.2 andrew 196: }
1.1 andrew 197:
1.4 andrew 198: isnt( $got_password, $new_password, 'Didn\'t get new password' );
1.8 andrew 199:
200: ok( $pdb->Unlock($new_password), 'Unlock' );
201:
202: my @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
203:
204: is_deeply( \@plaintext, \@recs, 'Account Matches' );
205:
206: ok( $pdb->Lock(), 'Lock' );
207:
208: my @cleared = map { { 0 => $_->{0} } } @recs;
209: @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
210:
211: is_deeply( \@plaintext, \@cleared, 'Cleared records' );
1.1 andrew 212:
1.4 andrew 213: ok( unlink($file), 'Remove test pdb v' . $options->{version} );
214: }
1.1 andrew 215: }
216:
217: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>