BIMI/Indicator.pm

filter: Statementstmt 73.6% 106 / 144 Branchbran 43.5% 27 / 62 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.9% 20 / 23 Podpod 100% 7 / 7 totaltotal 65.0% 162 / 249 CC 36 SCAR 43.7
CC
36
Cov
63%
app_validate
272.0
_build_is_valid
15.2
_build_data
5.8
CRAP 79.4
« BIMI/VMC.pm ↑ Summary BIMI/App/Command/checkrecord.pm »
1 package Mail::BIMI::Indicator;
2 # ABSTRACT: Class to model a BIMI indicator
3 29 our $VERSION = '3.20260824'; # VERSION
4 29 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Mail::BIMI::Prelude;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use File::Slurp qw{ read_file write_file };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use IO::Uncompress::Gunzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Term::ANSIColor qw{ :constants };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use XML::LibXML 2.0202;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 our @VALIDATOR_PROFILES = qw{ SVG_1.2_BIMI SVG_1.2_PS Tiny-1.2 };
14  
15 29 extends 'Mail::BIMI::Base';
16 29 with(
17 'Mail::BIMI::Role::HasError',
18 'Mail::BIMI::Role::HasHTTPClient',
19 'Mail::BIMI::Role::Data',
20 'Mail::BIMI::Role::Cacheable',
21 );
22 29 has uri => ( is => 'rw', isa => 'Str', traits => ['CacheKey'],
23 documentation => 'inputs: URL to retrieve Indicator from', );
24 29 has source => ( is => 'rw', isa => 'Str', traits => ['Cacheable'],
25 documentation => 'Human readable summary of where this indicator was retrieved from' );
26 29 has data => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_data', traits => ['Cacheable'],
27 documentation => 'inputs: Raw data representing the Indicator; Fetches from uri if not given', );
28 29 has data_uncompressed => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_data_uncompressed', traits => ['Cacheable'],
29 documentation => 'Raw data in uncompressed form' );
30 29 has data_xml => ( is => 'rw', lazy => 1, builder => '_build_data_xml',
31 documentation => 'XML::LibXML object representing the Indicator' );
32 29 has is_valid => ( is => 'rw', lazy => 1, builder => '_build_is_valid', traits => ['Cacheable'],
33 documentation => 'Is this indicator valid' );
34 29 has parser => ( is => 'rw', lazy => 1, builder => '_build_parser',
35 documentation => 'XML::LibXML::RelaxNG parser object used to validate the Indicator XML' );
36 29 has header => ( is => 'rw', lazy => 1, builder => '_build_header', traits => ['Cacheable'],
37 documentation => 'Indicator data encoded as Base64 ready for insertion as BIMI-Indicator header' );
38 29 has validator_profile => ( is => 'rw', isa => enum(\@VALIDATOR_PROFILES), lazy => 1, builder => '_build_validator_profile', traits => ['Cacheable'],
39 documentation => 'inputs: Validator profile used to validate the Indicator', );
40  
41  
42 9 ▶sub _build_validator_profile($self) {
Subroutine _build_validator_profile: called CC=1 SCAR=0.0
43 9 return $self->bimi_object->options->svg_profile;
44 }
45  
46  
47 4 ▶sub cache_valid_for($self) { return 3600 }
Subroutine cache_valid_for: called CC=1 SCAR=0.0
Pod documented
48  
49  
50 8 ▶sub http_client_max_fetch_size($self) { return $self->bimi_object->options->svg_max_fetch_size };
Subroutine http_client_max_fetch_size: called CC=1 SCAR=0.0
Pod documented
51  
52 28 ▶sub _build_data_uncompressed($self) {
Subroutine _build_data_uncompressed: called CC=3 SCAR=11.0
53 28 my $data = $self->data;
54 28 ▶ if ( $data =~ /^\037\213/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\037\213/u) { } 2 26 28
55 2 $self->log_verbose('Uncompressing SVG');
56  
57 2 my $unzipped;
58 2 IO::Uncompress::Gunzip::gunzip(\$data,\$unzipped);
59 2 ▶ if ( !$unzipped ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unzipped) 1 1 2
60 1 $self->add_error('SVG_UNZIP_ERROR');
61 1 return '';
62 }
63 1 return $unzipped;
64 }
65 else {
66 26 return $data;
67 }
68 }
69  
70  
71 1 ▶sub data_maybe_compressed($self) {
Subroutine data_maybe_compressed: called CC=1 SCAR=0.0
Pod documented
72 # Alias for clarity, the data is as received.
73 1 return $self->data;
74 }
75  
76  
77 0 ▶sub data_uncompressed_normalized($self) {
Subroutine data_uncompressed_normalized: not called CC=1 SCAR=6.9
Pod documented
78 0 my $data = $self->data_uncompressed;
79 0 $data =~ s/\r\n?/\n/g;
80 0 return $data;
81 }
82  
83 26 ▶sub _build_data_xml($self) {
Subroutine _build_data_xml: called CC=3 SCAR=11.0
84 26 my $xml;
85 26 my $data = $self->data_uncompressed;
86 26 ▶ if ( !$data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 25 26
87 1 $self->add_error('SVG_GET_ERROR');
88 1 return;
89 }
90 eval {
91 25 my $parser = XML::LibXML->new(
92 no_network => 1,
93 load_ext_dtd => 0,
94 expand_entities => 0,
95 );
96 25 $xml = $parser->load_xml(
97 string => $self->data_uncompressed,
98 );
99 24 1;
100 25 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $parser = "XML::LibXML"->new("no_network", 1, "load_ext_dtd", 0, "expand_entities", 0); $xml = $parser->load_xml("string", $self->data_uncompressed); 1 } } or do { $self->add_error("SVG_INVALID_XML"); $self->log_verbose("Invalid XML :\n" . $self->data_uncompressed); return } 24 1 25
101 1 $self->add_error('SVG_INVALID_XML');
102 1 $self->log_verbose("Invalid XML :\n".$self->data_uncompressed);
103 1 return;
104 };
105 24 return $xml;
106 }
107  
108 23 ▶sub _build_parser($self) {
Subroutine _build_parser: called CC=1 SCAR=0.0
109 23 state $parser = XML::LibXML::RelaxNG->new(
110 string => $self->get_data_from_file($self->validator_profile.'.rng'),
111 no_network => 1,
112 load_ext_dtd => 0,
113 expand_entities => 0,
114 );
115 23 return $parser;
116 }
117  
118 23 ▶sub _build_data($self) {
Subroutine _build_data: called CC=5 SCAR=17.6
119 23 ▶ if ( ! $self->uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->uri) 1 22 23
120 1 $self->add_error('CODE_MISSING_LOCATION');
121 1 return '';
122 }
123 22 ▶ if ($self->bimi_object->options->svg_from_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bimi_object->options->svg_from_file) 1 21 22
124 1 $self->log_verbose('Reading SVG from file '.$self->bimi_object->options->svg_from_file);
125 1 return scalar read_file $self->bimi_object->options->svg_from_file;
126 }
127 21 $self->log_verbose('HTTP Fetch: '.$self->uri);
128 21 my $response = $self->http_client_get( $self->uri );
129 21 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 21 21
130 0 ▶ if ( $response->{status} == 599 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'status'} == 599) { } 0 0 0
131 0 $self->add_error('SVG_FETCH_ERROR',$response->{content});
132 }
133 else {
134 0 $self->add_error('SVG_FETCH_ERROR',$response->{status});
135 }
136 0 return '';
137 }
138 21 return $response->{content};
139 }
140  
141 25 ▶sub _build_is_valid($self) {
Subroutine _build_is_valid: called CC=12 SCAR=27.2
142  
143 25 ▶ if (!(defined $self->data||$self->uri)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data or $self->uri) 0 25 25
Condition33%1/3
defined $self->data or $self->uri
l!l&&r!l&&!r
2500
MC/DC0%0/2
defined $self->data or $self->uri
defined $self->data $self->uri
Truth table33%1/3
A or B
A= defined $self->dataB= $self->uri
ABresult
1X1
011
000
144 0 $self->add_error('CODE_NOTHING_TO_VALIDATE');
145 0 return 0;
146 }
147  
148 25 ▶ if (!defined $self->data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 25 25
149 0 $self->add_error('CODE_NO_DATA');
150 0 return 0;
151 }
152  
153 25 ▶ if (!$self->data && $self->errors->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->data and @{$self->errors}) 0 25 25
Condition33%1/3
not $self->data and @{$self->errors}
!ll&&!rl&&r
2500
MC/DC0%0/2
not $self->data and @{$self->errors}
not $self->data @{$self->errors}
Truth table33%1/3
not(A) and B
A= not $self->dataB= @{$self->errors}
ABresult
0X0
100
111
154 0 return 0;
155 }
156  
157 25 my $is_valid;
158 25 ▶ if ( length $self->data_uncompressed > $self->bimi_object->options->svg_max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->data_uncompressed > $self->bimi_object->options->svg_max_size) { } 1 24 25
159 1 $self->add_error('SVG_SIZE');
160 }
161 else {
162 24 ▶ if ( $self->bimi_object->options->no_validate_svg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bimi_object->options->no_validate_svg) { } 1 23 24
163 1 $is_valid=1;
164 1 $self->log_verbose('Skipping SVG validation');
165 }
166 else {
167 eval {
168 23 my $data_xml = $self->data_xml;
169 23 ▶ if ($data_xml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_xml) 23 0 23
170 23 $self->parser->validate( $data_xml );
171 22 $is_valid=1;
172 22 $self->log_verbose('SVG is valid');
173 }
174 22 1;
175 23 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $data_xml = $self->data_xml; if ($data_xml) { $self->parser->validate($data_xml); $is_valid = 1; $self->log_verbose("SVG is valid"); } ; 1 } } or do { my $validation_error = $@; my $error_text = ref $validation_error eq "XML::LibXML::Error" ? $validation_error->as_string : $validation_error; $self->add_error("SVG_VALIDATION_ERROR", $error_text) } 22 1 23
176 1 my $validation_error = $@;
177 1 ▶ my $error_text = ref $validation_error eq 'XML::LibXML::Error' ? $validation_error->as_string : $validation_error;
Branch: 1 branch
BranchTrueFalseTotal
ref $validation_error eq 'XML::LibXML::Error' ? : 1 0 1
178 1 $self->add_error('SVG_VALIDATION_ERROR',$error_text);
179 };
180 }
181 }
182  
183 25 ▶ return 0 if $self->errors->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->errors} 2 23 25
184 23 return 1;
185 }
186  
187 4 ▶sub _build_header($self) {
Subroutine _build_header: called CC=2 SCAR=7.0
188 4 ▶ return if !$self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 4 4
189 4 my $base64 = encode_base64( $self->data_uncompressed );
190 4 $base64 =~ s/\n//g;
191 4 my @parts = unpack("(A70)*", $base64);
192 4 return join("\n ", @parts);
193 }
194  
195  
196 0 ▶sub header_crlf($self) {
Subroutine header_crlf: not called CC=1 SCAR=6.9
Pod documented
197 0 my $base64 = encode_base64( $self->data_uncompressed );
198 0 $base64 =~ s/\n//g;
199 0 my @parts = unpack("(A70)*", $base64);
200 0 return "BIMI-Indicator: " . join("\r\n ", @parts)
201 }
202  
203  
204 5 ▶sub finish($self) {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
205 5 $self->_write_cache;
206 }
207  
208  
209 0 ▶sub app_validate($self) {
Subroutine app_validate: not called CC=16 SCAR=56.1
Pod documented
210 0 my $size_raw = 0;
211 0 ▶ $size_raw = length($self->data) if $self->data;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data 0 0 0
212 0 my $size_uncompressed = 0;
213 0 ▶ $size_uncompressed = length($self->data_uncompressed) if $self->data_uncompressed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data_uncompressed 0 0 0
214 0 my $size_header = 0;
215 0 ▶ $size_header = length($self->header_crlf) if $self->header_crlf;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header_crlf 0 0 0
216  
217 0 ▶ say 'Indicator'.($self->source ? ' (From '.$self->source.')' : '' ).' Returned: '.($self->is_valid ? GREEN."\x{2713}" : BRIGHT_RED."\x{26A0}").RESET;
Branch: 2 branches
BranchTrueFalseTotal
$self->source ? : 0 0 0
$self->is_valid ? : 0 0 0
218 0 ▶ say YELLOW.' GZipped '.WHITE.': '.CYAN.($self->data_uncompressed eq $self->data?'No':'Yes').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$self->data_uncompressed eq $self->data ? : 0 0 0
219 0 ▶ say YELLOW.' BIMI-Indicator '.WHITE.': '.CYAN.$self->header.RESET if $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid 0 0 0
220 0 say YELLOW.' Profile Used '.WHITE.': '.CYAN.$self->validator_profile.RESET;
221 0 ▶ say YELLOW.' Is Valid '.WHITE.': '.($self->is_valid?GREEN.'Yes':BRIGHT_RED.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid ? : 0 0 0
222 0 ▶ say YELLOW.' Size (raw) '.WHITE.': '.$size_raw.' bytes'.RESET if $size_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $size_raw 0 0 0
223 0 ▶ say YELLOW.' Size (unzip) '.WHITE.': '.$size_uncompressed.' bytes'.RESET if $size_uncompressed;
Branch: 1 branch
BranchTrueFalseTotal
if $size_uncompressed 0 0 0
224 0 ▶ say YELLOW.' Size (header) '.WHITE.': '.$size_header.' bytes'.RESET if $size_header;
Branch: 1 branch
BranchTrueFalseTotal
if $size_header 0 0 0
225 0 ▶ if ( ! $self->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_valid) 0 0 0
226 0 say "Errors:";
227 0 foreach my $error ( $self->errors->@* ) {
228 0 my $error_code = $error->code;
229 0 my $error_text = $error->description;
230 0 ▶ my $error_detail = $error->detail // '';
Condition0%0/2
$error->detail // ""
l!l
00
MC/DC0%0/1
$error->detail // ""
$error->detail
Truth table0%0/2
A
A= $error->detail
Aresult
11
00
231 0 $error_detail =~ s/\n/\n /g;
232 0 ▶ say BRIGHT_RED." $error_code ".WHITE.': '.CYAN.$error_text.($error_detail?"\n ".$error_detail:'').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$error_detail ? : 0 0 0
233 }
234 }
235 }
236  
237 29 1;
238  
239 __END__
« BIMI/VMC.pm ↑ Summary BIMI/App/Command/checkrecord.pm »