Zonemaster/Engine/Normalization.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 143 CC 13 SCAR 36.7
CC
13
Cov
0%
normalize_name
90.0
normalize_label
30.0
trim_space
2.0
CRAP 39.3
1 package Zonemaster::Engine::Normalization;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use parent 'Exporter';
7  
8 use utf8;
9 use Carp;
10 use Encode;
11 use Readonly;
12 use Try::Tiny;
13 use Zonemaster::LDNS;
14  
15 use Zonemaster::Engine::Normalization::Error;
16  
17  
18 =head1 NAME
19  
20 Zonemaster::Engine::Normalization - utility functions for names normalization
21  
22  
23 =head1 SYNOPSIS
24  
25 use Zonemaster::Engine::Normalization;
26  
27 my ($errors, $final_domain) = normalize_name($domain);
28  
29 =head1 EXPORTED FUNCTIONS
30  
31 =over
32 =cut
33  
34  
35 our @EXPORT = qw[ normalize_name ];
36 our @EXPORT_OK = qw[ normalize_name normalize_label trim_space ];
37  
38 Readonly my $ASCII => qr/^[[:ascii:]]+$/;
39 Readonly my $VALID_ASCII => qr(^[A-Za-z0-9/_-]+$);
40  
41 Readonly my $ASCII_FULL_STOP => "\x{002E}";
42 Readonly my $ASCII_FULL_STOP_RE => qr/\x{002E}/;
43 Readonly my %FULL_STOPS => (
44 FULLWIDTH_FULL_STOP => q/\x{FF0E}/,
45 IDEOGRAPHIC_FULL_STOP => q/\x{3002}/,
46 HALFWIDTH_IDEOGRAPHIC_FULL_STOP => q/\x{FF61}/
47 );
48 Readonly my $FULL_STOPS_RE => (sub {
49 my $re = '[' . (join '', values %FULL_STOPS) . ']';
50 return qr/$re/;
51 })->();
52 Readonly my %WHITE_SPACES => (
53 SPACE => q/\x{0020}/,
54 CHARACTER_TABULATION => q/\x{0009}/,
55 NO_BREAK_SPACE => q/\x{00A0}/,
56 EN_QUAD => q/\x{2000}/,
57 EM_QUAD => q/\x{2001}/,
58 EN_SPACE => q/\x{2002}/,
59 EM_SPACE => q/\x{2003}/,
60 THREE_PER_EM_SPACE => q/\x{2004}/,
61 FOUR_PER_EM_SPACE => q/\x{2005}/,
62 SIX_PER_EM_SPACE => q/\x{2006}/,
63 FIGURE_SPACE => q/\x{2007}/,
64 PUNCTUATION_SPACE => q/\x{2008}/,
65 THIN_SPACE => q/\x{2009}/,
66 HAIR_SPACE => q/\x{200A}/,
67 MEDIUM_MATHEMATICAL_SPACE => q/\x{205F}/,
68 IDEOGRAPHIC_SPACE => q/\x{3000}/,
69 OGHAM_SPACE_MARK => q/\x{1680}/,
70 );
71 Readonly my $WHITE_SPACES_RE => (sub {
72 my $re = '[' . (join '', values %WHITE_SPACES) . ']';
73 return qr/$re/;
74 })->();
75 Readonly my %AMBIGUOUS_CHARACTERS => (
76 "LATIN CAPITAL LETTER I WITH DOT ABOVE" => q/\x{0130}/,
77 );
78  
79  
80  
81 =item normalize_label($label)
82  
83 Normalize a single label from a domain name.
84  
85 If the label is ASCII only, it is down cased, else it is converted according
86 to IDNA2008.
87  
88 Downcasing of upper case non-ASCII characters, normalization to the Unicode
89 NFC format and conversion from U-label to A-label is performed by libidn2
90 using L<Zonemaster::LDNS/to_idn($name, ...)>.
91  
92 Returns a tuple C<($errors: ArrayRef[Zonemaster::Engine::Normalization::Error], $alabel: String)>.
93  
94 In case of errors, the returned label will be undefined. If the method
95 succeeded an empty error array is returned.
96  
97 =cut
98  
99 sub normalize_label {
100 my ( $label ) = @_;
101 my @messages;
102  
103 my $alabel = "";
104  
105 if ( $label =~ $VALID_ASCII ) {
106 $alabel = lc $label;
107 } elsif ( $label =~ $ASCII ) {
108 push @messages, Zonemaster::Engine::Normalization::Error->new(INVALID_ASCII => {label => $label});
109  
110 return \@messages, undef;
111 } elsif ( Zonemaster::LDNS::has_idn ) {
112 try {
113 $alabel = Zonemaster::LDNS::to_idn($label);
114 } catch {
115 push @messages, Zonemaster::Engine::Normalization::Error->new(INVALID_U_LABEL => {label => $label});
116  
117 return \@messages, undef;
118 }
119 } else {
120 croak 'The domain name contains at least one non-ASCII character and this installation of Zonemaster has no support for IDNA.';
121 }
122  
123 if ( length($alabel) > 63 ) {
124 push @messages, Zonemaster::Engine::Normalization::Error->new(LABEL_TOO_LONG => {label => $label});
125 return \@messages, undef;
126 }
127  
128 return \@messages, $alabel;
129 }
130  
131 =item trim_space($str)
132  
133 Trim leading and trailing whitespace.
134  
135 Implements the space trimming part of L<normalization document|https://github.com/zonemaster/zonemaster/blob/master/docs/specifications/tests/RequirementsAndNormalizationOfDomainNames.md>.
136  
137 Returns a string.
138  
139 =cut
140  
141 sub trim_space {
142 my ( $str ) = @_;
143  
144 return $str =~ s/^${$WHITE_SPACES_RE}+|${WHITE_SPACES_RE}+$//gr;
145 }
146  
147 =item normalize_name($name)
148  
149 Normalize a domain name.
150  
151 Implements the normalization process, except the space trimming part, described
152 in L<normalization document|https://github.com/zonemaster/zonemaster/blob/master/docs/specifications/tests/RequirementsAndNormalizationOfDomainNames.md>.
153  
154 Returns a tuple C<($errors: ArrayRef[Zonemaster::Engine::Normalization::Error], $name: String)>.
155  
156 In case of errors, the returned name will be undefined. If the method succeeded
157 an empty error array is returned.
158  
159 =cut
160  
161 sub normalize_name {
162 my ( $uname ) = @_;
163 my @messages;
164  
165 if ( length($uname) == 0 ) {
166 push @messages, Zonemaster::Engine::Normalization::Error->new(EMPTY_DOMAIN_NAME => {});
167 return \@messages, undef;
168 }
169  
170 foreach my $char_name ( keys %AMBIGUOUS_CHARACTERS ) {
171 my $char = $AMBIGUOUS_CHARACTERS{$char_name};
172 if ( $uname =~ m/${char}/) {
173 push @messages, Zonemaster::Engine::Normalization::Error->new(AMBIGUOUS_DOWNCASING => { unicode_name => $char_name });
174 }
175 }
176  
177 if ( @messages ) {
178 return \@messages, undef;
179 }
180  
181 $uname =~ s/${FULL_STOPS_RE}/${ASCII_FULL_STOP}/g;
182  
183 if ( $uname eq $ASCII_FULL_STOP ) {
184 return \@messages, $uname;
185 }
186  
187 if ( $uname =~ m/^${ASCII_FULL_STOP_RE}/ ) {
188 push @messages, Zonemaster::Engine::Normalization::Error->new(INITIAL_DOT => {});
189 return \@messages, undef;
190 }
191  
192 if ( $uname =~ m/${ASCII_FULL_STOP_RE}{2,}/ ) {
193 push @messages, Zonemaster::Engine::Normalization::Error->new(REPEATED_DOTS => {});
194 return \@messages, undef;
195 }
196  
197 $uname =~ s/${ASCII_FULL_STOP_RE}$//g;
198  
199 my @labels = split $ASCII_FULL_STOP_RE, $uname;
200 my @label_results = map { [ normalize_label($_) ] } @labels;
201 my @label_errors = map { @{$_->[0]} } @label_results;
202  
203 push @messages, @label_errors;
204  
205 if ( @messages ) {
206 return \@messages, undef;
207 }
208  
209 my @label_ok = map { $_->[1] } @label_results;
210  
211 my $final_name = join '.', @label_ok;
212  
213 if ( length($final_name) > 253 ) {
214 push @messages, Zonemaster::Engine::Normalization::Error->new(DOMAIN_NAME_TOO_LONG => {});
215 return \@messages, undef;
216 }
217  
218 return \@messages, $final_name;
219 }
220  
221  
222 =back
223 =cut
224  
225 1;