lib/Mail/DMARC/Report/Aggregate.pm

filter: Statementstmt 96.8% 124 / 128 Branchbran 79.6% 43 / 54 Conditioncond 79.1% 19 / 24 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 10 totaltotal 85.4% 212 / 248 CC 48 SCAR 19.9
CC
48
Cov
90%
get_auth_results_as_xml
13.1
get_policy_published_as_xml
9.0
get_identifiers_as_xml
8.2
CRAP 7.3
« lib/Mail/DMARC/Report/URI.pm ↑ Summary lib/Mail/DMARC/Report/Store/SQL/Grammars/PostgreSQL.pm »
1 package Mail::DMARC::Report::Aggregate;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '2.20261110';
7  
8 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use parent 'Mail::DMARC::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Mail::DMARC::Report::Aggregate::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 require Mail::DMARC::Result::Reason;
15  
16 374 ▶sub metadata($self) {
Subroutine metadata: called CC=2 SCAR=6.9
Pod undocumented
17 374 ▶ return $self->{metadata} if ref $self->{metadata};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"metadata"} 335 39 374
18 39 return $self->{metadata} = Mail::DMARC::Report::Aggregate::Metadata->new;
19 }
20  
21 148 ▶sub policy_published( $self, $policy = undef ) {
Subroutine policy_published: called CC=4 SCAR=13.9
Pod undocumented
22 148 ▶ return $self->{policy_published} if !$policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy 112 36 148
23 36 ▶ croak "not a policy object!" if 'Mail::DMARC::Policy' ne ref $policy;
Branch: 1 branch
BranchTrueFalseTotal
if "Mail::DMARC::Policy" ne ref $policy 0 36 36
24 36 return $self->{policy_published} = $policy;
25 }
26  
27 73 ▶sub record( $self, $record = undef, @extra ) { ## no critic (Ambiguous)
Subroutine record: called CC=7 SCAR=19.9
Pod undocumented
28 73 ▶ if ( !$record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 41 32 73
29 41 ▶ return $self->{record} || [];
Condition100%2/2
$self->{'record'} || []
l!l
401
MC/DC100%1/1
$self->{'record'} || []
$self->{'record'}
Truth table100%2/2
A
A= $self->{'record'}
Aresult
11
00
30 }
31  
32 0 ▶ if (@extra) { croak "invalid args"; }
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) 0 32 32
33  
34 32 ▶ if ( 'Mail::DMARC::Report::Aggregate::Record' ne ref $record ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Mail::DMARC::Report::Aggregate::Record" ne ref $record) 0 32 32
35 0 croak "not a record object";
36 }
37  
38 32 ▶ $self->{record} ||= [];
Condition100%2/2
$self->{"record"} ||= []
l!l
329
MC/DC100%1/1
$self->{"record"} ||= []
$self->{"record"}
Truth table100%2/2
A
A= $self->{"record"}
Aresult
11
00
39  
40 32 push @{ $self->{record} }, $record;
41  
42 32 return $self->{record};
43 }
44  
45 0 ▶sub dump_report($self) {
Subroutine dump_report: not called CC=1 SCAR=6.9
Pod undocumented
46 0 carp Dumper( $self->{metadata}, $self->{policy_published}, $self->{record} );
47 0 return;
48 }
49  
50 13 ▶sub as_xml($self) {
Subroutine as_xml: called CC=1 SCAR=0.0
Pod undocumented
51 13 my $meta = $self->metadata->as_xml;
52 13 my $pubp = $self->get_policy_published_as_xml;
53 13 my $reco = $self->get_record_as_xml;
54  
55 return <<"EO_XML"
56 <?xml version="1.0"?>
57 <feedback xmlns="urn:ietf:params:xml:ns:dmarc-2.0">
58 $meta
59 $pubp
60 $reco</feedback>
61 EO_XML
62 13 ;
63 }
64  
65 15 ▶sub get_record_as_xml($self) {
Subroutine get_record_as_xml: called CC=5 SCAR=16.3
Pod undocumented
66  
67 15 my $rec_xml = '';
68 15 foreach my $rec ( @{ $self->{record} } ) {
69 18 $rec_xml .= "\t<record>\n";
70 18 ▶ my $ip = $rec->{row}{source_ip} or croak "no source IP!?";
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $rec->{"row"}{"source_ip"} or croak("no source IP!?") 18 0 18
71 18 ▶ my $count = $rec->{row}{count} or croak "no count!?";
Branch: 1 branch
BranchTrueFalseTotal
my $count = $rec->{"row"}{"count"} or croak("no count!?") 18 0 18
72 18 ▶ $rec->{row}{policy_evaluated}{disposition} or croak "no disposition?";
Branch: 1 branch
BranchTrueFalseTotal
$rec->{"row"}{"policy_evaluated"}{"disposition"} or croak("no disposition?") 18 0 18
73 18 $ip = XML::LibXML::Text->new($ip)->toString();
74 18 $count = XML::LibXML::Text->new($count)->toString();
75 18 $rec_xml
76 .= "\t\t<row>\n"
77 . "\t\t\t<source_ip>$ip</source_ip>\n"
78 . "\t\t\t<count>$count</count>\n"
79 . $self->get_policy_evaluated_as_xml($rec)
80 . "\t\t</row>\n"
81 . $self->get_identifiers_as_xml($rec)
82 . $self->get_auth_results_as_xml($rec);
83 18 $rec_xml .= "\t</record>\n";
84 }
85 15 return $rec_xml;
86 }
87  
88 18 ▶sub get_identifiers_as_xml( $self, $rec ) {
Subroutine get_identifiers_as_xml: called CC=8 SCAR=21.0
Pod undocumented
89 18 my $id = "\t\t<identifiers>\n";
90 18 foreach my $f (qw/ envelope_to envelope_from header_from /) {
91 54 ▶ if ( $f eq 'header_from' ) { # min occurs = 1
Branch: 3 branches
BranchTrueFalseTotal
if ($f eq 'header_from') { } 18 36 54
elsif ($f eq 'envelope_from') { } 18 18 36
elsif ($f eq 'envelope_to') { } 18 0 18
92 18 ▶ croak "missing header_from!" if !$rec->{identifiers}{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"identifiers"}{$f} 0 18 18
93 }
94 elsif ( $f eq 'envelope_from' ) { # min occurs = 1
95 18 ▶ $rec->{identifiers}{$f} = '' if !$rec->{identifiers}{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"identifiers"}{$f} 0 18 18
96 }
97 elsif ( $f eq 'envelope_to' ) { # min occurs = 0
98 18 ▶ next if !$rec->{identifiers}{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"identifiers"}{$f} 7 11 18
99 }
100  
101 47 my $val = XML::LibXML::Text->new( $rec->{identifiers}{$f} )->toString();
102 47 $id .= "\t\t\t<$f>$val</$f>\n";
103 }
104 18 $id .= "\t\t</identifiers>\n";
105 18 return $id;
106 }
107  
108 19 ▶sub get_auth_results_as_xml( $self, $rec ) {
Subroutine get_auth_results_as_xml: called CC=13 SCAR=25.7
Pod undocumented
109 19 my $ar = "\t\t<auth_results>\n";
110  
111 # RFC 9990 3.1.1.12-13; older stores hold helo rows and MTA-cased results
112 19 foreach my $dkim_sig ( @{ $rec->{auth_results}{dkim} } ) {
113 13 $ar .= "\t\t\t<dkim>\n";
114 13 foreach my $g (qw/ domain selector result human_result /) {
115 52 ▶ next if !defined $dkim_sig->{$g} && $g ne 'selector';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $dkim_sig->{$g} and $g ne "selector" 2 50 52
Condition100%3/3
not defined $dkim_sig->{$g} and $g ne "selector"
!ll&&!rl&&r
4912
MC/DC100%2/2
not defined $dkim_sig->{$g} and $g ne "selector"
not defined $dkim_sig->{$g} $g ne "selector"
Truth table100%3/3
not(A) and B
A= not defined $dkim_sig->{$g}B= $g ne "selector"
ABresult
0X0
100
111
116 50 ▶ my $val = $dkim_sig->{$g} // '';
Condition100%2/2
$dkim_sig->{$g} // ""
l!l
491
MC/DC100%1/1
$dkim_sig->{$g} // ""
$dkim_sig->{$g}
Truth table100%2/2
A
A= $dkim_sig->{$g}
Aresult
11
00
117 50 ▶ $val = lc $val if $g eq 'result';
Branch: 1 branch
BranchTrueFalseTotal
if $g eq "result" 13 37 50
118 50 $val = XML::LibXML::Text->new($val)->toString();
119 50 $ar .= "\t\t\t\t<$g>$val</$g>\n";
120 }
121 13 $ar .= "\t\t\t</dkim>\n";
122 }
123  
124 18 ▶ my ($spf) = grep { lc( $_->{scope} // 'mfrom' ) ne 'helo' }
Condition50%1/2
$_->{"scope"} // "mfrom"
l!l
180
MC/DC0%0/1
$_->{"scope"} // "mfrom"
$_->{"scope"}
Truth table50%1/2
A
A= $_->{"scope"}
Aresult
11
00
125 19 ▶ @{ $rec->{auth_results}{spf} || [] };
Condition50%1/2
$rec->{'auth_results'}{'spf'} || []
l!l
190
MC/DC0%0/1
$rec->{'auth_results'}{'spf'} || []
$rec->{'auth_results'}{'spf'}
Truth table50%1/2
A
A= $rec->{'auth_results'}{'spf'}
Aresult
11
00
126 19 ▶ if ($spf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf) 12 7 19
127 12 $ar .= "\t\t\t<spf>\n";
128 12 foreach my $g (qw/ domain scope result /) {
129 36 ▶ next if !defined $spf->{$g};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spf->{$g} 0 36 36
130 36 ▶ my $val = $g eq 'domain' ? $spf->{$g} : lc $spf->{$g};
Branch: 1 branch
BranchTrueFalseTotal
$g eq 'domain' ? : 12 24 36
131 36 $val = XML::LibXML::Text->new($val)->toString();
132 36 $ar .= "\t\t\t\t<$g>$val</$g>\n";
133 }
134 12 $ar .= "\t\t\t</spf>\n";
135 }
136  
137 19 $ar .= "\t\t</auth_results>\n";
138 19 return $ar;
139 }
140  
141 15 ▶sub get_policy_published_as_xml($self) {
Subroutine get_policy_published_as_xml: called CC=9 SCAR=22.0
Pod undocumented
142 15 ▶ my $pp = $self->policy_published or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $pp = $self->policy_published or return "" 15 0 15
143 15 my $xml = "\t<policy_published>\n\t\t<domain>$pp->{domain}</domain>\n";
144 15 foreach my $f (qw/ adkim aspf p sp np testing fo discovery_method /) {
145 120 ▶ my $v = $f eq 'testing' ? $pp->{t} : $pp->{$f};
Branch: 1 branch
BranchTrueFalseTotal
$f eq 'testing' ? : 15 105 120
146  
147 # Set some default values for missing optional fields if necessary
148 120 ▶ if ( $f eq 'sp' && !defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "sp" and not defined $v) 15 105 120
Condition66%2/3
$f eq "sp" and not defined $v
!ll&&!rl&&r
105015
MC/DC50%1/2
$f eq "sp" and not defined $v
$f eq "sp" not defined $v
Truth table66%2/3
A and not(B)
A= $f eq "sp"B= not defined $v
ABresult
0X0
100
111
149 15 $v = $pp->{'p'};
150 }
151 120 ▶ if ( $f eq 'fo' && !defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "fo" and not defined $v) 12 108 120
Condition100%3/3
$f eq "fo" and not defined $v
!ll&&!rl&&r
105312
MC/DC100%2/2
$f eq "fo" and not defined $v
$f eq "fo" not defined $v
Truth table100%3/3
A and not(B)
A= $f eq "fo"B= not defined $v
ABresult
0X0
100
111
152 12 $v = '0';
153 }
154 120 ▶ next if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 62 58 120
155  
156 # the schema enumerations are lowercase; older stores kept DNS case
157 58 $v = XML::LibXML::Text->new( lc $v )->toString();
158 58 $xml .= "\t\t<$f>$v</$f>\n";
159 }
160 15 $xml .= "\t</policy_published>";
161 15 return $xml;
162 }
163  
164 # RFC 9990 3.1.6 dropped forwarded and sampled_out
165 my %override_type
166 10 = map { $_ => 1 } qw/ local_policy mailing_list other policy_test_mode trusted_forwarder /;
167  
168 19 ▶sub get_policy_evaluated_as_xml( $self, $rec ) {
Subroutine get_policy_evaluated_as_xml: called CC=7 SCAR=19.5
Pod undocumented
169 19 my $pe = "\t\t\t<policy_evaluated>\n";
170  
171 19 foreach my $f (qw/ disposition dkim spf /) {
172 57 my $val = XML::LibXML::Text->new( $rec->{row}{policy_evaluated}{$f} )
173 ->toString();
174 57 $pe .= "\t\t\t\t<$f>$val</$f>\n";
175 }
176  
177 19 my $reasons = $rec->{row}{policy_evaluated}{reason};
178 19 ▶ if ( $reasons && scalar @$reasons ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reasons and scalar @$reasons) 1 18 19
Condition50%1/2
$reasons and scalar @$reasons
!ll
019
MC/DC0%0/1
$reasons and scalar @$reasons
$reasons
Truth table50%1/2
A
A= $reasons
Aresult
00
11
179 1 foreach my $reason (@$reasons) {
180 my ( $type, $comment ) = Mail::DMARC::Result::Reason::as_known_type(
181 3 @$reason{qw/ type comment /}, \%override_type );
182 3 my $typeval = XML::LibXML::Text->new($type)->toString();
183 3 $pe .= "\t\t\t\t<reason>\n";
184 3 $pe .= "\t\t\t\t\t<type>$typeval</type>\n";
185  
186 3 ▶ if ( defined $comment && $comment =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment and $comment =~ /\S/) 2 1 3
Condition66%2/3
defined $comment and $comment =~ /\S/
!ll&&!rl&&r
102
MC/DC50%1/2
defined $comment and $comment =~ /\S/
defined $comment $comment =~ /\S/
Truth table66%2/3
A and B
A= defined $commentB= $comment =~ /\S/
ABresult
0X0
100
111
187 2 my $commentval = XML::LibXML::Text->new($comment)->toString();
188 2 $pe .= "\t\t\t\t\t<comment>$commentval</comment>\n";
189 }
190 3 $pe .= "\t\t\t\t</reason>\n";
191 }
192 }
193 19 $pe .= "\t\t\t</policy_evaluated>\n";
194 19 return $pe;
195 }
196  
197 10 1;
198  
199 __END__
« lib/Mail/DMARC/Report/URI.pm ↑ Summary lib/Mail/DMARC/Report/Store/SQL/Grammars/PostgreSQL.pm »