]> Dogcows Code - chaz/p5-File-KDBX/blob - t/hmac-block.t
Add iterator
[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::IO::HmacBlock;
10 use File::KDBX::Util qw(can_fork);
11 use IO::Handle;
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 $write = File::KDBX::IO::HmacBlock->new($write, block_size => 3, key => $KEY);
22 print $write $expected_plaintext;
23 close($write) or die "close failed: $!";
24
25 $read = File::KDBX::IO::HmacBlock->new($read, key => $KEY);
26 my $plaintext = do { local $/; <$read> };
27 close($read);
28
29 is $plaintext, $expected_plaintext, 'HMAC-block just a little bit';
30
31 is $File::KDBX::IO::HmacBlock::ERROR, undef, 'No error when successful';
32 }
33
34 SKIP: {
35 skip 'fork required to test long data streams' if !can_fork;
36
37 my $expected_plaintext = "\x64" x (1024*1024*12 - 57);
38
39 local $SIG{CHLD} = 'IGNORE';
40 pipe(my $read, my $write) or die "pipe failed: $!\n";
41
42 defined(my $pid = fork) or die "fork failed: $!\n";
43 if ($pid == 0) {
44 $write = File::KDBX::IO::HmacBlock->new($write, key => $KEY);
45 print $write $expected_plaintext;
46 close($write) or die "close failed: $!";
47 # exit;
48 require POSIX;
49 POSIX::_exit(0);
50 }
51
52 $read = File::KDBX::IO::HmacBlock->new($read, key => $KEY);
53 my $plaintext = do { local $/; <$read> };
54 close($read);
55
56 is $plaintext, $expected_plaintext, 'HMAC-block a lot';
57 }
58
59 subtest 'Error handling' => sub {
60 pipe(my $read, my $write) or die "pipe failed: $!\n";
61
62 $read = File::KDBX::IO::HmacBlock->new($read, key => $KEY);
63
64 print $write 'blah blah blah';
65 close($write) or die "close failed: $!";
66
67 is $read->error, '', 'Read handle starts out fine';
68 my $data = do { local $/; <$read> };
69 is $read->error, 1, 'Read handle can enter an error state';
70
71 like $File::KDBX::IO::HmacBlock::ERROR, qr/failed to read HMAC/i, 'Error object is available';
72 };
73
74 done_testing;
This page took 0.040454 seconds and 4 git commands to generate.