9 use Crypt
::Misc
0.029 qw(decode_b64 encode_b64);
12 BEGIN { use_ok
'File::KDBX::Key' }
14 subtest
'Primitives' => sub {
15 my $pkey = File
::KDBX
::Key-
>new('password');
16 isa_ok
$pkey, 'File::KDBX::Key::Password';
17 is $pkey->raw_key, decode_b64
('XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg='),
18 'Can calculate raw key from password' or diag encode_b64
($pkey->raw_key);
20 my $fkey = File
::KDBX
::Key-
>new(\'password
');
21 isa_ok $fkey, 'File
::KDBX
::Key
::File
';
22 is $fkey->raw_key, decode_b64('XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg
='),
23 'Can calculate raw key from file
' or diag encode_b64($fkey->raw_key);
25 my $ckey = File::KDBX::Key->new([
29 File::KDBX::Key::File->new(testfile(qw{keys hashed.key})),
31 isa_ok
$ckey, 'File::KDBX::Key::Composite';
32 is $ckey->raw_key, decode_b64
('FLV8/zOT9mEL8QKkzizq7mJflnb25ITblIPq608MGrk='),
33 'Can calculate raw key from composite' or diag encode_b64
($ckey->raw_key);
36 subtest
'File keys' => sub {
37 my $key = File
::KDBX
::Key
::File-
>new(testfile
(qw{keys xmlv1.key}));
38 is $key->raw_key, decode_b64
('OF9tj+tfww1kHNWQaJlZWIlBdoTVXOazP8g/vZK7NcI='),
39 'Can calculate raw key from XML file' or diag encode_b64
($key->raw_key);
40 is $key->type, 'xml', 'file type is detected as xml';
41 is $key->version, '1.0', 'file version is detected as xml';
43 $key = File
::KDBX
::Key
::File-
>new(testfile
(qw{keys xmlv2.key}));
44 is $key->raw_key, decode_b64
('OF9tj+tfww1kHNWQaJlZWIlBdoTVXOazP8g/vZK7NcI='),
45 'Can calculate raw key from XML file' or diag encode_b64
($key->raw_key);
46 is $key->type, 'xml', 'file type is detected as xml';
47 is $key->version, '2.0', 'file version is detected as xml';
49 $key = File
::KDBX
::Key
::File-
>new(testfile
(qw{keys binary.key}));
50 is $key->raw_key, decode_b64
('QlkDxuYbDPDpDXdK1470EwVBL+AJBH2gvPA9lxNkFEk='),
51 'Can calculate raw key from binary file' or diag encode_b64
($key->raw_key);
52 is $key->type, 'binary', 'file type is detected as binary';
54 $key = File
::KDBX
::Key
::File-
>new(testfile
(qw{keys hex.key}));
55 is $key->raw_key, decode_b64
('QlkDxuYbDPDpDXdK1470EwVBL+AJBH2gvPA9lxNkFEk='),
56 'Can calculate raw key from hex file' or diag encode_b64
($key->raw_key);
57 is $key->type, 'hex', 'file type is detected as hex';
59 $key = File
::KDBX
::Key
::File-
>new(testfile
(qw{keys hashed.key}));
60 is $key->raw_key, decode_b64
('8vAO4mrMeq6iCa1FHeWm/Mj5al8HIv2ajqsqsSeUC6U='),
61 'Can calculate raw key from binary file' or diag encode_b64
($key->raw_key);
62 is $key->type, 'hashed', 'file type is detected as hashed';
65 open(my $fh, '<', \
$buf) or die "open failed: $!\n";
67 $key = File
::KDBX
::Key
::File-
>new($fh);
68 is $key->raw_key, decode_b64
('XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg='),
69 'Can calculate raw key from file handle' or diag encode_b64
($key->raw_key);
70 is $key->type, 'hashed', 'file type is detected as hashed';
72 is exception
{ File
::KDBX
::Key
::File-
>new }, undef, 'Can instantiate uninitialized';
74 like exception
{ File
::KDBX
::Key
::File-
>init },
75 qr/^Missing key primitive/, 'Throws if no primitive is provided';
77 like exception
{ File
::KDBX
::Key
::File-
>new(testfile
(qw{keys nonexistent})) },
78 qr/^Failed to open key file/, 'Throws if file is missing';
80 like exception
{ File
::KDBX
::Key
::File-
>new({}) },
81 qr/^Unexpected primitive type/, 'Throws if primitive is the wrong type';