]> Dogcows Code - chaz/p5-File-KDBX/blob - t/keys.t
Prereq Test::More 1.001004_001 to fix broken tests
[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 File::KDBX::Constants qw(:key_file);
11 use File::KDBX::Key;
12 use File::Temp qw(tempfile);
13 use Test::More 1.001004_001;
14
15 subtest 'Primitives' => sub {
16 my $pkey = File::KDBX::Key->new('password');
17 isa_ok $pkey, 'File::KDBX::Key::Password';
18 is $pkey->raw_key, decode_b64('XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg='),
19 'Can calculate raw key from password' or diag encode_b64($pkey->raw_key);
20
21 my $fkey = File::KDBX::Key->new(\'password');
22 isa_ok $fkey, 'File::KDBX::Key::File';
23 is $fkey->raw_key, decode_b64('XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg='),
24 'Can calculate raw key from file' or diag encode_b64($fkey->raw_key);
25
26 my $ckey = File::KDBX::Key->new([
27 $pkey,
28 $fkey,
29 'another password',
30 File::KDBX::Key::File->new(testfile(qw{keys hashed.key})),
31 ]);
32 isa_ok $ckey, 'File::KDBX::Key::Composite';
33 is $ckey->raw_key, decode_b64('FLV8/zOT9mEL8QKkzizq7mJflnb25ITblIPq608MGrk='),
34 'Can calculate raw key from composite' or diag encode_b64($ckey->raw_key);
35 };
36
37 for my $test (
38 [KEY_FILE_TYPE_XML, 'xmlv1.key', 'OF9tj+tfww1kHNWQaJlZWIlBdoTVXOazP8g/vZK7NcI=', '1.0'],
39 [KEY_FILE_TYPE_XML, 'xmlv2.key', 'OF9tj+tfww1kHNWQaJlZWIlBdoTVXOazP8g/vZK7NcI=', '2.0'],
40 [KEY_FILE_TYPE_BINARY, 'binary.key', 'QlkDxuYbDPDpDXdK1470EwVBL+AJBH2gvPA9lxNkFEk='],
41 [KEY_FILE_TYPE_HEX, 'hex.key', 'QlkDxuYbDPDpDXdK1470EwVBL+AJBH2gvPA9lxNkFEk='],
42 [KEY_FILE_TYPE_HASHED, 'hashed.key', '8vAO4mrMeq6iCa1FHeWm/Mj5al8HIv2ajqsqsSeUC6U='],
43 ) {
44 my ($type) = @$test;
45 subtest "Load $type key file" => sub {
46 my ($type, $filename, $expected_key, $version) = @_;
47
48 my $key = File::KDBX::Key::File->new(testfile('keys', $filename));
49 is $key->raw_key, decode_b64($expected_key),
50 "Can calculate raw key from $type file" or diag encode_b64($key->raw_key);
51 is $key->type, $type, "File type is detected as $type";
52 is $key->version, $version, "File version is detected as $version" if defined $version;
53 }, @$test;
54
55 subtest "Save $type key file" => sub {
56 my ($type, $filename, $expected_key, $version) = @_;
57
58 my ($fh, $filepath) = tempfile('keyfile-XXXXXX', TMPDIR => 1, UNLINK => 1);
59 close($fh);
60 note $filepath;
61 my $key = File::KDBX::Key::File->new(
62 filepath => $filepath,
63 type => $type,
64 version => $version,
65 raw_key => decode_b64($expected_key),
66 );
67
68 my $e = exception { $key->save };
69
70 if ($type == KEY_FILE_TYPE_HASHED) {
71 like $e, qr/invalid type/i, "Cannot save $type file";
72 return;
73 }
74 is $e, undef, "Save $type file";
75
76 my $key2 = File::KDBX::Key::File->new($filepath);
77 is $key2->type, $key->type, 'Loaded key file has the same type';
78 is $key2->raw_key, $key->raw_key, 'Loaded key file has the same raw key';
79 }, @$test;
80 }
81
82 subtest 'IO handle key files' => sub {
83 my $buf = 'password';
84 open(my $fh, '<', \$buf) or die "open failed: $!\n";
85
86 my $key = File::KDBX::Key::File->new($fh);
87 is $key->raw_key, decode_b64('XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtg='),
88 'Can calculate raw key from file handle' or diag encode_b64($key->raw_key);
89 is $key->type, 'hashed', 'file type is detected as hashed';
90
91 my ($fh_save, $filepath) = tempfile('keyfile-XXXXXX', TMPDIR => 1, UNLINK => 1);
92 is exception { $key->save(fh => $fh_save, type => KEY_FILE_TYPE_XML) }, undef,
93 'Save key file using IO handle';
94 close($fh_save);
95
96 my $key2 = File::KDBX::Key::File->new($filepath);
97 is $key2->type, KEY_FILE_TYPE_XML, 'Loaded key file has the same type';
98 is $key2->filepath, $filepath, 'Loaded key remembers the filepath';
99 is $key2->raw_key, $key->raw_key, 'Loaded key file has the same raw key';
100 $key2->reload;
101 is $key2->raw_key, $key->raw_key, 'Raw key is the same when reloaded same file';
102
103 my $easy_raw_key = "\1" x 32;
104 $key->init(\$easy_raw_key);
105 $key->save(filepath => $filepath);
106
107 $key2->reload;
108 is $key2->raw_key, "\1" x 32, 'Raw key is changed after reload';
109 };
110
111 subtest 'Key file error handling' => sub {
112 is exception { File::KDBX::Key::File->new }, undef, 'Cannot instantiate uninitialized';
113
114 like exception { File::KDBX::Key::File->init },
115 qr/^Missing key primitive/, 'Throw if no primitive is provided';
116
117 like exception { File::KDBX::Key::File->new(testfile(qw{keys nonexistent})) },
118 qr/^Failed to open key file/, 'Throw if file is missing';
119
120 like exception { File::KDBX::Key::File->new({}) },
121 qr/^Unexpected primitive type/, 'Throw if primitive is the wrong type';
122 };
123
124 done_testing;
This page took 0.048706 seconds and 4 git commands to generate.