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