lib/Mail/AuthenticationResults/Parser.pm

filter: Statementstmt 100% 206 / 206 Branchbran 96.5% 112 / 116 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 98.9% 366 / 370 CC 65 SCAR 26.1
CC
65
Cov
99%
_parse_entry
22.0
tokenise
18.0
_parse_authservid
15.0
CRAP 13.6
« lib/Mail/AuthenticationResults/Header/Base.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Header/Version.pm »
1 package Mail::AuthenticationResults::Parser;
2 # ABSTRACT: Class for parsing Authentication Results Headers
3  
4 24 require 5.008;
5 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 our $VERSION = '2.20260216'; # VERSION
8 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 24 ▶use Mail::AuthenticationResults::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Mail::AuthenticationResults::Header::AuthServID;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Mail::AuthenticationResults::Header::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Mail::AuthenticationResults::Header::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use Mail::AuthenticationResults::Header::SubEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use Mail::AuthenticationResults::Header::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 24 ▶use Mail::AuthenticationResults::Token::Assignment;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 24 ▶use Mail::AuthenticationResults::Token::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 24 ▶use Mail::AuthenticationResults::Token::QuotedString;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶use Mail::AuthenticationResults::Token::Separator;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 24 ▶use Mail::AuthenticationResults::Token::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 24 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 62 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
27 62 my ( $class, $auth_header ) = @_;
28 62 my $self = {};
29 62 bless $self, $class;
30  
31 62 ▶ if ( $auth_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_header) 12 50 62
32 12 $self->parse( $auth_header );
33 }
34  
35 62 return $self;
36 }
37  
38  
39 61 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
40 61 my ( $self, $header ) = @_;
41  
42 61 $self->tokenise( $header );
43  
44 56 $self->_parse_authservid();
45  
46 49 ▶ while ( @{ $self->{ 'tokenised' } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"tokenised"};}) 114 45 159
47 114 $self->_parse_entry();
48 }
49  
50 45 return $self->parsed();
51 }
52  
53  
54 1 ▶sub from_authentication_results_json {
Subroutine from_authentication_results_json: called CC=1 SCAR=0.0
Pod documented
55 1 my ( $self, $json ) = @_;
56 1 my $j = JSON->new();
57 1 my $hashref = $j->decode( $json );
58 1 return $self->_from_hashref( $hashref );
59 }
60  
61 24 ▶sub _from_hashref {
Subroutine _from_hashref: called CC=11 SCAR=24.0
62 24 my ( $self, $hashref ) = @_;
63 24 my $type = $hashref->{'type'};
64 24 ▶ my $object
Branch: 5 branches
BranchTrueFalseTotal
$type eq 'header' ? : 1 23 24
$type eq 'authservid' ? : 1 22 23
$type eq 'entry' ? : 8 14 22
$type eq 'subentry' ? : 8 6 14
$type eq 'comment' ? : 6 0 6
65 = $type eq 'header' ? Mail::AuthenticationResults::Header->new()
66 : $type eq 'authservid' ? Mail::AuthenticationResults::Header::AuthServID->new()
67 : $type eq 'entry' ? Mail::AuthenticationResults::Header::Entry->new()
68 : $type eq 'subentry' ? Mail::AuthenticationResults::Header::SubEntry->new()
69 : $type eq 'comment' ? Mail::AuthenticationResults::Header::Comment->new()
70 : croak "unknown type $type";
71  
72 24 ▶ if ( $type eq 'header' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'header') { } 1 23 24
73 1 my $authserv_id = $self->_from_hashref( $hashref->{ 'authserv_id' } );
74 1 $object->set_value( $authserv_id );
75 }
76 else {
77 23 ▶ $object->set_key( $hashref->{'key'} ) if exists $hashref->{'key'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{"key"} 16 7 23
78 23 ▶ $object->safe_set_value( $hashref->{'value'} ) if exists $hashref->{'value'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{"value"} 23 0 23
79 }
80  
81 24 ▶ if ( exists $hashref->{'children'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hashref->{"children"}) 18 6 24
82 18 for my $child ( @{ $hashref->{'children'} } ) {
83 22 my $child_object = $self->_from_hashref( $child );
84 22 $object->add_child( $child_object );
85 }
86 }
87  
88 24 return $object;
89 }
90  
91  
92  
93 61 ▶sub tokenise {
Subroutine tokenise: called CC=18 SCAR=28.9
Pod documented
94 61 my ( $self, $header ) = @_;
95  
96 61 my @tokenised;
97  
98 61 $header =~ s/\n/ /g;
99 61 $header =~ s/\r/ /g;
100 61 $header =~ s/^\s+//;
101  
102 # Remove Header part if present
103 61 ▶ if ( $header =~ /^Authentication-Results:/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^Authentication-Results:/i) 13 48 61
104 13 $header =~ s/^Authentication-Results://i;
105 }
106  
107 61 my $args = {};
108 61 ▶ while ( length($header) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $header > 0) 1252 56 1308
109  
110 1252 my $token;
111 1252 $header =~ s/^\s+//;
112  
113 1252 ▶ my $last_non_comment_type = exists( $args->{ 'last_non_comment_type' } ) ? $args->{ 'last_non_comment_type' }->is() : 'none';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'last_non_comment_type'} ? : 1192 60 1252
114  
115 1252 ▶ if ( length( $header ) == 0 ) {
Branch: 9 branches
BranchTrueFalseTotal
if (length $header == 0) { } 2 1250 1252
elsif ($header =~ /^\(/) { } 73 1177 1250
elsif ($header =~ /^;/) { } 132 1045 1177
elsif ($header =~ /^"/) { } 38 1007 1045
elsif ($last_non_comment_type ne 'assignment' and $header =~ /^\./) { } 83 924 1007
elsif ($last_non_comment_type eq 'assignment' and $header =~ /^\./) { } 4 920 924
elsif ($last_non_comment_type ne 'assignment' and $header =~ m[^/]) { } 4 916 920
elsif ($last_non_comment_type eq 'assignment' and $header =~ m[^/]) { } 2 914 916
elsif ($header =~ /^=/) { } 269 645 914
116 2 last;
117 }
118 elsif ( $header =~ /^\(/ ) {
119 73 $token = Mail::AuthenticationResults::Token::Comment->new( $header, $args );
120 }
121 elsif ( $header =~ /^;/ ) {
122 132 $token = Mail::AuthenticationResults::Token::Separator->new( $header, $args );
123 132 $args->{ 'last_non_comment_type' } = $token;
124 }
125 elsif ( $header =~ /^"/ ) {
126 38 $token = Mail::AuthenticationResults::Token::QuotedString->new( $header, $args );
127 38 $args->{ 'last_non_comment_type' } = $token;
128 }
129 ▶ elsif ( $last_non_comment_type ne 'assignment' && $header =~ /^\./ ) {
Condition100%3/3
$last_non_comment_type ne "assignment" and $header =~ /^\./
!ll&&!rl&&r
32959583
MC/DC100%2/2
$last_non_comment_type ne "assignment" and $header =~ /^\./
$last_non_comment_type ne "assignment" $header =~ /^\./
Truth table100%3/3
A and B
A= $last_non_comment_type ne "assignment"B= $header =~ /^\./
ABresult
0X0
100
111
130 83 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
131 83 $args->{ 'last_non_comment_type' } = $token;
132 }
133 ▶ elsif ( $last_non_comment_type eq 'assignment' && $header =~ /^\./ ) {
Condition100%3/3
$last_non_comment_type eq "assignment" and $header =~ /^\./
!ll&&!rl&&r
5953254
MC/DC100%2/2
$last_non_comment_type eq "assignment" and $header =~ /^\./
$last_non_comment_type eq "assignment" $header =~ /^\./
Truth table100%3/3
A and B
A= $last_non_comment_type eq "assignment"B= $header =~ /^\./
ABresult
0X0
100
111
134 # a . after an assignment cannot be another assignment, likely an unquoted string.
135 4 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
136 2 $args->{ 'last_non_comment_type' } = $token;
137 }
138 ▶ elsif ( $last_non_comment_type ne 'assignment' && $header =~ /^\// ) {
Condition100%3/3
$last_non_comment_type ne "assignment" and $header =~ m[^/]
!ll&&!rl&&r
3255914
MC/DC100%2/2
$last_non_comment_type ne "assignment" and $header =~ m[^/]
$last_non_comment_type ne "assignment" $header =~ m[^/]
Truth table100%3/3
A and B
A= $last_non_comment_type ne "assignment"B= $header =~ m[^/]
ABresult
0X0
100
111
139 4 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
140 4 $args->{ 'last_non_comment_type' } = $token;
141 }
142 ▶ elsif ( $last_non_comment_type eq 'assignment' && $header =~ /^\// ) {
Condition100%3/3
$last_non_comment_type eq "assignment" and $header =~ m[^/]
!ll&&!rl&&r
5913232
MC/DC100%2/2
$last_non_comment_type eq "assignment" and $header =~ m[^/]
$last_non_comment_type eq "assignment" $header =~ m[^/]
Truth table100%3/3
A and B
A= $last_non_comment_type eq "assignment"B= $header =~ m[^/]
ABresult
0X0
100
111
143 # a / after an assignment cannot be another assignment, likely an unquoted string.
144 2 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
145 1 $args->{ 'last_non_comment_type' } = $token;
146 }
147 elsif ( $header =~ /^=/ ) {
148 269 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
149 269 $args->{ 'last_non_comment_type' } = $token;
150 }
151 else {
152 645 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
153 645 $args->{ 'last_non_comment_type' } = $token;
154 }
155  
156 1247 $header = $token->remainder();
157 1247 push @tokenised, $token;
158 }
159  
160 58 ▶ croak 'Nothing to parse' if ! @tokenised;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokenised 2 56 58
161  
162 56 $self->{ 'tokenised' } = \@tokenised;
163  
164 56 return;
165 }
166  
167 56 ▶sub _parse_authservid {
Subroutine _parse_authservid: called CC=15 SCAR=27.1
168 56 my ( $self ) = @_;
169 56 my $tokenised = $self->{ 'tokenised' };
170 56 my $token;
171  
172 56 my $authserv_id = Mail::AuthenticationResults::Header::AuthServID->new();
173  
174 # Find the ServID
175 56 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 57 0 57
176 57 $token = shift @$tokenised;
177 57 ▶ if ( $token->is() eq 'string' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->is eq 'string') { } 55 2 57
elsif ($token->is eq 'comment') { } 1 1 2
178 55 $authserv_id->set_value( $token->value() );
179 55 last;
180 }
181 elsif ( $token->is() eq 'comment' ) {
182 1 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
183 }
184 else {
185 # assignment or separator, both are bogus
186 1 croak 'Invalid AuthServ-ID';
187 }
188 }
189  
190 55 my $expecting = 'key';
191 55 my $key;
192  
193 TOKEN:
194 55 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 69 1 70
195 69 $token = shift @$tokenised;
196  
197 69 ▶ if ( $token->is() eq 'assignment' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->is eq 'assignment') { } 7 62 69
elsif ($token->is eq 'comment') { } 3 59 62
elsif ($token->is eq 'separator') { } 48 11 59
198 7 ▶ if ( $expecting eq 'assignment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq 'assignment') { } 4 3 7
199 4 ▶ if ( $token->value() eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value eq '=') { } 2 2 4
200 2 $expecting = 'value';
201 }
202 else {
203 2 croak 'unexpected token';
204 }
205 }
206 else {
207 3 croak 'not expecting an assignment';
208 }
209 }
210 elsif ( $token->is() eq 'comment' ) {
211 3 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
212 }
213 elsif ( $token->is() eq 'separator' ) {
214 48 last TOKEN;
215 }
216 16 ▶ if ( $token->is() eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is eq "string") 11 5 16
217 11 ▶ if ( $expecting eq 'key' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expecting eq 'key') { } 9 2 11
elsif ($expecting eq 'value') { } 1 1 2
218 9 $key = $token;
219 9 $expecting = 'assignment';
220 }
221 elsif ( $expecting eq 'value' ) {
222 1 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() )->set_value( $token->value() ) );
223 1 $expecting = 'key';
224 1 undef $key;
225 }
226 else {
227 1 croak 'not expecting a string';
228 }
229 }
230  
231 }
232 49 ▶ if ( $expecting ne 'key' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting ne "key") 4 45 49
233 4 ▶ if ( $key->value() =~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->value =~ /^[0-9]+$/) { } 2 2 4
234 # Looks like a version
235 2 $authserv_id->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $key->value() ) );
236 }
237 else {
238 # Probably bogus, but who knows!
239 2 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() ) );
240 }
241 }
242  
243 49 $self->{ 'header' } = Mail::AuthenticationResults::Header->new()->set_value( $authserv_id );
244 49 $self->{ 'tokenised' } = $tokenised;
245  
246 49 return;
247 }
248  
249 114 ▶sub _parse_entry {
Subroutine _parse_entry: called CC=22 SCAR=30.9
250 114 my ( $self ) = @_;
251 114 my $tokenised = $self->{ 'tokenised' };
252  
253 114 my $entry = Mail::AuthenticationResults::Header::Entry->new();
254 114 my $working_on = $entry;
255  
256 114 my $expecting = 'key';
257 114 my $is_subentry = 0;
258 TOKEN:
259 114 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 1081 38 1119
260 1081 my $token = shift @$tokenised;
261  
262 1081 ▶ if ( $token->is() eq 'assignment' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->is eq 'assignment') { } 339 742 1081
elsif ($token->is eq 'comment') { } 69 673 742
elsif ($token->is eq 'separator') { } 72 601 673
263 339 ▶ if ( $expecting eq 'assignment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq 'assignment') { } 338 1 339
264 338 ▶ if ( $token->value() eq '=' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->value eq '=') { } 257 81 338
elsif ($token->value eq '.') { } 79 2 81
elsif ($token->value eq '/') { } 2 0 2
265 257 $expecting = 'value';
266 }
267 elsif ( $token->value() eq '.' ) {
268 79 $expecting = 'keymod';
269 }
270 elsif ( $token->value() eq '/' ) {
271 2 $expecting = 'version';
272 }
273 }
274 else {
275 1 croak 'not expecting an assignment';
276 }
277 }
278 elsif ( $token->is() eq 'comment' ) {
279 69 $working_on->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
280 }
281 elsif ( $token->is() eq 'separator' ) {
282 72 last TOKEN;
283 }
284 1008 ▶ if ( $token->is() eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is eq "string") 601 407 1008
285 601 ▶ if ( $expecting eq 'key' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($expecting eq 'key') { } 268 333 601
elsif ($expecting eq 'keymod') { } 79 254 333
elsif ($expecting eq 'version') { } 2 252 254
elsif ($expecting eq 'value') { } 250 2 252
286 268 ▶ if ( ! $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_subentry) { } 114 154 268
287 114 ▶ if ( $token->value() eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value eq 'none') { } 7 107 114
288 # Special case the none
289 7 $expecting = 'no_more_after_none';
290 }
291 else {
292 107 $entry->set_key( $token->value() );
293 107 $expecting = 'assignment';
294 }
295 }
296 else {
297 154 $working_on = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $token->value() );
298 154 $expecting = 'assignment';
299 }
300 }
301 elsif ( $expecting eq 'keymod' ) {
302 79 $working_on->set_key( $working_on->key() . '.' . $token->value() );
303 79 $expecting = 'assignment';
304 }
305 elsif ( $expecting eq 'version' ) {
306 2 ▶ if ( $token->value() =~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value =~ /^[0-9]+$/) { } 1 1 2
307 # Looks like a version
308 1 $working_on->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $token->value() ) );
309 }
310 else {
311 1 croak 'bad version token';
312 }
313 1 $expecting = 'assignment';
314 }
315 elsif ( $expecting eq 'value' ) {
316 250 ▶ if ( ! $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_subentry) { } 100 150 250
317 100 $entry->set_value( $token->value() );
318 100 $is_subentry = 1;
319 }
320 else {
321 150 $entry->add_child( $working_on->set_value( $token->value() ) );
322 }
323 250 $expecting = 'key';
324 }
325 else {
326 2 croak 'not expecting a string';
327 }
328 }
329  
330 }
331  
332 110 ▶ if ( $expecting eq 'no_more_after_none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq "no_more_after_none") 5 105 110
333 5 $self->{ 'tokenised' } = $tokenised;
334 # We may have comment entries, if so add those to the header object
335 5 foreach my $child ( @{ $entry->children() } ) {
336 2 delete $child->{ 'parent' };
337 2 $self->{ 'header' }->add_child( $child );
338 }
339 5 return;
340 }
341  
342 105 ▶ if ( $expecting ne 'key' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting ne "key") 9 96 105
343 9 ▶ if ( $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_subentry) 4 5 9
344 4 $entry->add_child( $working_on );
345 }
346 }
347  
348 105 $self->{ 'header' }->add_child( $entry );
349 105 $self->{ 'tokenised' } = $tokenised;
350  
351 105 return;
352 }
353  
354  
355 57 ▶sub parsed {
Subroutine parsed: called CC=1 SCAR=0.0
Pod documented
356 57 my ( $self ) = @_;
357 57 return $self->{ 'header' };
358 }
359  
360 24 1;
361  
362 __END__
« lib/Mail/AuthenticationResults/Header/Base.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Header/Version.pm »