]> Dogcows Code - chaz/p5-File-KDBX/blob - t/keys.t
add initial WIP
[chaz/p5-File-KDBX] / t / keys.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use lib 't/lib';
7 use TestCommon;
8
9 use Crypt::Misc 0.029 qw(decode_b64 encode_b64);
10 use Test::More;
11
12 BEGIN { use_ok 'File::KDBX::Key' }
13
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);
19
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);
24
25 my $ckey = File::KDBX::Key->new([
26 $pkey,
27 $fkey,
28 'another password',
29 File::KDBX::Key::File->new(testfile(qw{keys hashed.key})),
30 ]);
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);
34 };
35
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';
42
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';
48
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';
53
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';
58
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';
63
64 my $buf = 'password';
65 open(my $fh, '<', \$buf) or die "open failed: $!\n";
66
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';
71
72 is exception { File::KDBX::Key::File->new }, undef, 'Can instantiate uninitialized';
73
74 like exception { File::KDBX::Key::File->init },
75 qr/^Missing key primitive/, 'Throws if no primitive is provided';
76
77 like exception { File::KDBX::Key::File->new(testfile(qw{keys nonexistent})) },
78 qr/^Failed to open key file/, 'Throws if file is missing';
79
80 like exception { File::KDBX::Key::File->new({}) },
81 qr/^Unexpected primitive type/, 'Throws if primitive is the wrong type';
82 };
83
84 done_testing;
This page took 0.037074 seconds and 4 git commands to generate.