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