LIVR/Rules/Format.pm

filter: Statementstmt 100% 165 / 165 Branchbran 98.9% 93 / 94 Conditioncond 85.0% 51 / 60 MC/DCmcdc 78.7% 26 / 33 Subroutinesub 100% 40 / 40 Podpod 0.0% 0 / 16 totaltotal 91.9% 375 / 408 CC 72 SCAR 14.5
CC
72
Cov
97%
_count_ipv6_groups
7.0
__ANON__
7.0
digest
6.1
CRAP 4.3
« LIVR.pm ↑ Summary LIVR/Rules/Modifiers.pm »
1 package Validator::LIVR::Rules::Format;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Validator::LIVR::Rules::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '2.22';
9  
10 # LIVR 2.5: formats and checksums. They check the string form of a string or a number and
11 # output it. A boolean gets the error code of the rule (true is not a hostname, "true" is), a
12 # hash or a list gets FORMAT_ERROR. The builder of "hex" is livr_hex, as Perl has hex().
13 #
14 # Grammars use ASCII classes and match the whole value: \z, not $, which also matches before a
15 # trailing newline. Inputs have no length limit, so the regular expressions stay linear
16  
17 2 my $HEX = '[0-9a-fA-F]';
18  
19 # A rule that checks the string form with a regular expression
20 298 ▶sub _regex_rule {
Subroutine _regex_rule: called CC=1 SCAR=0.0
21 298 my ( $error_code, $re ) = @_;
22  
23 291 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
24 291 my ( $value, undef, $output_ref ) = @_;
25 291 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 45 246 291
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
2322246
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
26  
27 246 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 36 210 246
28 36 ▶ return ref($value) =~ /Boolean/ ? $error_code : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 7 29 36
29 }
30  
31 210 my $string = Validator::LIVR::Rules::Types::string_form($value);
32 210 ▶ return $error_code unless $string =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /$re/ 138 72 210
33  
34 72 $$output_ref = $string;
35 72 return;
36 298 };
37 }
38  
39 # A rule that checks the string form with a sub
40 498 ▶sub _check_rule {
Subroutine _check_rule: called CC=1 SCAR=0.0
41 498 my ( $error_code, $is_valid ) = @_;
42  
43 455 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
44 455 my ( $value, undef, $output_ref ) = @_;
45 455 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 54 401 455
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
2727401
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
46  
47 401 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 46 355 401
48 46 ▶ return ref($value) =~ /Boolean/ ? $error_code : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 10 36 46
49 }
50  
51 355 my $string = Validator::LIVR::Rules::Types::string_form($value);
52 355 ▶ return $error_code unless $is_valid->($string);
Branch: 1 branch
BranchTrueFalseTotal
unless &$is_valid($string) 219 136 355
53  
54 136 $$output_ref = $string;
55 136 return;
56 498 };
57 }
58  
59 467 ▶sub _no_arguments {
Subroutine _no_arguments: called CC=2 SCAR=6.9
60 467 my $rule_name = shift;
61 467 ▶ die "Rule [$rule_name] takes no arguments\n" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 10 457 467
62 }
63  
64 # RFC 9562: the version digit (the first of the third group) is 1-8, the variant digit (the
65 # first of the fourth group) is 8, 9, a or b. The nil and the max UUID pass only without versions
66 2 my $UUID_RE = qr/\A${HEX}{8}-${HEX}{4}-[1-8]${HEX}{3}-[89abAB]${HEX}{3}-${HEX}{12}\z/;
67 2 my $ANY_UUID_RE = qr/\A(?:
68 ${HEX}{8}-${HEX}{4}-[1-8]${HEX}{3}-[89abAB]${HEX}{3}-${HEX}{12}
69 | 00000000-0000-0000-0000-000000000000
70 | [fF]{8}-[fF]{4}-[fF]{4}-[fF]{4}-[fF]{12}
71 )\z/x;
72  
73 82 ▶sub uuid {
Subroutine uuid: called CC=6 SCAR=17.9
Pod undocumented
74 82 my @versions = @_;
75 82 pop @versions; # rule_builders
76  
77 82 ▶ return _regex_rule( 'WRONG_UUID', $ANY_UUID_RE ) if !@versions;
Branch: 1 branch
BranchTrueFalseTotal
unless @versions 50 32 82
78  
79 # Versions are strings "v1"-"v8", as in ip and cidr: 4, "4" and "V4" die
80 32 my %is_allowed;
81 32 foreach my $version (@versions) {
82 54 ▶ if ( !defined($version) || ref($version) || $version !~ /\Av([1-8])\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version or ref $version or not $version =~ /\Av([1-8])\z/) 12 42 54
Condition100%6/6
not defined $version or ref $version or not $version =~ /\Av([1-8])\z/
l!l&&r!l&&!r
4842
not defined $version or ref $version
l!l&&r!l&&!r
3150
MC/DC100%3/3
not defined $version or ref $version or not $version =~ /\Av([1-8])\z/
not defined $version ref $version not $version =~ /\Av([1-8])\z/
Truth table100%4/4
not(A) or B or not(C)
A= not defined $versionB= ref $versionC= not $version =~ /\Av([1-8])\z/
ABCresult
1XX1
01X1
0011
0000
83 12 die qq{Rule [uuid] takes versions "v1"-"v8"\n};
84 }
85 42 $is_allowed{$1} = 1;
86 }
87  
88 # The version digit is the 15th character: "xxxxxxxx-xxxx-Vxxx-..."
89 20 ▶ return _check_rule( 'WRONG_UUID', sub { $_[0] =~ $UUID_RE && $is_allowed{ substr( $_[0], 14, 1 ) } } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$UUID_RE/ and $is_allowed{substr $_[0], 14, 1} 12 8 20
90 }
91  
92 # Four numbers 0-255 without leading zeros: some systems read "010" as octal
93 2 my $IPV4_OCTET = qr/25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]/;
94 2 my $IPV4_RE = qr/\A(?:$IPV4_OCTET)(?:\.(?:$IPV4_OCTET)){3}\z/;
95 2 my $HEX_GROUP_RE = qr/\A${HEX}{1,4}\z/;
96  
97 # The number of 16-bit groups in a part of an IPv6 address: hex groups separated by ":", the
98 # last one may be an IPv4 address (two groups) at the end of the address. -1 for a wrong group
99 105 ▶sub _count_ipv6_groups {
Subroutine _count_ipv6_groups: called CC=7 SCAR=19.5
100 105 my ( $part, $is_address_end ) = @_;
101 105 ▶ return 0 if $part eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "" 20 85 105
102  
103 85 my @groups = split /:/, $part, -1;
104 85 my $last = pop @groups;
105  
106 85 foreach my $group (@groups) {
107 87 ▶ return -1 unless $group =~ $HEX_GROUP_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $group =~ /$HEX_GROUP_RE/ 5 82 87
108 }
109  
110 80 ▶ return @groups + 1 if $last =~ $HEX_GROUP_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $last =~ /$HEX_GROUP_RE/ 38 42 80
111 42 ▶ return @groups + 2 if $is_address_end && $last =~ $IPV4_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $is_address_end and $last =~ /$IPV4_RE/ 9 33 42
Condition100%3/3
$is_address_end and $last =~ /$IPV4_RE/
!ll&&!rl&&r
1329
MC/DC100%2/2
$is_address_end and $last =~ /$IPV4_RE/
$is_address_end $last =~ /$IPV4_RE/
Truth table100%3/3
A and B
A= $is_address_endB= $last =~ /$IPV4_RE/
ABresult
0X0
100
111
112 33 return -1;
113 }
114  
115 # RFC 4291, section 2.2: eight groups, or fewer with one "::" for one or more groups of zeros.
116 # No zone ID, brackets or prefix length
117 70 ▶sub _is_ipv6 {
Subroutine _is_ipv6: called CC=6 SCAR=17.9
118 70 my $address = shift;
119  
120 # The longest form is 8 groups of 4 digits with an IPv4 address in the last two
121 70 ▶ return 0 if length($address) > 45;
Branch: 1 branch
BranchTrueFalseTotal
if length $address > 45 0 70 70
122  
123 70 my $gap = index( $address, '::' );
124 70 ▶ return _count_ipv6_groups( $address, 1 ) == 8 if $gap < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $gap < 0 31 39 70
125  
126 # A second "::" (":::" too) is ambiguous
127 39 ▶ return 0 if index( $address, '::', $gap + 1 ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($address, "::", $gap + 1) != -1 2 37 39
128  
129 37 my $before = _count_ipv6_groups( substr( $address, 0, $gap ), 0 );
130 37 my $after = _count_ipv6_groups( substr( $address, $gap + 2 ), 1 );
131 37 ▶ return $before >= 0 && $after >= 0 && $before + $after <= 7;
Condition100%6/6
$before >= 0 && $after >= 0 && $before + $after <= 7
!ll&&!rl&&r
16120
$before >= 0 and $after >= 0
!ll&&!rl&&r
41221
MC/DC100%3/3
$before >= 0 && $after >= 0 && $before + $after <= 7
$before >= 0 $after >= 0 $before + $after <= 7
Truth table100%4/4
A and B && C
A= $before >= 0B= $after >= 0C= $before + $after <= 7
ABCresult
0XX0
10X0
1100
1111
132 }
133  
134 # "v4" or "v6" as 4 or 6, 0 for both
135 136 ▶sub _ip_version {
Subroutine _ip_version: called CC=6 SCAR=17.9
136 136 my $rule_name = shift;
137 136 my @args = @_;
138 136 pop @args; # rule_builders
139  
140 136 ▶ return 0 if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 113 23 136
141  
142 # Arguments after the version are ignored
143 23 ▶ if ( defined( $args[0] ) && !ref( $args[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[0] and not ref $args[0]) 21 2 23
Condition66%2/3
defined $args[0] and not ref $args[0]
!ll&&!rl&&r
2021
MC/DC50%1/2
defined $args[0] and not ref $args[0]
defined $args[0] not ref $args[0]
Truth table66%2/3
A and not(B)
A= defined $args[0]B= not ref $args[0]
ABresult
0X0
100
111
144 21 ▶ return 4 if $args[0] eq 'v4';
Branch: 1 branch
BranchTrueFalseTotal
if $args[0] eq "v4" 8 13 21
145 13 ▶ return 6 if $args[0] eq 'v6';
Branch: 1 branch
BranchTrueFalseTotal
if $args[0] eq "v6" 7 6 13
146 }
147  
148 8 die qq{Rule [$rule_name] takes no arguments, "v4" or "v6"\n};
149 }
150  
151 83 ▶sub ip {
Subroutine ip: called CC=3 SCAR=11.0
Pod undocumented
152 83 my $version = _ip_version( 'ip', @_ );
153  
154 77 ▶ return _regex_rule( 'WRONG_IP', $IPV4_RE ) if $version == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 4 6 71 77
155 71 ▶ return _check_rule( 'WRONG_IP', \&_is_ipv6 ) if $version == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 6 4 67 71
156 67 ▶ return _check_rule( 'WRONG_IP', sub { $_[0] =~ $IPV4_RE || _is_ipv6( $_[0] ) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$IPV4_RE/ or _is_ipv6($_[0]) 5 51 56
157 }
158  
159 # A prefix length: decimal without leading zeros
160 2 my $PREFIX_LENGTH_RE = qr/\A(?:0|[1-9][0-9]{0,2})\z/;
161  
162 53 ▶sub cidr {
Subroutine cidr: called CC=1 SCAR=0.0
Pod undocumented
163 53 my $version = _ip_version( 'cidr', @_ );
164  
165 # Host bits may be set: "10.0.0.1/8" passes
166 40 ▶ return _check_rule( 'WRONG_CIDR', sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
167 40 my $slash = index( $_[0], '/' );
168 40 ▶ return 0 if $slash < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $slash < 0 4 36 40
169  
170 36 my $address = substr( $_[0], 0, $slash );
171 36 my $prefix_length = substr( $_[0], $slash + 1 );
172 36 ▶ return 0 unless $prefix_length =~ $PREFIX_LENGTH_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix_length =~ /$PREFIX_LENGTH_RE/ 13 23 36
173  
174 23 ▶ return $prefix_length <= 32 if $version != 6 && $address =~ $IPV4_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $version != 6 and $address =~ /$IPV4_RE/ 7 16 23
Condition100%3/3
$version != 6 and $address =~ /$IPV4_RE/
!ll&&!rl&&r
3137
MC/DC100%2/2
$version != 6 and $address =~ /$IPV4_RE/
$version != 6 $address =~ /$IPV4_RE/
Truth table100%3/3
A and B
A= $version != 6B= $address =~ /$IPV4_RE/
ABresult
0X0
100
111
175 16 ▶ return $prefix_length <= 128 if $version != 4 && _is_ipv6($address);
Branch: 1 branch
BranchTrueFalseTotal
if $version != 4 and _is_ipv6($address) 8 8 16
Condition100%3/3
$version != 4 and _is_ipv6($address)
!ll&&!rl&&r
178
MC/DC100%2/2
$version != 4 and _is_ipv6($address)
$version != 4 _is_ipv6($address)
Truth table100%3/3
A and B
A= $version != 4B= _is_ipv6($address)
ABresult
0X0
100
111
176 8 return 0;
177 51 } );
178 }
179  
180 # RFC 1123: labels of 1-63 characters, not starting or ending with "-", no trailing dot.
181 # "." is not in a label, so labels do not backtrack into each other
182 2 my $HOSTNAME_RE = qr/\A
183 [a-zA-Z0-9] (?: [a-zA-Z0-9-]{0,61} [a-zA-Z0-9] )?
184 (?: \. [a-zA-Z0-9] (?: [a-zA-Z0-9-]{0,61} [a-zA-Z0-9] )? )*
185 \z/x;
186  
187 58 ▶sub hostname {
Subroutine hostname: called CC=1 SCAR=0.0
Pod undocumented
188 58 _no_arguments( 'hostname', @_ );
189  
190 # Labels may be all digits, except the last one (RFC 1123, section 2.1): "192.0.2.1" and
191 # "123" are not hostnames
192 46 ▶ return _check_rule( 'WRONG_HOSTNAME', sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
193 46 ▶ return length( $_[0] ) <= 253
Condition100%6/6
length $_[0] <= 253 && $_[0] =~ /$HOSTNAME_RE/x && substr($_[0], rindex($_[0], '.') + 1) =~ /[^0-9]/
!ll&&!rl&&r
22816
length $_[0] <= 253 and $_[0] =~ /$HOSTNAME_RE/x
!ll&&!rl&&r
12124
MC/DC100%3/3
length $_[0] <= 253 && $_[0] =~ /$HOSTNAME_RE/x && substr($_[0], rindex($_[0], '.') + 1) =~ /[^0-9]/
length $_[0] <= 253 $_[0] =~ /$HOSTNAME_RE/x substr($_[0], rindex($_[0], '.') + 1) =~ /[^0-9]/
Truth table100%4/4
A and B && C
A= length $_[0] <= 253B= $_[0] =~ /$HOSTNAME_RE/xC= substr($_[0], rindex($_[0], '.') + 1) =~ /[^0-9]/
ABCresult
0XX0
10X0
1100
1111
194 && $_[0] =~ $HOSTNAME_RE
195 && substr( $_[0], rindex( $_[0], '.' ) + 1 ) =~ /[^0-9]/;
196 57 } );
197 }
198  
199 # RFC 4648. A character class with "*" and the length checked apart: (?:X{4})* fails on
200 # long values in old versions of Perl
201 2 my $BASE64_RE = qr/\A[A-Za-z0-9+\/]*(={0,2})\z/;
202 2 my $BASE64_URL_RE = qr/\A[A-Za-z0-9_-]*(={0,2})\z/;
203  
204 58 ▶sub base64 {
Subroutine base64: called CC=5 SCAR=16.2
Pod undocumented
205 58 my @args = @_;
206 58 pop @args; # rule_builders
207  
208 # The standard alphabet: the length is a multiple of 4, the padding "=" or "==" only at the end
209 58 ▶ return _check_rule( 'WRONG_BASE64', sub { $_[0] =~ $BASE64_RE && length( $_[0] ) % 4 == 0 } ) if !@args;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
unless @args 36 22 58
$_[0] =~ /$BASE64_RE/ and length($_[0]) % 4 == 0 12 13 25
210  
211 22 ▶ if ( defined( $args[0] ) && !ref( $args[0] ) && $args[0] eq 'url' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[0] and not ref $args[0] and $args[0] eq "url") 19 3 22
Condition50%3/6
defined $args[0] and not ref $args[0] and $args[0] eq "url"
!ll&&!rl&&r
0319
defined $args[0] and not ref $args[0]
!ll&&!rl&&r
0022
MC/DC33%1/3
defined $args[0] and not ref $args[0] and $args[0] eq "url"
defined $args[0] not ref $args[0] $args[0] eq "url"
Truth table50%2/4
A and not(B) and C
A= defined $args[0]B= not ref $args[0]C= $args[0] eq "url"
ABCresult
0XX0
10X0
1100
1111
212 # The URL-safe alphabet, the padding is optional: either complete or absent. A lone
213 # character in the last group is never valid
214 18 ▶ return _check_rule( 'WRONG_BASE64', sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
215 18 ▶ return 0 unless $_[0] =~ $BASE64_URL_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /$BASE64_URL_RE/ 5 13 18
216 13 ▶ return length( $1 ) ? length( $_[0] ) % 4 == 0 : length( $_[0] ) % 4 != 1;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 6 7 13
217 19 } );
218 }
219  
220 3 die qq{Rule [base64] takes no arguments or "url"\n};
221 }
222  
223 # No "0x" prefix
224 2 my $HEX_RE = qr/\A$HEX+\z/;
225  
226 37 ▶sub livr_hex {
Subroutine livr_hex: called CC=1 SCAR=0.0
Pod undocumented
227 37 _no_arguments( 'hex', @_ );
228 36 return _regex_rule( 'WRONG_HEX', $HEX_RE );
229 }
230  
231 2 my %HEX_LENGTH_OF = ( md5 => 32, sha1 => 40, sha256 => 64, sha384 => 96, sha512 => 128 );
232  
233 45 ▶sub digest {
Subroutine digest: called CC=6 SCAR=18.1
Pod undocumented
234 45 my @algorithms = @_;
235 45 pop @algorithms; # rule_builders
236  
237 45 ▶ die "Rule [digest] needs one or more algorithms\n" if !@algorithms;
Branch: 1 branch
BranchTrueFalseTotal
unless @algorithms 1 44 45
238  
239 44 my %is_length;
240 44 foreach my $algorithm (@algorithms) {
241 60 ▶ if ( !defined($algorithm) || ref($algorithm) || !$HEX_LENGTH_OF{$algorithm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $algorithm or ref $algorithm or not $HEX_LENGTH_OF{$algorithm}) 3 57 60
Condition50%3/6
not defined $algorithm or ref $algorithm or not $HEX_LENGTH_OF{$algorithm}
l!l&&r!l&&!r
0357
not defined $algorithm or ref $algorithm
l!l&&r!l&&!r
0060
MC/DC33%1/3
not defined $algorithm or ref $algorithm or not $HEX_LENGTH_OF{$algorithm}
not defined $algorithm ref $algorithm not $HEX_LENGTH_OF{$algorithm}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $algorithmB= ref $algorithmC= not $HEX_LENGTH_OF{$algorithm}
ABCresult
1XX1
01X1
0011
0000
242 3 die qq{Rule [digest] takes "md5", "sha1", "sha256", "sha384" or "sha512"\n};
243 }
244 57 $is_length{ $HEX_LENGTH_OF{$algorithm} } = 1;
245 }
246  
247 41 ▶ return _check_rule( 'WRONG_DIGEST', sub { $is_length{ length( $_[0] ) } && $_[0] =~ $HEX_RE } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$is_length{length $_[0]} and $_[0] =~ /$HEX_RE/ 15 15 30
248 }
249  
250 # A BSON ObjectId: 12 bytes in hex
251 24 ▶sub mongo_id {
Subroutine mongo_id: called CC=1 SCAR=0.0
Pod undocumented
252 24 _no_arguments( 'mongo_id', @_ );
253 23 return _regex_rule( 'WRONG_MONGO_ID', qr/\A${HEX}{24}\z/ );
254 }
255  
256 # #RGB, #RGBA, #RRGGBB or #RRGGBBAA
257 32 ▶sub hex_color {
Subroutine hex_color: called CC=1 SCAR=0.0
Pod undocumented
258 32 _no_arguments( 'hex_color', @_ );
259 31 return _regex_rule( 'WRONG_HEX_COLOR', qr/\A#(?:${HEX}{3,4}|${HEX}{6}|${HEX}{8})\z/ );
260 }
261  
262 # Hex pairs separated by ":" or by "-": the back reference keeps the first separator.
263 # 48 bits are six pairs (MAC-48, EUI-48), 64 bits are eight pairs (EUI-64)
264 2 my %MAC_RE = (
265 48 => qr/\A${HEX}{2}([:-])${HEX}{2}(?:\1${HEX}{2}){4}\z/,
266 64 => qr/\A${HEX}{2}([:-])${HEX}{2}(?:\1${HEX}{2}){6}\z/,
267 );
268  
269 47 ▶sub mac {
Subroutine mac: called CC=4 SCAR=13.9
Pod undocumented
270 47 my @args = @_;
271 47 pop @args; # rule_builders
272  
273 # A bit count, "48" is 48. Arguments after it are ignored
274 47 ▶ my $bits = @args ? $args[0] : 48;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 17 30 47
275 47 ▶ if ( !Validator::LIVR::Rules::Types::is_length($bits) || !$MAC_RE{ $bits + 0 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Validator::LIVR::Rules::Types::is_length($bits) and $MAC_RE{$bits + 0}) 2 45 47
Condition100%3/3
Validator::LIVR::Rules::Types::is_length($bits) and $MAC_RE{$bits + 0}
!ll&&!rl&&r
1145
MC/DC100%2/2
Validator::LIVR::Rules::Types::is_length($bits) and $MAC_RE{$bits + 0}
Validator::LIVR::Rules::Types::is_length($bits) $MAC_RE{$bits + 0}
Truth table100%3/3
A and B
A= Validator::LIVR::Rules::Types::is_length($bits)B= $MAC_RE{$bits + 0}
ABresult
0X0
100
111
276 2 die "Rule [mac] takes no arguments, 48 or 64\n";
277 }
278  
279 45 return _regex_rule( 'WRONG_MAC', $MAC_RE{ $bits + 0 } );
280 }
281  
282 # "+", then 7-15 digits, the first is not 0
283 45 ▶sub e164 {
Subroutine e164: called CC=1 SCAR=0.0
Pod undocumented
284 45 _no_arguments( 'e164', @_ );
285 44 return _regex_rule( 'WRONG_E164', qr/\A\+[1-9][0-9]{6,14}\z/ );
286 }
287  
288 # The regular expression of semver.org 2.0.0, with [0-9] for \d
289 2 my $SEMVER_NUMBER = '(?:0|[1-9][0-9]*)';
290 2 my $SEMVER_PRE_ID = "(?:$SEMVER_NUMBER|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)";
291 2 my $SEMVER_RE = qr/\A
292 $SEMVER_NUMBER \. $SEMVER_NUMBER \. $SEMVER_NUMBER
293 (?: - $SEMVER_PRE_ID (?: \. $SEMVER_PRE_ID )* )?
294 (?: \+ [0-9a-zA-Z-]+ (?: \. [0-9a-zA-Z-]+ )* )?
295 \z/x;
296  
297 64 ▶sub semver {
Subroutine semver: called CC=1 SCAR=0.0
Pod undocumented
298 64 _no_arguments( 'semver', @_ );
299 63 return _regex_rule( 'WRONG_SEMVER', $SEMVER_RE );
300 }
301  
302 # The structure of a JWS compact serialization: header.payload.signature in base64url without
303 # padding, the signature is empty in an unsecured JWT. The segments are not decoded
304 2 my $JWT_RE = qr/\A([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]*)\z/;
305  
306 51 ▶sub jwt {
Subroutine jwt: called CC=1 SCAR=0.0
Pod undocumented
307 51 _no_arguments( 'jwt', @_ );
308  
309 # No base64url segment has a length of 1 modulo 4: such a length cannot come from bytes
310 39 ▶ return _check_rule( 'WRONG_JWT', sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
311 39 ▶ return $_[0] =~ $JWT_RE && length($1) % 4 != 1 && length($2) % 4 != 1 && length($3) % 4 != 1;
Condition77%7/9
$_[0] =~ /$JWT_RE/ && length($1) % 4 != 1 && length($2) % 4 != 1 && length($3) % 4 != 1
!ll&&!rl&&r
19119
$_[0] =~ /$JWT_RE/ and length($1) % 4 != 1 and length($2) % 4 != 1
!ll&&!rl&&r
19020
$_[0] =~ /$JWT_RE/ and length($1) % 4 != 1
!ll&&!rl&&r
19020
MC/DC50%2/4
$_[0] =~ /$JWT_RE/ && length($1) % 4 != 1 && length($2) % 4 != 1 && length($3) % 4 != 1
$_[0] =~ /$JWT_RE/ length($1) % 4 != 1 length($2) % 4 != 1 length($3) % 4 != 1
Truth table60%3/5
A and B and C && D
A= $_[0] =~ /$JWT_RE/B= length($1) % 4 != 1C= length($2) % 4 != 1D= length($3) % 4 != 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
312 50 } );
313 }
314  
315 # Luhn mod 10: every second digit from the right is doubled, minus 9 when above 9
316 23 ▶sub _has_luhn_checksum {
Subroutine _has_luhn_checksum: called CC=4 SCAR=13.9
317 23 my $digits = shift;
318 23 my ( $sum, $is_doubled ) = ( 0, 0 );
319  
320 23 ▶ for ( my $i = length($digits) - 1; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 292 23 315
321 292 my $digit = substr( $digits, $i, 1 );
322  
323 292 ▶ if ($is_doubled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_doubled) 143 149 292
324 143 $digit *= 2;
325 143 ▶ $digit -= 9 if $digit > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $digit > 9 18 125 143
326 }
327  
328 292 $sum += $digit;
329 292 $is_doubled = !$is_doubled;
330 }
331  
332 23 return $sum % 10 == 0;
333 }
334  
335 # The string form is checked as it is: no spaces or dashes are removed
336 34 ▶sub luhn {
Subroutine luhn: called CC=1 SCAR=0.0
Pod undocumented
337 34 _no_arguments( 'luhn', @_ );
338 33 ▶ return _check_rule( 'WRONG_CHECKSUM', sub { $_[0] =~ /\A[0-9]{2,}\z/ && _has_luhn_checksum( $_[0] ) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\A[0-9]{2,}\z/ and _has_luhn_checksum($_[0]) 10 12 22
339 }
340  
341 # ISO/IEC 7812 card numbers have 12-19 digits. No card network detection
342 56 ▶sub credit_card {
Subroutine credit_card: called CC=1 SCAR=0.0
Pod undocumented
343 56 _no_arguments( 'credit_card', @_ );
344 55 ▶ return _check_rule( 'WRONG_CREDIT_CARD_NUMBER', sub { $_[0] =~ /\A[0-9]{12,19}\z/ && _has_luhn_checksum( $_[0] ) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\A[0-9]{12,19}\z/ and _has_luhn_checksum($_[0]) 13 10 23
345 }
346  
347 # ISO 13616: a country code, two check digits, 11-30 characters. No per-country length table.
348 # The first four characters moved to the end, letters replaced by two digits (A = 10 ... Z = 35):
349 # the number modulo 97 is 1. The remainder is computed a character at a time, so the number
350 # stays small
351 18 ▶sub _has_iban_checksum {
Subroutine _has_iban_checksum: called CC=3 SCAR=11.0
352 18 my $iban = shift;
353 18 my $remainder = 0;
354  
355 18 foreach my $code ( unpack 'C*', substr( $iban, 4 ) . substr( $iban, 0, 4 ) ) {
356 420 ▶ $remainder = $code <= 57
Branch: 1 branch
BranchTrueFalseTotal
$code <= 57 ? : 295 125 420
357 ? ( $remainder * 10 + $code - 48 ) % 97
358 : ( $remainder * 100 + $code - 55 ) % 97;
359 }
360  
361 18 return $remainder == 1;
362 }
363  
364 66 ▶sub iban {
Subroutine iban: called CC=1 SCAR=0.0
Pod undocumented
365 66 _no_arguments( 'iban', @_ );
366 65 ▶ return _check_rule( 'WRONG_IBAN', sub { $_[0] =~ /\A[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}\z/ && _has_iban_checksum( $_[0] ) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\A[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}\z/ and _has_iban_checksum($_[0]) 18 14 32
367 }
368  
369 2 1;
370  
371 =head1 NAME
372  
373 Validator::LIVR::Rules::Format - LIVR 2.5 format and checksum rules of Validator::LIVR: uuid, ip, cidr, hostname, base64, hex, digest, mongo_id, hex_color, mac, e164, semver, jwt, luhn, credit_card and iban
374  
375 =head1 DESCRIPTION
376  
377 This module has the code of these rules, which are documented in
378 L<Validator::LIVR/LIVR 2.5 rules>. Validator::LIVR loads it when a validator
379 uses one of the rules.
380  
381 Each sub is the builder of the rule with the same name. The builder of C<hex>
382 is C<livr_hex>, as Perl has C<hex()>.
383  
384 =cut
« LIVR.pm ↑ Summary LIVR/Rules/Modifiers.pm »