TLSRPT/Policy.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 91.1% 31 / 34 Conditioncond 71.8% 23 / 32 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 94.4% 17 / 18 Podpod 100% 8 / 8 totaltotal 87.7% 164 / 187 CC 35 SCAR 20.8
CC
35
Cov
91%
new_from_data
17.3
__ANON__
6.5
policy_mode
6.0
CRAP 8.0
« TLSRPT/App/Command/prometheus.pm ↑ Summary TLSRPT/App/Command/csv.pm »
1 package Mail::TLSRPT::Policy;
2 # ABSTRACT: TLSRPT policy object
3 13 our $VERSION = '2.20261112'; # VERSION
4 13 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Mail::TLSRPT::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Mail::TLSRPT::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 has policy_type => (is => 'rw', isa => Str, required => 1);
9 13 has policy_string => (is => 'rw', isa => ArrayRef, required => 1);
10 13 has policy_domain => (is => 'rw', isa => Str, required => 1);
11 13 has policy_mx_host => (is => 'rw', isa => Str, required => 1);
12 13 has _policy_mx_hosts => (is => 'ro', isa => ArrayRef[Str], predicate => '_has_policy_mx_hosts');
13 13 has total_successful_session_count => (is => 'rw', isa => Int, required => 1);
14 13 has total_failure_session_count => (is => 'rw', isa => Int, required => 1);
15 0 ▶ has failures => (is => 'rw', isa => ArrayRef, required => 0, lazy => 1, builder => sub{return []} );
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine _build_failures: called CC=1 SCAR=0.4
16  
17 59 ▶around BUILDARGS => sub($orig,$class,@args) {
Subroutine __ANON__: called CC=6 SCAR=18.7
18 59 my $args = $class->$orig(@args);
19 59 ▶ if ( exists $args->{policy_mx_hosts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"policy_mx_hosts"}) 6 53 59
20 6 my $hosts = delete $args->{policy_mx_hosts};
21 6 ▶ croak 'Give policy_mx_host or policy_mx_hosts, not both' if exists $args->{policy_mx_host};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"policy_mx_host"} 1 5 6
22 5 ▶ croak 'policy_mx_hosts is not a list of strings' if ref $hosts ne 'ARRAY' || grep { ! defined $_ || ref $_ } $hosts->@*;
Branch: 1 branch
BranchTrueFalseTotal
if ref $hosts ne "ARRAY" or grep {not defined $_ or ref $_;} @$hosts 0 5 5
Condition33%2/6
ref $hosts ne "ARRAY" or grep {not defined $_ or ref $_;} @$hosts
l!l&&r!l&&!r
005
!defined($_) || ref $_
l!l&&r!l&&!r
007
MC/DC0%0/2
ref $hosts ne "ARRAY" or grep {not defined $_ or ref $_;} @$hosts
ref $hosts ne "ARRAY" grep {not defined $_ or ref $_;} @$hosts
MC/DC0%0/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table33%1/3
A or B
A= ref $hosts ne "ARRAY"B= grep {not defined $_ or ref $_;} @$hosts
ABresult
1X1
011
000
Truth table33%1/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
23 5 $args->{policy_mx_host} = join( ' ', $hosts->@* );
24 5 $args->{_policy_mx_hosts} = [ $hosts->@* ];
25 }
26 58 return $args;
27 13 };
28  
29  
30 47 ▶sub new_from_data($class,$data) {
Subroutine new_from_data: called CC=17 SCAR=28.5
Pod documented
31 47 ▶ croak 'Policy is not an object' unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 47 47
32 47 foreach my $key ( qw{ policy summary } ) {
33 94 ▶ croak "$key: not an object" if defined $data->{$key} && ref $data->{$key} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$key} and ref $data->{$key} ne "HASH" 1 93 94
Condition66%2/3
defined $data->{$key} and ref $data->{$key} ne "HASH"
!ll&&!rl&&r
0931
MC/DC50%1/2
defined $data->{$key} and ref $data->{$key} ne "HASH"
defined $data->{$key} ref $data->{$key} ne "HASH"
Truth table66%2/3
A and B
A= defined $data->{$key}B= ref $data->{$key} ne "HASH"
ABresult
0X0
100
111
34 }
35 46 ▶ my $mx_host = $data->{policy}->{'mx-host'} // '';
Condition100%2/2
$data->{"policy"}{"mx-host"} // ""
l!l
3412
MC/DC100%1/1
$data->{"policy"}{"mx-host"} // ""
$data->{"policy"}{"mx-host"}
Truth table100%2/2
A
A= $data->{"policy"}{"mx-host"}
Aresult
11
00
36 46 ▶ if ( ref $mx_host eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mx_host eq "ARRAY") 6 40 46
37 6 ▶ croak 'policy.mx-host: not a list of strings' if grep { ! defined $_ || ref $_ } $mx_host->@*;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_;} @$mx_host 2 4 6
Condition66%2/3
!defined($_) || ref $_
l!l&&r!l&&!r
026
MC/DC50%1/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table66%2/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
38 }
39 44 ▶ my $failure_details = $data->{'failure-details'} // [];
Condition100%2/2
$data->{"failure-details"} // []
l!l
2618
MC/DC100%1/1
$data->{"failure-details"} // []
$data->{"failure-details"}
Truth table100%2/2
A
A= $data->{"failure-details"}
Aresult
11
00
40 44 ▶ croak 'failure-details: not a list' unless ref $failure_details eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $failure_details eq "ARRAY" 1 43 44
41 43 my @failures;
42 43 foreach my $i ( keys $failure_details->@* ) {
43 52 my $failure = eval { Mail::TLSRPT::Failure->new_from_data($failure_details->[$i]) };
44 52 ▶ die "failure-details[$i]: $@" unless $failure;
Branch: 1 branch
BranchTrueFalseTotal
unless $failure 3 49 52
45 49 push @failures, $failure;
46 }
47 my $self = $class->new(
48 policy_type => $data->{policy}->{'policy-type'},
49 policy_string => $data->{policy}->{'policy-string'} // [],
50 policy_domain => $data->{policy}->{'policy-domain'},
51 ref $mx_host eq 'ARRAY'
52 ? ( policy_mx_hosts => $mx_host )
53 : ( policy_mx_host => $mx_host ),
54 total_successful_session_count => $data->{summary}->{'total-successful-session-count'} // 0,
55 40 ▶ total_failure_session_count => $data->{summary}->{'total-failure-session-count'} // 0,
Branch: 1 branch
BranchTrueFalseTotal
ref $mx_host eq 'ARRAY' ? : 4 36 40
Condition66%4/6
$data->{"policy"}{"policy-string"} // []
l!l
2812
$data->{"summary"}{"total-successful-session-count"} // 0
l!l
400
$data->{"summary"}{"total-failure-session-count"} // 0
l!l
400
MC/DC100%1/1
$data->{"policy"}{"policy-string"} // []
$data->{"policy"}{"policy-string"}
MC/DC0%0/1
$data->{"summary"}{"total-successful-session-count"} // 0
$data->{"summary"}{"total-successful-session-count"}
MC/DC0%0/1
$data->{"summary"}{"total-failure-session-count"} // 0
$data->{"summary"}{"total-failure-session-count"}
Truth table100%2/2
A
A= $data->{"policy"}{"policy-string"}
Aresult
11
00
Truth table50%1/2
A
A= $data->{"summary"}{"total-successful-session-count"}
Aresult
11
00
Truth table50%1/2
A
A= $data->{"summary"}{"total-failure-session-count"}
Aresult
11
00
56 failures => \@failures,
57 );
58 39 return $self;
59 }
60  
61  
62 7 ▶sub policy_mx_hosts($self) {
Subroutine policy_mx_hosts: called CC=3 SCAR=11.0
Pod documented
63 7 ▶ return [ $self->_policy_mx_hosts->@* ]
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_policy_mx_hosts and $self->policy_mx_host eq join(" ", @{$self->_policy_mx_hosts}) 5 2 7
Condition100%3/3
$self->_has_policy_mx_hosts and $self->policy_mx_host eq join(" ", @{$self->_policy_mx_hosts})
!ll&&!rl&&r
115
MC/DC100%2/2
$self->_has_policy_mx_hosts and $self->policy_mx_host eq join(" ", @{$self->_policy_mx_hosts})
$self->_has_policy_mx_hosts $self->policy_mx_host eq join(" ", @{$self->_policy_mx_hosts})
Truth table100%3/3
A and B
A= $self->_has_policy_mx_hostsB= $self->policy_mx_host eq join(" ", @{$self->_policy_mx_hosts})
ABresult
0X0
100
111
64 if $self->_has_policy_mx_hosts && $self->policy_mx_host eq join( ' ', $self->_policy_mx_hosts->@* );
65 2 return [ split( ' ', $self->policy_mx_host ) ];
66 }
67  
68  
69 6 ▶sub policy_mode($self) {
Subroutine policy_mode: called CC=6 SCAR=17.9
Pod documented
70 6 ▶ return undef unless $self->policy_type eq 'sts';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->policy_type eq "sts" 2 4 6
71 4 foreach my $line ( $self->policy_string->@* ) {
72 7 ▶ next if ! defined $line || ref $line;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line or ref $line 1 6 7
Condition66%2/3
not defined $line or ref $line
l!l&&r!l&&!r
106
MC/DC50%1/2
not defined $line or ref $line
not defined $line ref $line
Truth table66%2/3
not(A) or B
A= not defined $lineB= ref $line
ABresult
1X1
011
000
73 6 ▶ return $1 if $line =~ /^mode:[ \t]*(\S+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^mode:[ \t]*(\S+)/mu 3 3 6
74 }
75 1 return undef;
76 }
77  
78  
79 8 ▶sub known_policy_types($class) {
Subroutine known_policy_types: called CC=1 SCAR=0.0
Pod documented
80 8 return qw( tlsa sts no-policy-found );
81 }
82  
83  
84 7 ▶sub is_known_policy_type($self) {
Subroutine is_known_policy_type: called CC=1 SCAR=0.0
Pod documented
85 7 return scalar grep { $_ eq $self->policy_type } $self->known_policy_types;
86 }
87  
88  
89 11 ▶sub as_struct($self) {
Subroutine as_struct: called CC=3 SCAR=11.0
Pod documented
90 11 my @failures = map {$_->as_struct} $self->failures->@*;
91 return {
92 11 ▶ policy => {
Branch: 2 branches
BranchTrueFalseTotal
$self->_has_policy_mx_hosts ? : 5 6 11
scalar @{$self->failures} ? : 2 9 11
93 'policy-type' => $self->policy_type,
94 'policy-string' => $self->policy_string,
95 'policy-domain' => $self->policy_domain,
96 'mx-host' => $self->_has_policy_mx_hosts ? $self->policy_mx_hosts : $self->policy_mx_host,
97 },
98 summary => {
99 'total-successful-session-count' => $self->total_successful_session_count,
100 'total-failure-session-count' => $self->total_failure_session_count,
101 },
102 scalar $self->failures->@* ? ( 'failure-details' => \@failures ) : (),
103 };
104 }
105  
106  
107 5 ▶sub as_string($self) {
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
108 return join( "\n",
109 'Policy:',
110 ' Type: '.$self->policy_type,
111 16 ▶ ' String: '. join('; ', map { $_ // '' } $self->policy_string->@*),
Condition100%2/2
$_ // ""
l!l
151
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
112 ' Domain: '.$self->policy_domain,
113 ' MX-Host: '.$self->policy_mx_host,
114 ' Successful-Session-Count: '.$self->total_successful_session_count,
115 ' Failure-Session-Count: '.$self->total_failure_session_count,
116 5 map { $_->as_string } $self->failures->@*,
117 );
118 }
119  
120 2 ▶sub _register_prometheus($self,$prometheus) {
Subroutine _register_prometheus: called CC=1 SCAR=0.0
121 2 $prometheus->declare('tlsrpt_sessions_total', help=>'TLSRPT tls sessions', type=>'counter' );
122 }
123  
124  
125 2 ▶sub process_prometheus($self,$report,$prometheus) {
Subroutine process_prometheus: called CC=3 SCAR=11.0
Pod documented
126 2 $self->_register_prometheus($prometheus);
127 2 $prometheus->add('tlsrpt_sessions_total',$self->total_successful_session_count,{
128 result=>'successful',
129 organization_name=>$report->organization_name,
130 policy_type=>$self->policy_type,
131 policy_domain=>$self->policy_domain,
132 policy_mx_host=>$self->policy_mx_host,
133 });
134 2 $prometheus->add('tlsrpt_sessions_total',$self->total_failure_session_count,{
135 result=>'failure',
136 organization_name=>$report->organization_name,
137 policy_type=>$self->policy_type,
138 policy_domain=>$self->policy_domain,
139 policy_mx_host=>$self->policy_mx_host,
140 });
141 2 ▶ Mail::TLSRPT::Failure->_register_prometheus($prometheus) if ! scalar $self->failures->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->failures} 0 2 2
142 2 foreach my $failure ( $self->failures->@* ) {
143 4 $failure->process_prometheus($self,$report,$prometheus);
144 }
145 }
146  
147 2 ▶sub _csv_headers($self) {
Subroutine _csv_headers: called CC=1 SCAR=0.0
148 return (
149 2 'policy type',
150 'policy string',
151 'policy domain',
152 'policy mx host',
153 'total successful session count',
154 'total failure session count',
155 );
156 }
157  
158 5 ▶sub _csv_fragment($self) {
Subroutine _csv_fragment: called CC=2 SCAR=6.9
159 return (
160 $self->policy_type,
161 5 ▶ join('; ', map { $_ // '' } $self->policy_string->@*),
Condition100%2/2
$_ // ""
l!l
171
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
162 $self->policy_domain,
163 $self->policy_mx_host,
164 $self->total_successful_session_count,
165 $self->total_failure_session_count,
166 );
167 }
168  
169 13 1;
170  
171 __END__
« TLSRPT/App/Command/prometheus.pm ↑ Summary TLSRPT/App/Command/csv.pm »