blib/lib/Mail/AuthenticationResults/Header/Base.pm untested

filter: Statementstmt 0.0% 0 / 275 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 42 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 555 CC 74 SCAR 48.6
CC
74
Cov
0%
search
552.0
set_key
56.0
remove_child
56.0
CRAP 129.5
1 package Mail::AuthenticationResults::Header::Base;
2 # ABSTRACT: Base class for modelling parts of the Authentication Results Header
3  
4 require 5.008;
5 use strict;
6 use warnings;
7 our $VERSION = '2.20260216'; # VERSION
8 use Scalar::Util qw{ weaken refaddr };
9 use JSON;
10 use Carp;
11 use Clone qw{ clone };
12  
13 use Mail::AuthenticationResults::Header::Group;
14 use Mail::AuthenticationResults::FoldableHeader;
15  
16 use Mail::AuthenticationResults::Header::Entry;
17 use Mail::AuthenticationResults::Header::SubEntry;
18 use Mail::AuthenticationResults::Header::Comment;
19  
20  
21  
22 sub _HAS_KEY{ return 0; }
23 sub _HAS_VALUE{ return 0; }
24 sub _HAS_CHILDREN{ return 0; }
25 sub _ALLOWED_CHILDREN{ # uncoverable subroutine
26 # does not run in Base as HAS_CHILDREN returns 0
27 return 0; # uncoverable statement
28 }
29  
30  
31 sub new {
32 my ( $class ) = @_;
33 my $self = {};
34 bless $self, $class;
35 return $self;
36 }
37  
38  
39 sub set_key {
40 my ( $self, $key ) = @_;
41 croak 'Does not have key' if ! $self->_HAS_KEY();
42 croak 'Key cannot be undefined' if ! defined $key;
43 croak 'Key cannot be empty' if $key eq q{};
44 croak 'Invalid characters in key' if $key =~ /"/;
45 croak 'Invalid characters in key' if $key =~ /\n/;
46 croak 'Invalid characters in key' if $key =~ /\r/;
47 $self->{ 'key' } = $key;
48 return $self;
49 }
50  
51  
52 sub key {
53 my ( $self ) = @_;
54 croak 'Does not have key' if ! $self->_HAS_KEY();
55 return q{} if ! defined $self->{ 'key' }; #5.8
56 return $self->{ 'key' };
57 }
58  
59  
60 sub safe_set_value {
61 my ( $self, $value ) = @_;
62  
63 $value = q{} if ! defined $value;
64  
65 $value =~ s/\t/ /g;
66 $value =~ s/\n/ /g;
67 $value =~ s/\r/ /g;
68 $value =~ s/\(/ /g;
69 $value =~ s/\)/ /g;
70 $value =~ s/\\/ /g;
71 $value =~ s/"/ /g;
72 $value =~ s/;/ /g;
73 $value =~ s/^\s+//;
74 $value =~ s/\s+$//;
75  
76 #$value =~ s/ /_/g;
77  
78 $self->set_value( $value );
79 return $self;
80 }
81  
82  
83 sub set_value {
84 my ( $self, $value ) = @_;
85 croak 'Does not have value' if ! $self->_HAS_VALUE();
86 croak 'Value cannot be undefined' if ! defined $value;
87 #croak 'Value cannot be empty' if $value eq q{};
88 croak 'Invalid characters in value' if $value =~ /"/;
89 croak 'Invalid characters in value' if $value =~ /\n/;
90 croak 'Invalid characters in value' if $value =~ /\r/;
91 $self->{ 'value' } = $value;
92 return $self;
93 }
94  
95  
96 sub value {
97 my ( $self ) = @_;
98 croak 'Does not have value' if ! $self->_HAS_VALUE();
99 return q{} if ! defined $self->{ 'value' }; # 5.8
100 return $self->{ 'value' };
101 }
102  
103  
104 sub stringify {
105 my ( $self, $value ) = @_;
106 my $string = $value;
107 $string = q{} if ! defined $string; #5.8;
108 my $strict_quotes = $self->strict_quotes;
109  
110 if ( ( $strict_quotes && $string =~ /[\s\t \(\);=<>@,:\\\/\[\]\?]/ )
111 || ( !$strict_quotes && $string =~ /[\s\t \(\);=]/ ) ) {
112 $string = '"' . $string . '"';
113 }
114  
115 return $string;
116 }
117  
118  
119 sub children {
120 my ( $self ) = @_;
121 croak 'Does not have children' if ! $self->_HAS_CHILDREN();
122 return [] if ! defined $self->{ 'children' }; #5.8
123 return $self->{ 'children' };
124 }
125  
126  
127 sub orphan {
128 my ( $self, $parent ) = @_;
129 croak 'Child does not have a parent' if ! exists $self->{ 'parent' };
130 delete $self->{ 'parent' };
131 return;
132 }
133  
134  
135 sub copy_children_from {
136 my ( $self, $object ) = @_;
137 for my $original_entry (@{$object->children()}) {
138 my $entry = clone $original_entry;
139 $entry->orphan if exists $entry->{ 'parent' };;
140 $self->add_child( $entry );
141 }
142 }
143  
144  
145 sub add_parent {
146 my ( $self, $parent ) = @_;
147 return if ( ref $parent eq 'Mail::AuthenticationResults::Header::Group' );
148 croak 'Child already has a parent' if exists $self->{ 'parent' };
149 croak 'Cannot add parent' if ! $parent->_ALLOWED_CHILDREN( $self ); # uncoverable branch true
150 # Does not run as test is also done in add_child before add_parent is called.
151 $self->{ 'parent' } = $parent;
152 weaken $self->{ 'parent' };
153 return;
154 }
155  
156  
157 sub parent {
158 my ( $self ) = @_;
159 return $self->{ 'parent' };
160 }
161  
162  
163 sub remove_child {
164 my ( $self, $child ) = @_;
165 croak 'Does not have children' if ! $self->_HAS_CHILDREN();
166 croak 'Cannot add child' if ! $self->_ALLOWED_CHILDREN( $child );
167 croak 'Cannot add a class as its own parent' if refaddr $self == refaddr $child; # uncoverable branch true
168 # Does not run as there are no ALLOWED_CHILDREN results which permit this
169  
170 my @children;
171 my $child_removed = 0;
172 foreach my $mychild ( @{ $self->{ 'children' } } ) {
173 if ( refaddr $child == refaddr $mychild ) {
174 if ( ref $self ne 'Mail::AuthenticationResults::Header::Group' ) {
175 $child->orphan();
176 }
177 $child_removed = 1;
178 }
179 else {
180 push @children, $mychild;
181 }
182 }
183 my $children = $self->{ 'children' };
184  
185 croak 'Not a child of this class' if ! $child_removed;
186  
187 $self->{ 'children' } = \@children;
188  
189 return $self;
190 }
191  
192  
193 sub add_child {
194 my ( $self, $child ) = @_;
195 croak 'Does not have children' if ! $self->_HAS_CHILDREN();
196 croak 'Cannot add child' if ! $self->_ALLOWED_CHILDREN( $child );
197 croak 'Cannot add a class as its own parent' if refaddr $self == refaddr $child; # uncoverable branch true
198 # Does not run as there are no ALLOWED_CHILDREN results which permit this
199  
200 $child->add_parent( $self );
201 push @{ $self->{ 'children' } }, $child;
202  
203 return $child;
204 }
205  
206  
207 sub add_entry {
208 my ($self, $key, $value) = @_;
209 my $child = Mail::AuthenticationResults::Header::Entry->new->set_key($key)->safe_set_value($value);
210 $self->add_child($child);
211 return $child;
212 }
213  
214  
215 sub add_sub_entry {
216 my ($self, $key, $value) = @_;
217 my $child = Mail::AuthenticationResults::Header::SubEntry->new->set_key($key)->safe_set_value($value);
218 $self->add_child($child);
219 return $child;
220 }
221  
222  
223 sub add_comment {
224 my ($self, $value) = @_;
225 my $child = Mail::AuthenticationResults::Header::Comment->new->safe_set_value($value);
226 $self->add_child($child);
227 return $child;
228 }
229  
230  
231 sub ancestor {
232 my ( $self ) = @_;
233  
234 my $depth = 0;
235 my $ancestor = $self->parent();
236 my $eldest = $self;
237 while ( defined $ancestor ) {
238 $eldest = $ancestor;
239 $ancestor = $ancestor->parent();
240 $depth++;
241 }
242  
243 return ( $eldest, $depth );
244 }
245  
246  
247 sub strict_quotes {
248 my ( $self ) = @_;
249  
250 return $self->{ 'strict_quotes' } if defined $self->{ 'strict_quotes' };
251  
252 my ( $eldest, $depth ) = $self->ancestor();
253 return 0 if $depth == 0;
254 return $eldest->strict_quotes;
255 }
256  
257  
258 sub set_strict_quotes {
259 my ( $self, $value ) = @_;
260 $self->{ 'strict_quotes' } = $value ? 1 : 0;
261 return $self;
262 }
263  
264  
265 sub as_string_prefix {
266 my ( $self, $header ) = @_;
267  
268 my ( $eldest, $depth ) = $self->ancestor();
269  
270 my $indents = 1;
271 if ( $eldest->can( 'indent_by' ) ) {
272 $indents = $eldest->indent_by();
273 }
274  
275 my $eol = "\n";
276 if ( $eldest->can( 'eol' ) ) {
277 $eol = $eldest->eol();
278 }
279  
280 my $indent = ' ';
281 my $added = 0;
282 if ( $eldest->can( 'indent_on' ) ) {
283 if ( $eldest->indent_on( ref $self ) ) {
284 $header->space( $eol );
285 $header->space( ' ' x ( $indents * $depth ) );
286 $added = 1;
287 }
288 }
289 $header->space( ' ' ) if ! $added;
290  
291 return $indent;
292 }
293  
294 sub _as_hashref {
295 my ( $self ) = @_;
296  
297 my $type = lc ref $self;
298 $type =~ s/^(.*::)//;
299 my $hashref = { 'type' => $type };
300  
301 $hashref->{'key'} = $self->key() if $self->_HAS_KEY();
302 $hashref->{'value'} = $self->value() if $self->_HAS_VALUE();
303 if ( $self->_HAS_CHILDREN() ) {
304 my @children = map { $_->_as_hashref() } @{ $self->children() };
305 $hashref->{'children'} = \@children;
306 }
307 return $hashref;
308 }
309  
310  
311 sub as_json {
312 my ( $self ) = @_;
313 my $J = JSON->new();
314 $J->canonical();
315 return $J->encode( $self->_as_hashref() );
316 }
317  
318  
319 sub as_string {
320 my ( $self ) = @_;
321 my $header = Mail::AuthenticationResults::FoldableHeader->new();
322 $self->build_string( $header );
323 return $header->as_string();
324 }
325  
326  
327 sub build_string {
328 my ( $self, $header ) = @_;
329  
330 if ( ! $self->key() ) {
331 return;
332 }
333  
334 $header->string( $self->stringify( $self->key() ) );
335 if ( $self->value() ) {
336 $header->assignment( '=' );
337 $header->string( $self->stringify( $self->value() ) );
338 }
339 elsif ( $self->value() eq '0' ) {
340 $header->assignment( '=' );
341 $header->string( '0' );
342 }
343 elsif ( $self->value() eq q{} ) {
344 # special case none here
345 if ( $self->key() ne 'none' ) {
346 $header->assignment( '=' );
347 $header->string( '""' );
348 }
349 }
350 if ( $self->_HAS_CHILDREN() ) { # uncoverable branch false
351 # There are no classes which run this code without having children
352 foreach my $child ( @{$self->children()} ) {
353 $child->as_string_prefix( $header );
354 $child->build_string( $header );
355 }
356 }
357 return;
358 }
359  
360  
361 sub search {
362 my ( $self, $search ) = @_;
363  
364 my $group = Mail::AuthenticationResults::Header::Group->new();
365  
366 my $match = 1;
367  
368 if ( exists( $search->{ 'key' } ) ) {
369 if ( $self->_HAS_KEY() ) {
370 if ( ref $search->{ 'key' } eq 'Regexp' && $self->key() =~ m/$search->{'key'}/ ) {
371 $match = $match && 1; # uncoverable statement
372 # $match is always 1 at this point, left this way for consistency
373 }
374 elsif ( lc $search->{ 'key' } eq lc $self->key() ) {
375 $match = $match && 1; # uncoverable statement
376 # $match is always 1 at this point, left this way for consistency
377 }
378 else {
379 $match = 0;
380 }
381 }
382 else {
383 $match = 0;
384 }
385 }
386  
387 if ( exists( $search->{ 'value' } ) ) {
388 $search->{ 'value' } = '' if ! defined $search->{ 'value' };
389 if ( $self->_HAS_VALUE() ) {
390 if ( ref $search->{ 'value' } eq 'Regexp' && $self->value() =~ m/$search->{'value'}/ ) {
391 $match = $match && 1;
392 }
393 elsif ( lc $search->{ 'value' } eq lc $self->value() ) {
394 $match = $match && 1;
395 }
396 else {
397 $match = 0;
398 }
399 }
400 else {
401 $match = 0; # uncoverable statement
402 # There are no code paths with the current classes which end up here
403 }
404 }
405  
406 if ( exists( $search->{ 'authserv_id' } ) ) {
407 if ( $self->_HAS_VALUE() ) {
408 if ( lc ref $self eq 'mail::authenticationresults::header' ) {
409 my $authserv_id = eval{ $self->value()->value() } || q{};
410 if ( ref $search->{ 'authserv_id' } eq 'Regexp' && $authserv_id =~ m/$search->{'authserv_id'}/ ) {
411 $match = $match && 1;
412 }
413 elsif ( lc $search->{ 'authserv_id' } eq lc $authserv_id ) {
414 $match = $match && 1;
415 }
416 else {
417 $match = 0;
418 }
419 }
420 else {
421 $match = 0;
422 }
423 }
424 else {
425 $match = 0; # uncoverable statement
426 # There are no code paths with the current classes which end up here
427 }
428 }
429  
430 if ( exists( $search->{ 'isa' } ) ) {
431 if ( lc ref $self eq 'mail::authenticationresults::header::' . lc $search->{ 'isa' } ) {
432 $match = $match && 1;
433 }
434 elsif ( lc ref $self eq 'mail::authenticationresults::header' && lc $search->{ 'isa' } eq 'header' ) {
435 $match = $match && 1;
436 }
437 else {
438 $match = 0;
439 }
440 }
441  
442 if ( exists( $search->{ 'has' } ) ) {
443 foreach my $query ( @{ $search->{ 'has' } } ) {
444 $match = 0 if ( scalar @{ $self->search( $query )->children() } == 0 );
445 }
446 }
447  
448 if ( $match ) {
449 $group->add_child( $self );
450 }
451  
452 if ( $self->_HAS_CHILDREN() ) {
453 foreach my $child ( @{$self->children()} ) {
454 my $childfound = $child->search( $search );
455 if ( scalar @{ $childfound->children() } ) {
456 $group->add_child( $childfound );
457 }
458 }
459 }
460  
461 return $group;
462 }
463  
464 1;
465  
466 __END__