Annotation of palm/Palm-Keyring/t/keyring.t, Revision 1.21
1.12 andrew 1: #!/usr/bin/perl -T
1.21 ! andrew 2: # $RedRiver: keyring.t,v 1.20 2008/09/18 06:06:11 andrew Exp $
1.7 andrew 3: use strict;
4: use warnings;
1.1 andrew 5:
1.21 ! andrew 6: use Test::More tests => 194;
1.15 andrew 7: use Data::Dumper;
1.1 andrew 8:
1.18 andrew 9: BEGIN {
10: use_ok('Palm::PDB');
11: use_ok('Palm::Keyring');
1.9 andrew 12: }
1.1 andrew 13:
1.18 andrew 14: my $file = 'Keys-test.pdb';
15: my $password = '12345';
1.3 andrew 16: my $new_password = '54321';
17:
1.8 andrew 18: my @o = (
1.18 andrew 19: { version => 4,
1.8 andrew 20: password => $password,
21: },
1.18 andrew 22: { version => 5,
23: password => $password,
24: cipher => 1,
1.8 andrew 25: },
26: );
27:
1.19 andrew 28: my $acct = {
29: 0 => {
30: label => 'name',
31: label_id => 0,
32: data => 'test3',
33: font => 0,
34: },
35: 1 => {
36: label => 'account',
37: label_id => 1,
38: data => 'atestaccount',
39: font => 0,
40: },
41: 2 => {
42: label => 'password',
43: label_id => 2,
44: data => $password,
45: font => 0,
46: },
47: 3 => {
48: label => 'lastchange',
49: label_id => 3,
50: data => {
51: day => 2,
52: month => 2,
53: year => 99,
1.14 andrew 54: },
1.19 andrew 55: font => 0,
56: },
57: 255 => {
58: label => 'notes',
59: label_id => 255,
60: data => 'now that really roxorZ!',
61: font => 0,
62: },
63: };
1.9 andrew 64:
1.20 andrew 65: my $bad_cipher = 999;
66: my %crypt_1_details = (
67: 'default_iter' => 1000,
68: 'keylen' => 24,
69: 'blocksize' => 8,
70: 'name' => 'DES_EDE3',
71: 'alias' => 'DES-EDE3',
72: 'DES_odd_parity' => 1
73: );
74:
75: my $bad_label = 999;
76: my $bad_label_name = 'not_a_label_name';
77: my %label_1_details = (
78: id => 1,
79: name => 'account',
80: );
81: my %label_not_found_details = (
82: id => $bad_label,
83: name => undef,
84: );
85:
86: # Crypts
87: is_deeply( Palm::Keyring::crypts(1), \%crypt_1_details, 'Got crypt 1' );
88: is_deeply( Palm::Keyring::crypts('DES-EDE3'),
89: \%crypt_1_details, 'Got crypt DES-EDE3' );
90: is( Palm::Keyring::crypts(), undef, "Didn't get crypt empty cipher" );
91: is( Palm::Keyring::crypts($bad_cipher),
92: undef, "Didn't get crypt $bad_cipher" );
93:
94: # Bad Cipher
95: eval { Palm::Keyring->new( { version => 5, cipher => $bad_cipher } ) };
96: like(
97: $@,
98: qr/^Unknown \s cipher \s $bad_cipher/xms,
99: "Failed to create keyring with cipher $bad_cipher"
100: );
101:
102: # Labels
103: is_deeply( Palm::Keyring::labels(1), \%label_1_details, 'Got label 1' );
104: is_deeply( Palm::Keyring::labels('account'),
105: \%label_1_details, 'Got label account' );
106: is( Palm::Keyring::labels(), undef, "Didn't get label empty label" );
107: is_deeply( Palm::Keyring::labels($bad_label),
108: \%label_not_found_details, "Got default label for $bad_label" );
1.21 ! andrew 109: is( Palm::Keyring::labels($bad_label_name), undef, "Didn't get label for $bad_label_name"
1.20 andrew 110: );
111:
1.21 ! andrew 112:
! 113:
1.20 andrew 114: my $pdb;
1.21 ! andrew 115:
1.20 andrew 116: eval { $pdb = new Palm::Keyring( -file => 't/Keys-invalid_version.pdb' ) };
117: like(
118: $@,
119: qr/^Unsupported \s Version \s 999/xms,
120: 'Couldn\'t load pdb with invalid version'
121: );
122:
123: eval { $pdb = new Palm::Keyring( -file => 't/Keys-invalid_cipher.pdb' ) };
124: like(
125: $@,
126: qr/^Unknown \s cipher \s 999/xms,
127: 'Couldn\'t load pdb with Unknown Cipher'
128: );
129:
130: ok( $pdb = new Palm::Keyring( -file => 't/Keys-no_data.pdb' ),
131: 'Loaded Palm::Keyring file with no data' );
132:
1.21 ! andrew 133: ok( $pdb->Password($password), 'Entering Password' );
! 134:
1.20 andrew 135: my $record;
136: ok( $record = $pdb->append_Record(), 'Append Record' );
137: ok( $pdb->Encrypt( $record, $password, $acct ),
138: 'Encrypt account into record' );
1.21 ! andrew 139: ok( $pdb->PackRecord($record), 'Pack Proper Record');
! 140: ok( $record = $pdb->ParseRecord(%{ $record }), 'Parse Proper Packed');
! 141:
1.20 andrew 142: my $record2;
143: ok( $record2 = $pdb->append_Record(), 'Append Record' );
1.21 ! andrew 144: ok( $pdb->PackRecord($record2), 'Pack Empty Record');
! 145: ok( $record = $pdb->ParseRecord(%{ $record2 }), 'Parse Empty Packed');
1.20 andrew 146:
147:
148: $pdb->{version} = 999;
149: eval{ $pdb->PackRecord($record) };
150: like( $@,
151: qr/^Unsupported \s Version \s 999/xms,
152: 'Couldn\'t PackRecord with Invalid Version'
153: );
154:
155: eval{ $pdb->ParseRecord(%{ $record2 }) };
156: like( $@,
157: qr/^Unsupported \s Version \s 999/xms,
158: 'Couldn\'t ParseRecord with Invalid Version'
159: );
160:
1.21 ! andrew 161: eval { $pdb->Write($file) };
! 162: like(
! 163: $@,
! 164: qr/^Unsupported \s Version \s 999/xms,
! 165: 'Couldn\'t Write file with unsupported version'
! 166: );
! 167:
! 168:
1.20 andrew 169: $pdb = undef;
170:
171: unlink $file;
172:
1.18 andrew 173: foreach my $options (@o) {
1.20 andrew 174: foreach my $config_type ( 'hashref', 'cgi-style', 'list' ) {
175:
176: my $pdb;
177: my $record;
178: my $decrypted;
179:
180: my $Num_Tests_Left = 25;
181: SKIP: {
182: if ( defined $options->{cipher} && $options->{cipher} > 0 ) {
183: my $crypt = Palm::Keyring::crypts( $options->{cipher} );
184: skip 'Crypt::CBC not installed', $Num_Tests_Left
185: unless eval "require Crypt::CBC";
186: if ($crypt) {
187: skip 'Crypt::' . $crypt->{name} . ' not installed',
188: $Num_Tests_Left
189: unless eval "require Crypt::$crypt->{name}";
190: }
191: else {
192: skip 'Unknown Crypt: ' . $options->{cipher},
193: $Num_Tests_Left;
194: }
195: }
196:
197: if ( $options->{version} == 4 ) {
198: skip 'Crypt::DES not installed', $Num_Tests_Left
199: unless eval "require Crypt::DES ";
200: skip 'Digest::MD5 not installed', $Num_Tests_Left
201: unless eval "require Digest::MD5 ";
202: }
203: elsif ( $options->{version} == 5 ) {
204: skip 'Digest::HMAC_SHA1 not installed', $Num_Tests_Left
205: unless eval "require Digest::HMAC_SHA1 ";
206: }
207:
208: my @options = ($options);
209: if ( $config_type eq 'cgi-style' ) {
210: @options = (
211: '-version' => $options->{version},
212: '-password' => $options->{password},
213: );
214: if ( $options->{cipher} ) {
215: push @options, '-cipher', $options->{cipher};
216: }
217: }
218: elsif ( $config_type eq 'list' ) {
219: @options = ( $options->{password}, $options->{version} );
220: if ( $options->{cipher} ) {
221: push @options, $options->{cipher};
222: }
223: }
224:
225: ok( $pdb = new Palm::Keyring(@options),
226: 'new Palm::Keyring v' . $options->{version}
227: );
228:
229: ok( $pdb->Write($file), 'Write "empty" file' );
1.7 andrew 230:
1.20 andrew 231: ok( $record = $pdb->append_Record(), 'Append Record' );
1.7 andrew 232:
1.20 andrew 233: ok( $pdb->Encrypt( $record, $password, $acct ),
234: 'Encrypt account into record' );
1.7 andrew 235:
1.20 andrew 236: ok( $pdb->Write($file), 'Write file' );
1.7 andrew 237:
1.20 andrew 238: $pdb = undef;
1.7 andrew 239:
1.20 andrew 240: ok( $pdb = new Palm::Keyring(), 'new Palm::Keyring' );
1.9 andrew 241:
1.20 andrew 242: ok( $pdb->Load($file), 'Load File' );
1.11 andrew 243:
1.20 andrew 244: ok( $pdb->Password($password), 'Verify Password' );
1.7 andrew 245:
1.20 andrew 246: my $rec_num = 0;
247: ok( $decrypted = $pdb->Decrypt( $pdb->{records}->[$rec_num] ),
248: 'Decrypt record' );
1.7 andrew 249:
1.20 andrew 250: is( $decrypted->{2}->{data}, $password, 'Got password' );
1.7 andrew 251:
1.20 andrew 252: is_deeply( $decrypted, $acct, 'Account Matches' );
1.7 andrew 253:
1.20 andrew 254: my $old_date = $decrypted->{3}->{data};
1.7 andrew 255:
1.20 andrew 256: ok( $pdb->Password( $password, $new_password ),
257: 'Change PDB Password' );
1.1 andrew 258:
1.20 andrew 259: ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ),
260: 'Decrypt with new password' );
1.7 andrew 261:
1.20 andrew 262: my $new_date = $decrypted->{3}->{data};
1.7 andrew 263:
1.20 andrew 264: is_deeply( $old_date, $new_date, 'Date didn\'t change' );
1.7 andrew 265:
1.20 andrew 266: $decrypted->{2}->{data} = $new_password;
1.7 andrew 267:
1.20 andrew 268: $pdb->{records}->[$rec_num]->{plaintext} = $decrypted;
1.4 andrew 269:
1.20 andrew 270: ok( $pdb->Encrypt( $pdb->{'records'}->[$rec_num] ),
271: 'Change record' );
1.4 andrew 272:
1.20 andrew 273: ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ),
274: 'Decrypt new record' );
1.15 andrew 275:
1.20 andrew 276: $new_date = $decrypted->{3}->{data};
1.7 andrew 277:
1.20 andrew 278: my $od = join '/', map { $old_date->{$_} } sort keys %{$old_date};
279: my $nd = join '/', map { $new_date->{$_} } sort keys %{$new_date};
1.7 andrew 280:
1.20 andrew 281: isnt( $od, $nd, 'Date changed' );
1.4 andrew 282:
1.20 andrew 283: is( $decrypted->{2}->{data}, $new_password, 'Got new password' );
1.7 andrew 284:
1.20 andrew 285: my $last_decrypted = $decrypted;
1.7 andrew 286:
1.20 andrew 287: $decrypted = {};
288: ok( $pdb->Password(), 'Forget password' );
1.7 andrew 289:
1.20 andrew 290: eval {
291: $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] );
292: };
293: ok( $@, 'Don\'t decrypt' );
1.15 andrew 294:
1.20 andrew 295: isnt( $decrypted->{password},
296: $new_password, 'Didn\'t get new password' );
1.7 andrew 297:
1.20 andrew 298: ok( $pdb->Unlock($new_password), 'Unlock' );
1.7 andrew 299:
1.20 andrew 300: my @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
1.4 andrew 301:
1.20 andrew 302: is_deeply( $plaintext[0], $last_decrypted, 'Account Matches' );
1.15 andrew 303:
1.20 andrew 304: ok( $pdb->Lock(), 'Lock' );
1.15 andrew 305:
1.20 andrew 306: my $cleared_decrypted = {};
307: $cleared_decrypted->{0} = $last_decrypted->{0};
308: @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
1.15 andrew 309:
1.20 andrew 310: is_deeply( $plaintext[0], $cleared_decrypted, 'Cleared records' );
1.15 andrew 311:
1.20 andrew 312: $pdb->{records}->[0]->{data} = undef;
1.21 ! andrew 313: ok( $pdb->Write($file), 'Write file without data' );
! 314: ok( $pdb->Load($file), 'Load File without data' );
1.15 andrew 315:
1.20 andrew 316: ok( unlink($file), 'Remove test pdb v' . $options->{version} );
1.15 andrew 317:
1.20 andrew 318: }
1.11 andrew 319: }
1.8 andrew 320: }
1.1 andrew 321:
322: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>