Annotation of palm/Palm-Keyring/t/keyring5.t, Revision 1.7
1.5 andrew 1: #!/usr/bin/perl -T
1.7 ! andrew 2: # $RedRiver: keyring5.t,v 1.6 2007/02/27 17:08:05 andrew Exp $
1.1 andrew 3: use strict;
4: use warnings;
5:
1.7 ! andrew 6: use Test::More tests => 122;
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.4 andrew 113: SKIP: {
114: if ($cipher > 0) {
115: skip 'Crypt::CBC not installed', 31 unless
116: eval "require Crypt::CBC";
117: skip 'Crypt::' . $crypt->{name} . ' not installed', 31 unless
118: eval "require Crypt::$crypt->{name}";
119: }
120: skip 'Digest::HMAC_SHA1 not installed', 31 unless
121: eval " require Digest::HMAC_SHA1 ";
122:
123: ok( $pdb = new Palm::Keyring($options), 'New Palm::Keyring v'
124: . $options->{version}
125: . ' Cipher '
126: . $options->{cipher}
127: );
128:
129: foreach my $acct (@{ $original_accts} ) {
130: ok( $record = $pdb->append_Record(), 'Append Record' );
1.7 ! andrew 131: ok( $pdb->Encrypt($record, $acct, $password),
! 132: 'Encrypt account into record' );
1.3 andrew 133: }
1.1 andrew 134:
1.4 andrew 135: ok( $pdb->Write($file), 'Write file' );
1.1 andrew 136:
1.4 andrew 137: $pdb = undef;
1.1 andrew 138:
1.4 andrew 139: ok( $pdb = new Palm::PDB(), 'New Palm::PDB' );
1.1 andrew 140:
1.4 andrew 141: ok( $pdb->Load($file), 'Load File' );
1.1 andrew 142:
1.4 andrew 143: ok( $pdb->Password($password), 'Verify Password' );
1.1 andrew 144:
1.7 ! andrew 145: my $rec_id = 0;
1.4 andrew 146: foreach my $rec (@{ $pdb->{records} }) {
1.7 ! andrew 147: ok( $decrypted = $pdb->Decrypt($rec), 'Decrypt record' );
! 148: if ($rec_id == 1) {
! 149: is( $decrypted->{0}->{data}, $original_accts->[1]->{0}->{data},
! 150: 'Checking record name' );
! 151: }
! 152: push @recs, $decrypted;
! 153: $rec_id++;
1.4 andrew 154: }
1.1 andrew 155:
1.4 andrew 156: is_deeply( \@recs, $original_accts, 'Account Matches' );
1.1 andrew 157:
1.4 andrew 158: @recs = ();
159: my $rec_num = 1;
1.1 andrew 160:
1.4 andrew 161: ok( $pdb->Password($password, $new_password), 'Change PDB Password' );
1.1 andrew 162:
1.4 andrew 163: foreach my $rec (@{ $pdb->{records} }) {
1.2 andrew 164: ok( $decrypted = $pdb->Decrypt($rec), 'Decrypt record' );
165: push @recs, $decrypted;
1.4 andrew 166: }
1.1 andrew 167:
1.4 andrew 168: is_deeply( \@recs, $original_accts, 'Account Matches' );
1.1 andrew 169:
1.4 andrew 170: my $acct;
171: ok( $acct = $pdb->Decrypt( $pdb->{records}->[$rec_num]), 'decrypt record ' . $rec_num);
1.1 andrew 172:
1.7 ! andrew 173: ok($acct->{2}->{data} = $new_password, 'Change password');
1.1 andrew 174:
1.7 ! andrew 175: ok( $pdb->Encrypt($pdb->{'records'}->[$rec_num], $acct),
! 176: 'Change record' );
1.1 andrew 177:
1.7 ! andrew 178: ok( $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]),
! 179: 'Decrypt changed record' );
1.1 andrew 180:
1.4 andrew 181: is_deeply($acct, $decrypted, 'Compare changed record');
1.1 andrew 182:
1.7 ! andrew 183: $decrypted = {};
1.4 andrew 184: ok( $pdb->Password(), 'Forget password' );
1.1 andrew 185:
1.4 andrew 186: eval{ $decrypted = $pdb->Decrypt($pdb->{'records'}->[$rec_num]) };
187: ok($@, 'Don\'t decrypt');
1.1 andrew 188:
1.4 andrew 189: my $got_password = 'Got nothing';
190: if ($decrypted) {
1.7 ! andrew 191: $got_password = $decrypted->{2}->{data};
1.2 andrew 192: }
1.1 andrew 193:
1.4 andrew 194: isnt( $got_password, $new_password, 'Didn\'t get new password' );
1.1 andrew 195:
1.4 andrew 196: ok( unlink($file), 'Remove test pdb v' . $options->{version} );
197: }
1.1 andrew 198: }
199:
200: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>