Zonemaster/Test/Delegation.pm untested

filter: Statementstmt 0.0% 0 / 241 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 398 CC 40 SCAR 34.8
CC
40
Cov
0%
all
72.0
delegation04
56.0
delegation06
56.0
CRAP 32.3
1 package Zonemaster::Test::Delegation v1.0.1;
2  
3 use 5.14.2;
4 use strict;
5 use warnings;
6  
7 use Zonemaster;
8 use Zonemaster::Util;
9 use Zonemaster::Test::Address;
10 use Zonemaster::Test::Syntax;
11 use Zonemaster::TestMethods;
12 use Zonemaster::Constants ':all';
13  
14 use Net::IP::XS;
15 use List::MoreUtils qw[uniq];
16 use Net::LDNS::Packet;
17 use Net::LDNS::RR;
18  
19 ###
20 ### Entry points
21 ###
22  
23 sub all {
24 my ( $class, $zone ) = @_;
25 my @results;
26  
27 push @results, $class->delegation01( $zone ) if Zonemaster->config->should_run('delegation01');
28 push @results, $class->delegation02( $zone ) if Zonemaster->config->should_run('delegation02');
29 push @results, $class->delegation03( $zone ) if Zonemaster->config->should_run('delegation03');
30 push @results, $class->delegation04( $zone ) if Zonemaster->config->should_run('delegation04');
31 push @results, $class->delegation05( $zone ) if Zonemaster->config->should_run('delegation05');
32 push @results, $class->delegation06( $zone ) if Zonemaster->config->should_run('delegation06');
33 push @results, $class->delegation07( $zone ) if Zonemaster->config->should_run('delegation07');
34  
35 return @results;
36 }
37  
38 ###
39 ### Metadata Exposure
40 ###
41  
42 sub metadata {
43 my ( $class ) = @_;
44  
45 return {
46 delegation01 => [
47 qw(
48 ENOUGH_NS_GLUE
49 NOT_ENOUGH_NS_GLUE
50 ENOUGH_NS
51 NOT_ENOUGH_NS
52 ENOUGH_NS_TOTAL
53 NOT_ENOUGH_NS_TOTAL
54 )
55 ],
56 delegation02 => [
57 qw(
58 SAME_IP_ADDRESS
59 )
60 ],
61 delegation03 => [
62 qw(
63 REFERRAL_SIZE_LARGE
64 REFERRAL_SIZE_OK
65 )
66 ],
67 delegation04 => [
68 qw(
69 IS_NOT_AUTHORITATIVE
70 IPV4_DISABLED
71 IPV6_DISABLED
72 )
73 ],
74 delegation05 => [
75 qw(
76 NS_RR_IS_CNAME
77 )
78 ],
79 delegation06 => [
80 qw(
81 SOA_NOT_EXISTS
82 IPV4_DISABLED
83 IPV6_DISABLED
84 )
85 ],
86 delegation07 => [
87 qw(
88 EXTRA_NAME_PARENT
89 EXTRA_NAME_CHILD
90 TOTAL_NAME_MISMATCH
91 NAMES_MATCH
92 )
93 ],
94 };
95 } ## end sub metadata
96  
97 sub translation {
98 return {
99 "REFERRAL_SIZE_LARGE" =>
100 "The smallest possible legal referral packet is larger than 512 octets (it is {size}).",
101 "EXTRA_NAME_CHILD" => "Child has nameserver(s) not listed at parent ({extra}).",
102 "REFERRAL_SIZE_OK" => "The smallest possible legal referral packet is smaller than 513 octets (it is {size}).",
103 "IS_NOT_AUTHORITATIVE" => "Nameserver {ns} response is not authoritative on {proto} port 53.",
104 "ENOUGH_NS_GLUE" => "Parent lists enough nameservers ({glue}). Lower limit set to {minimum}.",
105 "NS_RR_IS_CNAME" => "Nameserver {ns} {address_type} RR point to CNAME.",
106 "SAME_IP_ADDRESS" => "IP {address} refers to multiple nameservers ({nss}).",
107 "DISTINCT_IP_ADDRESS" => "All the IP addresses used by the nameservers are unique",
108 "ENOUGH_NS" => "Child lists enough nameservers ({ns}). Lower limit set to {minimum}.",
109 "NAMES_MATCH" => "All of the nameserver names are listed both at parent and child.",
110 "TOTAL_NAME_MISMATCH" => "None of the nameservers listed at the parent are listed at the child.",
111 "SOA_NOT_EXISTS" => "A SOA query NOERROR response from {ns} was received empty.",
112 "EXTRA_NAME_PARENT" => "Parent has nameserver(s) not listed at the child ({extra}).",
113 "NOT_ENOUGH_NS_GLUE" => "Parent does not list enough nameservers ({glue}). Lower limit set to {minimum}.",
114 "NOT_ENOUGH_NS" => "Child does not list enough nameservers ({ns}). Lower limit set to {minimum}.",
115 "ARE_AUTHORITATIVE" => "All the nameservers are authoritative.",
116 "NS_RR_NO_CNAME" => "No nameserver point to CNAME alias.",
117 "SOA_EXISTS" => "All the nameservers have SOA record.",
118 "ENOUGH_NS_TOTAL" => "Parent and child list enough nameservers ({ns}). Lower limit set to {minimum}.",
119 "NOT_ENOUGH_NS_TOTAL" =>
120 "Parent and child do not list enough nameservers ({ns}). Lower limit set to {minimum}.",
121 'IPV4_DISABLED' => 'IPv4 is disabled, not sending "{type}" query to {ns}.',
122 'IPV6_DISABLED' => 'IPv6 is disabled, not sending "{type}" query to {ns}.',
123 };
124 } ## end sub translation
125  
126 sub version {
127 return "$Zonemaster::Test::Delegation::VERSION";
128 }
129  
130 ###
131 ### Tests
132 ###
133  
134 sub delegation01 {
135 my ( $class, $zone ) = @_;
136 my @results;
137  
138 my @parent_nsnames = map { $_->string } @{ Zonemaster::TestMethods->method2( $zone ) };
139  
140 if ( scalar( @parent_nsnames ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
141 push @results,
142 info(
143 ENOUGH_NS_GLUE => {
144 count => scalar( @parent_nsnames ),
145 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
146 glue => join( q{;}, @parent_nsnames ),
147 }
148 );
149 }
150 else {
151 push @results,
152 info(
153 NOT_ENOUGH_NS_GLUE => {
154 count => scalar( @parent_nsnames ),
155 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
156 glue => join( q{;}, @parent_nsnames ),
157 }
158 );
159 }
160  
161 my @child_nsnames = map { $_->string } @{ Zonemaster::TestMethods->method3( $zone ) };
162  
163 if ( scalar( @child_nsnames ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
164 push @results,
165 info(
166 ENOUGH_NS => {
167 count => scalar( @child_nsnames ),
168 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
169 ns => join( q{;}, @child_nsnames ),
170 }
171 );
172 }
173 else {
174 push @results,
175 info(
176 NOT_ENOUGH_NS => {
177 count => scalar( @child_nsnames ),
178 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
179 ns => join( q{;}, @child_nsnames ),
180 }
181 );
182 }
183  
184 my @all_nsnames = uniq map { $_->string } @{ Zonemaster::TestMethods->method2( $zone ) },
185 @{ Zonemaster::TestMethods->method3( $zone ) };
186  
187 if ( scalar( @all_nsnames ) >= $MINIMUM_NUMBER_OF_NAMESERVERS ) {
188 push @results,
189 info(
190 ENOUGH_NS_TOTAL => {
191 count => scalar( @all_nsnames ),
192 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
193 ns => join( q{;}, @all_nsnames ),
194 }
195 );
196 }
197 else {
198 push @results,
199 info(
200 NOT_ENOUGH_NS_TOTAL => {
201 count => scalar( @all_nsnames ),
202 minimum => $MINIMUM_NUMBER_OF_NAMESERVERS,
203 ns => join( q{;}, @all_nsnames ),
204 }
205 );
206 }
207  
208 return @results;
209 } ## end sub delegation01
210  
211 sub delegation02 {
212 my ( $class, $zone ) = @_;
213 my @results;
214 my %nsnames_and_ip;
215 my %ips;
216  
217 foreach
218 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
219 {
220  
221 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
222  
223 push @{ $ips{ $local_ns->address->short } }, $local_ns->name->string;
224  
225 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
226  
227 }
228  
229 foreach my $local_ip ( sort keys %ips ) {
230 if ( scalar @{ $ips{$local_ip} } > 1 ) {
231 push @results,
232 info(
233 SAME_IP_ADDRESS => {
234 nss => join( q{;}, @{ $ips{$local_ip} } ),
235 address => $local_ip,
236 }
237 );
238 }
239 }
240  
241 if ( scalar keys %ips and not scalar @results ) {
242 push @results,
243 info( DISTINCT_IP_ADDRESS => {} );
244 }
245  
246 return @results;
247 } ## end sub delegation02
248  
249 sub delegation03 {
250 my ( $class, $zone ) = @_;
251 my @results;
252 my %nsnames_and_ip;
253  
254 my @nsnames = uniq map { $_->string } @{ Zonemaster::TestMethods->method2( $zone ) },
255 @{ Zonemaster::TestMethods->method3( $zone ) };
256 my @needs_glue;
257  
258 foreach
259 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
260 {
261 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
262 if ( $zone->is_in_zone( $local_ns->name ) ) {
263 push @needs_glue, $local_ns;
264 }
265 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
266 }
267 @needs_glue = sort { length( $a->name->string ) <=> length( $b->name->string ) } @needs_glue;
268 my @needs_v4_glue = grep { $_->address->version == $IP_VERSION_4 } @needs_glue;
269 my @needs_v6_glue = grep { $_->address->version == $IP_VERSION_6 } @needs_glue;
270 my $long_name = _max_length_name_for( $zone->name );
271  
272 my $p = Net::LDNS::Packet->new( $long_name, q{NS}, q{IN} );
273  
274 foreach my $ns ( @nsnames ) {
275 my $rr = Net::LDNS::RR->new( sprintf( q{%s IN NS %s}, $zone->name, $ns ) );
276 $p->unique_push( q{authority}, $rr );
277 }
278  
279 if ( @needs_v4_glue ) {
280 my $ns = $needs_v4_glue[0];
281 my $rr = Net::LDNS::RR->new( sprintf( q{%s IN A %s}, $ns->name, $ns->address->short ) );
282 $p->unique_push( q{additional}, $rr );
283 }
284  
285 if ( @needs_v6_glue ) {
286 my $ns = $needs_v6_glue[0];
287 my $rr = Net::LDNS::RR->new( sprintf( q{%s IN AAAA %s}, $ns->name, $ns->address->short ) );
288 $p->unique_push( q{additional}, $rr );
289 }
290  
291 my $size = length( $p->data );
292 if ( $size > $UDP_PAYLOAD_LIMIT ) {
293 push @results,
294 info(
295 REFERRAL_SIZE_LARGE => {
296 size => $size,
297 }
298 );
299 }
300 else {
301 push @results,
302 info(
303 REFERRAL_SIZE_OK => {
304 size => $size,
305 }
306 );
307 }
308  
309 return @results;
310 } ## end sub delegation03
311  
312 sub delegation04 {
313 my ( $class, $zone ) = @_;
314 my @results;
315 my %nsnames;
316  
317 foreach
318 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
319 {
320  
321 if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 ) {
322 push @results,
323 info(
324 IPV6_DISABLED => {
325 ns => "$local_ns",
326 type => q{SOA},
327 }
328 );
329 next;
330 }
331  
332 if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 ) {
333 push @results,
334 info(
335 IPV4_DISABLED => {
336 ns => "$local_ns",
337 type => q{SOA},
338 }
339 );
340 next;
341 }
342  
343 next if $nsnames{ $local_ns->name };
344  
345 foreach my $usevc ( 0, 1 ) {
346 my $p = $local_ns->query( $zone->name, q{SOA}, { usevc => $usevc } );
347 if ( not $p or not $p->aa ) { # Consider non-responsive server non-auth
348 push @results,
349 info(
350 IS_NOT_AUTHORITATIVE => {
351 ns => $local_ns->name,
352 proto => $usevc ? q{TCP} : q{UDP},
353 }
354 );
355 }
356 }
357  
358 $nsnames{ $local_ns->name }++;
359 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
360  
361 if (
362 (
363 scalar @{ Zonemaster::TestMethods->method4( $zone ) }
364 or scalar @{ Zonemaster::TestMethods->method5( $zone ) }
365 )
366 and not scalar @results
367 )
368 {
369 push @results,
370 info( ARE_AUTHORITATIVE => {} );
371 }
372  
373 return @results;
374 } ## end sub delegation04
375  
376 sub delegation05 {
377 my ( $class, $zone ) = @_;
378 my @results;
379  
380 my @nsnames = uniq map { $_->string } @{ Zonemaster::TestMethods->method2( $zone ) },
381 @{ Zonemaster::TestMethods->method3( $zone ) };
382  
383 foreach my $local_nsname ( @nsnames ) {
384  
385 foreach my $address_type ( q{A}, q{AAAA} ) {
386 my $p = $zone->query_one( $local_nsname, $address_type );
387 if ( $p ) {
388 if ( $p->has_rrs_of_type_for_name( q{CNAME}, $zone->name ) ) {
389 push @results,
390 info(
391 NS_RR_IS_CNAME => {
392 ns => $local_nsname,
393 address_type => $address_type,
394 }
395 );
396 }
397 }
398 }
399  
400 }
401  
402 if (
403 (
404 scalar @{ Zonemaster::TestMethods->method2( $zone ) }
405 or scalar @{ Zonemaster::TestMethods->method3( $zone ) }
406 )
407 and not scalar @results
408 )
409 {
410 push @results,
411 info( NS_RR_NO_CNAME => {} );
412 }
413  
414 return @results;
415 } ## end sub delegation05
416  
417 sub delegation06 {
418 my ( $class, $zone ) = @_;
419 my @results;
420 my %nsnames;
421  
422 foreach
423 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
424 {
425  
426 if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 ) {
427 push @results,
428 info(
429 IPV6_DISABLED => {
430 ns => "$local_ns",
431 type => q{SOA},
432 }
433 );
434 next;
435 }
436  
437 if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 ) {
438 push @results,
439 info(
440 IPV4_DISABLED => {
441 ns => "$local_ns",
442 type => q{SOA},
443 }
444 );
445 next;
446 }
447  
448 next if $nsnames{ $local_ns->name };
449  
450 my $p = $local_ns->query( $zone->name, q{SOA} );
451 if ( $p and $p->rcode eq q{NOERROR} ) {
452 if ( not $p->get_records( q{SOA}, q{answer} ) ) {
453 push @results,
454 info(
455 SOA_NOT_EXISTS => {
456 ns => $local_ns->name,
457 }
458 );
459 }
460 }
461  
462 $nsnames{ $local_ns->name }++;
463 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
464  
465 if (
466 (
467 scalar @{ Zonemaster::TestMethods->method4( $zone ) }
468 or scalar @{ Zonemaster::TestMethods->method5( $zone ) }
469 )
470 and not scalar @results
471 )
472 {
473 push @results,
474 info( SOA_EXISTS => {} );
475 }
476  
477 return @results;
478 } ## end sub delegation06
479  
480 sub delegation07 {
481 my ( $class, $zone ) = @_;
482 my @results;
483  
484 my %names;
485 foreach my $name ( @{ Zonemaster::TestMethods->method2( $zone ) } ) {
486 $names{$name} += 1;
487 }
488 foreach my $name ( @{ Zonemaster::TestMethods->method3( $zone ) } ) {
489 $names{$name} -= 1;
490 }
491  
492 my @same_name = sort grep { $names{$_} == 0 } keys %names;
493 my @extra_name_parent = sort grep { $names{$_} > 0 } keys %names;
494 my @extra_name_child = sort grep { $names{$_} < 0 } keys %names;
495  
496 if ( @extra_name_parent ) {
497 push @results,
498 info(
499 EXTRA_NAME_PARENT => {
500 extra => join( q{;}, @extra_name_parent ),
501 }
502 );
503 }
504  
505 if ( @extra_name_child ) {
506 push @results,
507 info(
508 EXTRA_NAME_CHILD => {
509 extra => join( q{;}, @extra_name_child ),
510 }
511 );
512 }
513  
514 if ( @extra_name_parent == 0 and @extra_name_child == 0 ) {
515 push @results,
516 info(
517 NAMES_MATCH => {
518 names => join( q{;}, @same_name ),
519 }
520 );
521 }
522  
523 if ( scalar( @same_name ) == 0 ) {
524 push @results,
525 info(
526 TOTAL_NAME_MISMATCH => {
527 glue => join( q{;}, @extra_name_parent ),
528 child => join( q{;}, @extra_name_child ),
529 }
530 );
531 }
532  
533 return @results;
534 } ## end sub delegation07
535  
536 ###
537 ### Helper functions
538 ###
539  
540 # Make up a name of maximum length in the given domain
541 sub _max_length_name_for {
542 my ( $top ) = @_;
543 my @chars = q{A} .. q{Z};
544  
545 my $name = name( $top )->fqdn;
546 $name = '' if $name eq '.'; # Special case for root zone
547  
548 while ( length( $name ) < $FQDN_MAX_LENGTH - 1 ) {
549 my $len = $FQDN_MAX_LENGTH - length( $name ) - 1;
550 $len = $LABEL_MAX_LENGTH if $len > $LABEL_MAX_LENGTH;
551 $name = join( q{}, map { $chars[ rand @chars ] } 1 .. $len ) . q{.} . $name;
552 }
553  
554 return $name;
555 }
556  
557 1;
558  
559 =head1 NAME
560  
561 Zonemaster::Test::Delegation - Tests regarding delegation details
562  
563 =head1 SYNOPSIS
564  
565 my @results = Zonemaster::Test::Delegation->all($zone);
566  
567 =head1 METHODS
568  
569 =over
570  
571 =item all($zone)
572  
573 Runs the default set of tests and returns a list of log entries made by the tests.
574  
575 =item translation()
576  
577 Returns a refernce to a hash with translation data. Used by the builtin translation system.
578  
579 =item metadata()
580  
581 Returns a reference to a hash, the keys of which are the names of all test methods in the module, and the corresponding values are references to
582 lists with all the tags that the method can use in log entries.
583  
584 =item version()
585  
586 Returns a version string for the module.
587  
588 =back
589  
590 =head1 TESTS
591  
592 =over
593  
594 =item delegation01($zone)
595  
596 Verify that there is more than two nameserver.
597  
598 =item delegation02($zone)
599  
600 Verify that name servers have distinct IP addresses.
601  
602 =item delegation03($zone)
603  
604 Verify that there is no truncation on referrals.
605  
606 =item delegation04($zone)
607  
608 Verify that nameservers are authoritative.
609  
610 =item delegation05($zone)
611  
612 Verify that NS RRs do not point to CNAME alias.
613  
614 =item delegation06($zone)
615  
616 Verify existence of SOA.
617  
618 =item delegation07($zone)
619  
620 Verify that parent glue name records are present in child.
621 =back
622  
623 =cut