1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#!/usr/bin/perl
# $Id: 24-NSEC-encloser.t 1856 2021-12-02 14:36:25Z willem $ -*-perl-*-
#
use strict;
use warnings;
use Test::More;
use Net::DNS;
my @prerequisite = qw(
Net::DNS::ZoneFile
Net::DNS::RR::NSEC
);
foreach my $package (@prerequisite) {
next if eval "require $package"; ## no critic
plan skip_all => "$package not installed";
exit;
}
plan tests => 9;
## Based on example from RFC7129 3.2
my @nsec = grep { $_->type eq 'NSEC' } Net::DNS::ZoneFile->parse( <<'END' );
$ORIGIN example.org.
example.org. SOA ( ns1 dns )
; DNSKEY ( ... )
; NS a.example.org.
NSEC a.example.org. NS SOA RRSIG NSEC DNSKEY
; RRSIG(NS) ( ... )
; RRSIG(SOA) ( ... )
; RRSIG(NSEC) ( ... )
; RRSIG(DNSKEY) ( ... )
a.example.org. A 192.0.2.1
; TXT "a record"
NSEC d.example.org. A TXT RRSIG NSEC
; RRSIG(A) ( ... )
; RRSIG(TXT) ( ... )
; RRSIG(NSEC) ( ... )
d.example.org. A 192.0.2.1
; TXT "d record"
NSEC example.org. A TXT RRSIG NSEC
END
sub closest_encloser {
my $qname = shift;
my $encloser;
foreach my $nsec (@nsec) {
my $ancestor = $nsec->encloser($qname);
$encloser = $ancestor if $ancestor;
}
foreach my $nsec ( reverse @nsec ) { # check order independence
my $ancestor = $nsec->encloser($qname);
$encloser = $ancestor if $ancestor;
}
return $encloser;
}
sub next_closer_name {
my $qname = shift;
my $nextcloser;
foreach my $nsec (@nsec) {
next unless $nsec->encloser($qname);
$nextcloser = $nsec->nextcloser;
}
return $nextcloser;
}
sub closer_wildcard {
my $qname = shift;
my $wildcard;
foreach my $nsec (@nsec) {
next unless $nsec->encloser($qname);
$wildcard = $nsec->wildcard;
}
return $wildcard;
}
is( closest_encloser('example.org.'), undef, 'encloser(example.org)' );
is( closest_encloser('a.example.org.'), 'example.org', 'encloser(a.example.org)' );
is( closest_encloser('d.example.org.'), 'example.org', 'encloser(d.example.org)' );
is( closest_encloser('b.example.org.'), 'example.org', 'encloser(b.example.org)' );
is( next_closer_name('b.example.org.'), 'b.example.org', 'nextcloser(b.example.org)' );
is( closer_wildcard('b.example.org.'), '*.example.org', 'wildcard(b.example.org)' );
is( closest_encloser('a.b.c.example.org.'), 'example.org', 'encloser(a.b.c.example.org)' );
is( next_closer_name('a.b.c.example.org.'), 'c.example.org', 'nextcloser(a.b.c.example.org)' );
is( closer_wildcard('a.b.c.example.org.'), '*.example.org', 'wildcard(a.b.c.example.org)' );
exit;
__END__
|