pNet-DNS 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. data/README +68 -0
  2. data/lib/Net/DNS.rb +879 -0
  3. data/lib/Net/DNS/Header.rb +303 -0
  4. data/lib/Net/DNS/Nameserver.rb +601 -0
  5. data/lib/Net/DNS/Packet.rb +851 -0
  6. data/lib/Net/DNS/Question.rb +117 -0
  7. data/lib/Net/DNS/RR.rb +630 -0
  8. data/lib/Net/DNS/RR/A.rb +103 -0
  9. data/lib/Net/DNS/RR/AAAA.rb +147 -0
  10. data/lib/Net/DNS/RR/AFSDB.rb +114 -0
  11. data/lib/Net/DNS/RR/CERT.rb +191 -0
  12. data/lib/Net/DNS/RR/CNAME.rb +89 -0
  13. data/lib/Net/DNS/RR/DNAME.rb +84 -0
  14. data/lib/Net/DNS/RR/EID.rb +70 -0
  15. data/lib/Net/DNS/RR/HINFO.rb +108 -0
  16. data/lib/Net/DNS/RR/ISDN.rb +118 -0
  17. data/lib/Net/DNS/RR/LOC.rb +341 -0
  18. data/lib/Net/DNS/RR/MB.rb +92 -0
  19. data/lib/Net/DNS/RR/MG.rb +96 -0
  20. data/lib/Net/DNS/RR/MINFO.rb +109 -0
  21. data/lib/Net/DNS/RR/MR.rb +92 -0
  22. data/lib/Net/DNS/RR/MX.rb +124 -0
  23. data/lib/Net/DNS/RR/NAPTR.rb +182 -0
  24. data/lib/Net/DNS/RR/NIMLOC.rb +70 -0
  25. data/lib/Net/DNS/RR/NS.rb +100 -0
  26. data/lib/Net/DNS/RR/NSAP.rb +273 -0
  27. data/lib/Net/DNS/RR/NULL.rb +68 -0
  28. data/lib/Net/DNS/RR/OPT.rb +251 -0
  29. data/lib/Net/DNS/RR/PTR.rb +93 -0
  30. data/lib/Net/DNS/RR/PX.rb +131 -0
  31. data/lib/Net/DNS/RR/RP.rb +108 -0
  32. data/lib/Net/DNS/RR/RT.rb +115 -0
  33. data/lib/Net/DNS/RR/SOA.rb +195 -0
  34. data/lib/Net/DNS/RR/SPF.rb +46 -0
  35. data/lib/Net/DNS/RR/SRV.rb +153 -0
  36. data/lib/Net/DNS/RR/SSHFP.rb +190 -0
  37. data/lib/Net/DNS/RR/TKEY.rb +219 -0
  38. data/lib/Net/DNS/RR/TSIG.rb +358 -0
  39. data/lib/Net/DNS/RR/TXT.rb +162 -0
  40. data/lib/Net/DNS/RR/UNKNOWN.rb +76 -0
  41. data/lib/Net/DNS/RR/X25.rb +90 -0
  42. data/lib/Net/DNS/Resolver.rb +2090 -0
  43. data/lib/Net/DNS/Resolver/Recurse.rb +478 -0
  44. data/lib/Net/DNS/Update.rb +189 -0
  45. data/test/custom.txt +4 -0
  46. data/test/resolv.conf +4 -0
  47. data/test/tc_escapedchars.rb +498 -0
  48. data/test/tc_header.rb +91 -0
  49. data/test/tc_inet6.rb +169 -0
  50. data/test/tc_misc.rb +137 -0
  51. data/test/tc_online.rb +236 -0
  52. data/test/tc_packet.rb +174 -0
  53. data/test/tc_packet_unique_push.rb +126 -0
  54. data/test/tc_question.rb +49 -0
  55. data/test/tc_recurse.rb +69 -0
  56. data/test/tc_res_env.rb +59 -0
  57. data/test/tc_res_file.rb +55 -0
  58. data/test/tc_res_opt.rb +135 -0
  59. data/test/tc_resolver.rb +102 -0
  60. data/test/tc_rr-opt.rb +40 -0
  61. data/test/tc_rr-rrsort.rb +116 -0
  62. data/test/tc_rr-txt.rb +138 -0
  63. data/test/tc_rr-unknown.rb +95 -0
  64. data/test/tc_rr.rb +246 -0
  65. data/test/tc_tcp.rb +34 -0
  66. data/test/tc_tkey.rb +115 -0
  67. data/test/tc_update.rb +226 -0
  68. data/test/ts_netdns.rb +17 -0
  69. data/test/ts_offline.rb +32 -0
  70. data/test/ts_online.rb +33 -0
  71. metadata +119 -0
@@ -0,0 +1,138 @@
1
+ # The contents of this file are subject to the Mozilla
2
+ # Public Licence Version 1.1 (the "Licence"); you may
3
+ # not use this file except in compliance with the
4
+ # Licence. You may obtain a copy of the Licence at
5
+ # http://www.mozilla.org/MPL
6
+ # Software distributed under the Licence is distributed
7
+ # on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
8
+ # either express or implied. See the Licence of the
9
+ # specific language governing rights and limitations
10
+ # under the Licence.
11
+ # The Original Code is pNet::DNS.
12
+ # The Initial Developer of the Original Code is
13
+ # Nominet UK (www.nominet.org.uk). Portions created by
14
+ # Nominet UK are Copyright (c) Nominet UK 2006.
15
+ # All rights reserved.
16
+ require 'test/unit'
17
+ require 'Net/DNS'
18
+ class TestRrTest < Test::Unit::TestCase
19
+ #Stimulus, expected response, and test name:
20
+
21
+ TESTLIST = [
22
+ { # 2-5
23
+ :stim => %<"">,
24
+ :rdatastr => %<"">,
25
+ :char_str_list_r => ['',],
26
+ :descr => 'Double-quoted null string',
27
+ },
28
+ { # 6-9
29
+ :stim => %<''>,
30
+ :rdatastr => %<"">,
31
+ :char_str_list_r => ['',],
32
+ :descr => 'Single-quoted null string',
33
+ },
34
+ { # 10-13
35
+ :stim => %<" \t">,
36
+ :rdatastr => %<" \t">,
37
+ :char_str_list_r => [ %< \t>, ],
38
+ :descr => 'Double-quoted whitespace string',
39
+ },
40
+ { # 14-17
41
+ :stim => %<noquotes>,
42
+ :rdatastr => %<"noquotes">,
43
+ :char_str_list_r => [ %<noquotes>, ],
44
+ :descr => 'unquoted single string',
45
+ },
46
+ { # 18-21
47
+ :stim => %<"yes_quotes">,
48
+ :rdatastr => %<"yes_quotes">,
49
+ :char_str_list_r => [ %<yes_quotes>, ],
50
+ :descr => 'Double-quoted single string',
51
+ },
52
+ { # 26-29
53
+ :stim => %<two tokens>,
54
+ :rdatastr => %<"two" "tokens">,
55
+ :char_str_list_r => [ %q|two|, %q|tokens|, ],
56
+ :descr => 'Two unquoted strings',
57
+ },
58
+ # @TODO@ Why don't escaped quotes work?
59
+ # { # 22-25
60
+ # :stim => %<"escaped \" quote">,
61
+ # :rdatastr => %<"escaped \" quote">,
62
+ # :char_str_list_r => [ %<escaped " quote>, ],
63
+ # :descr => 'Quoted, escaped double-quote',
64
+ # },
65
+ # { # 30-33
66
+ # :stim => %<"missing quote>,
67
+ # :rdatastr => %<>,
68
+ # :char_str_list_r => [],
69
+ # :descr => 'Unbalanced quotes work',
70
+ # }
71
+ ]
72
+
73
+ def test_RrTest
74
+ #------------------------------------------------------------------------------
75
+ # Canned data.
76
+ #------------------------------------------------------------------------------
77
+
78
+ name = 'foo.example.com';
79
+ klass = 'IN';
80
+ type = 'TXT';
81
+ ttl = 43201;
82
+
83
+ rr_base = [name, ttl, klass, type, " " ].join(' ')
84
+
85
+
86
+ #------------------------------------------------------------------------------
87
+ # Run the tests
88
+ #------------------------------------------------------------------------------
89
+
90
+ TESTLIST.each do |test_hr|
91
+ puts(test_hr.inspect)
92
+ assert( uut = Net::DNS::RR.create(rr_base + test_hr[:stim]),
93
+ test_hr[:descr] + " -- Stimulus " )
94
+
95
+ print "\nuut : " + uut.inspect + "\n"
96
+
97
+ assert_equal(test_hr[:rdatastr], uut.rdatastr(),
98
+ test_hr[:descr] + " -- Response ( rdatastr ) " )
99
+
100
+ list = uut.char_str_list()
101
+
102
+ assert_equal(test_hr[:char_str_list_r], list,
103
+ test_hr[:descr] + " -- char_str_list equality" )
104
+ end
105
+
106
+ string1 = %<no>
107
+ string2 = %<quotes>
108
+
109
+ rdata = [string1.length].pack("C") + string1
110
+ rdata += [string2.length].pack("C") + string2
111
+
112
+ # RR.new_from_hash() drops stuff straight into the hash and
113
+ # re-blesses it, breaking encapsulation.
114
+
115
+ work_hash = {
116
+ :name => name,
117
+ :ttl => ttl,
118
+ :rrclass => klass,
119
+ :type => type,
120
+ }
121
+
122
+
123
+ # Don't break RR.new_from_hash (e.i. "See the manual pages for each RR
124
+ # type to see what fields the type requires.").
125
+
126
+ work_hash[:txtdata] = %<no quotes>
127
+
128
+ uut = Net::DNS::RR.create(work_hash)
129
+ assert( uut , # 30
130
+ "RR.new_from_hash with txtdata -- Stimulus")
131
+ assert( uut.rdatastr() == %<"no" "quotes">, # 31
132
+ "RR.new_from_hash with txtdata -- Response (rdatastr())")
133
+
134
+ assert( uut.rr_rdata() == rdata , "TXT.rr_rdata" ) # 32
135
+
136
+
137
+ end
138
+ end
@@ -0,0 +1,95 @@
1
+ # The contents of this file are subject to the Mozilla
2
+ # Public Licence Version 1.1 (the "Licence"); you may
3
+ # not use this file except in compliance with the
4
+ # Licence. You may obtain a copy of the Licence at
5
+ # http://www.mozilla.org/MPL
6
+ # Software distributed under the Licence is distributed
7
+ # on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
8
+ # either express or implied. See the Licence of the
9
+ # specific language governing rights and limitations
10
+ # under the Licence.
11
+ # The Original Code is pNet::DNS.
12
+ # The Initial Developer of the Original Code is
13
+ # Nominet UK (www.nominet.org.uk). Portions created by
14
+ # Nominet UK are Copyright (c) Nominet UK 2006.
15
+ # All rights reserved.
16
+ require 'test/unit'
17
+ require 'Net/DNS'
18
+ class TestRrUnknown < Test::Unit::TestCase
19
+ def test_RrUnknown
20
+ assert_equal(10226, Net::DNS::typesbyname('TYPE10226'), 'typesbyname(TYPE10226) returns 10226')
21
+ assert_equal('TYPE10226', Net::DNS::typesbyval(10226), 'typesbyval(10226) returns TYPE10226')
22
+ assert_equal('A', Net::DNS::typesbyval(1), ' typesbyval(1) returns A')
23
+
24
+ assert_equal(Net::DNS::typesbyval(Net::DNS::typesbyname('TYPE001')), 'A', 'typesbyval(typebyname(TYPE001)) returns A')
25
+
26
+
27
+ begin
28
+ Net::DNS::typesbyval(0xffff+1)
29
+ flunk("Should fail on large TYPE code")
30
+ rescue Exception
31
+ end
32
+
33
+ assert_equal(Net::DNS::classesbyname('CLASS124'), 124, 'classesbyname(CLASS124) returns 124')
34
+ assert_equal(Net::DNS::classesbyval(125), 'CLASS125','classesbyval(125) returns CLASS125')
35
+ assert_equal(Net::DNS::classesbyval(1), 'IN', 'classesbyval(1) returns IN')
36
+
37
+ assert_equal('HS', Net::DNS::classesbyval(Net::DNS::classesbyname('CLASS04')), 'classesbyval(typebyname(CLASS04)) returns HS')
38
+
39
+ begin
40
+ Net::DNS::classesbyval(0xffff+1)
41
+ flunk("Should fail on large CLASS code")
42
+ rescue Exception
43
+ end
44
+ end
45
+
46
+ def test_rr_new
47
+ rr = Net::DNS::RR.create('e.example CLASS01 TYPE01 10.0.0.2')
48
+ assert_equal('A', rr.type, 'TYPE01 parsed OK')
49
+ assert_equal('IN', rr.rrclass,'CLASS01 parsed OK')
50
+
51
+ rr = Net::DNS::RR.create('e.example IN A \# 4 0A0000 01 ')
52
+ assert_equal('10.0.0.1', rr.address,'Unknown RR representation for A parsed OK')
53
+
54
+ begin
55
+ res=Net::DNS::RR.create('e.example IN A \# 4 0A0000 01 11 ')
56
+ flunk "Should fail on inconsistent length and hex presentation"
57
+ rescue Exception
58
+ #like($@, '/\\\# 4 0A0000 01 11 assert_equal inconsistent\ length does not match content/', 'Fails on inconsassert_equaltent length and hex presentation')
59
+ end
60
+
61
+
62
+ rr = Net::DNS::RR.create('e.example IN TYPE4555 \# 4 0A0000 01 ')
63
+ assert_equal('e.example. 0 IN TYPE4555 \# 4 0a000001', rr.inspect, 'Fully unknown RR parsed correctly')
64
+
65
+ rr4 = Net::DNS::RR.create('e.example CLASS122 TYPE4555 \# 4 0A0000 01 ')
66
+ assert_equal('e.example. 0 CLASS122 TYPE4555 \# 4 0a000001', rr4.inspect, 'Fully unknown RR in unknown CLASS parsed correctly')
67
+ end
68
+
69
+ def test_real_data
70
+ uuencodedPacket=%w{
71
+ 02 79 85 00 00 01
72
+ 00 01 00 01 00 01 04 54 45 53 54 07 65 78 61 6d
73
+ 70 6c 65 03 63 6f 6d 00 00 ff 00 01 c0 0c 30 39
74
+ 00 01 00 00 00 7b 00 0a 11 22 33 44 55 aa bb cc
75
+ dd ee c0 11 00 02 00 01 00 00 03 84 00 05 02 6e
76
+ 73 c0 11 c0 44 00 01 00 01 00 00 03 84 00 04 7f
77
+ 00 00 01}
78
+
79
+ # packetdata = uuencodedPacket.pack('H*')
80
+ # packetdata = packetdata.gsub("\s*", "")
81
+
82
+ uuencodedPacket.map!{|e| e.hex}
83
+ packetdata = uuencodedPacket.pack('c*')
84
+ packet = Net::DNS::Packet.new_from_binary(packetdata)
85
+
86
+ string_representation = (packet.answer)[0].inspect
87
+ #string_representation =~ s/\s+/ /g,
88
+ string_representation = string_representation.gsub(/\s+/, " ")
89
+ assert_equal(
90
+ 'TEST.example.com. 123 IN TYPE12345 \# 10 1122334455aabbccddee',
91
+ string_representation,
92
+ 'Packet read from a packet dumped by bind...'
93
+ )
94
+ end
95
+ end
@@ -0,0 +1,246 @@
1
+ # The contents of this file are subject to the Mozilla
2
+ # Public Licence Version 1.1 (the "Licence"); you may
3
+ # not use this file except in compliance with the
4
+ # Licence. You may obtain a copy of the Licence at
5
+ # http://www.mozilla.org/MPL
6
+ # Software distributed under the Licence is distributed
7
+ # on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
8
+ # either express or implied. See the Licence of the
9
+ # specific language governing rights and limitations
10
+ # under the Licence.
11
+ # The Original Code is pNet::DNS.
12
+ # The Initial Developer of the Original Code is
13
+ # Nominet UK (www.nominet.org.uk). Portions created by
14
+ # Nominet UK are Copyright (c) Nominet UK 2006.
15
+ # All rights reserved.
16
+ require 'test/unit'
17
+ require 'Net/DNS'
18
+ class TestRR < Test::Unit::TestCase
19
+ # @todo Add DNSSEC stuff when DNSSEC exists
20
+ def test_rr
21
+ #------------------------------------------------------------------------------
22
+ # Canned data.
23
+ #------------------------------------------------------------------------------
24
+
25
+ name = "foo.example.com";
26
+ klass = "IN";
27
+ ttl = 43200;
28
+
29
+ rrs = [
30
+ { #[0]
31
+ :type => 'A',
32
+ :address => '10.0.0.1',
33
+ },
34
+ { #[1]
35
+ :type => 'AAAA',
36
+ :address => '102:304:506:708:90a:b0c:d0e:ff10',
37
+ },
38
+ { #[2]
39
+ :type => 'AFSDB',
40
+ :subtype => 1,
41
+ :hostname => 'afsdb-hostname.example.com',
42
+ },
43
+ { #[3]
44
+ :type => 'CNAME',
45
+ :cname => 'cname-cname.example.com',
46
+ },
47
+ { #[4]
48
+ :type => 'DNAME',
49
+ :dname => 'dname.example.com',
50
+ },
51
+ { #[5]
52
+ :type => 'HINFO',
53
+ :cpu => 'test-cpu',
54
+ :os => 'test-os',
55
+ },
56
+ { #[6]
57
+ :type => 'ISDN',
58
+ :address => '987654321',
59
+ :sa => '001',
60
+ },
61
+ { #[7]
62
+ :type => 'MB',
63
+ :madname => 'mb-madname.example.com',
64
+ },
65
+ { #[8]
66
+ :type => 'MG',
67
+ :mgmname => 'mg-mgmname.example.com',
68
+ },
69
+ { #[9]
70
+ :type => 'MINFO',
71
+ :rmailbx => 'minfo-rmailbx.example.com',
72
+ :emailbx => 'minfo-emailbx.example.com',
73
+ },
74
+ { #[10]
75
+ :type => 'MR',
76
+ :newname => 'mr-newname.example.com',
77
+ },
78
+ { #[11]
79
+ :type => 'MX',
80
+ :preference => 10,
81
+ :exchange => 'mx-exchange.example.com',
82
+ },
83
+ { #[12]
84
+ :type => 'NAPTR',
85
+ :order => 100,
86
+ :preference => 10,
87
+ :flags => 'naptr-flags',
88
+ :service => 'naptr-service',
89
+ :regexp => 'naptr-regexp',
90
+ :replacement => 'naptr-replacement.example.com',
91
+ },
92
+ { #[13]
93
+ :type => 'NS',
94
+ :nsdname => 'ns-nsdname.example.com',
95
+ },
96
+ { #[14]
97
+ :type => 'NSAP',
98
+ :afi => '47',
99
+ :idi => '0005',
100
+ :dfi => '80',
101
+ :aa => '005a00',
102
+ :rd => '1000',
103
+ :area => '0020',
104
+ :id => '00800a123456',
105
+ :sel => '00',
106
+ },
107
+ { #[15]
108
+ :type => 'PTR',
109
+ :ptrdname => 'ptr-ptrdname.example.com',
110
+ },
111
+ { #[16]
112
+ :type => 'PX',
113
+ :preference => 10,
114
+ :map822 => 'px-map822.example.com',
115
+ :mapx400 => 'px-mapx400.example.com',
116
+ },
117
+ { #[17]
118
+ :type => 'RP',
119
+ :mbox => 'rp-mbox.example.com',
120
+ :txtdname => 'rp-txtdname.example.com',
121
+ },
122
+ { #[18]
123
+ :type => 'RT',
124
+ :preference => 10,
125
+ :intermediate => 'rt-intermediate.example.com',
126
+ },
127
+ { #[19]
128
+ :type => 'SOA',
129
+ :mname => 'soa-mname.example.com',
130
+ :rname => 'soa-rname.example.com',
131
+ :serial => 12345,
132
+ :refresh => 7200,
133
+ :retry => 3600,
134
+ :expire => 2592000,
135
+ :minimum => 86400,
136
+ },
137
+ { #[20]
138
+ :type => 'SRV',
139
+ :priority => 1,
140
+ :weight => 2,
141
+ :port => 3,
142
+ :target => 'srv-target.example.com',
143
+ },
144
+ { #[21]
145
+ :type => 'TXT',
146
+ :txtdata => 'txt-txtdata',
147
+ },
148
+ { #[22]
149
+ :type => 'X25',
150
+ :psdn => 123456789,
151
+ },
152
+ { #[23]
153
+ :type => 'LOC',
154
+ :version => 0,
155
+ :size => 3000,
156
+ :horiz_pre => 500000,
157
+ :vert_pre => 500,
158
+ :latitude => 2001683648,
159
+ :longitude => 1856783648,
160
+ :altitude => 9997600,
161
+ }, #[24]
162
+ {
163
+ :type => 'CERT',
164
+ :format => 3,
165
+ :tag => 1,
166
+ :algorithm => 1,
167
+ :certificate => '123456789abcdefghijklmnopqrstuvwxyz',
168
+ },
169
+
170
+ { #[25]
171
+ :type => 'SPF',
172
+ :txtdata => 'txt-txtdata',
173
+ },
174
+ ]
175
+
176
+
177
+
178
+
179
+
180
+ #------------------------------------------------------------------------------
181
+ # Create the packet
182
+ #------------------------------------------------------------------------------
183
+
184
+ packet = Net::DNS::Packet.new_from_values(name);
185
+ assert(packet, 'Packet created');
186
+
187
+ rrs.each do |data|
188
+ data.update({ :name => name,
189
+ :ttl => ttl,
190
+ })
191
+ rr=Net::DNS::RR.create(data)
192
+
193
+ packet.push('answer', rr );
194
+ end
195
+
196
+
197
+ #------------------------------------------------------------------------------
198
+ # Re-create the packet from data.
199
+ #------------------------------------------------------------------------------
200
+
201
+ data = packet.data;
202
+ assert(data, 'Packet has data after pushes');
203
+
204
+ packet=nil;
205
+ packet = Net::DNS::Packet.new_from_binary(data);
206
+
207
+ assert(packet, 'Packet reconstructed from data');
208
+
209
+ answer = packet.answer;
210
+
211
+ puts(answer)
212
+
213
+ i = 0
214
+ rrs.each do |rec|
215
+ ret_rr = answer[i]
216
+ i += 1
217
+ rec.each do |key, value|
218
+ # method = key+'=?'
219
+ x = ret_rr.send(key)
220
+ assert_equal(value, x, "Packet returned wrong answer section for #{ret_rr.to_s}, #{key}")
221
+ end
222
+ end
223
+
224
+
225
+
226
+ while (!answer.empty? and !rrs.empty?)
227
+ data = rrs.shift;
228
+ rr = answer.shift;
229
+ type = data[:type];
230
+
231
+ assert(rr, "#{type} - RR defined");
232
+ assert_equal(name, rr.name, "#{type} - name() correct");
233
+ assert_equal(klass, rr.rrclass, "#{type} - class() correct");
234
+ assert_equal(ttl, rr.ttl, "#{type} - ttl() correct");
235
+
236
+ # foreach my $meth (keys %{data}) {
237
+ data.keys.each do |meth|
238
+
239
+ assert_equal(data[meth], rr.send(meth), "#{type} - #{meth}() correct");
240
+ end
241
+
242
+ rr2 = Net::DNS::RR.create(rr.inspect);
243
+ assert_equal(rr.inspect, rr2.inspect, "#{type} - Parsing from string works");
244
+ end
245
+ end
246
+ end