]> Dogcows Code - chaz/p5-File-KDBX/blob - t/util.t
Add key file saving and refactor some stuff
[chaz/p5-File-KDBX] / t / util.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 File::KDBX::Util qw(:all);
10 use Test::More;
11
12 can_ok('File::KDBX::Util', qw{
13 assert_64bit
14 can_fork
15 dumper
16 empty
17 erase
18 erase_scoped
19 format_uuid
20 generate_uuid
21 gunzip
22 gzip
23 load_optional
24 nonempty
25 pad_pkcs7
26 query
27 search
28 simple_expression_query
29 snakify
30 split_url
31 trim
32 uri_escape_utf8
33 uri_unescape_utf8
34 uuid
35 });
36
37 subtest 'Emptiness' => sub {
38 my @empty;
39 my @nonempty = 0;
40 ok empty(@empty), 'Empty array should be empty';
41 ok !nonempty(@empty), 'Empty array should be !nonempty';
42 ok !empty(@nonempty), 'Array should be !empty';
43 ok nonempty(@nonempty), 'Array should be nonempty';
44
45 my %empty;
46 my %nonempty = (a => 'b');
47 ok empty(%empty), 'Empty hash should be empty';
48 ok !nonempty(%empty), 'Empty hash should be !nonempty';
49 ok !empty(%nonempty), 'Hash should be !empty';
50 ok nonempty(%nonempty), 'Hash should be nonempty';
51
52 my $empty = '';
53 my $nonempty = '0';
54 my $eref1 = \$empty;
55 my $eref2 = \$eref1;
56 my $nref1 = \$nonempty;
57 my $nref2 = \$nref1;
58
59 for my $test (
60 [0, $empty, 'Empty string'],
61 [0, undef, 'Undef'],
62 [0, \undef, 'Reference to undef'],
63 [0, {}, 'Empty hashref'],
64 [0, [], 'Empty arrayref'],
65 [0, $eref1, 'Reference to empty string'],
66 [0, $eref2, 'Reference to reference to empty string'],
67 [0, \\\\\\\'', 'Deep reference to empty string'],
68 [1, $nonempty, 'String'],
69 [1, 'hi', 'String'],
70 [1, 1, 'Number'],
71 [1, 0, 'Zero'],
72 [1, {a => 'b'}, 'Hashref'],
73 [1, [0], 'Arrayref'],
74 [1, $nref1, 'Reference to string'],
75 [1, $nref2, 'Reference to reference to string'],
76 [1, \\\\\\\'z', 'Deep reference to string'],
77 ) {
78 my ($expected, $thing, $note) = @$test;
79 if ($expected) {
80 ok !empty($thing), "$note should be !empty";
81 ok nonempty($thing), "$note should be nonempty";
82 }
83 else {
84 ok empty($thing), "$note should be empty";
85 ok !nonempty($thing), "$note should be !nonempty";
86 }
87 }
88 };
89
90 subtest 'UUIDs' => sub {
91 my $uuid = "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89\xab\xcd\xef";
92 my $uuid1 = uuid('01234567-89AB-CDEF-0123-456789ABCDEF');
93 my $uuid2 = uuid('0123456789ABCDEF0123456789ABCDEF');
94 my $uuid3 = uuid('012-3-4-56-789AB-CDEF---012-34567-89ABC-DEF');
95
96 is $uuid1, $uuid, 'Formatted UUID is packed';
97 is $uuid2, $uuid, 'Formatted UUID does not need dashes';
98 is $uuid2, $uuid, 'Formatted UUID can have weird dashes';
99
100 is format_uuid($uuid), '0123456789ABCDEF0123456789ABCDEF', 'UUID unpacks to hex string';
101 is format_uuid($uuid, '-'), '01234567-89AB-CDEF-0123-456789ABCDEF', 'Formatted UUID can be delimited';
102
103 my %uuid_set = ($uuid => 'whatever');
104
105 my $new_uuid = generate_uuid(\%uuid_set);
106 isnt $new_uuid, $uuid, 'Generated UUID is not in set';
107
108 $new_uuid = generate_uuid(sub { !$uuid_set{$_} });
109 isnt $new_uuid, $uuid, 'Generated UUID passes a test function';
110
111 like generate_uuid(print => 1), qr/^[A-Za-z0-9]+$/, 'Printable UUID is printable (1)';
112 like generate_uuid(printable => 1), qr/^[A-Za-z0-9]+$/, 'Printable UUID is printable (2)';
113 };
114
115 subtest 'Snakification' => sub {
116 is snakify('FooBar'), 'foo_bar', 'Basic snakification';
117 is snakify('MyUUIDSet'), 'my_uuid_set', 'Acronym snakification';
118 is snakify('Numbers123'), 'numbers_123', 'Snake case with numbers';
119 is snakify('456Baz'), '456_baz', 'Prefixed numbers';
120 };
121
122 subtest 'Padding' => sub {
123 plan tests => 8;
124
125 is pad_pkcs7('foo', 2), "foo\x01", 'Pad one byte to fill the second block';
126 is pad_pkcs7('foo', 4), "foo\x01", 'Pad one byte to fill one block';
127 is pad_pkcs7('foo', 8), "foo\x05\x05\x05\x05\x05", 'Pad to fill one block';
128 is pad_pkcs7('moof', 4), "moof\x04\x04\x04\x04", 'Add a whole block of padding';
129 is pad_pkcs7('', 3), "\x03\x03\x03", 'Pad an empty string';
130 like exception { pad_pkcs7(undef, 8) }, qr/must provide a string/i, 'String must be defined';
131 like exception { pad_pkcs7('bar') }, qr/must provide block size/i, 'Size must defined';
132 like exception { pad_pkcs7('bar', 0) }, qr/must provide block size/i, 'Size must be non-zero';
133 };
134
135 done_testing;
This page took 0.043742 seconds and 4 git commands to generate.