net-dns 0.6.1 → 0.7.0

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 (53) hide show
  1. data/.gitignore +8 -6
  2. data/.travis.yml +14 -0
  3. data/CHANGELOG.md +79 -0
  4. data/Gemfile +4 -0
  5. data/Rakefile +56 -66
  6. data/demo/check_soa.rb +1 -1
  7. data/demo/threads.rb +1 -1
  8. data/lib/net/dns.rb +24 -22
  9. data/lib/net/dns/header.rb +77 -103
  10. data/lib/net/dns/{names/names.rb → names.rb} +19 -20
  11. data/lib/net/dns/packet.rb +231 -256
  12. data/lib/net/dns/question.rb +11 -40
  13. data/lib/net/dns/resolver.rb +248 -250
  14. data/lib/net/dns/resolver/socks.rb +6 -6
  15. data/lib/net/dns/resolver/timeouts.rb +1 -1
  16. data/lib/net/dns/rr.rb +112 -117
  17. data/lib/net/dns/rr/a.rb +98 -89
  18. data/lib/net/dns/rr/aaaa.rb +84 -68
  19. data/lib/net/dns/rr/classes.rb +91 -106
  20. data/lib/net/dns/rr/cname.rb +64 -45
  21. data/lib/net/dns/rr/hinfo.rb +90 -50
  22. data/lib/net/dns/rr/mr.rb +61 -44
  23. data/lib/net/dns/rr/mx.rb +73 -48
  24. data/lib/net/dns/rr/ns.rb +60 -46
  25. data/lib/net/dns/rr/null.rb +11 -12
  26. data/lib/net/dns/rr/ptr.rb +47 -34
  27. data/lib/net/dns/rr/soa.rb +5 -6
  28. data/lib/net/dns/rr/srv.rb +1 -4
  29. data/lib/net/dns/rr/txt.rb +14 -14
  30. data/lib/net/dns/rr/types.rb +13 -13
  31. data/lib/net/dns/version.rb +8 -14
  32. data/net-dns.gemspec +35 -0
  33. data/setup.rb +3 -2
  34. data/test/header_test.rb +18 -18
  35. data/test/names_test.rb +21 -0
  36. data/test/packet_test.rb +38 -31
  37. data/test/question_test.rb +23 -24
  38. data/test/resolver/timeouts_test.rb +13 -13
  39. data/test/resolver_test.rb +28 -20
  40. data/test/rr/a_test.rb +70 -23
  41. data/test/rr/aaaa_test.rb +109 -0
  42. data/test/rr/classes_test.rb +61 -49
  43. data/test/rr/cname_test.rb +97 -0
  44. data/test/rr/hinfo_test.rb +117 -0
  45. data/test/rr/mr_test.rb +105 -0
  46. data/test/rr/mx_test.rb +112 -0
  47. data/test/rr/ns_test.rb +34 -12
  48. data/test/rr/types_test.rb +4 -4
  49. data/test/rr_test.rb +1 -1
  50. metadata +77 -52
  51. data/AUTHORS.rdoc +0 -7
  52. data/CHANGELOG.rdoc +0 -46
  53. data/VERSION.yml +0 -5
@@ -0,0 +1,109 @@
1
+ require 'test_helper'
2
+ require 'net/dns/rr'
3
+
4
+ class RRAAAATest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @rr_name = "www.nic.it."
8
+ @rr_type = "AAAA"
9
+ @rr_cls = "IN"
10
+ @rr_ttl = 60
11
+ @rr_value = "2a00:d40:1:1::239"
12
+ @rr_address = IPAddr.new(@rr_value)
13
+
14
+ @rr_output = "www.nic.it. 60 IN AAAA 2a00:d40:1:1::239"
15
+
16
+ @rr = Net::DNS::RR::AAAA.new(:name => @rr_name, :address => @rr_address, :ttl => @rr_ttl)
17
+ end
18
+
19
+
20
+ def test_initialize_from_hash
21
+ @record = Net::DNS::RR::AAAA.new(:name => @rr_name, :address => @rr_value, :ttl => @rr_ttl)
22
+ assert_equal @rr_output, @record.to_s
23
+ assert_equal @rr_name, @record.name
24
+ assert_equal @rr_type, @record.type
25
+ assert_equal @rr_cls, @record.cls
26
+ assert_equal @rr_ttl, @record.ttl
27
+ assert_equal @rr_address, @record.address
28
+ assert_equal @rr_value, @record.value
29
+ end
30
+
31
+ def test_initialize_from_string
32
+ @record = Net::DNS::RR::AAAA.new("#{@rr_name} #{@rr_ttl} #{@rr_cls} #{@rr_type} #{@rr_value}")
33
+ assert_equal @rr_output, @record.to_s
34
+ assert_equal @rr_name, @record.name
35
+ assert_equal @rr_type, @record.type
36
+ assert_equal @rr_cls, @record.cls
37
+ assert_equal @rr_ttl, @record.ttl
38
+ assert_equal @rr_address, @record.address
39
+ assert_equal @rr_value, @record.value
40
+ end
41
+
42
+ def test_parse
43
+ data = "\003www\003nic\002it\000\000\034\000\001\000\000\000<\000\020*\000\r@\000\001\000\001\000\000\000\000\000\000\0029"
44
+ @record = Net::DNS::RR.parse(data)
45
+ assert_equal @rr_output, @record.to_s
46
+ assert_equal @rr_name, @record.name
47
+ assert_equal @rr_type, @record.type
48
+ assert_equal @rr_cls, @record.cls
49
+ assert_equal @rr_ttl, @record.ttl
50
+ assert_equal @rr_address, @record.address
51
+ assert_equal @rr_value, @record.value
52
+ end
53
+
54
+
55
+ InvalidArguments = [
56
+ { :name => "google.com", :address => "2a00" },
57
+ { :name => "google.com" },
58
+ Object.new,
59
+ Array.new(7),
60
+ "10800 IN AAAA",
61
+ # FIXME: "google.com. 10800 IN B",
62
+ # FIXME: "google.com. 10800 IM AAAA",
63
+ ]
64
+
65
+ InvalidArguments.each_with_index do |arguments, index|
66
+ define_method "test_initialize_should_raise_with_invalid_arguments_#{index}" do
67
+ assert_raises(ArgumentError) { Net::DNS::RR::AAAA.new(arguments) }
68
+ end
69
+ end
70
+
71
+
72
+ def test_address_getter
73
+ assert_equal @rr_address, @rr.address
74
+ end
75
+
76
+ def test_address_setter
77
+ assert_raises(ArgumentError) { @rr.address = nil }
78
+
79
+ expected = IPAddr.new("2a00:d40:1:1::239")
80
+ assert_equal expected, @rr.address = "2a00:d40:1:1::239"
81
+ assert_equal expected, @rr.address
82
+
83
+ expected = IPAddr.new("2a00:d40:1:1::240")
84
+ assert_equal expected, @rr.address = IPAddr.new("2a00:d40:1:1::240")
85
+ assert_equal expected, @rr.address
86
+ end
87
+
88
+
89
+ def test_value
90
+ assert_equal @rr_value, @rr.value
91
+ end
92
+
93
+
94
+ def test_inspect
95
+ assert_equal "www.nic.it. 60 IN AAAA 2a00:d40:1:1::239",
96
+ @rr.inspect
97
+ end
98
+
99
+ def test_to_s
100
+ assert_equal "www.nic.it. 60 IN AAAA 2a00:d40:1:1::239",
101
+ @rr.to_s
102
+ end
103
+
104
+ def test_to_a
105
+ assert_equal ["www.nic.it.", 60, "IN", "AAAA", "2a00:d40:1:1::239"],
106
+ @rr.to_a
107
+ end
108
+
109
+ end
@@ -2,72 +2,84 @@ require 'test_helper'
2
2
  require 'net/dns/rr'
3
3
 
4
4
  class RRClassesTest < Test::Unit::TestCase
5
-
5
+
6
6
  def setup
7
7
  @classes = {
8
- 'IN' => 1, # RFC 1035
9
- 'CH' => 3, # RFC 1035
10
- 'HS' => 4, # RFC 1035
11
- 'NONE' => 254, # RFC 2136
12
- 'ANY' => 255, # RFC 1035
13
8
  }
14
9
  @regexp_string = "ANY|CH|HS|IN|NONE"
15
10
  end
16
-
17
- def test_default
11
+
12
+
13
+ StrAndNum = [
14
+ ['IN' , 1],
15
+ ['CH' , 3],
16
+ ['HS' , 4],
17
+ ['NONE' , 254],
18
+ ['ANY' , 255],
19
+ ]
20
+
21
+ StrAndNum.each do |str, num|
22
+ define_method "test_initialize_from_str_#{str}" do
23
+ instance = Net::DNS::RR::Classes.new(str)
24
+ assert_equal str, instance.to_s
25
+ assert_equal num, instance.to_i
26
+ end
27
+ define_method "test_initialize_from_num_#{num}" do
28
+ instance = Net::DNS::RR::Classes.new(num)
29
+ assert_equal str, instance.to_s
30
+ assert_equal num, instance.to_i
31
+ end
32
+ end
33
+
34
+ def test_initialize_should_raise_with_invalid_class
35
+ assert_raises(ArgumentError) { Net::DNS::RR::Classes.new(Hash.new) }
36
+ end
37
+
38
+
39
+ def test_inspect
40
+ assert_equal 1, Net::DNS::RR::Classes.new(1).inspect
41
+ assert_equal 1, Net::DNS::RR::Classes.new("IN").inspect
42
+ end
43
+
44
+ def test_to_s
45
+ assert_equal "IN", Net::DNS::RR::Classes.new(1).to_s
46
+ assert_equal "IN", Net::DNS::RR::Classes.new("IN").to_s
47
+ end
48
+
49
+ def test_to_i
50
+ assert_equal 1, Net::DNS::RR::Classes.new(1).to_i
51
+ assert_equal 1, Net::DNS::RR::Classes.new("IN").to_i
52
+ end
53
+
54
+
55
+ def test_self_default
18
56
  # Default type should be ANY => 255
19
57
  instance = Net::DNS::RR::Classes.new(nil)
20
- assert_equal("1", instance.to_str)
21
- assert_equal("IN", instance.to_s)
22
-
58
+ assert_equal 1, instance.to_i
59
+ assert_equal "IN", instance.to_s
60
+
23
61
  # Let's change default behaviour
24
62
  Net::DNS::RR::Classes.default = "CH"
25
63
  instance = Net::DNS::RR::Classes.new(nil)
26
- assert_equal("3", instance.to_str)
27
- assert_equal("CH", instance.to_s)
64
+ assert_equal 3, instance.to_i
65
+ assert_equal "CH", instance.to_s
28
66
 
29
67
  Net::DNS::RR::Classes.default = "IN"
30
68
  instance = Net::DNS::RR::Classes.new(nil)
31
- assert_equal("1", instance.to_str)
32
- assert_equal("IN", instance.to_s)
69
+ assert_equal 1, instance.to_i
70
+ assert_equal "IN", instance.to_s
33
71
  end
34
72
 
35
- def test_classes
36
- @classes.each do |key,num|
37
- instance_from_string = Net::DNS::RR::Classes.new(key)
38
- instance_from_num = Net::DNS::RR::Classes.new(num)
39
- assert_equal(key, instance_from_string.to_s)
40
- assert_equal(num.to_s, instance_from_string.to_str)
41
- assert_equal(key, instance_from_num.to_s)
42
- assert_equal(num.to_s, instance_from_num.to_str)
43
- end
44
- assert_raise(ClassArgumentError) do
45
- Net::DNS::RR::Classes.new(Hash.new)
46
- end
73
+ def test_self_valid?
74
+ assert Net::DNS::RR::Classes.valid?("IN")
75
+ assert Net::DNS::RR::Classes.valid?(1)
76
+ assert !Net::DNS::RR::Classes.valid?("Q")
77
+ assert !Net::DNS::RR::Classes.valid?(256)
78
+ assert_raises(ArgumentError) { Net::DNS::RR::Classes.valid?(Hash.new) }
47
79
  end
48
80
 
49
- def test_regexp
50
- assert_equal(@regexp_string, Net::DNS::RR::Classes.regexp)
51
- end
52
-
53
- def test_valid
54
- assert_equal(true, Net::DNS::RR::Classes.valid?("IN"))
55
- assert_equal(true, Net::DNS::RR::Classes.valid?(1))
56
- assert_equal(false, Net::DNS::RR::Classes.valid?("Q"))
57
- assert_equal(false, Net::DNS::RR::Classes.valid?(256))
58
- assert_raise(ClassArgumentError) do
59
- Net::DNS::RR::Classes.valid? Hash.new
60
- end
61
- end
62
-
63
- def test_to_str
64
- assert_equal("IN", Net::DNS::RR::Classes.to_str(1))
65
- assert_raise(ClassArgumentError) do
66
- Net::DNS::RR::Classes.to_str(256)
67
- end
68
- assert_raise(ClassArgumentError) do
69
- Net::DNS::RR::Classes.to_str("string")
70
- end
81
+ def test_self_regexp
82
+ assert_equal @regexp_string, Net::DNS::RR::Classes.regexp
71
83
  end
72
84
 
73
85
  end
@@ -0,0 +1,97 @@
1
+ require 'test_helper'
2
+ require 'net/dns/rr'
3
+
4
+ class RRCNAMETest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @rr_name = "www.google.com."
8
+ @rr_type = "CNAME"
9
+ @rr_cls = "IN"
10
+ @rr_ttl = 550317
11
+ @rr_value = "www.l.google.com."
12
+ @rr_cname = @rr_value
13
+
14
+ @rr_output = "www.google.com. 550317 IN CNAME www.l.google.com."
15
+
16
+ @rr = Net::DNS::RR::CNAME.new(:name => @rr_name, :cname => @rr_cname, :ttl => @rr_ttl)
17
+ end
18
+
19
+
20
+ def test_initialize_from_hash
21
+ @record = Net::DNS::RR::CNAME.new(:name => @rr_name, :cname => @rr_value, :ttl => @rr_ttl)
22
+ assert_equal @rr_output, @record.to_s
23
+ assert_equal @rr_name, @record.name
24
+ assert_equal @rr_type, @record.type
25
+ assert_equal @rr_cls, @record.cls
26
+ assert_equal @rr_ttl, @record.ttl
27
+ assert_equal @rr_cname, @record.cname
28
+ assert_equal @rr_value, @record.value
29
+ end
30
+
31
+ def test_initialize_from_string
32
+ @record = Net::DNS::RR::CNAME.new("#{@rr_name} #{@rr_ttl} #{@rr_cls} #{@rr_type} #{@rr_value}")
33
+ assert_equal @rr_output, @record.to_s
34
+ assert_equal @rr_name, @record.name
35
+ assert_equal @rr_type, @record.type
36
+ assert_equal @rr_cls, @record.cls
37
+ assert_equal @rr_ttl, @record.ttl
38
+ assert_equal @rr_cname, @record.cname
39
+ assert_equal @rr_value, @record.value
40
+ end
41
+
42
+ def test_parse
43
+ data = "\003www\006google\003com\000\000\005\000\001\000\be\255\000\022\003www\001l\006google\003com\000"
44
+ @record = Net::DNS::RR.parse(data)
45
+ assert_equal @rr_output, @record.to_s
46
+ assert_equal @rr_name, @record.name
47
+ assert_equal @rr_type, @record.type
48
+ assert_equal @rr_cls, @record.cls
49
+ assert_equal @rr_ttl, @record.ttl
50
+ assert_equal @rr_cname, @record.cname
51
+ assert_equal @rr_value, @record.value
52
+ end
53
+
54
+
55
+ InvalidArguments = [
56
+ # FIXME: { :name => "google.com", :cname => "foo___bar" },
57
+ # FIXME: { :name => "google.com", :cname => "foo$bar" },
58
+ { :name => "google.com" },
59
+ Object.new,
60
+ Array.new(7),
61
+ "10800 IN CNAME",
62
+ "google.com. 10800 IN CNAME",
63
+ ]
64
+
65
+ InvalidArguments.each_with_index do |arguments, index|
66
+ define_method "test_initialize_should_raise_with_invalid_arguments_#{index}" do
67
+ assert_raises(ArgumentError) { p Net::DNS::RR::CNAME.new(arguments) }
68
+ end
69
+ end
70
+
71
+
72
+ def test_cname_getter
73
+ assert_equal @rr_cname, @rr.cname
74
+ end
75
+
76
+
77
+ def test_value
78
+ assert_equal @rr_value, @rr.value
79
+ end
80
+
81
+
82
+ def test_inspect
83
+ assert_equal "www.google.com. 550317 IN CNAME www.l.google.com.",
84
+ @rr.inspect
85
+ end
86
+
87
+ def test_to_s
88
+ assert_equal "www.google.com. 550317 IN CNAME www.l.google.com.",
89
+ @rr.to_s
90
+ end
91
+
92
+ def test_to_a
93
+ assert_equal ["www.google.com.", 550317, "IN", "CNAME", "www.l.google.com."],
94
+ @rr.to_a
95
+ end
96
+
97
+ end
@@ -0,0 +1,117 @@
1
+ require 'test_helper'
2
+ require 'net/dns/rr'
3
+ require 'net/dns/rr/hinfo'
4
+
5
+ class RRHINFOTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @rr_name = ""
9
+ @rr_type = "HINFO"
10
+ @rr_cls = "IN"
11
+ @rr_ttl = nil
12
+ @rr_value = %Q{"PC-Intel-700mhz" "Redhat Linux 7.1"}
13
+ @rr_output = %Q{ IN HINFO "PC-Intel-700mhz" "Redhat Linux 7.1"}
14
+
15
+ @rr_cpu = "PC-Intel-700mhz"
16
+ @rr_os = "Redhat Linux 7.1"
17
+
18
+ @rr = Net::DNS::RR::HINFO.new(:name => @rr_name, :cpu => @rr_cpu, :os => @rr_os)
19
+ end
20
+
21
+
22
+ def test_initialize_from_hash
23
+ @record = Net::DNS::RR::HINFO.new(:name => @rr_name, :cpu => @rr_cpu, :os => @rr_os)
24
+ assert_equal @rr_output, @record.to_s
25
+ assert_equal @rr_name, @record.name
26
+ assert_equal @rr_type, @record.type
27
+ assert_equal @rr_cls, @record.cls
28
+ assert_equal 10800, @record.ttl
29
+ assert_equal @rr_value, @record.value
30
+
31
+ assert_equal @rr_cpu, @record.cpu
32
+ assert_equal @rr_os, @record.os
33
+ end
34
+
35
+ def test_initialize_from_string
36
+ @record = Net::DNS::RR::HINFO.new(%Q{#{@rr_name} #{@rr_ttl} #{@rr_cls} #{@rr_type} PC-Intel-700mhz "Redhat Linux 7.1"})
37
+ assert_equal @rr_output, @record.to_s
38
+ assert_equal @rr_value, @record.value
39
+
40
+ assert_equal @rr_cpu, @record.cpu
41
+ assert_equal @rr_os, @record.os
42
+ end
43
+
44
+ def test_initialize_from_string_without_quotes
45
+ @record = Net::DNS::RR::HINFO.new("#{@rr_name} #{@rr_ttl} #{@rr_cls} #{@rr_type} #{@rr_value}")
46
+ assert_equal @rr_output, @record.to_s
47
+ # FIXME: assert_equal @rr_name, @record.name
48
+ assert_equal @rr_type, @record.type
49
+ assert_equal @rr_cls, @record.cls
50
+ assert_equal 10800, @record.ttl
51
+ assert_equal @rr_value, @record.value
52
+
53
+ assert_equal @rr_cpu, @record.cpu
54
+ assert_equal @rr_os, @record.os
55
+ end
56
+
57
+ # FIXME: Can't get valid data
58
+ # def test_parse
59
+ # data = "\002in\000\000\r\000\001\000\000*0\000!\017PC-Intel-700mhz\020Redhat Linux 7.1"
60
+ # @record = Net::DNS::RR.parse(data)
61
+ # assert_equal @rr_output, @record.to_s
62
+ # assert_equal @rr_name, @record.name
63
+ # assert_equal @rr_type, @record.type
64
+ # assert_equal @rr_cls, @record.cls
65
+ # assert_equal @rr_ttl, @record.ttl
66
+ # assert_equal @rr_value, @record.value
67
+ #
68
+ # assert_equal @rr_cpu, @record.cpu
69
+ # assert_equal @rr_os, @record.os
70
+ # end
71
+
72
+
73
+ InvalidArguments = [
74
+ { :name => "google.com" },
75
+ Object.new,
76
+ Array.new(7),
77
+ "10800 IN HINFO",
78
+ "IN HINFO",
79
+ ]
80
+
81
+ InvalidArguments.each_with_index do |arguments, index|
82
+ define_method "test_initialize_should_raise_with_invalid_arguments_#{index}" do
83
+ assert_raises(ArgumentError) { p Net::DNS::RR::HINFO.new(arguments) }
84
+ end
85
+ end
86
+
87
+
88
+ def test_cpu
89
+ assert_equal @rr_cpu, @rr.cpu
90
+ end
91
+
92
+ def test_os
93
+ assert_equal @rr_os, @rr.os
94
+ end
95
+
96
+
97
+ def test_value
98
+ assert_equal %Q{"PC-Intel-700mhz" "Redhat Linux 7.1"}, @rr.value
99
+ end
100
+
101
+
102
+ def test_inspect
103
+ assert_equal %Q{ IN HINFO "PC-Intel-700mhz" "Redhat Linux 7.1"},
104
+ @rr.inspect
105
+ end
106
+
107
+ def test_to_s
108
+ assert_equal %Q{ IN HINFO "PC-Intel-700mhz" "Redhat Linux 7.1"},
109
+ @rr.to_s
110
+ end
111
+
112
+ def test_to_a
113
+ assert_equal [nil, nil, "IN", "HINFO", %Q{"PC-Intel-700mhz" "Redhat Linux 7.1"}],
114
+ @rr.to_a
115
+ end
116
+
117
+ end