Zonemaster/Engine/Test/Address.pm untested

filter: Statementstmt 0.0% 0 / 178 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 296 CC 28 SCAR 38.5
CC
28
Cov
0%
address01
132.0
address02
42.0
address03
42.0
CRAP 47.1
1 package Zonemaster::Engine::Test::Address;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare("v1.0.8");
7  
8 use Carp;
9 use List::MoreUtils qw[none any uniq];
10 use Locale::TextDomain qw[Zonemaster-Engine];
11 use Readonly;
12  
13 use Zonemaster::Engine::Recursor;
14 use Zonemaster::Engine::Constants qw[:addresses :ip];
15 use Zonemaster::Engine::TestMethods;
16 use Zonemaster::Engine::Util qw[name should_run_test];
17  
18 =head1 NAME
19  
20 Zonemaster::Engine::Test::Address - Module implementing tests focused on IP addresses of name servers
21  
22 =head1 SYNOPSIS
23  
24 my @results = Zonemaster::Engine::Test::Address->all( $zone );
25  
26 =head1 METHODS
27  
28 =over
29  
30 =item all()
31  
32 my @logentry_array = all( $zone );
33  
34 Runs the default set of tests for that module, i.e. between L<two and three tests|/TESTS> depending on the tested zone.
35 If L<ADDRESS02|/address02()> passes, L<ADDRESS03|/address03()> is run.
36  
37 Takes a L<Zonemaster::Engine::Zone> object.
38  
39 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
40  
41 =back
42  
43 =cut
44  
45 sub all {
46 my ( $class, $zone ) = @_;
47  
48 my @results;
49  
50 push @results, $class->address01( $zone )
51 if should_run_test( q{address01} );
52  
53 my $ns_with_reverse = 1;
54 if ( should_run_test( q{address02} ) ) {
55 push @results, $class->address02( $zone );
56 $ns_with_reverse = any { $_->tag eq q{NAMESERVERS_IP_WITH_REVERSE} } @results;
57 }
58  
59 # Perform ADDRESS03 if ADDRESS02 passed or was skipped
60 if ( $ns_with_reverse ) {
61 push @results, $class->address03( $zone )
62 if should_run_test( q{address03} );
63 }
64  
65 return @results;
66 }
67  
68 =over
69  
70 =item metadata()
71  
72 my $hash_ref = metadata();
73  
74 Returns a reference to a hash, the keys of which are the names of all Test Cases in the module, and the corresponding values are references to
75 an array containing all the message tags that the Test Case can use in L<log entries|Zonemaster::Engine::Logger::Entry>.
76  
77 =back
78  
79 =cut
80  
81 sub metadata {
82 my ( $class ) = @_;
83  
84 return {
85 address01 => [
86 qw(
87 A01_ADDR_NOT_GLOBALLY_REACHABLE
88 A01_DOCUMENTATION_ADDR
89 A01_GLOBALLY_REACHABLE_ADDR
90 A01_LOCAL_USE_ADDR
91 A01_NO_GLOBALLY_REACHABLE_ADDR
92 A01_NO_NAME_SERVERS_FOUND
93 )
94 ],
95 address02 => [
96 qw(
97 NAMESERVER_IP_WITHOUT_REVERSE
98 NAMESERVERS_IP_WITH_REVERSE
99 NO_RESPONSE_PTR_QUERY
100 TEST_CASE_END
101 TEST_CASE_START
102 )
103 ],
104 address03 => [
105 qw(
106 NAMESERVER_IP_WITHOUT_REVERSE
107 NAMESERVER_IP_PTR_MISMATCH
108 NAMESERVER_IP_PTR_MATCH
109 NO_RESPONSE_PTR_QUERY
110 TEST_CASE_END
111 TEST_CASE_START
112 )
113 ],
114 };
115 } ## end sub metadata
116  
117 Readonly my %TAG_DESCRIPTIONS => (
118 ADDRESS01 => sub {
119 __x # ADDRESS:ADDRESS01
120 'Name server address must be globally reachable';
121 },
122 ADDRESS02 => sub {
123 __x # ADDRESS:ADDRESS02
124 'Reverse DNS entry exists for name server IP address';
125 },
126 ADDRESS03 => sub {
127 __x # ADDRESS:ADDRESS03
128 'Reverse DNS entry matches name server name';
129 },
130 A01_ADDR_NOT_GLOBALLY_REACHABLE => sub {
131 __x # ADDRESS:A01_ADDR_NOT_GLOBALLY_REACHABLE
132 'IP address(es) not listed as globally reachable: "{ns_list}".', @_;
133 },
134 A01_DOCUMENTATION_ADDR => sub {
135 __x # ADDRESS:A01_DOCUMENTATION_ADDR
136 'IP address(es) intended for documentation purposes: "{ns_list}".', @_;
137 },
138 A01_GLOBALLY_REACHABLE_ADDR => sub {
139 __x # ADDRESS:A01_GLOBALLY_REACHABLE_ADDR
140 'Globally reachable IP address(es): "{ns_list}".', @_;
141 },
142 A01_LOCAL_USE_ADDR => sub {
143 __x # ADDRESS:A01_LOCAL_USE_ADDR
144 'IP address(es) intended for local use on network or service provider level: "{ns_list}".', @_;
145 },
146 A01_NO_GLOBALLY_REACHABLE_ADDR => sub {
147 __x # ADDRESS:A01_NO_GLOBALLY_REACHABLE_ADDR
148 'None of the name servers IP addresses are listed as globally reachable.';
149 },
150 A01_NO_NAME_SERVERS_FOUND => sub {
151 __x # ADDRESS:A01_NO_NAME_SERVERS_FOUND
152 'No name servers found.';
153 },
154 NAMESERVER_IP_WITHOUT_REVERSE => sub {
155 __x # ADDRESS:NAMESERVER_IP_WITHOUT_REVERSE
156 'Nameserver {nsname} has an IP address ({ns_ip}) without PTR configured.', @_;
157 },
158 NAMESERVER_IP_PTR_MISMATCH => sub {
159 __x # ADDRESS:NAMESERVER_IP_PTR_MISMATCH
160 'Nameserver {nsname} has an IP address ({ns_ip}) with mismatched PTR result ({names}).', @_;
161 },
162 NAMESERVERS_IP_WITH_REVERSE => sub {
163 __x # ADDRESS:NAMESERVERS_IP_WITH_REVERSE
164 "Reverse DNS entry exists for each Nameserver IP address.", @_;
165 },
166 NAMESERVER_IP_PTR_MATCH => sub {
167 __x # ADDRESS:NAMESERVER_IP_PTR_MATCH
168 "Every reverse DNS entry matches name server name.", @_;
169 },
170 NO_RESPONSE_PTR_QUERY => sub {
171 __x # ADDRESS:NO_RESPONSE_PTR_QUERY
172 'No response from nameserver(s) on PTR query ({domain}).', @_;
173 },
174 TEST_CASE_END => sub {
175 __x # ADDRESS:TEST_CASE_END
176 'TEST_CASE_END {testcase}.', @_;
177 },
178 TEST_CASE_START => sub {
179 __x # ADDRESS:TEST_CASE_START
180 'TEST_CASE_START {testcase}.', @_;
181 },
182 );
183  
184 =over
185  
186 =item tag_descriptions()
187  
188 my $hash_ref = tag_descriptions();
189  
190 Used by the L<built-in translation system|Zonemaster::Engine::Translator>.
191  
192 Returns a reference to a hash, the keys of which are the message tags and the corresponding values are strings (message IDs).
193  
194 =back
195  
196 =cut
197  
198 sub tag_descriptions {
199 return \%TAG_DESCRIPTIONS;
200 }
201  
202 =over
203  
204 =item version()
205  
206 my $version_string = version();
207  
208 Returns a string containing the version of the current module.
209  
210 =back
211  
212 =cut
213  
214 sub version {
215 return "$Zonemaster::Engine::Test::Address::VERSION";
216 }
217  
218 =head1 INTERNAL METHODS
219  
220 =over
221  
222 =item _emit_log()
223  
224 my $log_entry = _emit_log( $message_tag_string, $hash_ref );
225  
226 Adds a message to the L<logger|Zonemaster::Engine::Logger> for this module.
227 See L<Zonemaster::Engine::Logger::Entry/add($tag, $argref, $module, $testcase)> for more details.
228  
229 Takes a string (message tag) and a reference to a hash (arguments).
230  
231 Returns a L<Zonemaster::Engine::Logger::Entry> object.
232  
233 =back
234  
235 =cut
236  
237 sub _emit_log { my ( $tag, $argref ) = @_; return Zonemaster::Engine->logger->add( $tag, $argref, 'Address' ); }
238  
239 =over
240  
241 =item _find_special_address()
242  
243 my $hash_ref = _find_special_address( $ip );
244  
245 Verifies if an IP address is a special (private, reserved, ...) one.
246  
247 Takes a L<Net::IP::XS> object.
248  
249 Returns a reference to a hash if true (see L<Zonemaster::Engine::Constants/_extract_iana_ip_blocks()>), or C<undef> if false.
250  
251 =back
252  
253 =cut
254  
255 sub _find_special_address {
256 my ( $class, $ip ) = @_;
257 my @special_addresses;
258  
259 if ( $ip->version == $IP_VERSION_4 ) {
260 @special_addresses = @IPV4_SPECIAL_ADDRESSES;
261 }
262 elsif ( $ip->version == $IP_VERSION_6 ) {
263 @special_addresses = @IPV6_SPECIAL_ADDRESSES;
264 }
265  
266 foreach my $ip_details ( @special_addresses ) {
267 if ( $ip->overlaps( ${$ip_details}{ip} ) ) {
268 return $ip_details;
269 }
270 }
271  
272 return;
273 }
274  
275 =head1 TESTS
276  
277 =over
278  
279 =item address01()
280  
281 my @logentry_array = address01( $zone );
282  
283 Runs the L<Address01 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Address-TP/address01.md>.
284  
285 Takes a L<Zonemaster::Engine::Zone> object.
286  
287 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
288  
289 =back
290  
291 =cut
292  
293 sub address01 {
294 my ( $class, $zone ) = @_;
295  
296 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Address01';
297 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
298  
299 my @nss = uniq grep { $_->isa('Zonemaster::Engine::Nameserver') } (
300 @{ Zonemaster::Engine::TestMethodsV2->get_del_ns_names_and_ips( $zone ) // [] },
301 @{ Zonemaster::Engine::TestMethodsV2->get_zone_ns_names_and_ips( $zone ) // [] }
302 );
303  
304 unless ( @nss ) {
305 push @results, _emit_log( A01_NO_NAME_SERVERS_FOUND => {} );
306 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
307 }
308  
309 my ( @documentation_addr, @local_use_addr, @not_globally_reachable, @globally_reachable );
310 my %ip_already_processed;
311  
312 NSS:
313 foreach my $ns ( @nss ) {
314 my $ns_ip = $ns->address->short;
315  
316 next if exists $ip_already_processed{$ns_ip};
317 $ip_already_processed{$ns_ip} = [ grep { $_->address->short eq $ns_ip } @nss ];
318  
319 my @matching_nss = @{ $ip_already_processed{$ns_ip} };
320  
321 my $ip_details_ref = $class->_find_special_address( $ns->address );
322  
323 if ( $ip_details_ref ) {
324 my $ip_category = ${$ip_details_ref}{name};
325  
326 if ( index( $ip_category, 'Documentation' ) != -1 ) {
327 push @documentation_addr, @matching_nss;
328 next;
329 }
330  
331 my @categories = ( 'Private-Use', 'Loopback', 'Link Local', 'Link-Local', 'Unique-Local', 'Shared Address Space' );
332 foreach my $category ( @categories ) {
333 if ( index( $ip_category, $category ) != -1 ) {
334 push @local_use_addr, @matching_nss;
335 next NSS;
336 }
337 }
338  
339 if ( index( ${$ip_details_ref}{globally_reachable}, 'True' ) == -1 ) {
340 push @not_globally_reachable, @matching_nss;
341 next;
342 }
343 }
344  
345 push @globally_reachable, @matching_nss;
346 }
347  
348 if ( @globally_reachable ) {
349 push @results,
350 _emit_log(
351 A01_GLOBALLY_REACHABLE_ADDR => {
352 ns_list => join( q{;}, uniq sort @globally_reachable )
353 }
354 );
355 }
356 else {
357 push @results,
358 _emit_log(
359 A01_NO_GLOBALLY_REACHABLE_ADDR => {}
360 );
361 }
362  
363 if ( @documentation_addr ) {
364 push @results,
365 _emit_log(
366 A01_DOCUMENTATION_ADDR => {
367 ns_list => join( q{;}, uniq sort @documentation_addr )
368 }
369 );
370 }
371  
372 if ( @local_use_addr ) {
373 push @results,
374 _emit_log(
375 A01_LOCAL_USE_ADDR => {
376 ns_list => join( q{;}, uniq sort @local_use_addr )
377 }
378 );
379 }
380  
381 if ( @not_globally_reachable ) {
382 push @results,
383 _emit_log(
384 A01_ADDR_NOT_GLOBALLY_REACHABLE => {
385 ns_list => join( q{;}, uniq sort @not_globally_reachable )
386 }
387 );
388 }
389  
390 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
391 } ## end sub address01
392  
393 =over
394  
395 =item address02()
396  
397 my @logentry_array = address02( $zone );
398  
399 Runs the L<Address02 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Address-TP/address02.md>.
400  
401 Takes a L<Zonemaster::Engine::Zone> object.
402  
403 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
404  
405 =back
406  
407 =cut
408  
409 sub address02 {
410 my ( $class, $zone ) = @_;
411  
412 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Address02';
413 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
414  
415 my %ips;
416 my $ptr_query;
417  
418 foreach
419 my $local_ns ( @{ Zonemaster::Engine::TestMethods->method4( $zone ) }, @{ Zonemaster::Engine::TestMethods->method5( $zone ) } )
420 {
421  
422 next if $ips{ $local_ns->address->short };
423  
424 my $reverse_ip_query = $local_ns->address->reverse_ip;
425 $ptr_query = $reverse_ip_query;
426  
427 my $p = Zonemaster::Engine::Recursor->recurse( $ptr_query, q{PTR} );
428  
429 # In case of Classless IN-ADDR.ARPA delegation, query returns
430 # CNAME records. A PTR query is done on the CNAME.
431 if ( $p and $p->rcode eq q{NOERROR} and $p->get_records( q{CNAME}, q{answer} ) ) {
432 my ( $cname ) = $p->get_records( q{CNAME}, q{answer} );
433 $ptr_query = $cname->cname;
434 $p = Zonemaster::Engine::Recursor->recurse( $ptr_query, q{PTR} );
435 }
436  
437 if ( $p ) {
438 if ( $p->rcode ne q{NOERROR} or not $p->get_records( q{PTR}, q{answer} ) ) {
439 push @results,
440 _emit_log(
441 NAMESERVER_IP_WITHOUT_REVERSE => {
442 nsname => $local_ns->name->string,
443 ns_ip => $local_ns->address->short,
444 }
445 );
446 }
447 }
448 else {
449 push @results,
450 _emit_log(
451 NO_RESPONSE_PTR_QUERY => {
452 domain => $ptr_query,
453 }
454 );
455 }
456  
457 $ips{ $local_ns->address->short }++;
458  
459 } ## end foreach my $local_ns ( @{ Zonemaster::Engine::TestMethods...})
460  
461 if ( scalar keys %ips and not grep { $_->tag ne q{TEST_CASE_START} } @results ) {
462 push @results, _emit_log( NAMESERVERS_IP_WITH_REVERSE => {} );
463 }
464  
465 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
466 } ## end sub address02
467  
468 =over
469  
470 =item address03()
471  
472 my @logentry_array = address03( $zone );
473  
474 Runs the L<Address03 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Address-TP/address03.md>.
475  
476 Takes a L<Zonemaster::Engine::Zone> object.
477  
478 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
479  
480 =back
481  
482 =cut
483  
484 sub address03 {
485 my ( $class, $zone ) = @_;
486  
487 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Address03';
488 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
489 my $ptr_query;
490  
491 my %ips;
492  
493 foreach my $local_ns ( @{ Zonemaster::Engine::TestMethods->method5( $zone ) } ) {
494  
495 next if $ips{ $local_ns->address->short };
496  
497 my $reverse_ip_query = $local_ns->address->reverse_ip;
498 $ptr_query = $reverse_ip_query;
499  
500 my $p = Zonemaster::Engine::Recursor->recurse( $ptr_query, q{PTR} );
501  
502 if ( $p ) {
503 my @ptr = $p->get_records( q{PTR}, 'answer' );
504 if ( $p->rcode eq q{NOERROR} and scalar @ptr ) {
505 if ( none { name( $_->ptrdname ) eq $local_ns->name->string . q{.} } @ptr ) {
506 push @results,
507 _emit_log(
508 NAMESERVER_IP_PTR_MISMATCH => {
509 nsname => $local_ns->name->string,
510 ns_ip => $local_ns->address->short,
511 names => join( q{/}, map { $_->ptrdname } @ptr ),
512 }
513 );
514 }
515 }
516 else {
517 push @results,
518 _emit_log(
519 NAMESERVER_IP_WITHOUT_REVERSE => {
520 nsname => $local_ns->name->string,
521 ns_ip => $local_ns->address->short,
522 }
523 );
524 }
525 } ## end if ( $p )
526 else {
527 push @results,
528 _emit_log(
529 NO_RESPONSE_PTR_QUERY => {
530 domain => $ptr_query,
531 }
532 );
533 }
534  
535 $ips{ $local_ns->address->short }++;
536  
537 } ## end foreach my $local_ns ( @{ Zonemaster::Engine::TestMethods...})
538  
539 if ( scalar keys %ips and not grep { $_->tag ne q{TEST_CASE_START} } @results ) {
540 push @results, _emit_log( NAMESERVER_IP_PTR_MATCH => {} );
541 }
542  
543 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
544 } ## end sub address03
545  
546 1;