11 use File
::KDBX
::Constants
qw(:version :kdf);
13 use Test
::More
1.001004_001
;
16 subtest
'Verify Format400' => sub {
17 my $kdbx = File
::KDBX-
>load(testfile
('Format400.kdbx'), 't');
20 ok_magic
$kdbx, KDBX_VERSION_4_0
, 'Get the correct KDBX4 file magic';
22 cmp_deeply
$kdbx->headers, {
23 cipher_id
=> "\326\3\212+\213oL\265\245\$3\2321\333\265\232",
24 compression_flags
=> 1,
25 encryption_iv
=> "3?\207P\233or\220\215h\2240",
27 "\$UUID" => "\357cm\337\214)DK\221\367\251\244\3\343\n\f",
31 S
=> "V\254\6m-\206*\260\305\f\0\366\24:4\235\364A\362\346\221\13)}\250\217P\303\303\2\331\245",
34 master_seed
=> ";\372y\300yS%\3331\177\231\364u\265Y\361\225\3273h\332R,\22\240a\240\302\271\357\313\23",
35 }, 'Extract headers' or diag explain
$kdbx->headers;
37 is $kdbx->meta->{database_name
}, 'Format400', 'Extract database name from meta';
38 is $kdbx->root->name, 'Format400', 'Extract name of root group';
40 my ($entry, @other) = $kdbx->entries->grep(\'400', 'title
')->each;
41 is scalar @other, 0, 'Database
has one entry
';
43 is $entry->title, 'Format400
', 'Entry
is titled
';
44 is $entry->username, 'Format400
', 'Entry
has a username set
';
45 is keys %{$entry->strings}, 6, 'Entry
has six strings
';
46 is $entry->string_value('Format400
'), 'Format400
', 'Entry
has a custom string
';
47 is keys %{$entry->binaries}, 1, 'Entry
has one binary
';
48 is $entry->binary_value('Format400
'), "Format400\n", 'Entry
has a binary string
';
51 subtest 'KDBX4 upgrade
' => sub {
52 my $kdbx = File::KDBX->new;
54 $kdbx->kdf_parameters->{+KDF_PARAM_UUID} = KDF_UUID_AES_CHALLENGE_RESPONSE;
55 is $kdbx->minimum_version, KDBX_VERSION_4_0, 'AES challenge-response KDF requires upgrade
';
56 $kdbx->kdf_parameters->{+KDF_PARAM_UUID} = KDF_UUID_ARGON2D;
57 is $kdbx->minimum_version, KDBX_VERSION_4_0, 'Argon2D KDF requires upgrade
';
58 $kdbx->kdf_parameters->{+KDF_PARAM_UUID} = KDF_UUID_ARGON2ID;
59 is $kdbx->minimum_version, KDBX_VERSION_4_0, 'Argon2ID KDF requires upgrade
';
60 $kdbx->kdf_parameters->{+KDF_PARAM_UUID} = KDF_UUID_AES;
61 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
63 $kdbx->public_custom_data->{foo} = 42;
64 is $kdbx->minimum_version, KDBX_VERSION_4_0, 'Public custom data requires upgrade
';
65 delete $kdbx->public_custom_data->{foo};
66 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
68 my $entry = $kdbx->add_entry;
69 $entry->custom_data(foo => 'bar
');
70 is $kdbx->minimum_version, KDBX_VERSION_4_0, 'Entry custom data requires upgrade
';
71 delete $entry->custom_data->{foo};
72 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
74 my $group = $kdbx->add_group;
75 $group->custom_data(foo => 'bar
');
76 is $kdbx->minimum_version, KDBX_VERSION_4_0, 'Group custom data requires upgrade
';
77 delete $group->custom_data->{foo};
78 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
81 subtest 'KDBX4
.1 upgrade
' => sub {
82 my $kdbx = File::KDBX->new;
84 my $group1 = $kdbx->add_group(label => 'One
');
85 my $group2 = $kdbx->add_group(label => 'Two
');
86 my $entry1 = $kdbx->add_entry(label => 'Meh
');
89 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Groups with tags requires upgrade
';
91 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
93 $entry1->quality_check(0);
94 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Disable entry quality check requires upgrade
';
95 $entry1->quality_check(1);
96 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
98 $group1->previous_parent_group($group2->uuid);
99 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Previous parent group on group requires upgrade
';
100 $group1->previous_parent_group(undef);
101 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
103 $entry1->previous_parent_group($group2->uuid);
104 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Previous parent group on entry requires upgrade
';
105 $entry1->previous_parent_group(undef);
106 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
108 $kdbx->add_custom_icon('data
');
109 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Icon with
no metadata requires
no upgrade
';
110 my $icon_uuid = $kdbx->add_custom_icon('data2
', name => 'icon name
');
111 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Icon with name requires upgrade
';
112 $kdbx->remove_custom_icon($icon_uuid);
113 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
114 $icon_uuid = $kdbx->add_custom_icon('data2
', last_modification_time => scalar gmtime);
115 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Icon with modtime requires upgrade
';
116 $kdbx->remove_custom_icon($icon_uuid);
117 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
119 $entry1->custom_data(foo => 'bar
', last_modification_time => scalar gmtime);
120 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Entry custom data modtime requires upgrade
';
121 delete $entry1->custom_data->{foo};
122 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
124 $group1->custom_data(foo => 'bar
', last_modification_time => scalar gmtime);
125 is $kdbx->minimum_version, KDBX_VERSION_4_1, 'Group custom data modtime requires upgrade
';
126 delete $group1->custom_data->{foo};
127 is $kdbx->minimum_version, KDBX_VERSION_3_1, 'Reset upgrade requirement
';
130 sub test_upgrade_master_key_integrity {
131 my ($modifier, $expected_version) = @_;
132 plan tests => $expected_version >= KDBX_VERSION_4_0 ? 6 : 5;
134 my $kdbx = File::KDBX->new;
135 $kdbx->kdf_parameters(fast_kdf);
137 is $kdbx->kdf->uuid, KDF_UUID_AES, 'Default KDF
is AES
';
143 is $kdbx->minimum_version, $expected_version,
144 sprintf('Got expected minimum version after modification
: %x', $kdbx->minimum_version);
146 my $master_key = ['fffqcvq4rc
', \'this is a keyfile', sub { 'chalresp 523rf2' }];
148 warnings
{ $kdbx->dump_string(\
$dump, $master_key) };
149 ok
$dump, 'Can dump the database' or diag explain
$dump;
151 like exception
{ File
::KDBX-
>load_string($dump, 'wrong key') },
152 qr/invalid credentials/i, 'Cannot load a KDBX with the wrong key';
154 # print STDERR "DUMP: [$dump]\n";
156 my $kdbx2 = File
::KDBX-
>load_string($dump, $master_key);
158 is $kdbx2->version, $expected_version, sprintf('Got expected version: %x', $kdbx2->version);
159 isnt
$kdbx2->kdf->uuid, KDF_UUID_AES
, 'No unexpected KDF' if $kdbx2->version >= KDBX_VERSION_4_0
;
161 # diag explain(File::KDBX->load_string($dump, $master_key, inner_format => 'Raw')->raw);
164 [KDBX_VERSION_3_1
, 'nothing', sub {}],
165 [KDBX_VERSION_3_1
, 'AES KDF', sub { $_->kdf_parameters(fast_kdf
(KDF_UUID_AES
)) }],
166 [KDBX_VERSION_4_0
, 'Argon2D KDF', sub { $_->kdf_parameters(fast_kdf
(KDF_UUID_ARGON2D
)) }],
167 [KDBX_VERSION_4_0
, 'Argon2ID KDF', sub { $_->kdf_parameters(fast_kdf
(KDF_UUID_ARGON2ID
)) }],
168 [KDBX_VERSION_4_0
, 'public custom data', sub { $_->public_custom_data->{foo
} = 'bar' }],
169 [KDBX_VERSION_3_1
, 'custom data', sub { $_->custom_data(foo
=> 'bar') }],
170 [KDBX_VERSION_4_0
, 'root group custom data', sub { $_->root->custom_data(baz
=> 'qux') }],
171 [KDBX_VERSION_4_0
, 'group custom data', sub { $_->add_group->custom_data(baz
=> 'qux') }],
172 [KDBX_VERSION_4_0
, 'entry custom data', sub { $_->add_entry->custom_data(baz
=> 'qux') }],
174 my ($expected_version, $name, $modifier) = @$test;
175 subtest
"Master key integrity: $name" => \
&test_upgrade_master_key_integrity
,
176 $modifier, $expected_version;
179 subtest
'Custom data' => sub {
180 my $kdbx = File
::KDBX-
>new;
181 $kdbx->kdf_parameters(fast_kdf
(KDF_UUID_AES
));
182 $kdbx->version(KDBX_VERSION_4_0
);
184 $kdbx->public_custom_data->{str
} = '你好';
185 $kdbx->public_custom_data->{num
} = 42;
186 $kdbx->public_custom_data->{bool
} = true
;
187 $kdbx->public_custom_data->{bytes
} = "\1\2\3\4";
189 my $group = $kdbx->add_group(label
=> 'Group');
190 $group->custom_data(str
=> '你好');
191 $group->custom_data(num
=> 42);
192 $group->custom_data(bool
=> true
);
194 my $entry = $kdbx->add_entry(label
=> 'Entry');
195 $entry->custom_data(str
=> '你好');
196 $entry->custom_data(num
=> 42);
197 $entry->custom_data(bool
=> false
);
199 my $dump = $kdbx->dump_string('a');
200 my $kdbx2 = File
::KDBX-
>load_string($dump, 'a');
202 is $kdbx2->public_custom_data->{str
}, '你好', 'Store a string in public custom data';
203 cmp_ok
$kdbx2->public_custom_data->{num
}, '==', 42, 'Store a number in public custom data';
204 is $kdbx2->public_custom_data->{bool
}, true
, 'Store a boolean in public custom data';
205 ok isBoolean
($kdbx2->public_custom_data->{bool
}), 'Boolean is indeed a boolean';
206 is $kdbx2->public_custom_data->{bytes
}, "\1\2\3\4", 'Store some bytes in public custom data';
208 my $group2 = $kdbx2->groups->grep(label
=> 'Group')->next;
209 is_deeply
$group2->custom_data_value('str'), '你好', 'Store a string in group custom data';
210 is_deeply
$group2->custom_data_value('num'), '42', 'Store a number in group custom data';
211 is_deeply
$group2->custom_data_value('bool'), '1', 'Store a boolean in group custom data';
213 my $entry2 = $kdbx2->entries->grep(label
=> 'Entry')->next;
214 is_deeply
$entry2->custom_data_value('str'), '你好', 'Store a string in entry custom data';
215 is_deeply
$entry2->custom_data_value('num'), '42', 'Store a number in entry custom data';
216 is_deeply
$entry2->custom_data_value('bool'), '0', 'Store a boolean in entry custom data';