lib/Mail/AuthenticationResults/Parser.pm

filter: Statementstmt 100% 204 / 204 Branchbran 96.4% 110 / 114 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 98.8% 352 / 356 SCAR 41.3
CC
62
Cov
99%
_parse_entry
22.0
_parse_authservid
15.0
tokenise
15.0
CRAP 62.0
« blib/lib/Mail/AuthenticationResults/Header/Version.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Token/Comment.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.20230112'; # 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 sub new {
27 61 ▶ my ( $class, $auth_header ) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
28 61 my $self = {};
29 61 bless $self, $class;
30  
31 61 ▶ if ( $auth_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_header) 12 49 61
32 12 $self->parse( $auth_header );
33 }
34  
35 61 return $self;
36 }
37  
38  
39 sub parse {
40 60 ▶ my ( $self, $header ) = @_;
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
41  
42 60 $self->tokenise( $header );
43  
44 56 $self->_parse_authservid();
45  
46 49 ▶ while ( @{ $self->{ 'tokenised' } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"tokenised"};}) 113 44 157
47 113 $self->_parse_entry();
48 }
49  
50 44 return $self->parsed();
51 }
52  
53  
54 sub from_authentication_results_json {
55 1 ▶ my ( $self, $json ) = @_;
Subroutine from_authentication_results_json: called CC=1 SCAR=0.0
Pod documented
56 1 my $j = JSON->new();
57 1 my $hashref = $j->decode( $json );
58 1 return $self->_from_hashref( $hashref );
59 }
60  
61 sub _from_hashref {
62 24 ▶ my ( $self, $hashref ) = @_;
Subroutine _from_hashref: called CC=11 SCAR=24.0
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 sub tokenise {
94 60 ▶ my ( $self, $header ) = @_;
Subroutine tokenise: called CC=15 SCAR=27.1
Pod documented
95  
96 60 my @tokenised;
97  
98 60 $header =~ s/\n/ /g;
99 60 $header =~ s/\r/ /g;
100 60 $header =~ s/^\s+//;
101  
102 # Remove Header part if present
103 60 ▶ if ( $header =~ /^Authentication-Results:/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^Authentication-Results:/i) 13 47 60
104 13 $header =~ s/^Authentication-Results://i;
105 }
106  
107 60 my $args = {};
108 60 ▶ while ( length($header) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $header > 0) 1244 56 1300
109  
110 1244 my $token;
111 1244 $header =~ s/^\s+//;
112  
113 1244 ▶ 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'} ? : 1185 59 1244
114  
115 1244 ▶ if ( length( $header ) == 0 ) {
Branch: 8 branches
BranchTrueFalseTotal
if (length $header == 0) { } 2 1242 1244
elsif ($header =~ /^\(/) { } 73 1169 1242
elsif ($header =~ /^;/) { } 131 1038 1169
elsif ($header =~ /^"/) { } 38 1000 1038
elsif ($last_non_comment_type ne 'assignment' and $header =~ /^\./) { } 83 917 1000
elsif ($last_non_comment_type eq 'assignment' and $header =~ /^\./) { } 4 913 917
elsif ($header =~ m[^/]) { } 5 908 913
elsif ($header =~ /^=/) { } 267 641 908
116 2 last;
117 }
118 elsif ( $header =~ /^\(/ ) {
119 73 $token = Mail::AuthenticationResults::Token::Comment->new( $header, $args );
120 }
121 elsif ( $header =~ /^;/ ) {
122 131 $token = Mail::AuthenticationResults::Token::Separator->new( $header, $args );
123 131 $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
32858983
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
5893244
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 ( $header =~ /^\// ) {
139 5 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
140 5 $args->{ 'last_non_comment_type' } = $token;
141 }
142 elsif ( $header =~ /^=/ ) {
143 267 $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
144 267 $args->{ 'last_non_comment_type' } = $token;
145 }
146 else {
147 641 $token = Mail::AuthenticationResults::Token::String->new( $header, $args );
148 641 $args->{ 'last_non_comment_type' } = $token;
149 }
150  
151 1240 $header = $token->remainder();
152 1240 push @tokenised, $token;
153 }
154  
155 58 ▶ croak 'Nothing to parse' if ! @tokenised;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokenised 2 56 58
156  
157 56 $self->{ 'tokenised' } = \@tokenised;
158  
159 56 return;
160 }
161  
162 sub _parse_authservid {
163 56 ▶ my ( $self ) = @_;
Subroutine _parse_authservid: called CC=15 SCAR=27.1
164 56 my $tokenised = $self->{ 'tokenised' };
165 56 my $token;
166  
167 56 my $authserv_id = Mail::AuthenticationResults::Header::AuthServID->new();
168  
169 # Find the ServID
170 56 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 57 0 57
171 57 $token = shift @$tokenised;
172 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
173 55 $authserv_id->set_value( $token->value() );
174 55 last;
175 }
176 elsif ( $token->is() eq 'comment' ) {
177 1 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
178 }
179 else {
180 # assignment or separator, both are bogus
181 1 croak 'Invalid AuthServ-ID';
182 }
183 }
184  
185 55 my $expecting = 'key';
186 55 my $key;
187  
188 TOKEN:
189 55 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 69 1 70
190 69 $token = shift @$tokenised;
191  
192 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
193 7 ▶ if ( $expecting eq 'assignment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq 'assignment') { } 4 3 7
194 4 ▶ if ( $token->value() eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value eq '=') { } 2 2 4
195 2 $expecting = 'value';
196 }
197 else {
198 2 croak 'unexpected token';
199 }
200 }
201 else {
202 3 croak 'not expecting an assignment';
203 }
204 }
205 elsif ( $token->is() eq 'comment' ) {
206 3 $authserv_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
207 }
208 elsif ( $token->is() eq 'separator' ) {
209 48 last TOKEN;
210 }
211 16 ▶ if ( $token->is() eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is eq "string") 11 5 16
212 11 ▶ if ( $expecting eq 'key' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expecting eq 'key') { } 9 2 11
elsif ($expecting eq 'value') { } 1 1 2
213 9 $key = $token;
214 9 $expecting = 'assignment';
215 }
216 elsif ( $expecting eq 'value' ) {
217 1 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() )->set_value( $token->value() ) );
218 1 $expecting = 'key';
219 1 undef $key;
220 }
221 else {
222 1 croak 'not expecting a string';
223 }
224 }
225  
226 }
227 49 ▶ if ( $expecting ne 'key' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting ne "key") 4 45 49
228 4 ▶ if ( $key->value() =~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->value =~ /^[0-9]+$/) { } 2 2 4
229 # Looks like a version
230 2 $authserv_id->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $key->value() ) );
231 }
232 else {
233 # Probably bogus, but who knows!
234 2 $authserv_id->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $key->value() ) );
235 }
236 }
237  
238 49 $self->{ 'header' } = Mail::AuthenticationResults::Header->new()->set_value( $authserv_id );
239 49 $self->{ 'tokenised' } = $tokenised;
240  
241 49 return;
242 }
243  
244 sub _parse_entry {
245 113 ▶ my ( $self ) = @_;
Subroutine _parse_entry: called CC=22 SCAR=30.9
246 113 my $tokenised = $self->{ 'tokenised' };
247  
248 113 my $entry = Mail::AuthenticationResults::Header::Entry->new();
249 113 my $working_on = $entry;
250  
251 113 my $expecting = 'key';
252 113 my $is_subentry = 0;
253 TOKEN:
254 113 ▶ while ( @$tokenised ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokenised) 1074 37 1111
255 1074 my $token = shift @$tokenised;
256  
257 1074 ▶ if ( $token->is() eq 'assignment' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->is eq 'assignment') { } 338 736 1074
elsif ($token->is eq 'comment') { } 69 667 736
elsif ($token->is eq 'separator') { } 71 596 667
258 338 ▶ if ( $expecting eq 'assignment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq 'assignment') { } 336 2 338
259 336 ▶ if ( $token->value() eq '=' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->value eq '=') { } 254 82 336
elsif ($token->value eq '.') { } 80 2 82
elsif ($token->value eq '/') { } 2 0 2
260 254 $expecting = 'value';
261 }
262 elsif ( $token->value() eq '.' ) {
263 80 $expecting = 'keymod';
264 }
265 elsif ( $token->value() eq '/' ) {
266 2 $expecting = 'version';
267 }
268 }
269 else {
270 2 croak 'not expecting an assignment';
271 }
272 }
273 elsif ( $token->is() eq 'comment' ) {
274 69 $working_on->add_child( Mail::AuthenticationResults::Header::Comment->new()->set_value( $token->value() ) );
275 }
276 elsif ( $token->is() eq 'separator' ) {
277 71 last TOKEN;
278 }
279 1001 ▶ if ( $token->is() eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is eq "string") 596 405 1001
280 596 ▶ if ( $expecting eq 'key' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($expecting eq 'key') { } 266 330 596
elsif ($expecting eq 'keymod') { } 79 251 330
elsif ($expecting eq 'version') { } 2 249 251
elsif ($expecting eq 'value') { } 247 2 249
281 266 ▶ if ( ! $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_subentry) { } 113 153 266
282 113 ▶ if ( $token->value() eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value eq 'none') { } 7 106 113
283 # Special case the none
284 7 $expecting = 'no_more_after_none';
285 }
286 else {
287 106 $entry->set_key( $token->value() );
288 106 $expecting = 'assignment';
289 }
290 }
291 else {
292 153 $working_on = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( $token->value() );
293 153 $expecting = 'assignment';
294 }
295 }
296 elsif ( $expecting eq 'keymod' ) {
297 79 $working_on->set_key( $working_on->key() . '.' . $token->value() );
298 79 $expecting = 'assignment';
299 }
300 elsif ( $expecting eq 'version' ) {
301 2 ▶ if ( $token->value() =~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->value =~ /^[0-9]+$/) { } 1 1 2
302 # Looks like a version
303 1 $working_on->add_child( Mail::AuthenticationResults::Header::Version->new()->set_value( $token->value() ) );
304 }
305 else {
306 1 croak 'bad version token';
307 }
308 1 $expecting = 'assignment';
309 }
310 elsif ( $expecting eq 'value' ) {
311 247 ▶ if ( ! $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_subentry) { } 98 149 247
312 98 $entry->set_value( $token->value() );
313 98 $is_subentry = 1;
314 }
315 else {
316 149 $entry->add_child( $working_on->set_value( $token->value() ) );
317 }
318 247 $expecting = 'key';
319 }
320 else {
321 2 croak 'not expecting a string';
322 }
323 }
324  
325 }
326  
327 108 ▶ if ( $expecting eq 'no_more_after_none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting eq "no_more_after_none") 5 103 108
328 5 $self->{ 'tokenised' } = $tokenised;
329 # We may have comment entries, if so add those to the header object
330 5 foreach my $child ( @{ $entry->children() } ) {
331 2 delete $child->{ 'parent' };
332 2 $self->{ 'header' }->add_child( $child );
333 }
334 5 return;
335 }
336  
337 103 ▶ if ( $expecting ne 'key' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting ne "key") 9 94 103
338 9 ▶ if ( $is_subentry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_subentry) 4 5 9
339 4 $entry->add_child( $working_on );
340 }
341 }
342  
343 103 $self->{ 'header' }->add_child( $entry );
344 103 $self->{ 'tokenised' } = $tokenised;
345  
346 103 return;
347 }
348  
349  
350 sub parsed {
351 56 ▶ my ( $self ) = @_;
Subroutine parsed: called CC=1 SCAR=0.0
Pod documented
352 56 return $self->{ 'header' };
353 }
354  
355 24 1;
356  
357 __END__
« blib/lib/Mail/AuthenticationResults/Header/Version.pm ↑ Summary blib/lib/Mail/AuthenticationResults/Token/Comment.pm »