lib/Mail/DKIM2/TagValueList.pm

filter: Statementstmt 62.9% 51 / 81 Branchbran 53.8% 14 / 26 Conditioncond 41.6% 5 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 71.4% 10 / 14 Podpod 100% 12 / 12 totaltotal 61.4% 94 / 153 CC 23 SCAR 20.3
CC
23
Cov
59%
split_tag_list
13.1
split_tag_list
13.1
parse
8.6
CRAP 7.6
« lib/Mail/Milter/Authentication/Handler/DKIM2Verify.pm ↑ Summary lib/Mail/DKIM2/Signer.pm »
1 package Mail::DKIM2::TagValueList;
2 222 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 222 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 222 our $VERSION = '0.34';
6  
7 # Simple tag=value list as defined in draft-ietf-dkim-dkim2-spec-09 Section 8.
8 # Preserves insertion order for serialization.
9  
10 3953 ▶sub new {
Subroutine new: called CC=1 SCAR=1.2
Pod documented
11 0 ▶ my ($class) = @_;
Subroutine new: not called CC=1 SCAR=1.2
Pod documented
12 0 return bless { tags => {}, order => [] }, $class;
13 }
14  
15 # The tag-list grammar shared by every DKIM2 field (-09 §8): a tag
16 # name is ALPHA *(ALPHA / DIGIT / "_"), and a value is printable ASCII other
17 # than ";" with FWS only between characters (x-tag-value). The known tags'
18 # own grammars are all narrower than that, so a field that fails it is
19 # malformed (§14.2) whatever its tags are. DKIM1 key records use the same
20 # value characters (RFC 6376 §3.2 VALCHAR).
21 222 our $TAG_NAME = qr/[A-Za-z][A-Za-z0-9_]*/;
22 222 our $TAG_VALUE = qr/(?:[\x21-\x3A\x3C-\x7E]+(?:[ \t\r\n]+[\x21-\x3A\x3C-\x7E]+)*)?/;
23  
24 # Split a tag-list into [name, value] pairs, the whitespace around names and
25 # values trimmed. A trailing ";" is allowed. Returns
26 # (\@pairs, $ok): $ok is false if an empty spec comes before the end
27 # (";;"), any other spec is not name=value, or a
28 # value has a character the grammar does not allow. The pairs still hold
29 # every spec that did parse, for diagnostics.
30 7342 ▶sub split_tag_list {
Subroutine split_tag_list: called CC=7 SCAR=25.7
Pod documented
31 0 ▶ my ($string) = @_;
Subroutine split_tag_list: not called CC=7 SCAR=25.7
Pod documented
32 0 my (@pairs, $bad);
33 0 my @specs = split /;/, $string, -1;
34 # -09 section 8: one ";" may end the list; an empty spec anywhere else
35 # (";;", a leading ";") is a syntax error.
36 0 ▶ pop @specs if @specs && $specs[-1] =~ /\A[ \t\r\n]*\z/;
Branch: 2 branches
BranchTrueFalseTotal
if @specs and $specs[-1] =~ /\A[ \t\r\n]*\z/ 5905 1437 7342
if @specs and $specs[-1] =~ /\A[ \t\r\n]*\z/ 0 0 0
Condition50%3/6
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
!ll&&!rl&&r
314345905
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
!ll&&!rl&&r
000
MC/DC100%2/2
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
@specs $specs[-1] =~ /\A[ \t\r\n]*\z/
MC/DC0%0/2
@specs and $specs[-1] =~ /\A[ \t\r\n]*\z/
@specs $specs[-1] =~ /\A[ \t\r\n]*\z/
Truth table100%3/3
A and B
A= @specsB= $specs[-1] =~ /\A[ \t\r\n]*\z/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @specsB= $specs[-1] =~ /\A[ \t\r\n]*\z/
ABresult
0X0
100
111
37 0 for my $spec (@specs) {
38 0 ▶ if ($spec =~ /\A[ \t\r\n]*\z/) { $bad = 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /\A[ \t\r\n]*\z/) 7 32002 32009
if ($spec =~ /\A[ \t\r\n]*\z/) 0 0 0
39 my ($name, $val) = $spec =~ /\A[ \t\r\n]*($TAG_NAME)[ \t\r\n]*=[ \t\r\n]*(.*?)[ \t\r\n]*\z/s
40 0 ▶ or do { $bad = 1; next };
Branch: 2 branches
BranchTrueFalseTotal
() = $spec =~ /\A[ \t\r\n]*($Mail::DKIM2::TagValueList::TAG_NAME)[ \t\r\n]*=[ \t\r\n]*(.*?)[ \t\r\n]*\z/s or do { $bad = 1; next } 31993 9 32002
() = $spec =~ /\A[ \t\r\n]*($Mail::DKIM2::TagValueList::TAG_NAME)[ \t\r\n]*=[ \t\r\n]*(.*?)[ \t\r\n]*\z/s or do { $bad = 1; next } 0 0 0
41 0 ▶ $bad = 1 unless $val =~ /\A$TAG_VALUE\z/;
Branch: 2 branches
BranchTrueFalseTotal
unless $val =~ /\A$Mail::DKIM2::TagValueList::TAG_VALUE\z/ 2 31991 31993
unless $val =~ /\A$Mail::DKIM2::TagValueList::TAG_VALUE\z/ 0 0 0
42 0 push @pairs, [$name, $val];
43 }
44 0 return (\@pairs, !$bad);
45 }
46  
47 # A constructor: always a fresh object, even when called on an existing one
48 # (parsing into one left its old tags and duplicate flag behind).
49 3150 ▶sub parse {
Subroutine parse: called CC=5 SCAR=21.6
Pod documented
50 0 ▶ my ($class, $string) = @_;
Subroutine parse: not called CC=5 SCAR=21.6
Pod documented
51 0 ▶ my $self = (ref($class) || $class)->new();
Condition33%2/6
ref $class || $class
l!l&&r!l&&!r
231480
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
52  
53 # Tag names keep their original case and order, and values their
54 # internal whitespace; as_string() writes "name=value" joined by "; ",
55 # so the whitespace around "=" and ";" and a trailing ";" are not kept.
56 # That is harmless for signing input: §12.8 deletes all WSP from these
57 # fields before hashing. Tag-names are case-sensitive (-09 §8, and the
58 # -08 contract D10): "M=" is a tag of its own, which DKIM2 does not
59 # know, never another spelling of "m=". A spec that is not a tag is never silently
60 # dropped from what is verified: it makes the whole field a syntax
61 # error (follow-up review F3).
62 0 my ($pairs, $ok) = split_tag_list($string);
63 0 ▶ $self->{_syntax_error} = 1 unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 8 3142 3150
unless $ok 0 0 0
64 0 my (@order, %seen);
65 0 for my $p (@$pairs) {
66 0 my ($name, $val) = @$p;
67 # §8: "there MUST be only one of each kind" — flag any repeat.
68 0 ▶ $self->{_duplicate} = $name if $seen{$name}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{$name}++ 2 21283 21285
if $seen{$name}++ 0 0 0
69 0 $self->{tags}{$name} = $val;
70 0 push @order, $name;
71 }
72 0 $self->{order} = \@order;
73 0 return $self;
74 }
75  
76 # -09 §8: tag-names are case-sensitive, so the lookup is exact.
77 50473 ▶sub get_tag {
Subroutine get_tag: called CC=1 SCAR=1.2
Pod documented
78 0 ▶ my ($self, $name) = @_;
Subroutine get_tag: not called CC=1 SCAR=1.2
Pod documented
79 0 return $self->{tags}{$name};
80 }
81  
82 # The tag name that appeared more than once, if any (-09 §8).
83 961 ▶sub duplicate_tag { return $_[0]->{_duplicate} }
Subroutine duplicate_tag: called CC=1 SCAR=0.0
Pod documented
84  
85 # True if the parsed field did not match the tag-list grammar.
86 1046 ▶sub syntax_error { return $_[0]->{_syntax_error} }
Subroutine syntax_error: called CC=1 SCAR=0.0
Pod documented
87  
88 6808 ▶sub set_tag {
Subroutine set_tag: called CC=2 SCAR=6.9
Pod documented
89 6808 my ($self, $name, $value) = @_;
90 6808 ▶ unless (exists $self->{tags}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"tags"}{$name}) 5494 1314 6808
91 5494 push @{$self->{order}}, $name;
92 }
93 6808 $self->{tags}{$name} = $value;
94 }
95  
96 4 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
97 4 my ($self) = @_;
98 4 return join('; ', map { "$_=$self->{tags}{$_}" } @{$self->{order}});
99 }
100  
101 222 1;
102  
103 __END__
« lib/Mail/Milter/Authentication/Handler/DKIM2Verify.pm ↑ Summary lib/Mail/DKIM2/Signer.pm »