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