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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#!/usr/bin/perl
# $Id: 04-packet.t 1980 2024-06-02 10:16:33Z willem $ -*-perl-*-
#
use strict;
use warnings;
use Test::More tests => 104;
use TestToolkit;
use_ok('Net::DNS::Packet');
# new() class constructor method must return object of appropriate class
my $object = Net::DNS::Packet->new();
ok( $object->isa('Net::DNS::Packet'), 'new() object' );
ok( $object->header, 'header() method works' );
ok( $object->header->isa('Net::DNS::Header'), 'header() returns header object' );
ok( $object->edns, 'edns() method works' );
ok( $object->edns->isa('Net::DNS::RR::OPT'), 'edns() returns OPT RR object' );
like( $object->string, '/HEADER/', 'string() returns representation of packet' );
$object->header->do(1);
$object->encode();
like( $object->string, '/EDNS/', 'string() contains representation of EDNS' );
$object->header->opcode('UPDATE');
like( $object->string, '/UPDATE/', 'string() returns representation of update' );
# Empty packet created when new() arguments omitted
my $empty = Net::DNS::Packet->new();
ok( $empty, 'create empty packet' );
foreach my $method ( qw(question answer authority additional), qw(zone pre prerequisite update) ) {
my @result = $empty->$method;
ok( @result == 0, "$method() returns empty list" );
}
# Create a DNS query packet
my ( $domain, $type, $class ) = qw(example.test MX IN);
my $question = Net::DNS::Question->new( $domain, $type, $class );
my $packet = Net::DNS::Packet->new( $domain, $type, $class );
like( $packet->string, "/$class\t$type/", 'create query packet' );
my @question = $packet->question;
ok( @question && @question == 1, 'packet->question() returns single element list' );
my ($q) = @question;
ok( $q->isa('Net::DNS::Question'), 'list element is a question object' );
is( $q->string, $question->string, 'question object correct' );
# encode() method returns non-empty scalar
my $packet_data = $packet->encode;
ok( $packet_data, 'packet->encode() method works' );
ok( $packet->data, 'packet->data() alias works' );
# new(\$data) class constructor method returns object of appropriate class
my $packet2 = Net::DNS::Packet->new( \$packet_data );
ok( $packet2->isa('Net::DNS::Packet'), 'new(\$data) object' );
is( $packet2->string, $packet->string, 'decoded packet matches original' );
is( unpack( 'H*', $packet2->encode ), unpack( 'H*', $packet_data ), 'retransmitted packet matches original' );
my $empty_packet = Net::DNS::Packet->new()->encode;
ok( Net::DNS::Packet->decode( \$empty_packet )->string, 'decoded empty packet' );
my $dso = Net::DNS::Packet->new();
$dso->header->opcode('DSO');
my $dso_packet = $dso->encode . pack( 'n2H*', 1, 2, 'beef' );
ok( Net::DNS::Packet->decode( \$dso_packet )->string, 'decoded DSO packet' );
# Use push() to add RRs to each section
my $update = Net::DNS::Packet->new('.');
my $index;
foreach my $section (qw(answer authority additional)) {
my $i = ++$index;
my $rr1 = Net::DNS::RR->new(
Name => "$section$i.example.test",
Type => "A",
Address => "10.0.0.$i"
);
my $string1 = $rr1->string;
my $count1 = $update->push( $section, $rr1 );
like( $update->string, "/$string1/", "push first RR into $section section" );
is( $count1, 1, "push() returns $section RR count" );
my $j = ++$index;
my $rr2 = Net::DNS::RR->new(
Name => "$section$j.example.test",
Type => "A",
Address => "10.0.0.$j"
);
my $string2 = $rr2->string;
my $count2 = $update->push( $section, $rr2 );
like( $update->string, "/$string2/", "push second RR into $section section" );
is( $count2, 2, "push() returns $section RR count" );
}
# Add enough distinct labels to render compression unusable at some point
for ( 0 .. 255 ) {
$update->push( 'answer', Net::DNS::RR->new( "X$_ TXT \"" . pack( "A255", "x" ) . '"' ) );
}
$update->push( 'answer', Net::DNS::RR->new('XY TXT ""') );
$update->push( 'answer', Net::DNS::RR->new('VW.XY TXT ""') );
# Decode data buffer and compare with original
my $buffer = $update->encode;
my $decoded = eval { Net::DNS::Packet->decode( \$buffer ) };
ok( $decoded, 'decode() from data buffer works' );
is( $decoded->size, length($buffer), '$decoded->size() works' );
$decoded->from('local');
ok( $decoded->from(), '$decoded->from() works' );
ok( $decoded->string(), '$decoded->string() works' );
foreach my $count (qw(qdcount ancount nscount arcount)) {
is( $decoded->header->$count, $update->header->$count, "check header->$count correct" );
}
ok( $decoded->answersize, 'answersize() alias works' );
ok( $decoded->answerfrom, 'answerfrom() alias works' );
foreach my $section (qw(question)) {
my @original = map { $_->string } $update->$section;
my @content = map { $_->string } $decoded->$section;
is_deeply( \@content, \@original, "check content of $section section" );
}
foreach my $section (qw(answer authority additional)) {
my @original = map { $_->ttl(0); $_->string } $update->$section; # almost! need TTL defined
my @content = map { $_->string } $decoded->$section;
is_deeply( \@content, \@original, "check content of $section section" );
}
# check that pop() removes RR from section Memo to self: no RR in question section!
foreach my $section (qw(answer authority additional)) {
my $c1 = $update->push( $section, Net::DNS::RR->new('X TXT ""') );
my $rr = $update->pop($section);
my $c2 = $update->push($section);
is( $c2, $c1 - 1, "pop() RR from $section section" );
}
for my $packet ( Net::DNS::Packet->new('example.com') ) {
my $case1 = $packet->pop(''); ## check tolerance of invalid pop
my $case2 = $packet->pop('bogus');
}
# Test using a predefined answer.
# This is an answer that was generated by a bind server, with an option munged on the end.
my $BIND = pack( 'H*',
'22cc85000001000000010001056461636874036e657400001e0001c00c0006000100000e100025026e730472697065c012046f6c6166c02a7754e1ae0000a8c0000038400005460000001c2000002910000000800000050000000130'
);
my $bind = Net::DNS::Packet->decode( \$BIND );
is( $bind->header->qdcount, 1, 'check question count in synthetic packet header' );
is( $bind->header->ancount, 0, 'check answer count in synthetic packet header' );
is( $bind->header->nscount, 1, 'check authority count in synthetic packet header' );
is( $bind->header->adcount, 1, 'check additional count in synthetic packet header' );
for my $packet ( Net::DNS::Packet->new('example.com') ) {
my $reply = $packet->reply(); ## check $packet->reply()
ok( $reply->isa('Net::DNS::Packet'), '$packet->reply() returns packet' );
like( $reply->string, '/HEADER/', 'reply->string() represents packet' );
my $udpmax = 2048;
$packet->edns->udpsize($udpmax);
$packet->encode;
is( $packet->reply($udpmax)->edns->udpsize(), $udpmax, 'packet->reply() supports EDNS' );
}
for my $packet ( Net::DNS::Packet->new() ) { ## check $packet->sigrr
my $sigrr = Net::DNS::RR->new( type => 'TSIG' );
my $other = Net::DNS::RR->new( type => 'AAAA' );
$packet->unique_push( 'additional' => $other );
is( $packet->sigrr(), undef, 'sigrr() undef for unsigned packet' );
is( $packet->verify(), undef, 'verify() fails for unsigned packet' );
ok( $packet->verifyerr(), 'verifyerr() returned for unsigned packet' );
is( ref( $packet->sign_tsig($sigrr) ), ref($sigrr), 'sign_tsig() returns TSIG record' );
is( $packet->verifyerr(), '', 'verifyerr() returns empty string' );
$packet->push( 'additional' => $sigrr );
is( ref( $packet->sigrr() ), ref($sigrr), 'sigrr() returns TSIG record' );
}
eval { ## no critic # exercise dump and debug diagnostics
require IO::File;
require Data::Dumper;
local $Data::Dumper::Maxdepth;
local $Data::Dumper::Sortkeys;
local $Data::Dumper::Useqq;
my $packet = Net::DNS::Packet->new();
my $buffer = $packet->encode;
my $corrupt = substr $buffer, 0, 10;
my $file = '04-packet.txt';
my $handle = IO::File->new( $file, '>' ) || die "Could not open $file for writing";
select( ( select($handle), $packet->dump )[0] );
$Data::Dumper::Maxdepth = 6;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
select( ( select($handle), $packet->dump )[0] );
select( ( select($handle), Net::DNS::Packet->decode( \$buffer, 1 )->dump )[0] );
select( ( select($handle), Net::DNS::Packet->decode( \$corrupt, 1 ) )[0] );
close($handle);
unlink($file);
};
for my $packet ( Net::DNS::Packet->new(qw(example.com. A IN)) ) {
my $wire = $packet->encode;
while ( length($wire) ) {
chop($wire);
my $n = length($wire); ## Note: need to re-raise exception trapped by constructor
exception( "decode truncated ($n octets)", sub { Net::DNS::Packet->decode( \$wire ); die } );
}
my $sig = Net::DNS::RR->new( type => 'SIG' );
exception( 'reply->reply()', sub { $packet->reply->reply } );
exception( 'sign_tsig(...)', sub { $packet->sign_tsig($packet) } );
exception( 'sign_sig0(...)', sub { $packet->sign_sig0($packet) } );
exception( 'sig0 verify()', sub { $packet->sign_sig0($sig); $packet->verify } );
}
exit;
|