KDBX/IO/Crypt.pm

filter: Statementstmt 92.5% 87 / 94 Branchbran 65.5% 38 / 58 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 83.7% 149 / 178 SCAR 36.3
CC
32
Cov
82%
_POPPED
7.4
_FILL
7.1
_WRITE
6.6
CRAP 37.6
« KDBX/IO/HmacBlock.pm ↑ Summary KDBX/Dumper/V3.pm »
1 package File::KDBX::IO::Crypt;
2 # ABSTRACT: Encrypter/decrypter IO handle
3  
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use File::KDBX::Util qw(:class :empty);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 extends 'File::KDBX::IO';
13  
14 11 our $VERSION = '0.904'; # VERSION
15 11 our $BUFFER_SIZE = 16384;
16 11 our $ERROR;
17  
18  
19 11 my %ATTRS = (
20 cipher => undef,
21 );
22 11 ▶while (my ($attr, $default) = each %ATTRS) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ATTRS) 11 11 22
23 11 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
24 *$attr = sub {
25 241 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=14.2
26 241 ▶ *$self->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 79 162 241
27 241 ▶ *$self->{$attr} //= (ref $default eq 'CODE') ? $default->($self) : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 0 56 56
Condition66%2/3
*$self->{$attr} //= ref $default eq "CODE" ? &$default($self) : $default
l!l&&r!l&&!r
185056
MC/DC50%1/2
*$self->{$attr} //= ref $default eq "CODE" ? &$default($self) : $default
*$self->{$attr} ref $default eq "CODE" ? &$default($self) : $default
Truth table66%2/3
A //= B
A= *$self->{$attr}B= ref $default eq "CODE" ? &$default($self) : $default
ABresult
1X1
011
000
28 11 };
29 }
30  
31  
32 sub new {
33 44 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.5
Pod documented
34 44 ▶ my %args = @_ % 2 == 1 ? (fh => shift, @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 44 0 44
35 44 my $self = $class->SUPER::new;
36 44 ▶ $self->_fh($args{fh}) or throw 'IO handle required';
Branch: 1 branch
BranchTrueFalseTotal
$self->_fh($args{"fh"}) or throw("IO handle required") 0 44 44
37 44 ▶ $self->cipher($args{cipher}) or throw 'Cipher required';
Branch: 1 branch
BranchTrueFalseTotal
$self->cipher($args{"cipher"}) or throw("Cipher required") 0 44 44
38 44 return $self;
39 }
40  
41 sub _FILL {
42 65 ▶ my ($self, $fh) = @_;
Subroutine _FILL: called CC=7 SCAR=19.6
43  
44 65 ▶ $ENV{DEBUG_STREAM} and print STDERR "FILL\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "FILL\t$self\n" 0 65 65
45 65 ▶ my $cipher = $self->cipher or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cipher = $self->cipher or return 21 44 65
46  
47 44 $fh->read(my $buf = '', $BUFFER_SIZE);
48 44 ▶ if (0 < length($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < length $buf) 27 17 44
49 27 my $plaintext = eval { $cipher->decrypt($buf) };
50 27 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 27 27
51 0 $self->_set_error($err);
52 0 return;
53 }
54 27 ▶ return $plaintext if 0 < length($plaintext);
Branch: 1 branch
BranchTrueFalseTotal
if 0 < length $plaintext 26 1 27
55 }
56  
57 # finish
58 18 my $plaintext = eval { $cipher->finish };
59 18 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 17 18
60 1 $self->_set_error($err);
61 1 return;
62 }
63 17 $self->cipher(undef);
64 17 return $plaintext;
65 }
66  
67 sub _WRITE {
68 80 ▶ my ($self, $buf, $fh) = @_;
Subroutine _WRITE: called CC=6 SCAR=18.8
69  
70 80 ▶ $ENV{DEBUG_STREAM} and print STDERR "WRITE\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "WRITE\t$self\n" 0 80 80
71 80 ▶ my $cipher = $self->cipher or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $cipher = $self->cipher or return 0 0 80 80
72  
73 80 ▶ my $new_data = eval { $cipher->encrypt($buf) } || '';
Condition100%2/2
eval { do { $cipher->encrypt($buf) } } || ''
l!l
764
MC/DC100%1/1
eval { do { $cipher->encrypt($buf) } } || ''
eval { do { $cipher->encrypt($buf) } }
Truth table100%2/2
A
A= eval { do { $cipher->encrypt($buf) } }
Aresult
11
00
74 80 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 80 80
75 0 $self->_set_error($err);
76 0 return 0;
77 }
78 80 ▶ $self->_buffer_out_add($new_data) if nonempty $new_data;
Branch: 1 branch
BranchTrueFalseTotal
if nonempty($new_data) 76 4 80
79 80 return length($buf);
80 }
81  
82 sub _POPPED {
83 44 ▶ my ($self, $fh) = @_;
Subroutine _POPPED: called CC=7 SCAR=20.1
84  
85 44 ▶ $ENV{DEBUG_STREAM} and print STDERR "POPPED\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "POPPED\t$self\n" 0 44 44
86 44 ▶ return if $self->_mode ne 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mode ne "w" 27 17 44
87 17 ▶ my $cipher = $self->cipher or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cipher = $self->cipher or return 0 17 17
88  
89 17 ▶ my $new_data = eval { $cipher->finish } || '';
Condition100%2/2
eval { do { $cipher->finish } } || ''
l!l
215
MC/DC100%1/1
eval { do { $cipher->finish } } || ''
eval { do { $cipher->finish } }
Truth table100%2/2
A
A= eval { do { $cipher->finish } }
Aresult
11
00
90 17 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 17 17
91 0 $self->_set_error($err);
92 0 return;
93 }
94 17 ▶ $self->_buffer_out_add($new_data) if nonempty $new_data;
Branch: 1 branch
BranchTrueFalseTotal
if nonempty($new_data) 2 15 17
95  
96 17 $self->cipher(undef);
97 17 $self->_FLUSH($fh);
98 }
99  
100 sub _FLUSH {
101 17 ▶ my ($self, $fh) = @_;
Subroutine _FLUSH: called CC=6 SCAR=18.5
102  
103 17 ▶ $ENV{DEBUG_STREAM} and print STDERR "FLUSH\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "FLUSH\t$self\n" 0 17 17
104 17 ▶ return if $self->_mode ne 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mode ne "w" 0 17 17
105  
106 17 my $buffer = $self->_buffer_out;
107 17 ▶ while (@$buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$buffer) 78 17 95
108 78 my $read = shift @$buffer;
109 78 ▶ next if empty $read;
Branch: 1 branch
BranchTrueFalseTotal
if empty($read) 0 78 78
110 78 ▶ $fh->print($read) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($read) or return -1 0 78 78
111 }
112 17 return 0;
113 }
114  
115 sub _set_error {
116 1 ▶ my $self = shift;
Subroutine _set_error: called CC=4 SCAR=15.9
117 1 ▶ $ENV{DEBUG_STREAM} and print STDERR "err\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "err\t$self\n" 0 1 1
118 1 ▶ if (exists &Errno::EPROTO) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists &Errno::EPROTO) { } 1 0 1
elsif (exists &Errno::EIO) { } 0 0 0
119 1 $! = &Errno::EPROTO;
120 }
121 elsif (exists &Errno::EIO) {
122 0 $! = &Errno::EIO;
123 }
124 1 $self->cipher(undef);
125 1 $self->_error($ERROR = File::KDBX::Error->new(@_));
126 }
127  
128 11 1;
129  
130 __END__
« KDBX/IO/HmacBlock.pm ↑ Summary KDBX/Dumper/V3.pm »