| 1 | package Zonemaster::Test::Basic 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::TestMethods; | ||
| 10 | use Zonemaster::Test::Address; | ||
| 11 | use Zonemaster::Test::Syntax; | ||
| 12 | use Zonemaster::Constants qw[:ip :name]; | ||
| 13 | use List::MoreUtils qw[any none]; | ||
| 14 | |||
| 15 | use Carp; | ||
| 16 | |||
| 17 | ### | ||
| 18 | ### Entry Points | ||
| 19 | ### | ||
| 20 | |||
| 21 | sub all { | ||
| 22 | my ( $class, $zone ) = @_; | ||
| 23 | my @results; | ||
| 24 | |||
| 25 | push @results, $class->basic00( $zone ); | ||
| 26 | |||
| 27 | if ( | ||
| 28 | none { | ||
| 29 | $_->tag eq q{DOMAIN_NAME_LABEL_TOO_LONG} | ||
| 30 | or $_->tag eq q{DOMAIN_NAME_ZERO_LENGTH_LABEL} | ||
| 31 | or $_->tag eq q{DOMAIN_NAME_TOO_LONG} | ||
| 32 | } | ||
| 33 | @results | ||
| 34 | ) | ||
| 35 | { | ||
| 36 | push @results, $class->basic01( $zone ); | ||
| 37 | |||
| 38 | # Perform BASIC2 if BASIC1 passed | ||
| 39 | if ( any { $_->tag eq q{HAS_GLUE} } @results ) { | ||
| 40 | push @results, $class->basic02( $zone ); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | push @results, | ||
| 44 | info( NO_GLUE_PREVENTS_NAMESERVER_TESTS => {} ); | ||
| 45 | } | ||
| 46 | |||
| 47 | # Perform BASIC3 if BASIC2 failed | ||
| 48 | if ( none { $_->tag eq q{HAS_NAMESERVERS} } @results ) { | ||
| 49 | push @results, $class->basic03( $zone ) if Zonemaster->config->should_run('basic03'); | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | push @results, | ||
| 53 | info( | ||
| 54 | HAS_NAMESERVER_NO_WWW_A_TEST => { | ||
| 55 | name => $zone->name, | ||
| 56 | } | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | } ## end if ( none { $_->tag eq...}) | ||
| 60 | |||
| 61 | return @results; | ||
| 62 | } ## end sub all | ||
| 63 | |||
| 64 | sub can_continue { | ||
| 65 | my ( $class, @results ) = @_; | ||
| 66 | my %tag = map { $_->tag => 1 } @results; | ||
| 67 | |||
| 68 | if ( $tag{HAS_GLUE} and $tag{HAS_NAMESERVERS} ) { | ||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | ### | ||
| 77 | ### Metadata Exposure | ||
| 78 | ### | ||
| 79 | |||
| 80 | sub metadata { | ||
| 81 | my ( $class ) = @_; | ||
| 82 | |||
| 83 | return { | ||
| 84 | basic00 => [ | ||
| 85 | qw( | ||
| 86 | DOMAIN_NAME_LABEL_TOO_LONG | ||
| 87 | DOMAIN_NAME_ZERO_LENGTH_LABEL | ||
| 88 | DOMAIN_NAME_TOO_LONG | ||
| 89 | ) | ||
| 90 | ], | ||
| 91 | basic01 => [ | ||
| 92 | qw( | ||
| 93 | NO_PARENT | ||
| 94 | HAS_GLUE | ||
| 95 | NO_GLUE | ||
| 96 | NO_DOMAIN | ||
| 97 | PARENT_REPLIES | ||
| 98 | NO_PARENT_RESPONSE | ||
| 99 | NOT_A_DOMAIN | ||
| 100 | ) | ||
| 101 | ], | ||
| 102 | basic02 => [ | ||
| 103 | qw( | ||
| 104 | NO_GLUE_PREVENTS_NAMESERVER_TESTS | ||
| 105 | NS_FAILED | ||
| 106 | NS_NO_RESPONSE | ||
| 107 | HAS_NAMESERVERS | ||
| 108 | IPV4_DISABLED | ||
| 109 | IPV6_DISABLED | ||
| 110 | IPV4_ENABLED | ||
| 111 | IPV6_ENABLED | ||
| 112 | ) | ||
| 113 | ], | ||
| 114 | basic03 => [ | ||
| 115 | qw( | ||
| 116 | NO_NAMESERVER_PREVENTS_WWW_A_TEST | ||
| 117 | HAS_A_RECORDS | ||
| 118 | IPV4_DISABLED | ||
| 119 | IPV6_DISABLED | ||
| 120 | IPV4_ENABLED | ||
| 121 | IPV6_ENABLED | ||
| 122 | A_QUERY_NO_RESPONSES | ||
| 123 | ) | ||
| 124 | ], | ||
| 125 | }; | ||
| 126 | } ## end sub metadata | ||
| 127 | |||
| 128 | sub translation { | ||
| 129 | return { | ||
| 130 | "DOMAIN_NAME_LABEL_TOO_LONG" => "Domain name ({name}) has a label ({label}) too long ({length}/{max}).", | ||
| 131 | "DOMAIN_NAME_ZERO_LENGTH_LABEL" => "Domain name ({name}) has a zero length label.", | ||
| 132 | "DOMAIN_NAME_TOO_LONG" => "Domain name is too long ({length}/{max}).", | ||
| 133 | 'NO_PARENT' => 'No parent domain could be found for the tested domain.', | ||
| 134 | 'NO_GLUE' => 'Nameservers for "{parent}" provided no NS records for tested zone. RCODE given was {rcode}.', | ||
| 135 | 'HAS_A_RECORDS' => 'Nameserver {source} returned A record(s) for {name}.', | ||
| 136 | 'NO_A_RECORDS' => 'Nameserver {source} did not return A record(s) for {name}.', | ||
| 137 | 'NO_DOMAIN' => 'Nameserver for zone {parent} responded with NXDOMAIN to query for glue.', | ||
| 138 | 'HAS_NAMESERVERS' => 'Nameserver {source} listed these servers as glue: {ns}.', | ||
| 139 | 'PARENT_REPLIES' => 'Nameserver for zone {parent} replies when trying to fetch glue.', | ||
| 140 | 'NO_PARENT_RESPONSE' => 'No response from nameserver for zone {parent} when trying to fetch glue.', | ||
| 141 | 'NO_GLUE_PREVENTS_NAMESERVER_TESTS' => 'No NS records for tested zone from parent. NS tests aborted.', | ||
| 142 | 'NS_FAILED' => 'Nameserver {source} did not return NS records. RCODE was {rcode}.', | ||
| 143 | 'NS_NO_RESPONSE' => 'Nameserver {source} did not respond to NS query.', | ||
| 144 | 'A_QUERY_NO_RESPONSES' => 'Nameservers did not respond to A query.', | ||
| 145 | 'HAS_NAMESERVER_NO_WWW_A_TEST' => 'Functional nameserver found. "A" query for www.{name} test aborted.', | ||
| 146 | 'HAS_GLUE' => 'Nameserver for zone {parent} listed these nameservers as glue: {ns}.', | ||
| 147 | 'IPV4_DISABLED' => 'IPv4 is disabled, not sending "{type}" query to {ns}.', | ||
| 148 | 'IPV4_ENABLED' => 'IPv4 is enabled, can send "{type}" query to {ns}.', | ||
| 149 | 'IPV6_DISABLED' => 'IPv6 is disabled, not sending "{type}" query to {ns}.', | ||
| 150 | 'IPV6_ENABLED' => 'IPv6 is enabled, can send "{type}" query to {ns}.', | ||
| 151 | 'NOT_A_DOMAIN' => '{name} is not a domain.', | ||
| 152 | }; | ||
| 153 | } ## end sub translation | ||
| 154 | |||
| 155 | sub version { | ||
| 156 | return "$Zonemaster::Test::Basic::VERSION"; | ||
| 157 | } | ||
| 158 | |||
| 159 | ### | ||
| 160 | ### Tests | ||
| 161 | ### | ||
| 162 | |||
| 163 | sub basic00 { | ||
| 164 | my ( $class, $zone ) = @_; | ||
| 165 | my $name = name( $zone ); | ||
| 166 | my @results; | ||
| 167 | |||
| 168 | foreach my $local_label ( @{ $name->labels } ) { | ||
| 169 | if ( length $local_label > $LABEL_MAX_LENGTH ) { | ||
| 170 | push @results, | ||
| 171 | info( | ||
| 172 | q{DOMAIN_NAME_LABEL_TOO_LONG} => { | ||
| 173 | name => "$name", | ||
| 174 | label => $local_label, | ||
| 175 | length => length( $local_label ), | ||
| 176 | max => $LABEL_MAX_LENGTH, | ||
| 177 | } | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | elsif ( length $local_label == 0 ) { | ||
| 181 | push @results, | ||
| 182 | info( | ||
| 183 | q{DOMAIN_NAME_ZERO_LENGTH_LABEL} => { | ||
| 184 | name => "$name", | ||
| 185 | } | ||
| 186 | ); | ||
| 187 | } | ||
| 188 | } ## end foreach my $local_label ( @...) | ||
| 189 | |||
| 190 | my $fqdn = $name->fqdn; | ||
| 191 | if ( length( $fqdn ) > $FQDN_MAX_LENGTH ) { | ||
| 192 | push @results, | ||
| 193 | info( | ||
| 194 | q{DOMAIN_NAME_TOO_LONG} => { | ||
| 195 | name => $fqdn, | ||
| 196 | length => length( $fqdn ), | ||
| 197 | max => $FQDN_MAX_LENGTH, | ||
| 198 | } | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | |||
| 202 | return @results; | ||
| 203 | |||
| 204 | } ## end sub basic00 | ||
| 205 | |||
| 206 | sub basic01 { | ||
| 207 | my ( $class, $zone ) = @_; | ||
| 208 | my @results; | ||
| 209 | |||
| 210 | my $parent = $zone->parent; | ||
| 211 | |||
| 212 | if ( not $parent ) { | ||
| 213 | return info( NO_PARENT => { zone => $zone->name->string } ); | ||
| 214 | } | ||
| 215 | |||
| 216 | my $p = $parent->query_one( $zone->name, q{NS} ); | ||
| 217 | |||
| 218 | if ( $p ) { | ||
| 219 | push @results, | ||
| 220 | info( | ||
| 221 | PARENT_REPLIES => { | ||
| 222 | parent => $parent->name->string, | ||
| 223 | } | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | push @results, | ||
| 228 | info( | ||
| 229 | NO_PARENT_RESPONSE => { | ||
| 230 | parent => $parent->name->string, | ||
| 231 | } | ||
| 232 | ); | ||
| 233 | return @results; | ||
| 234 | } | ||
| 235 | |||
| 236 | if ( $p->rcode eq q{NXDOMAIN} ) { | ||
| 237 | push @results, | ||
| 238 | info( | ||
| 239 | NO_DOMAIN => { | ||
| 240 | parent => $parent->name->string, | ||
| 241 | } | ||
| 242 | ); | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | if ( $p->has_rrs_of_type_for_name( q{NS}, $zone->name ) ) { | ||
| 246 | push @results, | ||
| 247 | info( | ||
| 248 | HAS_GLUE => { | ||
| 249 | parent => $parent->name->string, | ||
| 250 | ns => join( q{,}, sort map { $_->nsdname } $p->get_records_for_name( q{NS}, $zone->name ) ), | ||
| 251 | } | ||
| 252 | ); | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | push @results, | ||
| 256 | info( | ||
| 257 | NO_GLUE => { | ||
| 258 | parent => $parent->name->string, | ||
| 259 | rcode => $p->rcode, | ||
| 260 | } | ||
| 261 | ); | ||
| 262 | if ($p->aa and (grep {$_->type eq 'SOA'} $p->authority)) { | ||
| 263 | push @results, info( NOT_A_DOMAIN => { name => $zone->name->string } ); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } ## end else [ if ( $p->rcode eq q{NXDOMAIN})] | ||
| 267 | |||
| 268 | return @results; | ||
| 269 | } ## end sub basic01 | ||
| 270 | |||
| 271 | sub basic02 { | ||
| 272 | my ( $class, $zone ) = @_; | ||
| 273 | my @results; | ||
| 274 | |||
| 275 | foreach my $ns ( @{ Zonemaster::TestMethods->method4( $zone ) } ) { | ||
| 276 | if ( not Zonemaster->config->ipv4_ok and $ns->address->version == $IP_VERSION_4 ) { | ||
| 277 | push @results, | ||
| 278 | info( | ||
| 279 | IPV4_DISABLED => { | ||
| 280 | ns => "$ns", | ||
| 281 | type => q{NS}, | ||
| 282 | } | ||
| 283 | ); | ||
| 284 | next; | ||
| 285 | } | ||
| 286 | elsif ( Zonemaster->config->ipv4_ok and $ns->address->version == $IP_VERSION_4 ) { | ||
| 287 | push @results, | ||
| 288 | info( | ||
| 289 | IPV4_ENABLED => { | ||
| 290 | ns => "$ns", | ||
| 291 | type => q{NS}, | ||
| 292 | } | ||
| 293 | ); | ||
| 294 | } | ||
| 295 | |||
| 296 | if ( not Zonemaster->config->ipv6_ok and $ns->address->version == $IP_VERSION_6 ) { | ||
| 297 | push @results, | ||
| 298 | info( | ||
| 299 | IPV6_DISABLED => { | ||
| 300 | ns => "$ns", | ||
| 301 | type => q{NS}, | ||
| 302 | } | ||
| 303 | ); | ||
| 304 | next; | ||
| 305 | } | ||
| 306 | elsif ( Zonemaster->config->ipv6_ok and $ns->address->version == $IP_VERSION_6 ) { | ||
| 307 | push @results, | ||
| 308 | info( | ||
| 309 | IPV6_ENABLED => { | ||
| 310 | ns => "$ns", | ||
| 311 | type => q{NS}, | ||
| 312 | } | ||
| 313 | ); | ||
| 314 | } | ||
| 315 | |||
| 316 | my $p = $ns->query( $zone->name, q{NS} ); | ||
| 317 | if ( $p ) { | ||
| 318 | if ( $p->has_rrs_of_type_for_name( q{NS}, $zone->name ) ) { | ||
| 319 | push @results, | ||
| 320 | info( | ||
| 321 | HAS_NAMESERVERS => { | ||
| 322 | ns => join( q{,}, sort map { $_->nsdname } $p->get_records_for_name( q{NS}, $zone->name ) ), | ||
| 323 | source => $ns->string, | ||
| 324 | } | ||
| 325 | ); | ||
| 326 | } | ||
| 327 | else { | ||
| 328 | push @results, | ||
| 329 | info( | ||
| 330 | NS_FAILED => { | ||
| 331 | source => $ns->string, | ||
| 332 | rcode => $p->rcode, | ||
| 333 | } | ||
| 334 | ); | ||
| 335 | } | ||
| 336 | } ## end if ( $p ) | ||
| 337 | else { | ||
| 338 | push @results, | ||
| 339 | info( | ||
| 340 | NS_NO_RESPONSE => { | ||
| 341 | source => $ns->string, | ||
| 342 | } | ||
| 343 | ); | ||
| 344 | } | ||
| 345 | } ## end foreach my $ns ( @{ Zonemaster::TestMethods...}) | ||
| 346 | |||
| 347 | return @results; | ||
| 348 | } ## end sub basic02 | ||
| 349 | |||
| 350 | sub basic03 { | ||
| 351 | my ( $class, $zone ) = @_; | ||
| 352 | my @results; | ||
| 353 | |||
| 354 | my $name = q{www.} . $zone->name; | ||
| 355 | my $response_nb = 0; | ||
| 356 | foreach my $ns ( @{ Zonemaster::TestMethods->method4( $zone ) } ) { | ||
| 357 | if ( not Zonemaster->config->ipv4_ok and $ns->address->version == $IP_VERSION_4 ) { | ||
| 358 | push @results, | ||
| 359 | info( | ||
| 360 | IPV4_DISABLED => { | ||
| 361 | ns => "$ns", | ||
| 362 | type => q{A}, | ||
| 363 | } | ||
| 364 | ); | ||
| 365 | next; | ||
| 366 | } | ||
| 367 | else { | ||
| 368 | push @results, | ||
| 369 | info( | ||
| 370 | IPV4_ENABLED => { | ||
| 371 | ns => "$ns", | ||
| 372 | type => q{NS}, | ||
| 373 | } | ||
| 374 | ); | ||
| 375 | } | ||
| 376 | |||
| 377 | if ( not Zonemaster->config->ipv6_ok and $ns->address->version == $IP_VERSION_6 ) { | ||
| 378 | push @results, | ||
| 379 | info( | ||
| 380 | IPV6_DISABLED => { | ||
| 381 | ns => "$ns", | ||
| 382 | type => q{A}, | ||
| 383 | } | ||
| 384 | ); | ||
| 385 | next; | ||
| 386 | } | ||
| 387 | elsif ( Zonemaster->config->ipv6_ok and $ns->address->version == $IP_VERSION_6 ) { | ||
| 388 | push @results, | ||
| 389 | info( | ||
| 390 | IPV6_ENABLED => { | ||
| 391 | ns => "$ns", | ||
| 392 | type => q{NS}, | ||
| 393 | } | ||
| 394 | ); | ||
| 395 | } | ||
| 396 | |||
| 397 | my $p = $ns->query( $name, q{A} ); | ||
| 398 | next if not $p; | ||
| 399 | $response_nb++; | ||
| 400 | if ( $p->has_rrs_of_type_for_name( q{A}, $name ) ) { | ||
| 401 | push @results, | ||
| 402 | info( | ||
| 403 | HAS_A_RECORDS => { | ||
| 404 | source => $ns->string, | ||
| 405 | name => $name, | ||
| 406 | } | ||
| 407 | ); | ||
| 408 | } | ||
| 409 | else { | ||
| 410 | push @results, | ||
| 411 | info( | ||
| 412 | NO_A_RECORDS => { | ||
| 413 | source => $ns->string, | ||
| 414 | name => $name, | ||
| 415 | } | ||
| 416 | ); | ||
| 417 | } | ||
| 418 | } ## end foreach my $ns ( @{ Zonemaster::TestMethods...}) | ||
| 419 | |||
| 420 | if ( scalar( @{ Zonemaster::TestMethods->method4( $zone ) } ) and not $response_nb ) { | ||
| 421 | push @results, | ||
| 422 | info( A_QUERY_NO_RESPONSES => {} ); | ||
| 423 | } | ||
| 424 | |||
| 425 | return @results; | ||
| 426 | } ## end sub basic03 | ||
| 427 | |||
| 428 | 1; | ||
| 429 | |||
| 430 | =head1 NAME | ||
| 431 | |||
| 432 | Zonemaster::Test::Basic - module implementing test for very basic domain functionality | ||
| 433 | |||
| 434 | =head1 SYNOPSIS | ||
| 435 | |||
| 436 | my @results = Zonemaster::Test::Basic->all($zone); | ||
| 437 | |||
| 438 | =head1 METHODS | ||
| 439 | |||
| 440 | =over | ||
| 441 | |||
| 442 | =item all($zone) | ||
| 443 | |||
| 444 | Runs between one and three tests, depending on the zone. If L<basic01> passes, L<basic02> is run. If L<basic02> fails, L<basic03> is run. | ||
| 445 | |||
| 446 | =item metadata() | ||
| 447 | |||
| 448 | 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 | ||
| 449 | lists with all the tags that the method can use in log entries. | ||
| 450 | |||
| 451 | =item translation() | ||
| 452 | |||
| 453 | Returns a refernce to a hash with translation data. Used by the builtin translation system. | ||
| 454 | |||
| 455 | =item version() | ||
| 456 | |||
| 457 | Returns a version string for the module. | ||
| 458 | |||
| 459 | =item can_continue(@results) | ||
| 460 | |||
| 461 | Looks at the provided log entries and returns true if they indicate that further testing of the relevant zone is possible. | ||
| 462 | |||
| 463 | =back | ||
| 464 | |||
| 465 | =head1 TESTS | ||
| 466 | |||
| 467 | =over | ||
| 468 | |||
| 469 | =item basic00 | ||
| 470 | |||
| 471 | Checks if the domain name to be tested is valid. Not all syntax tests are done here, it "just" checks domain name total length and labels length. | ||
| 472 | In case of failure, all other tests are aborted. | ||
| 473 | |||
| 474 | =item basic01 | ||
| 475 | |||
| 476 | Checks that we can find a parent zone for the zone we're testing. If we can't, no further testing is done. | ||
| 477 | |||
| 478 | =item basic02 | ||
| 479 | |||
| 480 | Checks that the nameservers for the parent zone returns NS records for the tested zone, and that at least one of the nameservers thus pointed out | ||
| 481 | responds sensibly to an NS query for the tested zone. | ||
| 482 | |||
| 483 | =item basic03 | ||
| 484 | |||
| 485 | Checks if at least one of the nameservers pointed out by the parent zone gives a useful response when sent an A query for the C<www> label in the | ||
| 486 | tested zone (that is, if we're testing C<example.org> this test will as for A records for C<www.example.org>). This test is only run if the | ||
| 487 | L<basic02> test has I<failed>. | ||
| 488 | |||
| 489 | =back | ||
| 490 | |||
| 491 | =cut |