Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
One limitation for these tests is that they can only been done when doing active queries, i.e when ZONEMASTER_RECORD=1,
because the evaluated object ("dns" attribute of a Zonemaster::Engine::Nameserver object) is only changed
when an actual query is sent (i.e not in cache, using Engine::Nameserver::_query()).
  • Loading branch information
tgreenx committed Nov 21, 2024
1 parent 06f5912 commit 82d8df7
Showing 1 changed file with 75 additions and 46 deletions.
121 changes: 75 additions & 46 deletions t/nameserver.t
Original file line number Diff line number Diff line change
Expand Up @@ -119,98 +119,108 @@ isa_ok( $dsrr, 'Zonemaster::LDNS::RR::DS' );
is( $dsrr->keytag, 16696, 'Expected keytag' );
is( $dsrr->hexdigest, 'deadbeef', 'Expected digest data' );

Zonemaster::Engine::Profile->effective->set( q{resolver.source4}, q{127.0.0.1} );
my $ns_test = new_ok( 'Zonemaster::Engine::Nameserver' => [ { name => 'ns.nic.se', address => '212.247.7.228' } ] );
is($ns_test->dns->source, '127.0.0.1', 'Source IPv4 address set.');

Zonemaster::Engine::Profile->effective->set( q{resolver.source6}, q{::1} );
my $ns_test = new_ok( 'Zonemaster::Engine::Nameserver' => [ { name => 'ns.nic.se', address => '2001:67c:124c:100a::45' } ] );
is($ns_test->dns->source, '::1', 'Source IPv6 address set.');

Zonemaster::Engine::Profile->effective->set( q{no_network}, 0 );
# Address was 127.0.0.17 (https://github.com/zonemaster/zonemaster-engine/issues/219).
# 192.0.2.17 is part of TEST-NET-1 IP address range (See RFC6890) and should be reserved
# for documentation.
my $fail_ns = Zonemaster::Engine::Nameserver->new( { name => 'fail', address => '192.0.2.17' } );
my $fail_p = $fail_ns->_query( 'example.org', 'A', {} );
is( $fail_p, undef, 'No return from broken server' );
my ( $e ) = grep { $_->tag eq 'LOOKUP_ERROR' } @{ Zonemaster::Engine->logger->entries };
isa_ok( $e, 'Zonemaster::Engine::Logger::Entry' );

subtest 'dnssec, edns_size and edns_details{do, size} flags behavior for queries' => sub {
my $ns = new_ok( 'Zonemaster::Engine::Nameserver' => [ { name => 'd.nic.fr', address => '194.0.9.1' } ] );

my $p = $ns->query( 'fr', 'SOA' );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 0, 'edns_size flag is unset' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 0, 'edns_size flag is unset' );
}
ok( !$p->has_edns and !$p->do, 'non-EDNS response received on query with all default parameters' );

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 0 } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 0, 'edns_size flag is unset' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 0, 'edns_size flag is unset' );
}
ok( !$p->has_edns and !$p->do, 'non-EDNS response received on query with dnssec unset' );

# Note that the following tests also implicitly test that flags are correctly re-evaluated between
# each consecutive queries.

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 1 } );
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, $UDP_DNSSEC_QUERY_DEFAULT, 'edns_size uses default DNSSEC query value' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, $UDP_DNSSEC_QUERY_DEFAULT, 'edns_size uses default DNSSEC query value' );
}
ok( $p->has_edns and $p->do, 'DNSSEC response received on query with dnssec set' );

$p = $ns->query( 'fr', 'SOA', { "edns_size" => 1000 } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 1000, 'edns_size uses given value' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 1000, 'edns_size uses given value' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query with edns_size set' );

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 0, "edns_size" => 1001 } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 1001, 'edns_size uses given value instead of default for non-DNSSEC EDNS queries' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 1001, 'edns_size uses given value instead of default for non-DNSSEC EDNS queries' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query with dnssec unset and edns_size set' );

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 1, "edns_size" => 1002 } );
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, 1002, 'edns_size uses given value instead of default for DNSSEC queries' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, 1002, 'edns_size uses given value instead of default for DNSSEC queries' );
}
ok( $p->has_edns and $p->do, 'DNSSEC response received on query with dnssec set and edns_size set' );

$p = $ns->query( 'fr', 'SOA', { "edns_details" => {} } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, $UDP_EDNS_QUERY_DEFAULT, 'edns_size uses default EDNS query value for non-DNSSEC EDNS queries' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, $UDP_EDNS_QUERY_DEFAULT, 'edns_size uses default EDNS query value for non-DNSSEC EDNS queries' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query with edns_details set' );

$p = $ns->query( 'fr', 'SOA', { "edns_details" => { "do" => 1 } } );
ok( $ns->dns->dnssec, 'dnssec flag is also set via edns_details{do}' );
is( $ns->dns->edns_size, $UDP_DNSSEC_QUERY_DEFAULT, 'edns_size also uses default DNSSEC query value when set with edns_details{do}' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( $ns->dns->dnssec, 'dnssec flag is also set via edns_details{do}' );
is( $ns->dns->edns_size, $UDP_DNSSEC_QUERY_DEFAULT, 'edns_size also uses default DNSSEC query value when set with edns_details{do}' );
}
ok( $p->has_edns and $p->do, 'DNSSEC response received on query with edns_details{do} set' );

$p = $ns->query( 'fr', 'SOA', { "edns_details" => { "size" => 900 } } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 900, 'edns_size also uses given value when set with edns_details{size}' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 900, 'edns_size also uses given value when set with edns_details{size}' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query with edns_details{size} set' );

$p = $ns->query( 'fr', 'SOA', { "edns_details" => { "size" => 0 } } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 0, 'edns_size also uses given value when set with edns_details{size}' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 0, 'edns_size also uses given value when set with edns_details{size}' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query even with edns_details{size} set to 0' );

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 1, "edns_details" => { "do" => 0 } } );
ok( !$ns->dns->dnssec, 'edns_details{do} takes precedence over dnssec for (un)setting the dnssec flag' );
is( $ns->dns->edns_size, $UDP_EDNS_QUERY_DEFAULT, 'edns_size uses default EDNS query value when dnssec flag is unset by edns_details{do}' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'edns_details{do} takes precedence over dnssec for (un)setting the dnssec flag' );
is( $ns->dns->edns_size, $UDP_EDNS_QUERY_DEFAULT, 'edns_size uses default EDNS query value when dnssec flag is unset by edns_details{do}' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query with dnssec unset by edns_details{do}' );

$p = $ns->query( 'fr', 'SOA', { "edns_size" => 1003, "edns_details" => { "size" => 901 } } );
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 901, 'edns_details{size} takes precedence over edns_size for setting the edns_size flag' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( !$ns->dns->dnssec, 'dnssec flag is unset' );
is( $ns->dns->edns_size, 901, 'edns_details{size} takes precedence over edns_size for setting the edns_size flag' );
}
ok( $p->has_edns and !$p->do, 'non-DNSSEC EDNS response received on query with edns_size and edns_details{size} set' );

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 1, "edns_size" => 1004, "edns_details" => { "size" => 0 } } );
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, 0, 'edns_size is unset' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, 0, 'edns_size is unset' );
}
ok( $p->has_edns and $p->do, 'DNSSEC response received on query with dnssec set and even with edns_details{size} set to 0' );

$p = $ns->query( 'fr', 'SOA', { "dnssec" => 0, "edns_size" => 1005, "edns_details" => { "do" => 1, "size" => 0 } } );
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, 0, 'edns_size is unset' );
if ( $ENV{ZONEMASTER_RECORD} ) {
ok( $ns->dns->dnssec, 'dnssec flag is set' );
is( $ns->dns->edns_size, 0, 'edns_size is unset' );
}
ok( $p->has_edns and $p->do, 'DNSSEC response received on query with dnssec set by edns_details{do} and even with edns_details{size} set to 0' );

dies_ok { $p = $ns->query( 'fr', 'SOA', { "edns_size" => 65536 } ); } "dies when edns_size exceeds 65535";
Expand All @@ -219,6 +229,25 @@ subtest 'dnssec, edns_size and edns_details{do, size} flags behavior for queries
dies_ok { $p = $ns->query( 'fr', 'SOA', { "edns_details" => { "size" => -1 } } ); } "dies when edns_size (set with edns_details->size) is lower than 0";
};


Zonemaster::Engine::Profile->effective->set( q{resolver.source4}, q{127.0.0.1} );
my $ns_test = new_ok( 'Zonemaster::Engine::Nameserver' => [ { name => 'ns.nic.se', address => '212.247.7.228' } ] );
is($ns_test->dns->source, '127.0.0.1', 'Source IPv4 address set.');

Zonemaster::Engine::Profile->effective->set( q{resolver.source6}, q{::1} );
my $ns_test = new_ok( 'Zonemaster::Engine::Nameserver' => [ { name => 'ns.nic.se', address => '2001:67c:124c:100a::45' } ] );
is($ns_test->dns->source, '::1', 'Source IPv6 address set.');

Zonemaster::Engine::Profile->effective->set( q{no_network}, 0 );
# Address was 127.0.0.17 (https://github.com/zonemaster/zonemaster-engine/issues/219).
# 192.0.2.17 is part of TEST-NET-1 IP address range (See RFC6890) and should be reserved
# for documentation.
my $fail_ns = Zonemaster::Engine::Nameserver->new( { name => 'fail', address => '192.0.2.17' } );
my $fail_p = $fail_ns->_query( 'example.org', 'A', {} );
is( $fail_p, undef, 'No return from broken server' );
my ( $e ) = grep { $_->tag eq 'LOOKUP_ERROR' } @{ Zonemaster::Engine->logger->entries };
isa_ok( $e, 'Zonemaster::Engine::Logger::Entry' );

if ( $ENV{ZONEMASTER_RECORD} ) {
Zonemaster::Engine::Nameserver->save( $datafile );
}
Expand Down

0 comments on commit 82d8df7

Please sign in to comment.