File: 04-packet.t

package info (click to toggle)
libnet-dns-perl 0.59-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 812 kB
  • ctags: 400
  • sloc: perl: 6,650; sh: 117; ansic: 101; makefile: 60
file content (181 lines) | stat: -rw-r--r-- 5,615 bytes parent folder | download | duplicates (2)
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
# $Id: 04-packet.t 264 2005-04-06 09:16:15Z olaf $    -*-perl-*-


use Test::More tests => 39;
use strict;

BEGIN { use_ok('Net::DNS'); }     #1


my $domain = "example.com";
my $type   = "MX";
my $class  = "IN";

#------------------------------------------------------------------------------
# Make sure we can create a DNS packet.
#------------------------------------------------------------------------------

my $packet = Net::DNS::Packet->new($domain, $type, $class);

ok($packet,                                 'new() returned something');         #2
ok($packet->header,                         'header() method works');            #3
ok($packet->header->isa('Net::DNS::Header'),'header() returns right thing');     #4


my @question = $packet->question;
ok(@question && @question == 1,             'question() returned right number of items'); #5
ok($question[0]->isa('Net::DNS::Question'), 'question() returned the right thing');       #6


my @answer = $packet->answer;
ok(@answer == 0,     'answer() works when empty');     #7


my @authority = $packet->authority;
ok(@authority == 0,  'authority() works when empty');  #8

my @additional = $packet->additional;
ok(@additional == 0, 'additional() works when empty'); #9

$packet->push("answer", 
	Net::DNS::RR->new(
		Name    => "a1.example.com",
		Type    => "A",
		Address => "10.0.0.1"
	)
);
is($packet->header->ancount, 1, 'First push into answer section worked');      #10


$packet->push("answer", 
	Net::DNS::RR->new(
		Name    => "a2.example.com",
		Type    => "A",
		Address => "10.0.0.2"
	)
);
is($packet->header->ancount, 2, 'Second push into answer section worked');     #11


$packet->push("authority", 
	Net::DNS::RR->new(
		Name    => "a3.example.com",
		Type    => "A",
		Address => "10.0.0.3"
	)
);
is($packet->header->nscount, 1, 'First push into authority section worked');   #12


$packet->push("authority", 
	Net::DNS::RR->new(
		Name    => "a4.example.com",
		Type    => "A",
		Address => "10.0.0.4"
	)
);
is($packet->header->nscount, 2, 'Second push into authority section worked');  #13

$packet->push("additional", 
	Net::DNS::RR->new(
		Name    => "a5.example.com",
		Type    => "A",
		Address => "10.0.0.5"
	)
);
is($packet->header->adcount, 1, 'First push into additional section worked');  #14

$packet->push("additional", 
	Net::DNS::RR->new(
		Name    => "a6.example.com",
		Type    => "A",
		Address => "10.0.0.6"
	)
);
is($packet->header->adcount, 2, 'Second push into additional section worked'); #15

my $data = $packet->data;

my $packet2 = Net::DNS::Packet->new(\$data);

ok($packet2, 'new() from data buffer works');   #16

is($packet->string, $packet2->string, 'string () works correctly');  #17


my $string = $packet2->string;
for (1 .. 6) {
	my $ip = "10.0.0.$_";
	ok($string =~ m/\Q$ip/,  "Found $ip in packet");  # 18 though 23
}

is($packet2->header->qdcount, 1, 'header question count correct');   #24
is($packet2->header->ancount, 2, 'header answer count correct');     #25
is($packet2->header->nscount, 2, 'header authority count correct');  #26 
is($packet2->header->adcount, 2, 'header additional count correct'); #27



# Test using a predefined answer. This is an answer that was generated by a bind server.
#

$data=pack("H*","22cc85000001000000010001056461636874036e657400001e0001c00c0006000100000e100025026e730472697065c012046f6c6166c02a7754e1ae0000a8c0000038400005460000001c2000002910000000800000050000000030");
my $packet3 = Net::DNS::Packet->new(\$data);
ok($packet3,                                 'new(\$data) returned something');         #28

is($packet3->header->qdcount, 1, 'header question count in syntetic packet correct');   #29
is($packet3->header->ancount, 0, 'header answer count in syntetic packet correct');     #30
is($packet3->header->nscount, 1, 'header authority count in syntetic packet  correct'); #31 
is($packet3->header->adcount, 1, 'header additional in sytnetic  packet correct');      #32

my @rr=$packet3->additional;

is($rr[0]->type, "OPT", "Additional section packet is EDNS0 type");                         #33
is($rr[0]->class, "4096", "EDNS0 packet size correct");                                     #34

my $question2=Net::DNS::Question->new("bla.foo","TXT","CHAOS");
ok($question2->isa('Net::DNS::Question'),"Proper type of object created");  #35

# In theory its valid to have multiple questions in the question section.
# Not many servers digest it though.

$packet->push("question", $question2);
@question = $packet->question;
ok(@question && @question == 2,             'question() returned right number of items poptest:2'); #36


$packet->pop("question");

@question = $packet->question;
ok(@question && @question == 1,             'question() returned right number of items poptest:1'); #37

$packet->pop("question");

@question = $packet->question;


ok(@question ==0,              'question() returned right number of items poptest0'); #38





$packet=Net::DNS::Packet->new("254.9.11.10.in-addr.arpa","PTR","IN");

$packet->push("answer", Net::DNS::RR->new(
	"254.9.11.10.in-addr.arpa 86400 IN PTR
host-84-11-9-254.customer.example.com"));

$packet->push("authority", Net::DNS::RR->new(
        "9.11.10.in-addr.arpa 86400 IN NS autons1.example.com"));
$packet->push("authority", Net::DNS::RR->new(
        "9.11.10.in-addr.arpa 86400 IN NS autons2.example.com"));
$packet->push("authority", Net::DNS::RR->new(
        "9.11.10.in-addr.arpa 86400 IN NS autons3.example.com"));

$data=$packet->data;
$packet2=Net::DNS::Packet->new(\$data);


is($packet->string,$packet2->string,"Packet to data and back (failure indicates broken dn_comp)");  #39