2012-11-15 09:18:27 +01:00
|
|
|
#!perl -T
|
|
|
|
|
#
|
2015-02-12 15:18:18 +01:00
|
|
|
# testscript for Crypt::PWSafe3 Classes by T.v.Dein
|
2012-07-20 12:58:07 +02:00
|
|
|
#
|
|
|
|
|
# needs to be invoked using the command "make test" from
|
|
|
|
|
# the Crypt::PWSafe3 source directory.
|
|
|
|
|
#
|
|
|
|
|
# Under normal circumstances every test should succeed.
|
2015-02-12 15:18:18 +01:00
|
|
|
#
|
|
|
|
|
# Licensed under the terms of the Artistic License 2.0
|
|
|
|
|
# see: http://www.perlfoundation.org/artistic_license_2_0
|
|
|
|
|
#
|
2012-07-20 12:58:07 +02:00
|
|
|
|
|
|
|
|
use Data::Dumper;
|
2015-05-21 11:10:42 +02:00
|
|
|
use Test::More tests => 13;
|
2015-02-16 11:41:35 +01:00
|
|
|
#use Test::More qw(no_plan);
|
2012-07-20 12:58:07 +02:00
|
|
|
|
2015-02-17 10:27:55 +01:00
|
|
|
|
2015-02-16 11:41:35 +01:00
|
|
|
my %params = (create => 0, password => 'tom');
|
|
|
|
|
|
|
|
|
|
my %record = (
|
|
|
|
|
user => 'u3',
|
|
|
|
|
passwd => 'p3',
|
|
|
|
|
group => 'g3',
|
|
|
|
|
title => 't3',
|
|
|
|
|
notes => 'n3'
|
|
|
|
|
);
|
|
|
|
|
|
2015-02-17 10:27:55 +01:00
|
|
|
|
|
|
|
|
|
2015-02-16 11:41:35 +01:00
|
|
|
sub rdpw {
|
|
|
|
|
my $file = shift;
|
|
|
|
|
my $vault = Crypt::PWSafe3->new(file => $file, %params) or die "$!";
|
|
|
|
|
return $vault;
|
|
|
|
|
}
|
2012-07-20 12:58:07 +02:00
|
|
|
|
|
|
|
|
### 1
|
|
|
|
|
# load module
|
|
|
|
|
BEGIN { use_ok "Crypt::PWSafe3"};
|
|
|
|
|
require_ok( 'Crypt::PWSafe3' );
|
|
|
|
|
|
2015-05-21 11:10:42 +02:00
|
|
|
{
|
|
|
|
|
# I'm going to replace the secure random number generator
|
|
|
|
|
# backends with this very primitive and insecure one, because
|
|
|
|
|
# these are only unit tests and because we use external modules
|
|
|
|
|
# for the purpose anyway (which are not to be tested with these
|
|
|
|
|
# unit tests).
|
|
|
|
|
# This has to be done so that unit tests running on cpantesters
|
|
|
|
|
# don't block if we use a real (and exhausted) random source,
|
|
|
|
|
# which has reportedly happened in the past.
|
|
|
|
|
# ***** CAUTION: DO NOT USE THIS CODE IN PRODUCTION. EVER. ****
|
|
|
|
|
no warnings 'redefine';
|
|
|
|
|
*Crypt::PWSafe3::random = sub { return join'',map{chr(int(rand(255)))}(1..$_[1]); };
|
|
|
|
|
};
|
2015-02-17 10:27:55 +01:00
|
|
|
|
2012-07-20 12:58:07 +02:00
|
|
|
### 2
|
|
|
|
|
# open vault and read in all records
|
|
|
|
|
eval {
|
2015-02-16 11:41:35 +01:00
|
|
|
my $vault = &rdpw('t/tom.psafe3');
|
2012-07-20 12:58:07 +02:00
|
|
|
my @r = $vault->getrecords;
|
|
|
|
|
my $got = 0;
|
|
|
|
|
foreach my $rec (@r) {
|
|
|
|
|
if ($rec->uuid) {
|
|
|
|
|
$got++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (! $got) {
|
|
|
|
|
die "No records found in test database";
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-02-16 11:41:35 +01:00
|
|
|
ok(!$@, "open a pwsafe3 database ($@)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 1a
|
|
|
|
|
# create a new vault
|
|
|
|
|
my %rdata1a;
|
|
|
|
|
my $fd = File::Temp->new(TEMPLATE => '.myvaultXXXXXXXX', TMPDIR => 1, EXLOCK => 0) or die "Could not open tmpfile: $!\n";
|
|
|
|
|
my $tmpfile = "$fd";
|
|
|
|
|
close($fd);
|
|
|
|
|
|
|
|
|
|
eval {
|
2015-05-21 11:10:42 +02:00
|
|
|
my $vault = Crypt::PWSafe3->new(file => $tmpfile, password => 'tom') or die "$!";
|
2015-02-16 11:41:35 +01:00
|
|
|
$vault->newrecord(%record);
|
|
|
|
|
$vault->save();
|
|
|
|
|
};
|
|
|
|
|
ok(!$@, "create a new pwsafe3 database ($@)");
|
|
|
|
|
|
|
|
|
|
eval {
|
|
|
|
|
my $rvault1a = &rdpw($tmpfile);
|
|
|
|
|
my $rec1a = ($rvault1a->getrecords())[0];
|
|
|
|
|
foreach my $name (keys %record) {
|
|
|
|
|
$rdata1a{$name} = $rec1a->$name();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ok(!$@, "read created new pwsafe3 database ($@)");
|
|
|
|
|
is_deeply(\%record, \%rdata1a, "Write record to a new pwsafe3 database");
|
|
|
|
|
unlink($tmpfile);
|
2012-07-20 12:58:07 +02:00
|
|
|
|
|
|
|
|
### 3
|
|
|
|
|
# modify an existing record
|
|
|
|
|
my $uuid3;
|
|
|
|
|
my %rdata3;
|
|
|
|
|
my $rec3;
|
2015-02-16 11:41:35 +01:00
|
|
|
|
2012-07-20 12:58:07 +02:00
|
|
|
eval {
|
2015-02-16 11:41:35 +01:00
|
|
|
my $vault3 = &rdpw('t/tom.psafe3');
|
2012-07-20 12:58:07 +02:00
|
|
|
foreach my $uuid ($vault3->looprecord) {
|
|
|
|
|
$uuid3 = $uuid;
|
2015-02-16 11:41:35 +01:00
|
|
|
$vault3->modifyrecord($uuid3, %record);
|
2012-07-20 12:58:07 +02:00
|
|
|
last;
|
|
|
|
|
}
|
|
|
|
|
$vault3->save(file=>'t/3.out');
|
|
|
|
|
|
2015-02-16 11:41:35 +01:00
|
|
|
my $rvault3 = &rdpw('t/3.out');
|
2012-07-20 12:58:07 +02:00
|
|
|
$rec3 = $rvault3->getrecord($uuid3);
|
2015-02-16 11:41:35 +01:00
|
|
|
|
|
|
|
|
foreach my $name (keys %record) {
|
2012-07-20 12:58:07 +02:00
|
|
|
$rdata3{$name} = $rec3->$name();
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-05-21 11:10:42 +02:00
|
|
|
ok(!$@, "read a pwsafe3 database and change a record, traditional method ($@)");
|
|
|
|
|
is_deeply(\%record, \%rdata3, "Change a record an check if changes persist after saving, traditional method");
|
|
|
|
|
diag("3 done\n");
|
|
|
|
|
|
|
|
|
|
### 3a
|
|
|
|
|
# modify an existing record, new method
|
|
|
|
|
my $uuid3a;
|
|
|
|
|
my %rdata3a;
|
|
|
|
|
my $rec3a;
|
|
|
|
|
|
|
|
|
|
eval {
|
|
|
|
|
my $vault3a = &rdpw('t/tom.psafe3');
|
|
|
|
|
foreach my $rec ($vault3a->getrecords) {
|
|
|
|
|
$rec->notes('n3a');
|
|
|
|
|
$uuid3a = $rec->uuid;
|
|
|
|
|
last;
|
|
|
|
|
}
|
|
|
|
|
$vault3a->save(file=>'t/3a.out');
|
|
|
|
|
|
|
|
|
|
my $rvault3a = &rdpw('t/3a.out');
|
|
|
|
|
$rec3a = $rvault3a->getrecord($uuid3a);
|
|
|
|
|
};
|
|
|
|
|
ok(!$@, "read a pwsafe3 database and change a record, new method ($@)");
|
|
|
|
|
is_deeply($rec3a->notes, 'n3a', "Change a record an check if changes persist after saving, new method");
|
2012-07-20 12:58:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
### 4
|
|
|
|
|
# re-use $rec3 and change it the oop way
|
|
|
|
|
my $rec4;
|
|
|
|
|
eval {
|
2015-02-16 11:41:35 +01:00
|
|
|
my $vault4 = &rdpw('t/tom.psafe3');
|
2012-07-20 12:58:07 +02:00
|
|
|
$rec4 = $vault4->getrecord($uuid3);
|
2015-02-16 11:41:35 +01:00
|
|
|
|
2012-07-20 12:58:07 +02:00
|
|
|
$rec4->user("u4");
|
|
|
|
|
$rec4->passwd("p4");
|
|
|
|
|
|
|
|
|
|
$vault4->addrecord($rec4);
|
|
|
|
|
$vault4->markmodified();
|
|
|
|
|
$vault4->save(file=>'t/4.out');
|
|
|
|
|
|
2015-02-16 11:41:35 +01:00
|
|
|
my $rvault4 = &rdpw('t/4.out');
|
2012-07-20 12:58:07 +02:00
|
|
|
$rec4 = $rvault4->getrecord($uuid3);
|
|
|
|
|
if ($rec4->user ne 'u4') {
|
|
|
|
|
die "oop way record change failed";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ok(!$@, "re-use record and change it the oop way\n" . $@ . "\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 5 modify some header fields
|
|
|
|
|
eval {
|
2015-02-16 11:41:35 +01:00
|
|
|
my $vault5 = &rdpw('t/tom.psafe3');
|
2012-07-20 12:58:07 +02:00
|
|
|
|
|
|
|
|
my $h3 = new Crypt::PWSafe3::HeaderField(name => 'savedonhost', value => 'localhost');
|
|
|
|
|
|
|
|
|
|
$vault5->addheader($h3);
|
|
|
|
|
$vault5->markmodified();
|
|
|
|
|
$vault5->save(file=>'t/5.out');
|
|
|
|
|
|
2015-02-16 11:41:35 +01:00
|
|
|
my $rvault5 = &rdpw('t/5.out');
|
2012-07-20 12:58:07 +02:00
|
|
|
|
|
|
|
|
if ($rvault5->getheader('savedonhost')->value() ne 'localhost') {
|
|
|
|
|
die "header savedonhost not correct";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ok(!$@, "modify some header fields ($@)");
|
2012-11-15 09:26:18 +01:00
|
|
|
|
2012-11-18 01:34:38 +01:00
|
|
|
### 6 delete
|
|
|
|
|
eval {
|
2015-02-16 11:41:35 +01:00
|
|
|
my $vault6 = &rdpw('t/3.out');
|
2012-11-18 01:34:38 +01:00
|
|
|
my $uuid = $vault6->newrecord(user => 'xxx', passwd => 'y');
|
|
|
|
|
$vault6->save(file=>'t/6.out');
|
|
|
|
|
|
2015-02-16 11:41:35 +01:00
|
|
|
my $rvault6 = &rdpw('t/6.out');
|
2012-11-18 01:34:38 +01:00
|
|
|
my $rec = $rvault6->getrecord($uuid);
|
|
|
|
|
if ($rec->user ne 'xxx') {
|
|
|
|
|
die "oop way record change failed";
|
|
|
|
|
}
|
|
|
|
|
$rvault6->deleterecord($uuid);
|
|
|
|
|
if ($rvault6->getrecord($uuid)) {
|
|
|
|
|
die "deleted record still present in open vault";
|
|
|
|
|
}
|
|
|
|
|
$vault6->save(file=>'t/6a.out');
|
2015-02-16 11:41:35 +01:00
|
|
|
|
|
|
|
|
my $rvault6a = &rdpw('t/6a.out');
|
2012-11-18 01:34:38 +01:00
|
|
|
if ($rvault6->getrecord($uuid)) {
|
|
|
|
|
die "deleted record reappears after save and reload";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ok(!$@, "delete record\n" . $@ . "\n");
|
|
|
|
|
|
|
|
|
|
|
2012-11-15 09:26:18 +01:00
|
|
|
### clean temporary files
|
|
|
|
|
unlink('t/3.out');
|
|
|
|
|
unlink('t/4.out');
|
|
|
|
|
unlink('t/5.out');
|
2012-11-19 21:55:09 +01:00
|
|
|
unlink('t/6.out');
|
|
|
|
|
unlink('t/6a.out');
|