Annotation of palm/Palm-Keyring/t/keyring.t, Revision 1.22
1.12 andrew 1: #!/usr/bin/perl -T
1.22 ! andrew 2: # $RedRiver: keyring.t,v 1.21 2008/09/19 02:08:01 andrew Exp $
1.7 andrew 3: use strict;
4: use warnings;
1.1 andrew 5:
1.22 ! andrew 6: use Test::More tests => 195;
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.22 ! andrew 144: eval{ $pdb->PackRecord($record2) };
! 145: like(
! 146: $@,
! 147: qr/^No \s encrypted \s data \s in \s record/xms,
! 148: 'Pack Empty Record'
! 149: );
! 150:
! 151: $record2->{encrypted} = q{};
! 152: eval{ $pdb->PackRecord($record2) };
! 153: like(
! 154: $@,
! 155: qr/^No \s ivec/xms,
! 156: 'Pack Empty Record with encrypted, but no ivec'
! 157: );
! 158:
! 159: $pdb->{version} = 4;
! 160: delete $record->{encrypted};
! 161: delete $record->{data};
! 162: eval{ $pdb->PackRecord($record) };
! 163: like( $@,
! 164: qr/^No \s data \s in \s record \s to \s pack/xms,
! 165: 'Couldn\'t PackRecord without data'
! 166: );
1.20 andrew 167:
1.22 ! andrew 168: $pdb->{version} = 999;
! 169: eval { $pdb->Write($file) };
! 170: like(
! 171: $@,
! 172: qr/^Unsupported \s Version \s 999/xms,
! 173: 'Couldn\'t Write file with unsupported version'
! 174: );
1.20 andrew 175:
176: eval{ $pdb->PackRecord($record) };
177: like( $@,
178: qr/^Unsupported \s Version \s 999/xms,
179: 'Couldn\'t PackRecord with Invalid Version'
180: );
181:
1.22 ! andrew 182: $record2->{data} = q{nothing};
1.20 andrew 183: eval{ $pdb->ParseRecord(%{ $record2 }) };
184: like( $@,
185: qr/^Unsupported \s Version \s 999/xms,
186: 'Couldn\'t ParseRecord with Invalid Version'
1.21 andrew 187: );
188:
189:
1.20 andrew 190: $pdb = undef;
191:
192: unlink $file;
193:
1.18 andrew 194: foreach my $options (@o) {
1.20 andrew 195: foreach my $config_type ( 'hashref', 'cgi-style', 'list' ) {
196:
197: my $pdb;
198: my $record;
199: my $decrypted;
200:
201: my $Num_Tests_Left = 25;
202: SKIP: {
203: if ( defined $options->{cipher} && $options->{cipher} > 0 ) {
204: my $crypt = Palm::Keyring::crypts( $options->{cipher} );
205: skip 'Crypt::CBC not installed', $Num_Tests_Left
206: unless eval "require Crypt::CBC";
207: if ($crypt) {
208: skip 'Crypt::' . $crypt->{name} . ' not installed',
209: $Num_Tests_Left
210: unless eval "require Crypt::$crypt->{name}";
211: }
212: else {
213: skip 'Unknown Crypt: ' . $options->{cipher},
214: $Num_Tests_Left;
215: }
216: }
217:
218: if ( $options->{version} == 4 ) {
219: skip 'Crypt::DES not installed', $Num_Tests_Left
220: unless eval "require Crypt::DES ";
221: skip 'Digest::MD5 not installed', $Num_Tests_Left
222: unless eval "require Digest::MD5 ";
223: }
224: elsif ( $options->{version} == 5 ) {
225: skip 'Digest::HMAC_SHA1 not installed', $Num_Tests_Left
226: unless eval "require Digest::HMAC_SHA1 ";
227: }
228:
229: my @options = ($options);
230: if ( $config_type eq 'cgi-style' ) {
231: @options = (
232: '-version' => $options->{version},
233: '-password' => $options->{password},
234: );
235: if ( $options->{cipher} ) {
236: push @options, '-cipher', $options->{cipher};
237: }
238: }
239: elsif ( $config_type eq 'list' ) {
240: @options = ( $options->{password}, $options->{version} );
241: if ( $options->{cipher} ) {
242: push @options, $options->{cipher};
243: }
244: }
245:
246: ok( $pdb = new Palm::Keyring(@options),
247: 'new Palm::Keyring v' . $options->{version}
248: );
249:
250: ok( $pdb->Write($file), 'Write "empty" file' );
1.7 andrew 251:
1.20 andrew 252: ok( $record = $pdb->append_Record(), 'Append Record' );
1.7 andrew 253:
1.20 andrew 254: ok( $pdb->Encrypt( $record, $password, $acct ),
255: 'Encrypt account into record' );
1.7 andrew 256:
1.20 andrew 257: ok( $pdb->Write($file), 'Write file' );
1.7 andrew 258:
1.20 andrew 259: $pdb = undef;
1.7 andrew 260:
1.20 andrew 261: ok( $pdb = new Palm::Keyring(), 'new Palm::Keyring' );
1.9 andrew 262:
1.20 andrew 263: ok( $pdb->Load($file), 'Load File' );
1.11 andrew 264:
1.20 andrew 265: ok( $pdb->Password($password), 'Verify Password' );
1.7 andrew 266:
1.20 andrew 267: my $rec_num = 0;
268: ok( $decrypted = $pdb->Decrypt( $pdb->{records}->[$rec_num] ),
269: 'Decrypt record' );
1.7 andrew 270:
1.20 andrew 271: is( $decrypted->{2}->{data}, $password, 'Got password' );
1.7 andrew 272:
1.20 andrew 273: is_deeply( $decrypted, $acct, 'Account Matches' );
1.7 andrew 274:
1.20 andrew 275: my $old_date = $decrypted->{3}->{data};
1.7 andrew 276:
1.20 andrew 277: ok( $pdb->Password( $password, $new_password ),
278: 'Change PDB Password' );
1.1 andrew 279:
1.20 andrew 280: ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ),
281: 'Decrypt with new password' );
1.7 andrew 282:
1.20 andrew 283: my $new_date = $decrypted->{3}->{data};
1.7 andrew 284:
1.20 andrew 285: is_deeply( $old_date, $new_date, 'Date didn\'t change' );
1.7 andrew 286:
1.20 andrew 287: $decrypted->{2}->{data} = $new_password;
1.7 andrew 288:
1.20 andrew 289: $pdb->{records}->[$rec_num]->{plaintext} = $decrypted;
1.4 andrew 290:
1.20 andrew 291: ok( $pdb->Encrypt( $pdb->{'records'}->[$rec_num] ),
292: 'Change record' );
1.4 andrew 293:
1.20 andrew 294: ok( $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] ),
295: 'Decrypt new record' );
1.15 andrew 296:
1.20 andrew 297: $new_date = $decrypted->{3}->{data};
1.7 andrew 298:
1.20 andrew 299: my $od = join '/', map { $old_date->{$_} } sort keys %{$old_date};
300: my $nd = join '/', map { $new_date->{$_} } sort keys %{$new_date};
1.7 andrew 301:
1.20 andrew 302: isnt( $od, $nd, 'Date changed' );
1.4 andrew 303:
1.20 andrew 304: is( $decrypted->{2}->{data}, $new_password, 'Got new password' );
1.7 andrew 305:
1.20 andrew 306: my $last_decrypted = $decrypted;
1.7 andrew 307:
1.20 andrew 308: $decrypted = {};
309: ok( $pdb->Password(), 'Forget password' );
1.7 andrew 310:
1.20 andrew 311: eval {
312: $decrypted = $pdb->Decrypt( $pdb->{'records'}->[$rec_num] );
313: };
314: ok( $@, 'Don\'t decrypt' );
1.15 andrew 315:
1.20 andrew 316: isnt( $decrypted->{password},
317: $new_password, 'Didn\'t get new password' );
1.7 andrew 318:
1.20 andrew 319: ok( $pdb->Unlock($new_password), 'Unlock' );
1.7 andrew 320:
1.20 andrew 321: my @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
1.4 andrew 322:
1.20 andrew 323: is_deeply( $plaintext[0], $last_decrypted, 'Account Matches' );
1.15 andrew 324:
1.20 andrew 325: ok( $pdb->Lock(), 'Lock' );
1.15 andrew 326:
1.20 andrew 327: my $cleared_decrypted = {};
328: $cleared_decrypted->{0} = $last_decrypted->{0};
329: @plaintext = map { $_->{plaintext} } @{ $pdb->{records} };
1.15 andrew 330:
1.20 andrew 331: is_deeply( $plaintext[0], $cleared_decrypted, 'Cleared records' );
1.15 andrew 332:
1.20 andrew 333: $pdb->{records}->[0]->{data} = undef;
1.21 andrew 334: ok( $pdb->Write($file), 'Write file without data' );
335: ok( $pdb->Load($file), 'Load File without data' );
1.15 andrew 336:
1.20 andrew 337: ok( unlink($file), 'Remove test pdb v' . $options->{version} );
1.15 andrew 338:
1.20 andrew 339: }
1.11 andrew 340: }
1.8 andrew 341: }
1.1 andrew 342:
343: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>