]> Dogcows Code - chaz/p5-File-KDBX/blob - t/hmac-block.t
Add key file saving and refactor some stuff
[chaz/p5-File-KDBX] / t / hmac-block.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use lib 't/lib';
7 use TestCommon qw(:no_warnings_test);
8
9 use File::KDBX::Util qw(can_fork);
10 use IO::Handle;
11 use PerlIO::via::File::KDBX::HmacBlock;
12 use Test::More;
13
14 my $KEY = "\x01" x 64;
15
16 {
17 my $expected_plaintext = 'Tiny food from Spain!';
18
19 pipe(my $read, my $write) or die "pipe failed: $!\n";
20
21 PerlIO::via::File::KDBX::HmacBlock->push($write, block_size => 3, key => $KEY);
22 print $write $expected_plaintext;
23 binmode($write, ':pop'); # finish stream
24 close($write) or die "close failed: $!";
25
26 PerlIO::via::File::KDBX::HmacBlock->push($read, key => $KEY);
27 my $plaintext = do { local $/; <$read> };
28 close($read);
29
30 is $plaintext, $expected_plaintext, 'HMAC-block just a little bit';
31 }
32
33 SKIP: {
34 skip 'Tests require fork' if !can_fork;
35
36 my $expected_plaintext = "\x64" x (1024*1024*12 - 57);
37
38 pipe(my $read, my $write) or die "pipe failed: $!\n";
39
40 defined(my $pid = fork) or die "fork failed: $!\n";
41 if ($pid == 0) {
42 PerlIO::via::File::KDBX::HmacBlock->push($write, key => $KEY);
43 print $write $expected_plaintext;
44 binmode($write, ':pop'); # finish stream
45 close($write) or die "close failed: $!";
46 exit;
47 }
48
49 PerlIO::via::File::KDBX::HmacBlock->push($read, key => $KEY);
50 my $plaintext = do { local $/; <$read> };
51 close($read);
52
53 is $plaintext, $expected_plaintext, 'HMAC-block a lot';
54
55 waitpid($pid, 0) or die "wait failed: $!\n";
56 }
57
58 subtest 'Error handling' => sub {
59 pipe(my $read, my $write) or die "pipe failed: $!\n";
60
61 PerlIO::via::File::KDBX::HmacBlock->push($read, key => $KEY);
62
63 print $write 'blah blah blah';
64 close($write) or die "close failed: $!";
65
66 is $read->error, 0, 'Read handle starts out fine';
67 my $data = do { local $/; <$read> };
68 is $read->error, 1, 'Read handle can enter and error state';
69
70 like $PerlIO::via::File::KDBX::HmacBlock::ERROR, qr/failed to read HMAC/i,
71 'Error object is available';
72 };
73
74 done_testing;
This page took 0.035397 seconds and 4 git commands to generate.