Sisimai/RFC1123.pm

filter: Statementstmt 100% 103 / 103 Branchbran 93.3% 56 / 60 Conditioncond 88.8% 24 / 27 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 94.9% 206 / 217 CC 51 SCAR 29.0
CC
51
Cov
96%
is_internethost
22.2
find
22.0
is_domainliteral
9.0
CRAP 18.3
« Sisimai/SMTP/Transcript.pm ↑ Summary Sisimai/SMTP/Command.pm »
1 package Sisimai::RFC1123;
2 94 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 94 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 94 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 94 ▶use Sisimai::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 94 ▶use Sisimai::RFC791;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 94 state $Sandwiched = [
9 # (Postfix) postfix/src/smtp/smtp_proto.c: "host %s said: %s (in reply to %s)",
10 # - <kijitora@example.com>: host re2.example.com[198.51.100.2] said: 550 ...
11 # - <kijitora@example.org>: host r2.example.org[198.51.100.18] refused to talk to me:
12 ["host ", " said: "],
13 ["host ", " talk to me: "],
14 ["while talking to ", ":"], # (Sendmail) ... while talking to mx.bouncehammer.jp.:
15 ["host ", " ["], # (Exim) host mx.example.jp [192.0.2.20]: 550 5.7.0
16 [" by ", ". ["], # (Gmail) ...for the recipient domain example.jp by mx.example.jp. [192.0.2.1].
17  
18 # (MailFoundry)
19 # - Delivery failed for the following reason: Server mx22.example.org[192.0.2.222] failed with: 550...
20 # - Delivery failed for the following reason: mail.example.org[192.0.2.222] responded with failure: 552..
21 ["delivery failed for the following reason: ", " with"],
22 ["remote system: ", "("], # (MessagingServer) Remote system: dns;mx.example.net (mx. --
23 ["smtp server <", ">"], # (X6) SMTP Server <smtpd.libsisimai.org> rejected recipient ...
24 ["-mta: ", ">"], # (MailMarshal) Reporting-MTA: <rr1.example.com>
25 [" : ", "["], # (SendGrid) cat:000000:<cat@example.jp> : 192.0.2.1 : mx.example.jp:[192.0.2.2]...
26 ];
27 94 state $StartAfter = [
28 "generating server: ", # (Exchange2007) en-US/Generating server: mta4.example.org
29 "serveur de g", # (Exchange2007) fr-FR/Serveur de gènèration
30 "server di generazione", # (Exchange2007) it-CH
31 "genererande server", # (Exchange2007) sv-SE
32 ];
33 94 state $ExistUntil = [
34 " did not like our ", # (Dragonfly) mail-inbound.libsisimai.net [192.0.2.25] did not like our DATA: ...
35 ];
36  
37 23766 ▶sub is_internethost {
Subroutine is_internethost: called CC=22 SCAR=31.0
Pod documented
38 # Check that the argument is a valid Internet hostname or not
39 # @param [String] argv0 String to be checked
40 # @return [Boolean] 0: is not a valid hostname
41 # 1: is a valid hostname
42 # @since v5.2.0
43 23766 my $class = shift;
44 23766 ▶ my $argv0 = shift || return 0;
Condition100%2/2
shift() || (return 0)
l!l
217701996
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
45  
46 # Deal "localhost", "localhost6" as a valid hostname
47 21770 ▶ return 1 if $argv0 eq 'localhost' || $argv0 eq 'localhost6';
Branch: 1 branch
BranchTrueFalseTotal
if $argv0 eq "localhost" or $argv0 eq "localhost6" 268 21502 21770
Condition100%3/3
$argv0 eq "localhost" or $argv0 eq "localhost6"
l!l&&r!l&&!r
267121502
MC/DC100%2/2
$argv0 eq "localhost" or $argv0 eq "localhost6"
$argv0 eq "localhost" $argv0 eq "localhost6"
Truth table100%3/3
A or B
A= $argv0 eq "localhost"B= $argv0 eq "localhost6"
ABresult
1X1
011
000
48 21502 ▶ return 0 if length $argv0 > 255 || length $argv0 < 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $argv0 > 255 or length $argv0 < 4 6 21496 21502
Condition66%2/3
length $argv0 > 255 or length $argv0 < 4
l!l&&r!l&&!r
0621496
MC/DC50%1/2
length $argv0 > 255 or length $argv0 < 4
length $argv0 > 255 length $argv0 < 4
Truth table66%2/3
A or B
A= length $argv0 > 255B= length $argv0 < 4
ABresult
1X1
011
000
49 21496 ▶ return 0 if index($argv0, ".") == -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv0, ".") == -1 314 21182 21496
50 21182 ▶ return 0 if index($argv0, "..") > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv0, "..") != -1 308 20874 21182
51 20874 ▶ return 0 if index($argv0, "--") > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv0, "--") != -1 11 20863 20874
52 20863 ▶ return 0 if index($argv0, ".") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv0, ".") == 0 0 20863 20863
53 20863 ▶ return 0 if index($argv0, "-") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($argv0, "-") == 0 0 20863 20863
54 20863 ▶ return 0 if substr($argv0, -1, 1) eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if substr($argv0, -1, 1) eq "-" 1 20862 20863
55  
56 20862 my @characters = split("", uc $argv0);
57 20862 for my $e ( @characters ) {
58 # Check each characater is a number or an alphabet
59 306610 my $f = ord $e;
60 306610 ▶ return 0 if $f < 45; # 45 = '-'
Branch: 1 branch
BranchTrueFalseTotal
if $f < 45 218 306392 306610
61 306392 ▶ return 0 if $f == 47; # 47 = '/'
Branch: 1 branch
BranchTrueFalseTotal
if $f == 47 373 306019 306392
62 306019 ▶ return 0 if $f > 57 && $f < 65; # 57 = '9', 65 = 'A'
Branch: 1 branch
BranchTrueFalseTotal
if $f > 57 and $f < 65 1117 304902 306019
Condition100%3/3
$f > 57 and $f < 65
!ll&&!rl&&r
606742442281117
MC/DC100%2/2
$f > 57 and $f < 65
$f > 57 $f < 65
Truth table100%3/3
A and B
A= $f > 57B= $f < 65
ABresult
0X0
100
111
63 304902 ▶ return 0 if $f > 90; # 90 = 'Z'
Branch: 1 branch
BranchTrueFalseTotal
if $f > 90 175 304727 304902
64 }
65  
66 18979 my $p1 = rindex($argv0, ".");
67 18979 ▶ my $cv = substr($argv0, $p1 + 1,); return 0 if length $cv > 63;
Branch: 1 branch
BranchTrueFalseTotal
if length $cv > 63 0 18979 18979
68 18979 for my $e ( split("", $cv) ) {
69 # The top level domain should not include a number
70 46839 ▶ my $f = ord $e; return 0 if $f > 47 && $f < 58;
Branch: 1 branch
BranchTrueFalseTotal
if $f > 47 and $f < 58 1432 45407 46839
Condition66%2/3
$f > 47 and $f < 58
!ll&&!rl&&r
0454071432
MC/DC50%1/2
$f > 47 and $f < 58
$f > 47 $f < 58
Truth table66%2/3
A and B
A= $f > 47B= $f < 58
ABresult
0X0
100
111
71 }
72 17547 return 1;
73 }
74  
75 10628 ▶sub is_domainliteral {
Subroutine is_domainliteral: called CC=9 SCAR=22.0
Pod documented
76 # returns true if the domain part is [IPv4:...] or [IPv6:...].
77 # @param string email Email address.
78 # @return bool 0: the domain part is not a domain literal.
79 # 1: the domain part is a domain literal.
80 10628 my $class = shift;
81 10628 ▶ my $email = shift || return 0;
Condition100%2/2
shift() || (return 0)
l!l
106271
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
82  
83 10627 $email =~ s/\A[<]//g; $email =~ s/[>]\z//g;
84 10627 ▶ return 0 if length $email < 16; # e@[IPv4:0.0.0.0] is 16 characters
Branch: 1 branch
BranchTrueFalseTotal
if length $email < 16 381 10246 10627
85 10246 ▶ return 0 if substr($email, -1, 1) ne ']';
Branch: 1 branch
BranchTrueFalseTotal
if substr($email, -1, 1) ne "]" 10233 13 10246
86  
87 13 ▶ my $lastb = rindex($email, '@[IPv'); return 0 if $lastb < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lastb < 0 1 12 13
88 12 my $dpart = [split('@', $email)]->[-1];
89  
90 12 ▶ if( index($email, '@[IPv4:') > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($email, '@[IPv4:') > 0) { } 4 8 12
elsif (index($email, '@[IPv6:') > 0) { } 7 1 8
91 # neko@[IPv4:192.0.2.25]
92 4 my $ipv4a = substr($email, $lastb + 7,);
93 4 $ipv4a = substr($ipv4a, 0, length($ipv4a) - 1);
94 4 return Sisimai::RFC791->is_ipv4address($ipv4a);
95  
96 } elsif( index($email, '@[IPv6:') > 0 ) {
97 # neko@[IPv6:2001:0DB8:0000:0000:0000:0000:0000:0001]
98 # neko@[IPv6:2001:0DB8:0000:0000:0000:0000:0000:0001]
99 # IPv6-address-literal = "IPv6:" IPv6-addr
100 # IPv6-addr = IPv6-full / IPv6-comp / IPv6v4-full / IPv6v4-comp
101 # IPv6-hex = 1*4HEXDIG
102 # IPv6-full = IPv6-hex 7(":" IPv6-hex)
103 # IPv6-comp = [IPv6-hex *5(":" IPv6-hex)] "::"
104 # [IPv6-hex *5(":" IPv6-hex)]
105 # ; The "::" represents at least 2 16-bit groups of
106 # ; zeros. No more than 6 groups in addition to the
107 # ; "::" may be present.
108 # IPv6v4-full = IPv6-hex 5(":" IPv6-hex) ":" IPv4-address-literal
109 # IPv6v4-comp = [IPv6-hex *3(":" IPv6-hex)] "::"
110 # [IPv6-hex *3(":" IPv6-hex) ":"]
111 # IPv4-address-literal
112 # ; The "::" represents at least 2 16-bit groups of
113 # ; zeros. No more than 4 groups in addition to the
114 # ; "::" and IPv4-address-literal may be present.
115 7 ▶ return 1 if length $dpart > 2 && rindex($dpart, ':') > 7;
Branch: 1 branch
BranchTrueFalseTotal
if length $dpart > 2 and rindex($dpart, ":") > 7 6 1 7
Condition66%2/3
length $dpart > 2 and rindex($dpart, ":") > 7
!ll&&!rl&&r
016
MC/DC50%1/2
length $dpart > 2 and rindex($dpart, ":") > 7
length $dpart > 2 rindex($dpart, ":") > 7
Truth table66%2/3
A and B
A= length $dpart > 2B= rindex($dpart, ":") > 7
ABresult
0X0
100
111
116 }
117 2 return 0
118 }
119  
120 1849 ▶sub find {
Subroutine find: called CC=22 SCAR=30.9
Pod undocumented
121 # find() returns a valid internet hostname found from the argument
122 # @param string argv1 String including hostnames
123 # @return string A valid internet hostname found in the argument
124 # @since v5.2.0
125 1849 my $class = shift;
126 1849 ▶ my $argv1 = shift || return "";
Condition100%2/2
shift() || (return '')
l!l
183910
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
127  
128 1839 my $sourcetext = lc $argv1;
129 1839 my $sourcelist = [];
130 1839 my $foundtoken = [];
131 1839 my $thelongest = 0;
132 1839 my $hostnameis = "";
133  
134 # Replace some string for splitting by " "
135 # - mx.example.net[192.0.2.1] => mx.example.net [192.0.2.1]
136 # - mx.example.jp:[192.0.2.1] => mx.example.jp :[192.0.2.1]
137 1839 s/\[/ [/g, s/\(/ (/g, s/</ </g for $sourcetext; # Prefix a space character before each bracket
138 1839 s/\]/] /g, s/\)/) /g, s/</> /g for $sourcetext; # Suffix a space character behind each bracket
139 1839 s/:/: /g, s/;/; /g for $sourcetext; # Suffix a space character behind : and ;
140 1839 $sourcetext = Sisimai::String->sweep($sourcetext);
141  
142 1839 MAKELIST: while(1) {
143 1839 for my $e ( @$Sandwiched ) {
144 # Check a hostname exists between the $e->[0] and $e->[1] at array "Sandwiched"
145 # Each array in Sandwiched have 2 elements
146 17360 ▶ next unless Sisimai::String->aligned(\$sourcetext, $e);
Branch: 1 branch
BranchTrueFalseTotal
unless "Sisimai::String"->aligned(\$sourcetext, $e) 17161 199 17360
147  
148 199 my $p1 = index($sourcetext, $e->[0]);
149 199 my $p2 = index($sourcetext, $e->[1]);
150 199 my $cw = length $e->[0];
151 199 ▶ next if $p1 + $cw >= $p2;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 + $cw >= $p2 33 166 199
152  
153 166 $sourcelist = [split(" ", substr($sourcetext, $p1 + $cw, $p2 - $cw - $p1))];
154 166 last MAKELIST;
155 }
156  
157 # Check other patterns which are not sandwiched
158 1673 for my $e ( @$StartAfter ) {
159 # $StartAfter have some strings, not an array.
160 6410 ▶ my $p1 = index($sourcetext, $e); next if $p1 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 < 0 6309 101 6410
161 101 my $cw = length $e;
162 101 $sourcelist = [split(" ", substr($sourcetext, $p1 + $cw,))];
163 101 last MAKELIST;
164 }
165  
166 1572 for my $e ( @$ExistUntil ) {
167 # ExistUntil have some strings, not an array.
168 1572 ▶ my $p1 = index($sourcetext, $e); next if $p1 < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 < 0 1570 2 1572
169 2 $sourcelist = [split(" ", substr($sourcetext, 0, $p1))];
170 2 last MAKELIST;
171 }
172  
173 1570 ▶ $sourcelist = [split(" ", $sourcetext)] if scalar @$sourcelist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$sourcelist == 0 1570 0 1570
174 1570 last MAKELIST;
175 }
176  
177 1839 for my $e ( @$sourcelist ) {
178 # Pick some strings which is 4 or more length, is including "." character
179 50030 ▶ substr($e, -1, 1, "") if substr($e, -1, 1) eq "."; # Remove "." at the end of the string
Branch: 1 branch
BranchTrueFalseTotal
if substr($e, -1, 1) eq "." 2279 47751 50030
180 50030 $e =~ y/[]()<>:;//d; # Remove brackets, colon, and semi-colon
181  
182 50030 ▶ next if length $e < 4 || index($e, ".") < 0 || __PACKAGE__->is_internethost($e) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $e < 4 or index($e, ".") == -1 or __PACKAGE__->is_internethost($e) == 0 48885 1145 50030
Condition100%6/6
length $e < 4 or index($e, ".") == -1 or __PACKAGE__->is_internethost($e) == 0
l!l&&r!l&&!r
4564332421145
length $e < 4 or index($e, ".") == -1
l!l&&r!l&&!r
17735279084387
MC/DC100%3/3
length $e < 4 or index($e, ".") == -1 or __PACKAGE__->is_internethost($e) == 0
length $e < 4 index($e, ".") == -1 __PACKAGE__->is_internethost($e) == 0
Truth table100%4/4
A or B or C
A= length $e < 4B= index($e, ".") == -1C= __PACKAGE__->is_internethost($e) == 0
ABCresult
1XX1
01X1
0011
0000
183 1145 push @$foundtoken, $e;
184 }
185 1839 ▶ return "" if scalar @$foundtoken == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$foundtoken == 0 1249 590 1839
186 590 ▶ return $foundtoken->[0] if scalar @$foundtoken == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$foundtoken == 1 429 161 590
187  
188 161 for my $e ( @$foundtoken ) {
189 # Returns the longest hostname
190 716 ▶ my $cw = length $e; next if $thelongest >= $cw;
Branch: 1 branch
BranchTrueFalseTotal
if $thelongest >= $cw 532 184 716
191 184 $hostnameis = $e;
192 184 $thelongest = $cw;
193 }
194 161 return $hostnameis;
195 }
196  
197 94 1;
198 __END__
« Sisimai/SMTP/Transcript.pm ↑ Summary Sisimai/SMTP/Command.pm »