Zonemaster/Engine/Test/Delegation.pm untested

filter: Statementstmt 0.0% 0 / 386 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 602 CC 52 SCAR 39.5
CC
52
Cov
0%
delegation01
132.0
delegation05
110.0
all
72.0
CRAP 51.8
1 package Zonemaster::Engine::Test::Delegation;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare("v1.0.20");
7  
8 use List::MoreUtils qw[uniq];
9 use Locale::TextDomain qw[Zonemaster-Engine];
10 use Readonly;
11  
12 use Zonemaster::Engine::Profile;
13 use Zonemaster::Engine::Recursor;
14 use Zonemaster::Engine::Constants ':all';
15 use Zonemaster::Engine::Test::Address;
16 use Zonemaster::Engine::Test::Syntax;
17 use Zonemaster::Engine::TestMethods;
18 use Zonemaster::Engine::Util;
19 use Zonemaster::LDNS::Packet;
20 use Zonemaster::LDNS::RR;
21  
22 =head1 NAME
23  
24 Zonemaster::Engine::Test::Delegation - Module implementing tests focused on zone delegation
25  
26 =head1 SYNOPSIS
27  
28 my @results = Zonemaster::Engine::Test::Delegation->all( $zone );
29  
30 =head1 METHODS
31  
32 =over
33  
34 =item all()
35  
36 my @logentry_array = all( $zone );
37  
38 Runs the default set of tests for that module, i.e. L<seven tests|/TESTS>.
39  
40 Takes a L<Zonemaster::Engine::Zone> object.
41  
42 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
43  
44 =back
45  
46 =cut
47  
48 sub all {
49 my ( $class, $zone ) = @_;
50 my @results;
51  
52 push @results, $class->delegation01( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation01} );
53 push @results, $class->delegation02( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation02} );
54 push @results, $class->delegation03( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation03} );
55 push @results, $class->delegation04( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation04} );
56 push @results, $class->delegation05( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation05} );
57 push @results, $class->delegation06( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation06} );
58 push @results, $class->delegation07( $zone ) if Zonemaster::Engine::Util::should_run_test( q{delegation07} );
59  
60 return @results;
61 }
62  
63 =over
64  
65 =item metadata()
66  
67 my $hash_ref = metadata();
68  
69 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
70 an array containing all the message tags that the Test Case can use in L<log entries|Zonemaster::Engine::Logger::Entry>.
71  
72 =back
73  
74 =cut
75  
76 sub metadata {
77 my ( $class ) = @_;
78  
79 return {
80 delegation01 => [
81 qw(
82 ENOUGH_NS_CHILD
83 ENOUGH_NS_DEL
84 NOT_ENOUGH_NS_DEL
85 NOT_ENOUGH_NS_CHILD
86 ENOUGH_IPV4_NS_CHILD
87 ENOUGH_IPV4_NS_DEL
88 ENOUGH_IPV6_NS_CHILD
89 ENOUGH_IPV6_NS_DEL
90 NOT_ENOUGH_IPV4_NS_CHILD
91 NOT_ENOUGH_IPV4_NS_DEL
92 NOT_ENOUGH_IPV6_NS_CHILD
93 NOT_ENOUGH_IPV6_NS_DEL
94 NO_IPV4_NS_CHILD
95 NO_IPV4_NS_DEL
96 NO_IPV6_NS_CHILD
97 NO_IPV6_NS_DEL
98 TEST_CASE_END
99 TEST_CASE_START
100 )
101 ],
102 delegation02 => [
103 qw(
104 CHILD_DISTINCT_NS_IP
105 CHILD_NS_SAME_IP
106 DEL_DISTINCT_NS_IP
107 DEL_NS_SAME_IP
108 SAME_IP_ADDRESS
109 DISTINCT_IP_ADDRESS
110 TEST_CASE_END
111 TEST_CASE_START
112 )
113 ],
114 delegation03 => [
115 qw(
116 REFERRAL_SIZE_TOO_LARGE
117 REFERRAL_SIZE_OK
118 TEST_CASE_END
119 TEST_CASE_START
120 )
121 ],
122 delegation04 => [
123 qw(
124 IS_NOT_AUTHORITATIVE
125 IPV4_DISABLED
126 IPV6_DISABLED
127 ARE_AUTHORITATIVE
128 TEST_CASE_END
129 TEST_CASE_START
130 )
131 ],
132 delegation05 => [
133 qw(
134 NO_NS_CNAME
135 NO_RESPONSE
136 NS_IS_CNAME
137 UNEXPECTED_RCODE
138 TEST_CASE_END
139 TEST_CASE_START
140 )
141 ],
142 delegation06 => [
143 qw(
144 SOA_NOT_EXISTS
145 SOA_EXISTS
146 IPV4_DISABLED
147 IPV6_DISABLED
148 TEST_CASE_END
149 TEST_CASE_START
150 )
151 ],
152 delegation07 => [
153 qw(
154 EXTRA_NAME_PARENT
155 EXTRA_NAME_CHILD
156 TOTAL_NAME_MISMATCH
157 NAMES_MATCH
158 TEST_CASE_END
159 TEST_CASE_START
160 )
161 ],
162 };
163 } ## end sub metadata
164  
165 Readonly my %TAG_DESCRIPTIONS => (
166 DELEGATION01 => sub {
167 __x # DELEGATION:DELEGATION01
168 "Minimum number of name servers";
169 },
170 DELEGATION02 => sub {
171 __x # DELEGATION:DELEGATION02
172 "Name servers must have distinct IP addresses";
173 },
174 DELEGATION03 => sub {
175 __x # DELEGATION:DELEGATION03
176 "No truncation of referrals";
177 },
178 DELEGATION04 => sub {
179 __x # DELEGATION:DELEGATION04
180 "Name server is authoritative";
181 },
182 DELEGATION05 => sub {
183 __x # DELEGATION:DELEGATION05
184 "Name server must not point at CNAME alias";
185 },
186 DELEGATION06 => sub {
187 __x # DELEGATION:DELEGATION06
188 "Existence of SOA";
189 },
190 DELEGATION07 => sub {
191 __x # DELEGATION:DELEGATION07
192 "Parent glue name records present in child";
193 },
194 ARE_AUTHORITATIVE => sub {
195 __x # DELEGATION:ARE_AUTHORITATIVE
196 "All these nameservers are confirmed to be authoritative : {nsname_list}.", @_;
197 },
198 CHILD_DISTINCT_NS_IP => sub {
199 __x # DELEGATION:CHILD_DISTINCT_NS_IP
200 "All the IP addresses used by the nameservers in child are unique.", @_;
201 },
202 CHILD_NS_SAME_IP => sub {
203 __x # DELEGATION:CHILD_NS_SAME_IP
204 "IP {ns_ip} in child refers to multiple nameservers ({nsname_list}).", @_;
205 },
206 DEL_DISTINCT_NS_IP => sub {
207 __x # DELEGATION:DEL_DISTINCT_NS_IP
208 "All the IP addresses used by the nameservers in parent are unique.", @_;
209 },
210 DEL_NS_SAME_IP => sub {
211 __x # DELEGATION:DEL_NS_SAME_IP
212 "IP {ns_ip} in parent refers to multiple nameservers ({nsname_list}).", @_;
213 },
214 DISTINCT_IP_ADDRESS => sub {
215 __x # DELEGATION:DISTINCT_IP_ADDRESS
216 "All the IP addresses used by the nameservers are unique.", @_;
217 },
218 ENOUGH_IPV4_NS_CHILD => sub {
219 __x # DELEGATION:ENOUGH_IPV4_NS_CHILD
220 "Child lists enough ({count}) nameservers that resolve to IPv4 "
221 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
222 @_;
223 },
224 ENOUGH_IPV4_NS_DEL => sub {
225 __x # DELEGATION:ENOUGH_IPV4_NS_DEL
226 "Delegation lists enough ({count}) nameservers that resolve to IPv4 "
227 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
228 @_;
229 },
230 ENOUGH_IPV6_NS_CHILD => sub {
231 __x # DELEGATION:ENOUGH_IPV6_NS_CHILD
232 "Child lists enough ({count}) nameservers that resolve to IPv6 "
233 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
234 @_;
235 },
236 ENOUGH_IPV6_NS_DEL => sub {
237 __x # DELEGATION:ENOUGH_IPV6_NS_DEL
238 "Delegation lists enough ({count}) nameservers that resolve to IPv6 "
239 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
240 @_;
241 },
242 ENOUGH_NS_CHILD => sub {
243 __x # DELEGATION:ENOUGH_NS_CHILD
244 "Child lists enough ({count}) nameservers. Lower limit set to {minimum}. Name servers: {nsname_list}", @_;
245 },
246 ENOUGH_NS_DEL => sub {
247 __x # DELEGATION:ENOUGH_NS_DEL
248 "Delegation lists enough ({count}) nameservers. Lower limit set to {minimum}. Name servers: {nsname_list}", @_;
249 },
250 EXTRA_NAME_CHILD => sub {
251 __x # DELEGATION:EXTRA_NAME_CHILD
252 "Child has nameserver(s) not listed at parent ({extra}).", @_;
253 },
254 EXTRA_NAME_PARENT => sub {
255 __x # DELEGATION:EXTRA_NAME_PARENT
256 "Parent has nameserver(s) not listed at the child ({extra}).", @_;
257 },
258 IPV4_DISABLED => sub {
259 __x # DELEGATION:IPV4_DISABLED
260 'IPv4 is disabled, not sending "{rrtype}" query to {ns}.', @_;
261 },
262 IPV6_DISABLED => sub {
263 __x # DELEGATION:IPV6_DISABLED
264 'IPv6 is disabled, not sending "{rrtype}" query to {ns}.', @_;
265 },
266 IS_NOT_AUTHORITATIVE => sub {
267 __x # DELEGATION:IS_NOT_AUTHORITATIVE
268 "Nameserver {ns} response is not authoritative on {proto} port 53.", @_;
269 },
270 NAMES_MATCH => sub {
271 __x # DELEGATION:NAMES_MATCH
272 "All of the nameserver names are listed both at parent and child.", @_;
273 },
274 NO_RESPONSE => sub {
275 __x # DELEGATION:NO_RESPONSE
276 'Nameserver {ns} did not respond to a query for name {query_name} and type {rrtype}.', @_;
277 },
278 NOT_ENOUGH_IPV4_NS_CHILD => sub {
279 __x # DELEGATION:NOT_ENOUGH_IPV4_NS_CHILD
280 "Child does not list enough ({count}) nameservers that resolve to IPv4 "
281 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
282 @_;
283 },
284 NOT_ENOUGH_IPV4_NS_DEL => sub {
285 __x # DELEGATION:NOT_ENOUGH_IPV4_NS_DEL
286 "Delegation does not list enough ({count}) nameservers that resolve to IPv4 "
287 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
288 @_;
289 },
290 NOT_ENOUGH_IPV6_NS_CHILD => sub {
291 __x # DELEGATION:NOT_ENOUGH_IPV6_NS_CHILD
292 "Child does not list enough ({count}) nameservers that resolve to IPv6 "
293 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
294 @_;
295 },
296 NOT_ENOUGH_IPV6_NS_DEL => sub {
297 __x # DELEGATION:NOT_ENOUGH_IPV6_NS_DEL
298 "Delegation does not list enough ({count}) nameservers that resolve to IPv6 "
299 . "addresses. Lower limit set to {minimum}. Name servers: {ns_list}",
300 @_;
301 },
302 NOT_ENOUGH_NS_CHILD => sub {
303 __x # DELEGATION:NOT_ENOUGH_NS_CHILD
304 "Child does not list enough ({count}) nameservers. Lower limit set to {minimum}. Name servers: {nsname_list}", @_;
305 },
306 NOT_ENOUGH_NS_DEL => sub {
307 __x # DELEGATION:NOT_ENOUGH_NS_DEL
308 "Delegation does not list enough ({count}) nameservers. Lower limit set to {minimum}. Name servers: {nsname_list}", @_;
309 },
310 NO_IPV4_NS_CHILD => sub {
311 __x # DELEGATION:NO_IPV4_NS_CHILD
312 "Child lists no nameserver that resolves to an IPv4 address. "
313 . "If any were present, the minimum allowed would be {minimum}.",
314 @_;
315 },
316 NO_IPV4_NS_DEL => sub {
317 __x # DELEGATION:NO_IPV4_NS_DEL
318 "Delegation lists no nameserver that resolves to an IPv4 address. "
319 . "If any were present, the minimum allowed would be {minimum}.",
320 @_;
321 },
322 NO_IPV6_NS_CHILD => sub {
323 __x # DELEGATION:NO_IPV6_NS_CHILD
324 "Child lists no nameserver that resolves to an IPv6 address. "
325 . "If any were present, the minimum allowed would be {minimum}.",
326 @_;
327 },
328 NO_IPV6_NS_DEL => sub {
329 __x # DELEGATION:NO_IPV6_NS_DEL
330 "Delegation lists no nameserver that resolves to an IPv6 address. "
331 . "If any were present, the minimum allowed would be {minimum}.",
332 @_;
333 },
334 NS_IS_CNAME => sub {
335 __x # DELEGATION:NS_IS_CNAME
336 "Nameserver {nsname} RR points to CNAME.", @_;
337 },
338 NO_NS_CNAME => sub {
339 __x # DELEGATION:NO_NS_CNAME
340 "No nameserver points to CNAME alias.", @_;
341 },
342 REFERRAL_SIZE_TOO_LARGE => sub {
343 __x # DELEGATION:REFERRAL_SIZE_TOO_LARGE
344 "The smallest possible legal referral packet is larger than 512 octets (it is {size}).", @_;
345 },
346 REFERRAL_SIZE_OK => sub {
347 __x # DELEGATION:REFERRAL_SIZE_OK
348 "The smallest possible legal referral packet is smaller than 513 octets (it is {size}).", @_;
349 },
350 SAME_IP_ADDRESS => sub {
351 __x # DELEGATION:SAME_IP_ADDRESS
352 "IP {ns_ip} refers to multiple nameservers ({nsname_list}).", @_;
353 },
354 SOA_EXISTS => sub {
355 __x # DELEGATION:SOA_EXISTS
356 "All the nameservers have SOA record.", @_;
357 },
358 SOA_NOT_EXISTS => sub {
359 __x # DELEGATION:SOA_NOT_EXISTS
360 "Empty NOERROR response to SOA query was received from {ns}.", @_;
361 },
362 TEST_CASE_END => sub {
363 __x # DELEGATION:TEST_CASE_END
364 'TEST_CASE_END {testcase}.', @_;
365 },
366 TEST_CASE_START => sub {
367 __x # DELEGATION:TEST_CASE_START
368 'TEST_CASE_START {testcase}.', @_;
369 },
370 TOTAL_NAME_MISMATCH => sub {
371 __x # DELEGATION:TOTAL_NAME_MISMATCH
372 "None of the nameservers listed at the parent are listed at the child.", @_;
373 },
374 UNEXPECTED_RCODE => sub {
375 __x # DELEGATION:UNEXPECTED_RCODE
376 'Nameserver {ns} answered query for name {query_name} and type {rrtype} with RCODE {rcode}.', @_;
377 },
378  
379 );
380  
381 =over
382  
383 =item tag_descriptions()
384  
385 my $hash_ref = tag_descriptions();
386  
387 Used by the L<built-in translation system|Zonemaster::Engine::Translator>.
388  
389 Returns a reference to a hash, the keys of which are the message tags and the corresponding values are strings (message IDs).
390  
391 =back
392  
393 =cut
394  
395 sub tag_descriptions {
396 return \%TAG_DESCRIPTIONS;
397 }
398  
399 =over
400  
401 =item version()
402  
403 my $version_string = version();
404  
405 Returns a string containing the version of the current module.
406  
407 =back
408  
409 =cut
410  
411 sub version {
412 return "$Zonemaster::Engine::Test::Delegation::VERSION";
413 }
414  
415 =head1 INTERNAL METHODS
416  
417 =over
418  
419 =item _emit_log()
420  
421 my $log_entry = _emit_log( $message_tag_string, $hash_ref );
422  
423 Adds a message to the L<logger|Zonemaster::Engine::Logger> for this module.
424 See L<Zonemaster::Engine::Logger::Entry/add($tag, $argref, $module, $testcase)> for more details.
425  
426 Takes a string (message tag) and a reference to a hash (arguments).
427  
428 Returns a L<Zonemaster::Engine::Logger::Entry> object.
429  
430 =back
431  
432 =cut
433  
434 sub _emit_log { my ( $tag, $argref ) = @_; return Zonemaster::Engine->logger->add( $tag, $argref, 'Delegation' ); }
435  
436 =over
437  
438 =item _ip_disabled_message()
439  
440 my $bool = _ip_disabled_message( $logentry_array_ref, $ns, @query_type_array );
441  
442 Checks if the IP version of a given name server is allowed to be queried. If not, it adds a logging message and returns true. Else, it returns false.
443  
444 Takes a reference to an array of L<Zonemaster::Engine::Logger::Entry> objects, a L<Zonemaster::Engine::Nameserver> object and an array of strings (query type).
445  
446 Returns a boolean.
447  
448 =back
449  
450 =cut
451  
452 sub _ip_disabled_message {
453 my ( $results_array, $ns, @rrtypes ) = @_;
454  
455 if ( not Zonemaster::Engine::Profile->effective->get(q{net.ipv6}) and $ns->address->version == $IP_VERSION_6 ) {
456 push @$results_array, map {
457 _emit_log(
458 IPV6_DISABLED => {
459 ns => $ns->string,
460 rrtype => $_
461 }
462 )
463 } @rrtypes;
464 return 1;
465 }
466  
467 if ( not Zonemaster::Engine::Profile->effective->get(q{net.ipv4}) and $ns->address->version == $IP_VERSION_4 ) {
468 push @$results_array, map {
469 _emit_log(
470 IPV4_DISABLED => {
471 ns => $ns->string,
472 rrtype => $_,
473 }
474 )
475 } @rrtypes;
476 return 1;
477 }
478 return 0;
479 }
480  
481 =over
482  
483 =item _max_length_name_for()
484  
485 my $name_string = _max_length_name_for( $name );
486  
487 Makes up a name of maximum length in the given domain name. Used as an helper function for Test Case L<Delegation03|/delegation03()>.
488  
489 Takes a L<Zonemaster::Engine::DNSName> object.
490  
491 Returns a string.
492  
493 =back
494  
495 =cut
496  
497 sub _max_length_name_for {
498 my ( $top ) = @_;
499 my @chars = q{A} .. q{Z};
500  
501 my $name = name( $top )->fqdn;
502 $name = q{} if $name eq q{.}; # Special case for root zone
503  
504 while ( length( $name ) < $FQDN_MAX_LENGTH - 1 ) {
505 my $len = $FQDN_MAX_LENGTH - length( $name ) - 1;
506 $len = $LABEL_MAX_LENGTH if $len > $LABEL_MAX_LENGTH;
507 $name = join( q{}, map { $chars[ rand @chars ] } 1 .. $len ) . q{.} . $name;
508 }
509  
510 return $name;
511 }
512  
513 =over
514  
515 =item _find_dup_ns()
516  
517 my @logentry_array = _find_dup_ns( %hash );
518  
519 Checks if given name servers have distinct IP addresses. Used as an helper function for Test Case L<Delegation02|/delegation02()>.
520  
521 Takes a hash - the keys of which are C<duplicate_tag>, C<distinct_tag> and C<ns_list>, and their corresponding values are a string,
522 a string and a reference to an array of L<Zonemaster::Engine::Nameserver> objects, respectively.
523  
524 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
525  
526 =back
527  
528 =cut
529  
530 sub _find_dup_ns {
531 my %args = @_;
532 my $duplicate_tag = $args{duplicate_tag};
533 my $distinct_tag = $args{distinct_tag};
534 my @nss = @{ $args{ns_list} };
535  
536 my %nsnames_and_ip;
537 my %ips;
538 foreach my $local_ns ( @nss ) {
539  
540 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
541  
542 push @{ $ips{ $local_ns->address->short } }, $local_ns->name->string;
543  
544 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
545  
546 }
547  
548 my @results;
549 foreach my $local_ip ( sort keys %ips ) {
550 if ( scalar @{ $ips{$local_ip} } > 1 ) {
551 push @results,
552 _emit_log(
553 $duplicate_tag => {
554 nsname_list => join( q{;}, @{ $ips{$local_ip} } ),
555 ns_ip => $local_ip,
556 }
557 );
558 }
559 }
560  
561 if ( @nss && !@results ) {
562 push @results, _emit_log( $distinct_tag => {} );
563 }
564  
565 return @results;
566 }
567  
568 =head1 TESTS
569  
570 =over
571  
572 =item delegation01()
573  
574 my @logentry_array = delegation01( $zone );
575  
576 Runs the L<Delegation01 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation01.md>.
577  
578 Takes a L<Zonemaster::Engine::Zone> object.
579  
580 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
581  
582 =back
583  
584 =cut
585  
586 sub delegation01 {
587 my ( $class, $zone ) = @_;
588  
589 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation01';
590 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
591  
592 # Determine delegation NS names
593 my @del_nsnames = map { $_->string } @{ Zonemaster::Engine::TestMethods->method2( $zone ) };
594 my $del_nsnames_args = {
595 count => scalar( @del_nsnames ),
596 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
597 nsname_list => join( q{;}, sort @del_nsnames ),
598 };
599  
600 # Check delegation NS names
601 if ( scalar( @del_nsnames ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
602 push @results, _emit_log( ENOUGH_NS_DEL => $del_nsnames_args );
603 }
604 else {
605 push @results, _emit_log( NOT_ENOUGH_NS_DEL => $del_nsnames_args );
606 }
607  
608 # Determine child NS names
609 my @child_nsnames = map { $_->string } @{ Zonemaster::Engine::TestMethods->method3( $zone ) };
610 my $child_nsnames_args = {
611 count => scalar( @child_nsnames ),
612 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
613 nsname_list => join( q{;}, sort @child_nsnames ),
614 };
615  
616 # Check child NS names
617 if ( scalar( @child_nsnames ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
618 push @results, _emit_log( ENOUGH_NS_CHILD => $child_nsnames_args );
619 }
620 else {
621 push @results, _emit_log( NOT_ENOUGH_NS_CHILD => $child_nsnames_args );
622 }
623  
624 # Determine child NS names with addresses
625 my @child_ns = @{ Zonemaster::Engine::TestMethods->method5( $zone ) };
626 my @child_ns_ipv4 = map { $_ } grep { $_->address->version == 4 } @child_ns;
627 my @child_ns_ipv6 = map { $_ } grep { $_->address->version == 6 } @child_ns;
628  
629 my $child_ns_ipv4_args = {
630 count => scalar( uniq map { $_->name->string } @child_ns_ipv4 ),
631 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
632 ns_list => join( q{;}, sort map { $_->string } @child_ns_ipv4 ),
633 };
634 my $child_ns_ipv6_args = {
635 count => scalar( uniq map { $_->name->string } @child_ns_ipv6 ),
636 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
637 ns_list => join( q{;}, sort map { $_->string } @child_ns_ipv6 ),
638 };
639  
640 if ( scalar( uniq map { $_->name->string } @child_ns_ipv4 ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
641 push @results, _emit_log( ENOUGH_IPV4_NS_CHILD => $child_ns_ipv4_args );
642 }
643 elsif ( scalar( uniq map { $_->name->string } @child_ns_ipv4 ) > 0 ) {
644 push @results, _emit_log( NOT_ENOUGH_IPV4_NS_CHILD => $child_ns_ipv4_args );
645 }
646 else {
647 push @results, _emit_log( NO_IPV4_NS_CHILD => $child_ns_ipv4_args );
648 }
649  
650 if ( scalar( uniq map { $_->name->string } @child_ns_ipv6 ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
651 push @results, _emit_log( ENOUGH_IPV6_NS_CHILD => $child_ns_ipv6_args );
652 }
653 elsif ( scalar( uniq map { $_->name->string } @child_ns_ipv6 ) > 0 ) {
654 push @results, _emit_log( NOT_ENOUGH_IPV6_NS_CHILD => $child_ns_ipv6_args );
655 }
656 else {
657 push @results, _emit_log( NO_IPV6_NS_CHILD => $child_ns_ipv6_args );
658 }
659  
660 # Determine delegation NS names with addresses
661 my @del_ns = @{ Zonemaster::Engine::TestMethods->method4( $zone ) };
662 my @del_ns_ipv4 = map { $_ } grep { $_->address->version == 4 } @del_ns;
663 my @del_ns_ipv6 = map { $_ } grep { $_->address->version == 6 } @del_ns;
664  
665 my $del_ns_ipv4_args = {
666 count => scalar( uniq map { $_->name->string } @del_ns_ipv4 ),
667 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
668 ns_list => join( q{;}, sort map { $_->string } @del_ns_ipv4 ),
669 };
670 my $del_ns_ipv6_args = {
671 count => scalar( uniq map { $_->name->string } @del_ns_ipv6 ),
672 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
673 ns_list => join( q{;}, sort map { $_->string } @del_ns_ipv6 ),
674 };
675  
676 if ( scalar( uniq map { $_->name->string } @del_ns_ipv4 ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
677 push @results, _emit_log( ENOUGH_IPV4_NS_DEL => $del_ns_ipv4_args );
678 }
679 elsif ( scalar( uniq map { $_->name->string } @del_ns_ipv4 ) > 0 ) {
680 push @results, _emit_log( NOT_ENOUGH_IPV4_NS_DEL => $del_ns_ipv4_args );
681 }
682 else {
683 push @results, _emit_log( NO_IPV4_NS_DEL => $del_ns_ipv4_args );
684 }
685  
686 if ( scalar( uniq map { $_->name->string } @del_ns_ipv6 ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
687 push @results, _emit_log( ENOUGH_IPV6_NS_DEL => $del_ns_ipv6_args );
688 }
689 elsif ( scalar( uniq map { $_->name->string } @del_ns_ipv6 ) > 0 ) {
690 push @results, _emit_log( NOT_ENOUGH_IPV6_NS_DEL => $del_ns_ipv6_args );
691 }
692 else {
693 push @results, _emit_log( NO_IPV6_NS_DEL => $del_ns_ipv6_args );
694 }
695  
696 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
697 } ## end sub delegation01
698  
699 =over
700  
701 =item delegation02()
702  
703 my @logentry_array = delegation02( $zone );
704  
705 Runs the L<Delegation02 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation02.md>.
706  
707 Takes a L<Zonemaster::Engine::Zone> object.
708  
709 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
710  
711 =back
712  
713 =cut
714  
715 sub delegation02 {
716 my ( $class, $zone ) = @_;
717  
718 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation02';
719 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
720  
721 my @nss_del = @{ Zonemaster::Engine::TestMethods->method4( $zone ) };
722 my @nss_child = @{ Zonemaster::Engine::TestMethods->method5( $zone ) };
723  
724 push @results,
725 _find_dup_ns(
726 duplicate_tag => 'DEL_NS_SAME_IP',
727 distinct_tag => 'DEL_DISTINCT_NS_IP',
728 ns_list => [@nss_del],
729 );
730  
731 push @results,
732 _find_dup_ns(
733 duplicate_tag => 'CHILD_NS_SAME_IP',
734 distinct_tag => 'CHILD_DISTINCT_NS_IP',
735 ns_list => [@nss_child],
736 );
737  
738 push @results,
739 _find_dup_ns(
740 duplicate_tag => 'SAME_IP_ADDRESS',
741 distinct_tag => 'DISTINCT_IP_ADDRESS',
742 ns_list => [ @nss_del, @nss_child ],
743 );
744  
745 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
746 } ## end sub delegation02
747  
748 =over
749  
750 =item delegation03()
751  
752 my @logentry_array = delegation03( $zone );
753  
754 Runs the L<Delegation03 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation03.md>.
755  
756 Takes a L<Zonemaster::Engine::Zone> object.
757  
758 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
759  
760 =back
761  
762 =cut
763  
764 sub delegation03 {
765 my ( $class, $zone ) = @_;
766  
767 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation03';
768 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
769  
770 my $long_name = _max_length_name_for( $zone->name );
771 my @nsnames = map { $_->string } @{ Zonemaster::Engine::TestMethods->method2( $zone ) };
772 my @nss = @{ Zonemaster::Engine::TestMethods->method4( $zone ) };
773 my @nss_v4 = grep { $_->address->version == $IP_VERSION_4 } @nss;
774 my @nss_v6 = grep { $_->address->version == $IP_VERSION_6 } @nss;
775 my $parent = $zone->parent();
776  
777 my $p = Zonemaster::LDNS::Packet->new( $long_name, q{NS}, q{IN} );
778 for my $nsname ( @nsnames ) {
779 my $rr = Zonemaster::LDNS::RR->new( sprintf( q{%s IN NS %s}, $zone->name, $nsname ) );
780 $p->unique_push( q{authority}, $rr );
781 }
782  
783 # If @nss_v4 is non-empty and all of its elements are in bailiwick of parent
784 if ( @nss_v4 and not grep { not $parent->name->is_in_bailiwick( $_->name ) } @nss_v4 ) {
785 my $ns = $nss_v4[0];
786 my $rr = Zonemaster::LDNS::RR->new( sprintf( q{%s IN A %s}, $ns->name, $ns->address->short ) );
787 $p->unique_push( q{additional}, $rr );
788 }
789  
790 # If @nss_v6 is non-empty and all of its elements are in bailiwick of parent
791 if ( @nss_v6 and not grep { not $parent->name->is_in_bailiwick( $_->name ) } @nss_v6 ) {
792 my $ns = $nss_v6[0];
793 my $rr = Zonemaster::LDNS::RR->new( sprintf( q{%s IN AAAA %s}, $ns->name, $ns->address->short ) );
794 $p->unique_push( q{additional}, $rr );
795 }
796  
797 my $size = length( $p->data );
798 if ( $size > $UDP_PAYLOAD_LIMIT ) {
799 push @results,
800 _emit_log(
801 REFERRAL_SIZE_TOO_LARGE => {
802 size => $size,
803 }
804 );
805 }
806 else {
807 push @results,
808 _emit_log(
809 REFERRAL_SIZE_OK => {
810 size => $size,
811 }
812 );
813 }
814  
815 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
816 } ## end sub delegation03
817  
818 =over
819  
820 =item delegation04()
821  
822 my @logentry_array = delegation04( $zone );
823  
824 Runs the L<Delegation04 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation04.md>.
825  
826 Takes a L<Zonemaster::Engine::Zone> object.
827  
828 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
829  
830 =back
831  
832 =cut
833  
834 sub delegation04 {
835 my ( $class, $zone ) = @_;
836  
837 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation04';
838 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
839 my %nsnames;
840 my @authoritatives;
841 my $query_type = q{SOA};
842  
843 foreach
844 my $local_ns ( @{ Zonemaster::Engine::TestMethods->method4( $zone ) }, @{ Zonemaster::Engine::TestMethods->method5( $zone ) } )
845 {
846  
847 if ( _ip_disabled_message( \@results, $local_ns, $query_type ) ) {
848 next;
849 }
850  
851 next if $nsnames{ $local_ns->name->string };
852  
853 foreach my $usevc ( 0, 1 ) {
854 my $p = $local_ns->query( $zone->name, $query_type, { usevc => $usevc } );
855 if ( $p ) {
856 if ( not $p->aa ) {
857 push @results,
858 _emit_log(
859 IS_NOT_AUTHORITATIVE => {
860 ns => $local_ns->string,
861 proto => $usevc ? q{TCP} : q{UDP},
862 }
863 );
864 }
865 else {
866 push @authoritatives, $local_ns->name->string;
867 }
868 }
869 }
870  
871 $nsnames{ $local_ns->name }++;
872 } ## end foreach my $local_ns ( @{ Zonemaster::Engine::TestMethods...})
873  
874 if (
875 (
876 scalar @{ Zonemaster::Engine::TestMethods->method4( $zone ) }
877 or scalar @{ Zonemaster::Engine::TestMethods->method5( $zone ) }
878 )
879 and not grep { $_->tag ne q{TEST_CASE_START} } @results
880 and scalar @authoritatives
881 )
882 {
883 push @results,
884 _emit_log(
885 ARE_AUTHORITATIVE => {
886 nsname_list => join( q{;}, uniq sort @authoritatives ),
887 }
888 );
889 }
890  
891 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
892 } ## end sub delegation04
893  
894 =over
895  
896 =item delegation05()
897  
898 my @logentry_array = delegation05( $zone );
899  
900 Runs the L<Delegation05 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation05.md>.
901  
902 Takes a L<Zonemaster::Engine::Zone> object.
903  
904 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
905  
906 =back
907  
908 =cut
909  
910 sub delegation05 {
911 my ( $class, $zone ) = @_;
912  
913 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation05';
914 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
915  
916 my @nsnames = @{ Zonemaster::Engine::TestMethods->method2and3( $zone ) };
917  
918 foreach my $local_nsname ( @nsnames ) {
919  
920 if ( $zone->name->is_in_bailiwick( $local_nsname ) ) {
921 my @nss_del = @{ Zonemaster::Engine::TestMethods->method4( $zone ) };
922 my @nss_child = @{ Zonemaster::Engine::TestMethods->method5( $zone ) };
923 my %nss = map { $_->name->string . '/' . $_->address->short => $_ } @nss_del, @nss_child;
924  
925 for my $key ( sort keys %nss ) {
926 my $ns = $nss{$key};
927 my $ns_args = {
928 ns => $ns->string,
929 query_name => $local_nsname,
930 rrtype => q{A},
931 };
932  
933 if ( _ip_disabled_message( \@results, $ns, q{A} ) ) {
934 next;
935 }
936  
937 my $p = $ns->query( $local_nsname, q{A}, { recurse => 0 } );
938 if ( not $p ) {
939 push @results, _emit_log( NO_RESPONSE => $ns_args );
940 next;
941 }
942 elsif ($p->rcode ne q{NOERROR} ) {
943 $ns_args->{rcode} = $p->rcode;
944 push @results, _emit_log( UNEXPECTED_RCODE => $ns_args );
945 next;
946 }
947 elsif ( scalar $p->get_records( q{CNAME}, q{answer} ) > 0 ) {
948 push @results, _emit_log( NS_IS_CNAME => { nsname => $local_nsname } );
949 next;
950 }
951 elsif ($p->is_redirect) {
952 my $p = $ns->query( $local_nsname, q{A}, { recurse => 1 } );
953 if ( defined $p and scalar $p->get_records( q{CNAME}, q{answer} ) > 0 ) {
954 push @results, _emit_log( NS_IS_CNAME => { nsname => $local_nsname } );
955 }
956 }
957 }
958 }
959 else {
960 my $p = Zonemaster::Engine::Recursor->recurse( $local_nsname, q{A} );
961 if ( defined $p and scalar $p->get_records( q{CNAME}, q{answer} ) > 0 ) {
962 push @results, _emit_log( NS_IS_CNAME => { nsname => $local_nsname } );
963 }
964 }
965 }
966  
967 if ( not grep { $_->tag eq q{NS_IS_CNAME} } @results ) {
968 push @results, _emit_log( NO_NS_CNAME => {} );
969 }
970  
971 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
972 } ## end sub delegation05
973  
974 =over
975  
976 =item delegation06()
977  
978 my @logentry_array = delegation06( $zone );
979  
980 Runs the L<Delegation06 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation06.md>.
981  
982 Takes a L<Zonemaster::Engine::Zone> object.
983  
984 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
985  
986 =back
987  
988 =cut
989  
990 sub delegation06 {
991 my ( $class, $zone ) = @_;
992  
993 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation06';
994 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
995 my %nsnames;
996 my $query_type = q{SOA};
997  
998 foreach
999 my $local_ns ( @{ Zonemaster::Engine::TestMethods->method4( $zone ) }, @{ Zonemaster::Engine::TestMethods->method5( $zone ) } )
1000 {
1001  
1002 if ( _ip_disabled_message( \@results, $local_ns, $query_type ) ) {
1003 next;
1004 }
1005  
1006 next if $nsnames{ $local_ns->name->string };
1007  
1008 my $p = $local_ns->query( $zone->name, $query_type );
1009 if ( $p and $p->rcode eq q{NOERROR} ) {
1010 if ( not $p->get_records( $query_type, q{answer} ) ) {
1011 push @results, _emit_log( SOA_NOT_EXISTS => { ns => $local_ns->string } );
1012 }
1013 }
1014  
1015 $nsnames{ $local_ns->name->string }++;
1016 } ## end foreach my $local_ns ( @{ Zonemaster::Engine::TestMethods...})
1017  
1018 if (
1019 (
1020 scalar @{ Zonemaster::Engine::TestMethods->method4( $zone ) }
1021 or scalar @{ Zonemaster::Engine::TestMethods->method5( $zone ) }
1022 )
1023 and not grep { $_->tag ne q{TEST_CASE_START} } @results
1024 )
1025 {
1026 push @results, _emit_log( SOA_EXISTS => {} );
1027 }
1028  
1029 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
1030 } ## end sub delegation06
1031  
1032 =over
1033  
1034 =item delegation07()
1035  
1036 my @logentry_array = delegation07( $zone );
1037  
1038 Runs the L<Delegation07 Test Case|https://github.com/zonemaster/zonemaster/blob/master/docs/public/specifications/tests/Delegation-TP/delegation07.md>.
1039  
1040 Takes a L<Zonemaster::Engine::Zone> object.
1041  
1042 Returns a list of L<Zonemaster::Engine::Logger::Entry> objects.
1043  
1044 =back
1045  
1046 =cut
1047  
1048 sub delegation07 {
1049 my ( $class, $zone ) = @_;
1050  
1051 local $Zonemaster::Engine::Logger::TEST_CASE_NAME = 'Delegation07';
1052 push my @results, _emit_log( TEST_CASE_START => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } );
1053  
1054 my %names;
1055 foreach my $name ( @{ Zonemaster::Engine::TestMethods->method2( $zone ) } ) {
1056 $names{$name} += 1;
1057 }
1058 foreach my $name ( @{ Zonemaster::Engine::TestMethods->method3( $zone ) } ) {
1059 $names{$name} -= 1;
1060 }
1061  
1062 my @same_name = sort grep { $names{$_} == 0 } keys %names;
1063 my @extra_name_parent = sort grep { $names{$_} > 0 } keys %names;
1064 my @extra_name_child = sort grep { $names{$_} < 0 } keys %names;
1065  
1066 if ( @extra_name_parent ) {
1067 push @results,
1068 _emit_log(
1069 EXTRA_NAME_PARENT => {
1070 extra => join( q{;}, sort @extra_name_parent ),
1071 }
1072 );
1073 }
1074  
1075 if ( @extra_name_child ) {
1076 push @results,
1077 _emit_log(
1078 EXTRA_NAME_CHILD => {
1079 extra => join( q{;}, sort @extra_name_child ),
1080 }
1081 );
1082 }
1083  
1084 if ( @extra_name_parent == 0 and @extra_name_child == 0 ) {
1085 push @results,
1086 _emit_log(
1087 NAMES_MATCH => {
1088 names => join( q{;}, sort @same_name ),
1089 }
1090 );
1091 }
1092  
1093 if ( scalar( @same_name ) == 0 ) {
1094 push @results,
1095 _emit_log(
1096 TOTAL_NAME_MISMATCH => {
1097 glue => join( q{;}, sort @extra_name_parent ),
1098 child => join( q{;}, sort @extra_name_child ),
1099 }
1100 );
1101 }
1102  
1103 return ( @results, _emit_log( TEST_CASE_END => { testcase => $Zonemaster::Engine::Logger::TEST_CASE_NAME } ) );
1104 } ## end sub delegation07
1105  
1106 1;