BIMI/Indicator.pm

filter: Statementstmt 74.3% 122 / 164 Branchbran 48.6% 35 / 72 Conditioncond 36.3% 4 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 86.9% 20 / 23 Podpod 100% 7 / 7 totaltotal 66.5% 189 / 284 CC 42 SCAR 43.0
CC
42
Cov
65%
app_validate
272.0
_build_is_valid
17.6
_build_data_uncompressed
7.2
CRAP 73.6
« 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.20261116'; # 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 31 ▶sub _build_data_uncompressed($self) {
Subroutine _build_data_uncompressed: called CC=7 SCAR=19.7
53 31 my $data = $self->data;
54 31 ▶ if ( $data =~ /^\037\213/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\037\213/u) { } 5 26 31
55 5 $self->log_verbose('Uncompressing SVG');
56  
57 # Stream the decompression and stop as soon as we exceed the maximum
58 # allowed size, rather than expanding a potential gzip bomb into memory.
59 5 my $max_size = $self->bimi_object->options->svg_max_size;
60 5 my $unzipped = '';
61 5 my $gunzip = IO::Uncompress::Gunzip->new(\$data);
62 5 ▶ if ( !$gunzip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gunzip) 0 5 5
63 0 $self->add_error('SVG_UNZIP_ERROR');
64 0 return '';
65 }
66 5 while (1) {
67 7 my $buffer;
68 7 my $status = $gunzip->read($buffer, $max_size + 1 - length $unzipped);
69 7 ▶ if ( $status < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status < 0) 1 6 7
70 1 $gunzip->close;
71 1 $self->add_error('SVG_UNZIP_ERROR');
72 1 return '';
73 }
74 6 ▶ last if $status == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 2 4 6
75 4 $unzipped .= $buffer;
76 4 ▶ if ( length $unzipped > $max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $unzipped > $max_size) 2 2 4
77 2 $gunzip->close;
78 2 $self->add_error('SVG_SIZE');
79 2 return '';
80 }
81 }
82 2 $gunzip->close;
83 2 ▶ if ( !$unzipped ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unzipped) 0 2 2
84 0 $self->add_error('SVG_UNZIP_ERROR');
85 0 return '';
86 }
87 2 return $unzipped;
88 }
89 else {
90 26 return $data;
91 }
92 }
93  
94  
95 1 ▶sub data_maybe_compressed($self) {
Subroutine data_maybe_compressed: called CC=1 SCAR=0.0
Pod documented
96 # Alias for clarity, the data is as received.
97 1 return $self->data;
98 }
99  
100  
101 0 ▶sub data_uncompressed_normalized($self) {
Subroutine data_uncompressed_normalized: not called CC=1 SCAR=6.9
Pod documented
102 0 my $data = $self->data_uncompressed;
103 0 $data =~ s/\r\n?/\n/g;
104 0 return $data;
105 }
106  
107 26 ▶sub _build_data_xml($self) {
Subroutine _build_data_xml: called CC=3 SCAR=11.0
108 26 my $xml;
109 26 my $data = $self->data_uncompressed;
110 26 ▶ if ( !$data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 25 26
111 1 $self->add_error('SVG_GET_ERROR');
112 1 return;
113 }
114 eval {
115 25 my $parser = XML::LibXML->new(
116 no_network => 1,
117 load_ext_dtd => 0,
118 expand_entities => 0,
119 );
120 25 $xml = $parser->load_xml(
121 string => $self->data_uncompressed,
122 );
123 24 1;
124 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
125 1 $self->add_error('SVG_INVALID_XML');
126 1 $self->log_verbose("Invalid XML :\n".$self->data_uncompressed);
127 1 return;
128 };
129 24 return $xml;
130 }
131  
132 23 ▶sub _build_parser($self) {
Subroutine _build_parser: called CC=1 SCAR=0.0
133 23 state $parser = XML::LibXML::RelaxNG->new(
134 string => $self->get_data_from_file($self->validator_profile.'.rng'),
135 no_network => 1,
136 load_ext_dtd => 0,
137 expand_entities => 0,
138 );
139 23 return $parser;
140 }
141  
142 23 ▶sub _build_data($self) {
Subroutine _build_data: called CC=5 SCAR=17.6
143 23 ▶ if ( ! $self->uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->uri) 1 22 23
144 1 $self->add_error('CODE_MISSING_LOCATION');
145 1 return '';
146 }
147 22 ▶ if ($self->bimi_object->options->svg_from_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bimi_object->options->svg_from_file) 1 21 22
148 1 $self->log_verbose('Reading SVG from file '.$self->bimi_object->options->svg_from_file);
149 1 return scalar read_file $self->bimi_object->options->svg_from_file;
150 }
151 21 $self->log_verbose('HTTP Fetch: '.$self->uri);
152 21 my $response = $self->http_client_get( $self->uri );
153 21 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 21 21
154 0 ▶ if ( $response->{status} == 599 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'status'} == 599) { } 0 0 0
155 0 $self->add_error('SVG_FETCH_ERROR',$response->{content});
156 }
157 else {
158 0 $self->add_error('SVG_FETCH_ERROR',$response->{status});
159 }
160 0 return '';
161 }
162 21 return $response->{content};
163 }
164  
165 27 ▶sub _build_is_valid($self) {
Subroutine _build_is_valid: called CC=14 SCAR=28.7
166  
167 27 ▶ if (!(defined $self->data||$self->uri)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data or $self->uri) 0 27 27
Condition33%1/3
defined $self->data or $self->uri
l!l&&r!l&&!r
2700
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
168 0 $self->add_error('CODE_NOTHING_TO_VALIDATE');
169 0 return 0;
170 }
171  
172 27 ▶ if (!defined $self->data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 27 27
173 0 $self->add_error('CODE_NO_DATA');
174 0 return 0;
175 }
176  
177 27 ▶ if (!$self->data && $self->errors->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->data and @{$self->errors}) 0 27 27
Condition33%1/3
not $self->data and @{$self->errors}
!ll&&!rl&&r
2700
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
178 0 return 0;
179 }
180  
181 27 ▶ if (!$self->data_uncompressed && $self->errors->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->data_uncompressed and @{$self->errors}) 1 26 27
Condition66%2/3
not $self->data_uncompressed and @{$self->errors}
!ll&&!rl&&r
2601
MC/DC50%1/2
not $self->data_uncompressed and @{$self->errors}
not $self->data_uncompressed @{$self->errors}
Truth table66%2/3
not(A) and B
A= not $self->data_uncompressedB= @{$self->errors}
ABresult
0X0
100
111
182 1 return 0;
183 }
184  
185 26 my $is_valid;
186 26 ▶ 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 25 26
187 1 $self->add_error('SVG_SIZE');
188 }
189 else {
190 25 ▶ if ( $self->bimi_object->options->no_validate_svg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bimi_object->options->no_validate_svg) { } 2 23 25
191 2 $is_valid=1;
192 2 $self->log_verbose('Skipping SVG validation');
193 }
194 else {
195 eval {
196 23 my $data_xml = $self->data_xml;
197 23 ▶ if ($data_xml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_xml) 23 0 23
198 23 $self->parser->validate( $data_xml );
199 22 $is_valid=1;
200 22 $self->log_verbose('SVG is valid');
201 }
202 22 1;
203 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
204 1 my $validation_error = $@;
205 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
206 1 $self->add_error('SVG_VALIDATION_ERROR',$error_text);
207 };
208 }
209 }
210  
211 26 ▶ return 0 if $self->errors->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->errors} 2 24 26
212 24 return 1;
213 }
214  
215 4 ▶sub _build_header($self) {
Subroutine _build_header: called CC=2 SCAR=7.0
216 4 ▶ return if !$self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 4 4
217 4 my $base64 = encode_base64( $self->data_uncompressed );
218 4 $base64 =~ s/\n//g;
219 4 my @parts = unpack("(A70)*", $base64);
220 4 return join("\n ", @parts);
221 }
222  
223  
224 0 ▶sub header_crlf($self) {
Subroutine header_crlf: not called CC=1 SCAR=6.9
Pod documented
225 0 my $base64 = encode_base64( $self->data_uncompressed );
226 0 $base64 =~ s/\n//g;
227 0 my @parts = unpack("(A70)*", $base64);
228 0 return "BIMI-Indicator: " . join("\r\n ", @parts)
229 }
230  
231  
232 5 ▶sub finish($self) {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
233 5 $self->_write_cache;
234 }
235  
236  
237 0 ▶sub app_validate($self) {
Subroutine app_validate: not called CC=16 SCAR=56.1
Pod documented
238 0 my $size_raw = 0;
239 0 ▶ $size_raw = length($self->data) if $self->data;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data 0 0 0
240 0 my $size_uncompressed = 0;
241 0 ▶ $size_uncompressed = length($self->data_uncompressed) if $self->data_uncompressed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data_uncompressed 0 0 0
242 0 my $size_header = 0;
243 0 ▶ $size_header = length($self->header_crlf) if $self->header_crlf;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header_crlf 0 0 0
244  
245 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
246 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
247 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
248 0 say YELLOW.' Profile Used '.WHITE.': '.CYAN.$self->validator_profile.RESET;
249 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
250 0 ▶ say YELLOW.' Size (raw) '.WHITE.': '.$size_raw.' bytes'.RESET if $size_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $size_raw 0 0 0
251 0 ▶ say YELLOW.' Size (unzip) '.WHITE.': '.$size_uncompressed.' bytes'.RESET if $size_uncompressed;
Branch: 1 branch
BranchTrueFalseTotal
if $size_uncompressed 0 0 0
252 0 ▶ say YELLOW.' Size (header) '.WHITE.': '.$size_header.' bytes'.RESET if $size_header;
Branch: 1 branch
BranchTrueFalseTotal
if $size_header 0 0 0
253 0 ▶ if ( ! $self->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_valid) 0 0 0
254 0 say "Errors:";
255 0 foreach my $error ( $self->errors->@* ) {
256 0 my $error_code = $error->code;
257 0 my $error_text = $error->description;
258 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
259 0 $error_detail =~ s/\n/\n /g;
260 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
261 }
262 }
263 }
264  
265 29 1;
266  
267 __END__
« BIMI/VMC.pm ↑ Summary BIMI/App/Command/checkrecord.pm »