pNet-DNS 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,91 @@
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 TestHeader < Test::Unit::TestCase
19
+ def test_header
20
+ header = Net::DNS::Header.new();
21
+ assert(header, "new() returned something")
22
+
23
+ header.id=41
24
+ assert_equal(header.id, 41, "id() works")
25
+
26
+ header.qr=1
27
+ assert_equal(header.qr, 1, "qr() works")
28
+
29
+ header.opcode="QUERY"
30
+ assert_equal(header.opcode, "QUERY", "opcode() works")
31
+
32
+ header.aa=1
33
+ assert_equal(header.aa, 1, "aa() works")
34
+
35
+ header.tc=0
36
+ assert_equal(header.tc, 0, "tc() works")
37
+
38
+ header.rd=1
39
+ assert_equal(header.rd, 1, "rd() works")
40
+
41
+ header.ra=1
42
+ assert_equal(header.ra, 1, "ra() works")
43
+
44
+ header.qr=1
45
+ assert_equal(header.qr, 1, "qr() works")
46
+
47
+ header.rcode="NOERROR"
48
+ assert_equal(header.rcode, "NOERROR", "rcode() works")
49
+
50
+ header.qdcount=1
51
+ header.ancount=2
52
+ header.nscount=3
53
+ header.arcount=3
54
+
55
+
56
+ # Reenable when support for CD is there
57
+ #header.cd=0
58
+ #assert_equal(header.cd, 0, "cd() works")
59
+ puts(header.inspect)
60
+ data = header.data;
61
+
62
+ header2 = Net::DNS::Header.new(data);
63
+ puts(header2.inspect)
64
+
65
+ assert(header==(header2), 'Headers are the same');
66
+
67
+ #
68
+ # Is $header->string remotely sane?
69
+ #
70
+ assert(header.inspect =~ /opcode = QUERY/, 'string() has opcode correct');
71
+ assert(header.inspect =~ /ancount = 2/, 'string() has ancount correct');
72
+
73
+ header = Net::DNS::Header.new;
74
+
75
+ #
76
+ # Check that the aliases work properly.
77
+ #
78
+ header.zocount=(0);
79
+ header.prcount=(1);
80
+ header.upcount=(2);
81
+ header.adcount=(3);
82
+
83
+ assert_equal(header.zocount, 0, 'zocount works');
84
+ assert_equal(header.prcount, 1, 'prcount works');
85
+ assert_equal(header.upcount, 2, 'upcount works');
86
+ assert_equal(header.adcount, 3, 'adcount works');
87
+
88
+
89
+
90
+ end
91
+ end
@@ -0,0 +1,169 @@
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
+ require 'socket'
19
+ class TestInet6 < Test::Unit::TestCase
20
+ def ip6ok?
21
+ if (@checkedip6)
22
+ if !@ip6ok
23
+ return false
24
+ end
25
+ end
26
+ @checkedip6 = true
27
+ @ip6ok = true
28
+ # First use the local resolver to query for the AAAA record of a
29
+ # well known nameserver, than use v6 transport to get to that record.
30
+ puts ""
31
+ puts ""
32
+ puts "\tTesting for global IPv6 connectivity...\n"
33
+ puts "\t\t preparing..."
34
+
35
+ tstsock = UDPSocket.new()
36
+ begin
37
+ tstsock.bind("::1", 8765)
38
+ rescue Exception
39
+ # raise RuntimeError, "\n\n\t\tFailed to bind to ::1\n\t\t#{$!}\n\n\t\tWe assume there is no IPv6 connectivity and skip the tests\n\n"
40
+ puts "\n\n\t\tFailed to bind to ::1\n\t\t#{$!}\n\n\t\tWe assume there is no IPv6 connectivity and skip the tests\n\n"
41
+ @ip6ok=false
42
+ return false
43
+ ensure
44
+ tstsock.close
45
+ end
46
+ return true
47
+ end
48
+
49
+ def test_inet6
50
+ if (!ip6ok?)
51
+ return
52
+ end
53
+ res=Net::DNS::Resolver.new
54
+ # res.debug(1)
55
+ nsanswer=res.send("ripe.net",'NS','IN')
56
+ assert_instance_of(Net::DNS::RR::NS, (nsanswer.answer)[0], "Preparing for v6 transport, got NS records for ripe.net")
57
+
58
+ # foreach ns (nsanswer.answer){
59
+ nsanswer.answer.each do |ns|
60
+ next if ns.nsdname !~ /ripe\.net/ # User ripe.net only
61
+ a_answer=res.send(ns.nsdname, 'A','IN')
62
+ next if (a_answer.header.ancount == 0)
63
+ assert_instance_of(Net::DNS::RR::A, (a_answer.answer)[0], "Preparing for v4 transport, got A records for " + ns.nsdname)
64
+ a_address=(a_answer.answer)[0].address
65
+
66
+
67
+ puts("\n\t\t Will try to connect to " + ns.nsdname + " (#{a_address})")
68
+ break
69
+ end
70
+
71
+
72
+ aaaa_address=""
73
+ # foreach ns (nsanswer.answer){
74
+ nsanswer.answer.each do |ns|
75
+ next if ns.nsdname !~ /ripe\.net/ # User ripe.net only
76
+ aaaa_answer=res.send(ns.nsdname,'AAAA','IN')
77
+ next if (aaaa_answer.header.ancount == 0)
78
+ assert_equal((aaaa_answer.answer)[0].type,"AAAA", "Preparing for v6 transport, got AAAA records for " + ns.nsdname)
79
+ aaaa_address=(aaaa_answer.answer)[0].address
80
+
81
+
82
+ puts("\n\t\t Will try to connect to #{ns.nsdname} (#{aaaa_address})")
83
+ break
84
+ end
85
+
86
+ res.nameservers=(aaaa_address)
87
+ # res.print
88
+ answer=res.send("ripe.net",'SOA','IN')
89
+ if(res.errorstring =~ /Send error: /)
90
+ puts "\n\t\t Connection failed: " + res.errorstring
91
+ puts "\n\t\t It seems you do not have global IPv6 connectivity' \n"
92
+ puts "\t\t This is not an error in Net::DNS \n"
93
+
94
+ puts "\t\t You can confirm this by trying 'ping6 " + aaaa_address + "' \n\n"
95
+ end
96
+
97
+
98
+
99
+ # answer.print
100
+ assert_equal((answer.answer)[0].type, "SOA","Query over udp6 succeeded")
101
+
102
+ res.usevc(1)
103
+ res.force_v4(1)
104
+ # res.print
105
+ # res.debug(1)
106
+ answer=res.send("ripe.net",'SOA','IN')
107
+ assert_equal(res.errorstring,"no nameservers","Correct errorstring when forcing v4")
108
+
109
+
110
+ res.force_v4(0)
111
+ answer=res.send("ripe.net",'NS','IN')
112
+ if (answer)
113
+ assert_equal((answer.answer)[0].type, "NS","Query over tcp6 succeeded")
114
+ else
115
+ puts "You can safely ignore the following message:"
116
+ puts(res.errorstring) if (res.errorstring != "connection failed(IPv6 socket failure)")
117
+ puts("configuring " + aaaa_address + " " + a_address + " as nameservers")
118
+ res.nameservers(aaaa_address,a_address)
119
+ answer = nil
120
+ # res.print
121
+ answer=res.send("ripe.net",'NS','IN')
122
+ assert_equal((answer.answer)[0].type, "NS","Fallback to V4 succeeded")
123
+
124
+
125
+ end
126
+
127
+ end
128
+
129
+
130
+
131
+
132
+ def test_axfr
133
+ if (!ip6ok?)
134
+ return
135
+ end
136
+ #
137
+ # Now test AXFR functionality.
138
+ #
139
+ #
140
+ # First use the local resolver to query for the AAAA record of a
141
+
142
+ aaaa_address=""
143
+ res2=Net::DNS::Resolver.new
144
+ # res2.debug(1)
145
+ nsanswer=res2.send("net-dns.org",'NS','IN')
146
+ assert_equal((nsanswer.answer)[0].type, "NS","Preparing for v6 transport, got NS records for net-dns.org")
147
+ # foreach ns (nsanswer.answer){
148
+ nsanswer.answer.each do |ns|
149
+ # next if ns.nsdname !~ /ripe\.net/ # User rupe.net only
150
+ aaaa_answer=res2.send(ns.nsdname,'AAAA','IN')
151
+ next if (aaaa_answer.header.ancount == 0)
152
+ assert_equal((aaaa_answer.answer)[0].type,"AAAA", "Preparing for v6 transport, got AAAA records for " + ns.nsdname)
153
+ aaaa_address=(aaaa_answer.answer)[0].address
154
+
155
+ puts("\n\t\t Trying to connect to " + ns.nsdname + " (#{aaaa_address})")
156
+ break
157
+ end
158
+
159
+ res2.nameservers=(aaaa_address)
160
+ # res2.print
161
+
162
+ socket=res2.axfr_start('example.com')
163
+
164
+ assert_instance_of(Socket, socket,"axfr_start returns IPv6 Socket")
165
+ (rr,err)=res2.axfr_next
166
+ assert_equal(res2.errorstring,'Response code from server: NOTAUTH',"Transfer is not authorized (but our connection worked)")
167
+
168
+ end
169
+ end
@@ -0,0 +1,137 @@
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 TestMisc < Test::Unit::TestCase
19
+ def test_wildcard
20
+ # test to make sure that wildcarding works.
21
+ #
22
+ rr = Net::DNS::RR.create('*.t.net-dns.org 60 IN A 10.0.0.1')
23
+
24
+ assert(rr, 'RR got made')
25
+
26
+ assert_equal(rr.name, '*.t.net-dns.org', 'Name is correct' )
27
+ assert_equal(60, rr.ttl, 'TTL is correct' )
28
+ assert_equal('IN', rr.rrclass, 'CLASS is correct' )
29
+ assert_equal('A', rr.type, 'TYPE is correct' )
30
+ assert_equal('10.0.0.1', rr.address, 'Address is correct')
31
+ end
32
+
33
+ def test_misc
34
+
35
+ #
36
+ # Make sure the underscore in SRV hostnames work.
37
+ #
38
+ srv = Net::DNS::RR.create('_rvp._tcp.t.net-dns.org. 60 IN SRV 0 0 80 im.bastardsinc.biz')
39
+
40
+ assert(!$@, 'No errors')
41
+ assert(srv, 'SRV got made')
42
+
43
+
44
+ #~ # Test that the 5.005 Use of uninitialized value at
45
+ #~ # /usr/local/lib/perl5/site_perl/5.005/Net/DNS/RR.pm line 639. bug is gone
46
+ rr = Net::DNS::RR.create('mx.t.net-dns.org 60 IN MX 10 a.t.net-dns.org')
47
+ assert(rr, 'RR created')
48
+
49
+ assert_equal(rr.preference, 10, 'Preference works')
50
+
51
+
52
+
53
+ mx = Net::DNS::RR.create('mx.t.net-dns.org 60 IN MX 0 mail.net-dns.org')
54
+
55
+ assert(mx.inspect =~ /0 mail.net-dns.org/) # was 'like'
56
+ assert_equal(mx.preference, 0)
57
+ assert_equal(mx.exchange, 'mail.net-dns.org')
58
+
59
+ srv = Net::DNS::RR.create('srv.t.net-dns.org 60 IN SRV 0 2 3 target.net-dns.org')
60
+
61
+ p srv.inspect
62
+ assert(srv.inspect =~ /0 2 3 target.net-dns.org\./)
63
+ assert_equal(srv.rdatastr, '0 2 3 target.net-dns.org.')
64
+
65
+
66
+
67
+ end
68
+
69
+ def test_TXT_RR
70
+
71
+ #
72
+ #
73
+ # Below are some thests that have to do with TXT RRs
74
+ #
75
+ #
76
+
77
+
78
+ # QUESTION SECTION:
79
+ #txt2.t.net-dns.org. IN TXT
80
+
81
+ # ANSWER SECTION:
82
+ #txt2.t.net-dns.org. 60 IN TXT "Net-DNS\ complicated $tuff" "sort of \" text\ and binary \000 data"
83
+
84
+ # AUTHORITY SECTION:
85
+ #net-dns.org. 3600 IN NS ns1.net-dns.org.
86
+ #net-dns.org. 3600 IN NS ns.ripe.net.
87
+ #net-dns.org. 3600 IN NS ns.hactrn.net.
88
+
89
+ # ADDITIONAL SECTION:
90
+ #ns1.net-dns.org. 3600 IN A 193.0.4.49
91
+ #ns1.net-dns.org. 3600 IN AAAA
92
+
93
+ uuencodedPacket=%w{
94
+ 11 99 85 00 00 01
95
+ 00 01 00 03 00 02 04 74 78 74 32 01 74 07 6e 65
96
+ 74 2d 64 6e 73 03 6f 72 67 00 00 10 00 01 c0 0c
97
+ 00 10 00 01 00 00 00 3c 00 3d 1a 4e 65 74 2d 44
98
+ 4e 53 3b 20 63 6f 6d 70 6c 69 63 61 74 65 64 20
99
+ 24 74 75 66 66 21 73 6f 72 74 20 6f 66 20 22 20
100
+ 74 65 78 74 3b 20 61 6e 64 20 62 69 6e 61 72 79
101
+ 20 00 20 64 61 74 61 c0 13 00 02 00 01 00 00 0e
102
+ 10 00 06 03 6e 73 31 c0 13 c0 13 00 02 00 01 00
103
+ 00 0e 10 00 0d 02 6e 73 04 72 69 70 65 03 6e 65
104
+ 74 00 c0 13 00 02 00 01 00 00 0e 10 00 0c 02 6e
105
+ 73 06 68 61 63 74 72 6e c0 93 c0 79 00 01 00 01
106
+ 00 00 0e 10 00 04 c1 00 04 31 c0 79 00 1c 00 01
107
+ 00 00 0e 10 00 10 20 01 06 10 02 40 00 03 00 00
108
+ 12 34 be 21 e3 1e
109
+ }
110
+
111
+ uuencodedPacket.map!{|e| e.hex}
112
+ packetdata = uuencodedPacket.pack('c*')
113
+ packetdata.gsub!("\s*", "")
114
+
115
+ packet = Net::DNS::Packet.new_from_binary(packetdata)
116
+ txtRr=(packet.answer)[0]
117
+ assert_equal('Net-DNS; complicated $tuff',(txtRr.char_str_list())[0],"First Char string in TXT RR read from wireformat")
118
+
119
+ # Compare the second char_str this contains a NULL byte (space NULL
120
+ # space=200020 in hex)
121
+
122
+ temp = (txtRr.char_str_list())[1].unpack('H*')[0]
123
+ #assert_equal(unpack('H*',(TXTrr.char_str_list())[1]),"736f7274206f66202220746578743b20616e642062696e61727920002064617461", "Second Char string in TXT RR read from wireformat")
124
+ assert_equal("736f7274206f66202220746578743b20616e642062696e61727920002064617461", temp,"Second Char string in TXT RR read from wireformat")
125
+
126
+
127
+ txtRr2=Net::DNS::RR.create('txt2.t.net-dns.org. 60 IN TXT "Test1 \" \ more stuff" "Test2"')
128
+
129
+ assert_equal((txtRr2.char_str_list())[0],'Test1 " more stuff', "First arg string in TXT RR read from zonefileformat")
130
+ assert_equal((txtRr2.char_str_list())[1],'Test2',"Second Char string in TXT RR read from zonefileformat")
131
+
132
+
133
+ txtRr3 = Net::DNS::RR.create("baz.example.com 3600 HS TXT '\"' 'Char Str2'")
134
+
135
+ assert_equal( (txtRr3.char_str_list())[0],'"',"Escaped \" between the single quotes")
136
+ end
137
+ end
@@ -0,0 +1,236 @@
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
+ require 'socket'
19
+ class TestOnline < Test::Unit::TestCase
20
+ def test_online
21
+ res = Net::DNS::Resolver.new
22
+ rrs = [
23
+ {
24
+ :type => 'A',
25
+ :name => 'a.t.net-dns.org',
26
+ :address => '10.0.1.128'
27
+ },
28
+ {
29
+ :type => 'MX',
30
+ :name => 'mx.t.net-dns.org',
31
+ :exchange => 'a.t.net-dns.org',
32
+ :preference => 10
33
+ },
34
+ {
35
+ :type => 'CNAME',
36
+ :name => 'cname.t.net-dns.org',
37
+ :cname => 'a.t.net-dns.org'
38
+ },
39
+ {
40
+ :type => 'TXT',
41
+ :name => 'txt.t.net-dns.org',
42
+ :txtdata => 'Net-DNS'
43
+ }
44
+ ]
45
+
46
+ (rrs.each do |data|
47
+ packet = res.send(data[:name], data[:type], 'IN')
48
+
49
+ assert(packet, "Got an answer for #{data[:name]} IN #{data[:type]}")
50
+ assert_equal(1, packet.header.qdcount, 'Only one question')
51
+ assert_equal(1, packet.header.ancount, 'Got single answer')
52
+
53
+ question = (packet.question)[0]
54
+ answer = (packet.answer)[0]
55
+
56
+ assert(question, 'Got question' )
57
+ assert_equal(data[:name], question.qname, 'Question has right name' )
58
+ assert_equal(data[:type], question.qtype, 'Question has right type' )
59
+ assert_equal('IN', question.qclass, 'Question has right class')
60
+
61
+ assert(answer)
62
+ assert_equal(answer.rrclass, 'IN', 'Class correct' )
63
+
64
+
65
+ # foreach meth (keys %{data}) {
66
+ (data.keys.each do |meth|
67
+ assert_equal(answer.send(meth), data[meth], "#{meth} correct (#{data[:name]})")
68
+ end)
69
+ end) # do
70
+ end # test_online
71
+
72
+ def test_mx
73
+ # Does the mx() function work.
74
+ mx = Net::DNS.mx('mx2.t.net-dns.org')
75
+
76
+ wanted_names = ['a.t.net-dns.org', 'a2.t.net-dns.org']
77
+ names = mx.collect { |i| i.exchange } # $_.exchange)
78
+ #names = [ map { $_.exchange } @mx ]
79
+
80
+ assert_equal(names, wanted_names, "mx() seems to be working")
81
+
82
+ # some people seem to use mx() in scalar context
83
+ assert_equal(2, Net::DNS.mx('mx2.t.net-dns.org').length, "mx() works in scalar context")
84
+
85
+ #
86
+ # test that search() and query() DTRT with reverse lookups
87
+ #
88
+ tests = [
89
+ {
90
+ :ip => '198.41.0.4',
91
+ :host => 'a.root-servers.net',
92
+ },
93
+ {
94
+ :ip => '2001:500:1::803f:235',
95
+ :host => 'h.root-servers.net',
96
+ },
97
+ ]
98
+
99
+ res = Net::DNS::Resolver.new
100
+ testWords = ['search', 'query']
101
+ # foreach test (@tests) {
102
+ tests.each do |test|
103
+ # foreach method (qw(search query)) {
104
+ testWords.each do |method|
105
+ packet = res.send_method(method, test[:ip])
106
+
107
+ assert_instance_of(Net::DNS::Packet,packet)
108
+
109
+ next unless packet
110
+
111
+ assert_equal((packet.answer)[0].ptrdname, test[:host], "method(#{test[:ip]}) works")
112
+ end # do
113
+ end # do
114
+ end # test_mx
115
+
116
+ def test_search_query
117
+ res = Net::DNS::Resolver.new(
118
+ :domain => 't.net-dns.org',
119
+ :searchlist => ['t.net-dns.org', 'net-dns.org']
120
+ )
121
+
122
+
123
+ #
124
+ # test the search() and query() append the default domain and
125
+ # searchlist correctly.
126
+ #
127
+ res.defnames=(1)
128
+ res.dnsrch=(1)
129
+ res.persistent_udp=(0)
130
+
131
+ tests = [
132
+ {
133
+ :method => 'search',
134
+ :name => 'a'
135
+ },
136
+ {
137
+ :method => 'search',
138
+ :name => 'a.t'
139
+ },
140
+ {
141
+ :method => 'query',
142
+ :name => 'a'
143
+ }
144
+ ]
145
+
146
+
147
+ tests.each do |test|
148
+ method = test[:method]
149
+
150
+ ans = res.send_method(method,test[:name])
151
+
152
+ assert_instance_of(Net::DNS::Packet, ans)
153
+
154
+ assert_equal(ans.header.ancount, 1,"Correct answer count (with method)")
155
+ a = ans.answer[0]
156
+
157
+ assert_instance_of(Net::DNS::RR::A, a)
158
+ assert_equal(a.name, 'a.t.net-dns.org',"Correct name (with method)")
159
+ end
160
+ socket=res.bgsend('a.t.net-dns.org','A')
161
+ assert(socket.is_a?(UDPSocket),"Socket returned")
162
+
163
+ # burn a little CPU to get the socket ready.
164
+ sleep(0.1)
165
+ res.debug=false
166
+
167
+ assert(res.bgisready(socket),"Socket is ready")
168
+ if res.bgisready(socket)
169
+
170
+ ans= res.bgread(socket)
171
+ socket = nil
172
+ assert_equal(ans.header.ancount, 1,"Correct answer count")
173
+ a=ans.answer
174
+
175
+ assert_instance_of(Net::DNS::RR::A,a[0])
176
+ assert_equal(a[0].name, 'a.t.net-dns.org',"Correct name")
177
+ else
178
+ print "No socket to read from"
179
+ end
180
+ end
181
+
182
+
183
+ def test_searchlist
184
+ res = Net::DNS::Resolver.new(
185
+ :domain => 't.net-dns.org',
186
+ :searchlist => ["t.net-dns.org", "net-dns.org"]
187
+ )
188
+
189
+ #
190
+ # test the search() and query() append the default domain and
191
+ # searchlist correctly.
192
+ #
193
+
194
+ res.defnames=(1)
195
+ res.dnsrch=(1)
196
+ res.persistent_udp=(1)
197
+ # res.debug(1)
198
+ tests = [
199
+ {
200
+ :method => 'search',
201
+ :name => 'a'
202
+ },
203
+ {
204
+ :method => 'search',
205
+ :name => 'a.t'
206
+ },
207
+ {
208
+ :method => 'query',
209
+ :name => 'a'
210
+ }
211
+ ]
212
+
213
+ # res.send("a.t.net-dns.org A")
214
+ res.send("a.t.net-dns.org", "A")
215
+
216
+ sock_id= res.sockets['AF_INET']["UDP"]
217
+ assert(sock_id,"Persistent UDP socket identified")
218
+
219
+ (tests.each do |test|
220
+ method = test[:method]
221
+
222
+ ans = res.send_method(method,test[:name])
223
+ assert_equal( res.sockets['AF_INET']["UDP"],sock_id,"Persistent socket matches")
224
+
225
+ assert_instance_of(Net::DNS::Packet, ans)
226
+
227
+ assert_equal(1, ans.header.ancount, "Correct answer count (with persistent socket and method)")
228
+
229
+ a = ans.answer
230
+
231
+ assert_instance_of(Net::DNS::RR::A, a[0])
232
+ assert_equal(a[0].name, 'a.t.net-dns.org',"Correct name (with persistent socket and method)")
233
+ end)
234
+
235
+ end
236
+ end