Zonemaster/Test/Nameserver.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 66 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 335 CC 39 SCAR 38.1
CC
39
Cov
0%
nameserver02
72.0
nameserver05
72.0
all
56.0
CRAP 45.3
1 package Zonemaster::Test::Nameserver 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::Constants qw[:ip];
11  
12 use List::MoreUtils qw[uniq none];
13  
14 ###
15 ### Entry Points
16 ###
17  
18 sub all {
19 my ( $class, $zone ) = @_;
20 my @results;
21  
22 push @results, $class->nameserver01( $zone ) if Zonemaster->config->should_run('nameserver01');
23 push @results, $class->nameserver02( $zone ) if Zonemaster->config->should_run('nameserver02');
24 push @results, $class->nameserver03( $zone ) if Zonemaster->config->should_run('nameserver03');
25 push @results, $class->nameserver04( $zone ) if Zonemaster->config->should_run('nameserver04');
26 push @results, $class->nameserver05( $zone ) if Zonemaster->config->should_run('nameserver05');
27 push @results, $class->nameserver06( $zone ) if Zonemaster->config->should_run('nameserver06');
28  
29 return @results;
30 }
31  
32 ###
33 ### Metadata Exposure
34 ###
35  
36 sub metadata {
37 my ( $class ) = @_;
38  
39 return {
40 nameserver01 => [
41 qw(
42 IS_A_RECURSOR
43 NO_RECURSOR
44 )
45 ],
46 nameserver02 => [
47 qw(
48 EDNS0_BAD_QUERY
49 EDNS0_BAD_ANSWER
50 EDNS0_SUPPORT
51 )
52 ],
53 nameserver03 => [
54 qw(
55 AXFR_FAILURE
56 AXFR_AVAILABLE
57 )
58 ],
59 nameserver04 => [
60 qw(
61 DIFFERENT_SOURCE_IP
62 SAME_SOURCE_IP
63 )
64 ],
65 nameserver05 => [
66 qw(
67 QUERY_DROPPED
68 ANSWER_BAD_RCODE
69 IPV4_DISABLED
70 IPV6_DISABLED
71 )
72 ],
73 nameserver06 => [
74 qw(
75 CAN_NOT_BE_RESOLVED
76 CAN_BE_RESOLVED
77 NO_RESOLUTION
78 )
79 ],
80 };
81 } ## end sub metadata
82  
83 sub translation {
84 return {
85 'AAAA_WELL_PROCESSED' => 'The following nameservers answer AAAA queries without problems : {names}.',
86 'EDNS0_BAD_QUERY' => 'Nameserver {ns}/{address} does not support EDNS0 (replies with FORMERR).',
87 'DIFFERENT_SOURCE_IP' =>
88 'Nameserver {ns}/{address} replies on a SOA query with a different source address ({source}).',
89 'SAME_SOURCE_IP' => 'All nameservers reply with same IP used to query them.',
90 'AXFR_AVAILABLE' => 'Nameserver {ns}/{address} allow zone transfer using AXFR.',
91 'AXFR_FAILURE' => 'AXFR not available on nameserver {ns}/{address}.',
92 'QUERY_DROPPED' => 'Nameserver {ns}/{address} dropped AAAA query.',
93 'IS_A_RECURSOR' => 'Nameserver {ns}/{address} answered with a RCODE NXDOMAIN to SOA query on {dname}.',
94 'NO_RECURSOR' => 'None of the following nameservers is a recursor : {names}.',
95 'ANSWER_BAD_RCODE' => 'Nameserver {ns}/{address} answered AAAA query with an unexpected rcode ({rcode}).',
96 'EDNS0_BAD_ANSWER' => 'Nameserver {ns}/{address} does not support EDNS0 (OPT not set in reply).',
97 'EDNS0_SUPPORT' => 'The following nameservers support EDNS0 : {names}.',
98 'CAN_NOT_BE_RESOLVED' => 'The following nameservers failed to resolve to an IP address : {names}.',
99 'CAN_BE_RESOLVED' => 'All nameservers succeeded to resolve to an IP address.',
100 'NO_RESOLUTION' => 'No nameservers succeeded to resolve to an IP address.',
101 'IPV4_DISABLED' => 'IPv4 is disabled, not sending "{type}" query to {ns}.',
102 'IPV6_DISABLED' => 'IPv6 is disabled, not sending "{type}" query to {ns}.',
103 };
104 } ## end sub translation
105  
106 sub version {
107 return "$Zonemaster::Test::Nameserver::VERSION";
108 }
109  
110 sub nameserver01 {
111 my ( $class, $zone ) = @_;
112 my $nonexistent_name = q{xx--domain-cannot-exist.xx--illegal-syntax-tld};
113 my @results;
114 my %ips;
115 my %nsnames;
116  
117 foreach
118 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
119 {
120  
121 next if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 );
122  
123 next if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 );
124  
125 next if $ips{ $local_ns->address->short };
126  
127 my $p = $local_ns->query( $nonexistent_name, q{SOA}, { recurse => 1 } );
128  
129 if ( $p ) {
130 if ( $p->rcode eq q{NXDOMAIN} ) {
131 push @results,
132 info(
133 IS_A_RECURSOR => {
134 ns => $local_ns->name,
135 address => $local_ns->address->short,
136 dname => $nonexistent_name,
137 }
138 );
139 }
140 $nsnames{ $local_ns->name }++;
141 $ips{ $local_ns->address->short }++;
142 }
143  
144 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
145  
146 if ( scalar keys %nsnames and not scalar @results ) {
147 push @results,
148 info(
149 NO_RECURSOR => {
150 names => join( q{,}, sort keys %nsnames ),
151 }
152 );
153 }
154  
155 return @results;
156 } ## end sub nameserver01
157  
158 sub nameserver02 {
159 my ( $class, $zone ) = @_;
160 my @results;
161 my %nsnames_and_ip;
162  
163 foreach
164 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
165 {
166  
167 next if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 );
168  
169 next if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 );
170  
171 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
172  
173 my $p = $local_ns->query( $zone->name, q{SOA}, { edns_size => 512 } );
174 if ( $p ) {
175 if ( $p->rcode eq q{FORMERR} ) {
176 push @results,
177 info(
178 EDNS0_BAD_QUERY => {
179 ns => $local_ns->name,
180 address => $local_ns->address->short,
181 }
182 );
183 }
184 else {
185 if ( not $p->has_edns ) {
186 push @results,
187 info(
188 EDNS0_BAD_ANSWER => {
189 ns => $local_ns->name,
190 address => $local_ns->address->short,
191 }
192 );
193 }
194 }
195 } ## end if ( $p )
196  
197 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
198 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
199  
200 if ( scalar keys %nsnames_and_ip and not scalar @results ) {
201 push @results,
202 info(
203 EDNS0_SUPPORT => {
204 names => join( q{,}, keys %nsnames_and_ip ),
205 }
206 );
207 }
208  
209 return @results;
210 } ## end sub nameserver02
211  
212 sub nameserver03 {
213 my ( $class, $zone ) = @_;
214 my @results;
215 my %nsnames_and_ip;
216  
217 foreach
218 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
219 {
220  
221 next if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 );
222  
223 next if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 );
224  
225 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
226  
227 my $first_rr;
228 eval {
229 $local_ns->axfr( $zone->name, sub { ( $first_rr ) = @_; return 0; } );
230 1;
231 } or do {
232 push @results,
233 info(
234 AXFR_FAILURE => {
235 ns => $local_ns->name->string,
236 address => $local_ns->address->short,
237 }
238 );
239 };
240  
241 if ( $first_rr and $first_rr->type eq q{SOA} ) {
242 push @results,
243 info(
244 AXFR_AVAILABLE => {
245 ns => $local_ns->name->string,
246 address => $local_ns->address->short,
247 }
248 );
249 }
250  
251 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
252 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
253  
254 return @results;
255 } ## end sub nameserver03
256  
257 sub nameserver04 {
258 my ( $class, $zone ) = @_;
259 my @results;
260 my %nsnames_and_ip;
261  
262 foreach
263 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
264 {
265  
266 next if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 );
267  
268 next if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 );
269  
270 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
271  
272 my $p = $local_ns->query( $zone->name, q{SOA} );
273 if ( $p ) {
274 if ( $p->answerfrom and ( $local_ns->address->short ne Net::IP::XS->new( $p->answerfrom )->short ) ) {
275 push @results,
276 info(
277 DIFFERENT_SOURCE_IP => {
278 ns => $local_ns->name->string,
279 address => $local_ns->address->short,
280 source => $p->answerfrom,
281 }
282 );
283 }
284 }
285 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
286 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
287  
288 if ( scalar keys %nsnames_and_ip and not scalar @results ) {
289 push @results,
290 info(
291 SAME_SOURCE_IP => {
292 names => join( q{,}, keys %nsnames_and_ip ),
293 }
294 );
295 }
296  
297 return @results;
298 } ## end sub nameserver04
299  
300 sub nameserver05 {
301 my ( $class, $zone ) = @_;
302 my @results;
303 my %nsnames_and_ip;
304  
305 foreach
306 my $local_ns ( @{ Zonemaster::TestMethods->method4( $zone ) }, @{ Zonemaster::TestMethods->method5( $zone ) } )
307 {
308  
309 next if $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short };
310  
311 if ( not Zonemaster->config->ipv6_ok and $local_ns->address->version == $IP_VERSION_6 ) {
312 push @results,
313 info(
314 IPV6_DISABLED => {
315 ns => "$local_ns",
316 type => q{AAAA},
317 }
318 );
319 next;
320 }
321  
322 if ( not Zonemaster->config->ipv4_ok and $local_ns->address->version == $IP_VERSION_4 ) {
323 push @results,
324 info(
325 IPV4_DISABLED => {
326 ns => "$local_ns",
327 type => q{AAAA},
328 }
329 );
330 next;
331 }
332  
333 $nsnames_and_ip{ $local_ns->name->string . q{/} . $local_ns->address->short }++;
334  
335 my $p = $local_ns->query( $zone->name, q{AAAA} );
336  
337 if ( not $p ) {
338 push @results,
339 info(
340 QUERY_DROPPED => {
341 ns => $local_ns->name->string,
342 address => $local_ns->address->short,
343 }
344 );
345 next;
346 }
347  
348 next if not scalar $p->answer and $p->rcode eq q{NOERROR};
349  
350 if ( $p->rcode eq q{FORMERR}
351 or $p->rcode eq q{SERVFAIL}
352 or $p->rcode eq q{NXDOMAIN}
353 or $p->rcode eq q{NOTIMPL} )
354 {
355 push @results,
356 info(
357 ANSWER_BAD_RCODE => {
358 ns => $local_ns->name->string,
359 address => $local_ns->address->short,
360 rcode => $p->rcode,
361 }
362 );
363 next;
364 }
365  
366 } ## end foreach my $local_ns ( @{ Zonemaster::TestMethods...})
367  
368 if ( scalar keys %nsnames_and_ip and none { $_->tag eq q{ANSWER_BAD_RCODE} } @results ) {
369 push @results,
370 info(
371 AAAA_WELL_PROCESSED => {
372 names => join( q{,}, keys %nsnames_and_ip ),
373 }
374 );
375 }
376  
377 return @results;
378 } ## end sub nameserver05
379  
380 sub nameserver06 {
381 my ( $class, $zone ) = @_;
382 my @results;
383 my @all_nsnames = uniq map { lc( $_->string ) } @{ Zonemaster::TestMethods->method2( $zone ) },
384 @{ Zonemaster::TestMethods->method3( $zone ) };
385 my @all_nsnames_with_ip = uniq map { lc( $_->name->string ) } @{ Zonemaster::TestMethods->method4( $zone ) },
386 @{ Zonemaster::TestMethods->method5( $zone ) };
387 my @all_nsnames_without_ip;
388 my %diff;
389  
390 @diff{@all_nsnames} = undef;
391 delete @diff{@all_nsnames_with_ip};
392  
393 @all_nsnames_without_ip = keys %diff;
394 if ( scalar @all_nsnames_without_ip and scalar @all_nsnames_with_ip ) {
395 push @results,
396 info(
397 CAN_NOT_BE_RESOLVED => {
398 names => join( q{,}, @all_nsnames_without_ip ),
399 }
400 );
401 }
402 elsif ( not scalar @all_nsnames_with_ip ) {
403 push @results,
404 info(
405 NO_RESOLUTION => {
406 names => join( q{,}, @all_nsnames_without_ip ),
407 }
408 );
409 }
410 else {
411 push @results, info( CAN_BE_RESOLVED => {} );
412 }
413  
414 return @results;
415 } ## end sub nameserver06
416  
417 1;
418  
419 =head1 NAME
420  
421 Zonemaster::Test::Nameserver - module implementing tests of the properties of a name server
422  
423 =head1 SYNOPSIS
424  
425 my @results = Zonemaster::Test::Nameserver->all($zone);
426  
427 =head1 METHODS
428  
429 =over
430  
431 =item all($zone)
432  
433 Runs the default set of tests and returns a list of log entries made by the tests
434  
435 =item translation()
436  
437 Returns a refernce to a hash with translation data. Used by the builtin translation system.
438  
439 =item metadata()
440  
441 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
442 lists with all the tags that the method can use in log entries.
443  
444 =item version()
445  
446 Returns a version string for the module.
447  
448 =back
449  
450 =head1 TESTS
451  
452 =over
453  
454 =item nameserver01($zone)
455  
456 Verify that nameserver is not recursive.
457  
458 =item nameserver02($zone)
459  
460 Verify EDNS0 support.
461  
462 =item nameserver03($zone)
463  
464 Verify that zone transfer (AXFR) is not available.
465  
466 =item nameserver04($zone)
467  
468 Verify that replies from nameserver comes from the expected IP address.
469  
470 =item nameserver05($zone)
471  
472 Verify behaviour against AAAA queries.
473  
474 =item nameserver06($zone)
475  
476 Verify that each nameserver can be resolved to an IP address.
477  
478 =back
479  
480 =cut