lib/Mail/DKIM2/Wrap.pm

filter: Statementstmt 100% 66 / 66 Branchbran 95.8% 23 / 24 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 99.1% 111 / 112 CC 20 SCAR 26.2
CC
20
Cov
99%
wrap_single_part
19.0
__ANON__
2.0
BEGIN
1.0
CRAP 13.7
« lib/Mail/DKIM2/DSN.pm ↑ Summary lib/Mail/DKIM2/Reflector.pm »
1 package Mail::DKIM2::Wrap;
2 82 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 82 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 82 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 82 our $VERSION = '0.34';
7  
8 82 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 82 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 82 our @EXPORT_OK = qw(wrap_single_part);
12  
13 82 my $EOL = "\015\012";
14  
15 9 ▶sub _random_boundary { '=_dkim2_' . unpack('H*', random_bytes(12)) }
Subroutine _random_boundary: called CC=1 SCAR=0.0
16  
17 24 ▶sub wrap_single_part {
Subroutine wrap_single_part: called CC=19 SCAR=29.4
Pod documented
18 24 my ($message, %opt) = @_;
19 24 ▶ my $gen = $opt{Boundary} // \&_random_boundary;
Condition100%2/2
$opt{"Boundary"} // \&Mail::DKIM2::Wrap::_random_boundary
l!l
816
MC/DC100%1/1
$opt{"Boundary"} // \&Mail::DKIM2::Wrap::_random_boundary
$opt{"Boundary"}
Truth table100%2/2
A
A= $opt{"Boundary"}
Aresult
11
00
20  
21 # Nothing to wrap: undef, and in list context the reason.
22 24 ▶ my $none = sub { wantarray ? (undef, $_[0]) : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 3 8
23  
24 24 my $sep = index($message, "$EOL$EOL");
25 24 ▶ return $none->('no-header-end') if $sep < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sep < 0 2 22 24
26 22 my $head = substr($message, 0, $sep + 2);
27 22 my $body = substr($message, $sep + 4);
28  
29 22 my @fields = $head =~ /\G([^\r\n]*\r\n(?:[ \t][^\r\n]*\r\n)*)/g;
30 22 ▶ return $none->('unparsable-header') unless join('', @fields) eq $head;
Branch: 1 branch
BranchTrueFalseTotal
unless join("", @fields) eq $head 0 22 22
31 22 my @parsed;
32 22 for my $f (@fields) {
33 106 ▶ my ($name) = $f =~ /^([^:\s]+)[ \t]*:/ or return $none->('unparsable-header');
Branch: 1 branch
BranchTrueFalseTotal
my($name) = $f =~ /^([^:\s]+)[ \t]*:/u or return &$none("unparsable-header") 105 1 106
34 105 push @parsed, [$name, $f];
35 }
36  
37 21 my ($ct) = grep { lc $_->[0] eq 'content-type' } @parsed;
38 21 ▶ return $none->('multipart') if $ct && $ct->[1] =~ /^[^:]+:\s*multipart\//i;
Branch: 1 branch
BranchTrueFalseTotal
if $ct and $ct->[1] =~ m[^[^:]+:\s*multipart/]iu 4 17 21
Condition100%3/3
$ct and $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
!ll&&!rl&&r
1164
MC/DC100%2/2
$ct and $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
$ct $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
Truth table100%3/3
A and B
A= $ctB= $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
ABresult
0X0
100
111
39  
40 17 my $boundary;
41 17 for (1 .. 10) {
42 27 my $c = $gen->();
43 27 ▶ if (index($body, "--$c") < 0) { $boundary = $c; last }
Branch: 1 branch
BranchTrueFalseTotal
if (index($body, "--$c") == -1) 16 11 27
44 }
45 17 ▶ return $none->('boundary') unless defined $boundary;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $boundary 1 16 17
46  
47 16 my @content = grep { $_->[0] =~ /^content-/i } @parsed;
48 16 my $new_body = "--$boundary$EOL" . join('', map { $_->[1] } @content)
49 . $EOL . $body . "$EOL--$boundary--$EOL";
50 16 my $ct_value = qq{multipart/mixed; boundary="$boundary"};
51  
52 16 my (%count, @deletes, @out, $changed);
53 16 for my $p (@parsed) {
54 91 my $lc = lc $p->[0];
55 91 my $i = ++$count{$lc};
56 91 ▶ if ($lc eq 'content-type' && $i == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lc eq 'content-type' and $i == 1) { } 15 76 91
elsif ($lc =~ /^content-/u) { } 3 73 76
Condition100%3/3
$lc eq "content-type" and $i == 1
!ll&&!rl&&r
75115
MC/DC100%2/2
$lc eq "content-type" and $i == 1
$lc eq "content-type" $i == 1
Truth table100%3/3
A and B
A= $lc eq "content-type"B= $i == 1
ABresult
0X0
100
111
57 15 push @out, "$p->[0]: $ct_value$EOL";
58 15 $changed = 1;
59 } elsif ($lc =~ /^content-/) {
60 3 push @deletes, { op => 'delete', name => $p->[0], index => $i };
61 } else {
62 73 push @out, $p->[1];
63 }
64 }
65 16 my @edits = reverse @deletes;
66 16 ▶ push @edits, { op => 'change', name => 'Content-Type', index => 1, value => $ct_value }
Branch: 1 branch
BranchTrueFalseTotal
if $changed 15 1 16
67 if $changed;
68 16 my @adds;
69 16 ▶ push @adds, ['Content-Type', $ct_value] unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 1 15 16
70 16 ▶ push @adds, ['MIME-Version', '1.0'] unless $count{'mime-version'};
Branch: 1 branch
BranchTrueFalseTotal
unless $count{"mime-version"} 7 9 16
71 16 for my $a (@adds) {
72 8 push @out, "$a->[0]: $a->[1]$EOL";
73 8 push @edits, { op => 'add', name => $a->[0], value => $a->[1] };
74 }
75  
76 return {
77 16 message => join('', @out) . $EOL . $new_body,
78 body => $new_body,
79 boundary => $boundary,
80 edits => \@edits,
81 };
82 }
83  
84 82 1;
85  
86 __END__
« lib/Mail/DKIM2/DSN.pm ↑ Summary lib/Mail/DKIM2/Reflector.pm »