Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewritten and simplified Net::DNS::Classes. Improved test harness.

  • Loading branch information...
commit 5b054be7a56f02b79498f6ee86f14b68c443309c 1 parent 510beda
@weppos weppos authored
View
5 CHANGELOG.rdoc
@@ -1,6 +1,11 @@
= Changelog
+== 0.7.0
+
+* CHANGED: Rewritten and simplified Net::DNS::Classes. Improved test harness.
+
+
== master
* ADDED: Net::DNS::Packet#to_s method (alias of #inspect)
View
13 lib/net/dns/packet.rb
@@ -1,6 +1,6 @@
require 'logger'
-require 'net/dns/names'
require 'net/dns'
+require 'net/dns/names'
require 'net/dns/header'
require 'net/dns/question'
require 'net/dns/rr'
@@ -81,15 +81,6 @@ module DNS
#
# Logger level will be set to <tt>Logger::Debug</tt> if <tt>$DEBUG</tt> variable is set.
#
- # == Error classes
- #
- # Some error classes has been defined for the <tt>Net::DNS::Packet</tt> class,
- # which are listed here to keep a light and browsable main documentation.
- #
- # We have:
- #
- # PacketError:: Generic Packet Error
- #
class Packet
include Names
@@ -119,7 +110,7 @@ class PacketError < Error
#
def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
@header = Net::DNS::Header.new(:qdCount => 1)
- @question = [Net::DNS::Question.new(name,type,cls)]
+ @question = [Net::DNS::Question.new(name, type, cls)]
@answer = []
@authority = []
@additional = []
View
8 lib/net/dns/question.rb
@@ -41,7 +41,7 @@ module DNS
# more or less the same.
#
class Question
- include Net::DNS::Names
+ include Names
# Base error class.
class Error < StandardError
@@ -70,10 +70,10 @@ class NameError < Error
# If not specified, +type+ and +cls+ arguments defaults
# to Net::DNS::A and Net::DNS::IN respectively.
#
- def initialize(name,type=Net::DNS::A,cls=Net::DNS::IN)
+ def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
@qName = check_name name
- @qType = Net::DNS::RR::Types.new type
- @qClass = Net::DNS::RR::Classes.new cls
+ @qType = Net::DNS::RR::Types.new(type)
+ @qClass = Net::DNS::RR::Classes.new(cls)
end
# Return a new Net::DNS::Question object created by
View
2  lib/net/dns/rr.rb
@@ -40,7 +40,7 @@ module DNS
# of an hash. See the Net::DNS::RR.new method for details.
#
class RR
- include Net::DNS::Names
+ include Names
# Base error class.
View
183 lib/net/dns/rr/classes.rb
@@ -4,13 +4,16 @@ module DNS
class RR
#
- # This is an auxiliary class to hadle RR class field in a DNS packet.
+ # = Net::DNS::Classes
+ #
+ # This is an auxiliary class to handle <tt>Net::DNS::RR</tt>
+ # class field in a DNS packet.
#
class Classes
- # An hash with the values of each RR class stored with the
- # respective id number
- Classes = {
+ # Hash with the values of each RR class stored with the
+ # respective id number.
+ CLASSES = {
'IN' => 1, # RFC 1035
'CH' => 3, # RFC 1035
'HS' => 4, # RFC 1035
@@ -19,138 +22,112 @@ class Classes
}
# The default value when class is nil in Resource Records
- @@default = Classes["IN"]
+ @@default = CLASSES["IN"]
- # Be able to control the default class to assign when
- # cls argument is +nil+. Default to +IN+
- def self.default=(str)
- if Classes.has_key? str
- @@default = Classes[str]
- else
- raise ArgumentError, "Unknown class #{str}"
- end
- end
-
- # Checks whether +cls+ is a valid RR class.
- def self.valid?(cls)
- case cls
- when String
- return Classes.has_key?(cls)
- when Fixnum
- return Classes.invert.has_key?(cls)
- else
- raise ArgumentError, "Wrong cls class: #{cls.class}"
- end
- end
-
- # Returns the class in string format, as "IN" or "CH",
- # given the numeric value
- def self.to_str(cls)
- case cls
- when Fixnum
- if Classes.invert.has_key? cls
- return Classes.invert[cls]
- else
- raise ArgumentError, "Unknown class number #{cls}"
- end
- else
- raise ArgumentError, "Wrong cls class: #{cls.class}"
- end
- end
-
- # Gives in output the keys from the +Classes+ hash
- # in a format suited for regexps
- def self.regexp
- Classes.keys.sort.join("|")
- end
# Creates a new object representing an RR class. Performs some
# checks on the argument validity too. Il +cls+ is +nil+, the
# default value is +ANY+ or the one set with Classes.default=
def initialize(cls)
case cls
- when String
- # type in the form "A" or "NS"
- new_from_string(cls.upcase)
- when Fixnum
- # type in numeric form
- new_from_num(cls)
- when nil
- # default type, control with Classes.default=
- @str = Classes.invert[@@default]
- @num = @@default
- else
- raise ArgumentError, "Wrong cls class: #{cls.class}"
+ when String
+ initialize_from_str(cls)
+ when Fixnum
+ initialize_from_num(cls)
+ when nil
+ initialize_from_num(@@default)
end
- end
- # Constructor for string data class,
- # *PRIVATE* method
- def new_from_string(cls)
- case cls
- when /^CLASS\\d+/
- # TODO!!!
- else
- # String with name of class
- if Classes.has_key? cls
- @str = cls
- @num = Classes[cls]
- else
- raise ArgumentError, "Unknown cls #{cls}"
- end
- end
- end
-
- # Contructor for numeric data class
- # *PRIVATE* method
- def new_from_num(cls)
- if Classes.invert.has_key? cls
- @num = cls
- @str = Classes.invert[cls]
- else
- raise ArgumentError, "Unkown cls number #{cls}"
+ if @str.nil? || @num.nil?
+ raise ArgumentError, "Unable to create a `Classes' from `#{cls}'"
end
end
# Returns the class in number format
# (default for normal use)
+ #
+ # FIXME: inspect must return a String.
+ #
def inspect
@num
end
# Returns the class in string format,
- # i.d. "IN" or "CH" or such a string.
+ # ex. "IN" or "CH" or such a string.
def to_s
- @str
+ @str.to_s
end
# Returns the class in numeric format,
- # usable by the pack methods for data transfers
+ # usable by the pack methods for data transfers.
def to_i
@num.to_i
end
- # Should be used only for testing purpouses
- def to_str
- @num.to_s
+
+ # Be able to control the default class to assign when
+ # cls argument is +nil+. Default to +IN+
+ def self.default=(str)
+ if CLASSES[str]
+ @@default = CLASSES[str]
+ else
+ raise ArgumentError, "Unknown class `#{str}'"
+ end
+ end
+
+ # Returns whether <tt>cls</tt> is a valid RR class.
+ #
+ # Net::DNS::RR::Classes.valid?("IN")
+ # # => true
+ # Net::DNS::RR::Classes.valid?(1)
+ # # => true
+ # Net::DNS::RR::Classes.valid?("Q")
+ # # => false
+ # Net::DNS::RR::Classes.valid?(256)
+ # # => false
+ # Net::DNS::RR::Classes.valid?(Hash.new)
+ # # => ArgumentError
+ #
+ # FIXME: valid? should never raise.
+ #
+ # ==== Raises
+ # ArgumentError:: if <tt>cls</tt> isn't either a String or a Fixnum
+ #
+ def self.valid?(cls)
+ case cls
+ when String
+ CLASSES.has_key?(cls)
+ when Fixnum
+ CLASSES.invert.has_key?(cls)
+ else
+ raise ArgumentError, "Wrong cls class: #{cls.class}"
+ end
+ end
+
+ # Gives in output the keys from the +Classes+ hash
+ # in a format suited for regexps
+ def self.regexp
+ CLASSES.keys.sort.join("|")
end
- private :new_from_num, :new_from_string
-<<<<<<< HEAD
+ private
+
+ # Initialize a new instance from a Class name.
+ def initialize_from_str(str)
+ key = str.to_s.upcase
+ @num, @str = CLASSES[key], key
+ end
+
+ # Initialize a new instance from the Class value.
+ def initialize_from_num(num)
+ key = num.to_i
+ @num, @str = key, CLASSES.invert[key]
+ end
+
end
end
end
end
-
-class ClassArgumentError < ArgumentError # :nodoc:
-end
-=======
- end # class Classes
-
- end # class RR
- end # module DNS
-end # module Net
->>>>>>> argumenterror
View
14 test/packet_test.rb
@@ -8,7 +8,7 @@ def setup
@domain = 'example.com'
end
- def test_initialize_from_string
+ def test_initialize
@record = @klass.new(@domain, Net::DNS::MX, Net::DNS::HS)
assert_instance_of @klass, @record
assert_instance_of Net::DNS::Header, @record.header
@@ -19,6 +19,18 @@ def test_initialize_from_string
assert_instance_of Array, @record.additional
end
+ def test_initialize_should_set_question
+ @question = @klass.new(@domain).question.first
+ assert_equal @domain, @question.qName
+ assert_equal Net::DNS::RR::Types.new(Net::DNS::A).to_s, @question.qType.to_s
+ assert_equal Net::DNS::RR::Classes.new(Net::DNS::IN).to_s, @question.qClass.to_s
+
+ @question = @klass.new(@domain, Net::DNS::MX, Net::DNS::HS).question.first
+ assert_equal @domain, @question.qName
+ assert_equal Net::DNS::RR::Types.new(Net::DNS::MX).to_s, @question.qType.to_s
+ assert_equal Net::DNS::RR::Classes.new(Net::DNS::HS).to_s, @question.qClass.to_s
+ end
+
def test_self_parse
packet = "\337M\201\200\000\001\000\003\000\004\000\004\006google\003com\000\000\001\000\001\300\f\000\001\000\001\000\000\001,\000\004@\351\273c\300\f\000\001\000\001\000\000\001,\000\004H\016\317c\300\f\000\001\000\001\000\000\001,\000\004@\351\247c\300\f\000\002\000\001\000\003\364\200\000\006\003ns1\300\f\300\f\000\002\000\001\000\003\364\200\000\006\003ns2\300\f\300\f\000\002\000\001\000\003\364\200\000\006\003ns3\300\f\300\f\000\002\000\001\000\003\364\200\000\006\003ns4\300\f\300X\000\001\000\001\000\003\307\273\000\004\330\357 \n\300j\000\001\000\001\000\003\307\273\000\004\330\357\"\n\300|\000\001\000\001\000\003\307\273\000\004\330\357$\n\300\216\000\001\000\001\000\003\307\273\000\004\330\357&\n"
@record = @klass.parse(packet)
View
110 test/rr/classes_test.rb
@@ -2,72 +2,84 @@
require 'net/dns/rr'
class RRClassesTest < Test::Unit::TestCase
-
+
def setup
@classes = {
- 'IN' => 1, # RFC 1035
- 'CH' => 3, # RFC 1035
- 'HS' => 4, # RFC 1035
- 'NONE' => 254, # RFC 2136
- 'ANY' => 255, # RFC 1035
}
@regexp_string = "ANY|CH|HS|IN|NONE"
end
-
- def test_default
+
+
+ StrAndNum = [
+ ['IN' , 1],
+ ['CH' , 3],
+ ['HS' , 4],
+ ['NONE' , 254],
+ ['ANY' , 255],
+ ]
+
+ StrAndNum.each do |str, num|
+ define_method "test_initialize_from_str_#{str}" do
+ instance = Net::DNS::RR::Classes.new(str)
+ assert_equal str, instance.to_s
+ assert_equal num, instance.to_i
+ end
+ define_method "test_initialize_from_num_#{num}" do
+ instance = Net::DNS::RR::Classes.new(num)
+ assert_equal str, instance.to_s
+ assert_equal num, instance.to_i
+ end
+ end
+
+ def test_initialize_should_raise_with_invalid_class
+ assert_raise(ArgumentError) { Net::DNS::RR::Classes.new(Hash.new) }
+ end
+
+
+ def test_inspect
+ assert_equal 1, Net::DNS::RR::Classes.new(1).inspect
+ assert_equal 1, Net::DNS::RR::Classes.new("IN").inspect
+ end
+
+ def test_to_s
+ assert_equal "IN", Net::DNS::RR::Classes.new(1).to_s
+ assert_equal "IN", Net::DNS::RR::Classes.new("IN").to_s
+ end
+
+ def test_to_i
+ assert_equal 1, Net::DNS::RR::Classes.new(1).to_i
+ assert_equal 1, Net::DNS::RR::Classes.new("IN").to_i
+ end
+
+
+ def test_self_default
# Default type should be ANY => 255
instance = Net::DNS::RR::Classes.new(nil)
- assert_equal("1", instance.to_str)
- assert_equal("IN", instance.to_s)
-
+ assert_equal 1, instance.to_i
+ assert_equal "IN", instance.to_s
+
# Let's change default behaviour
Net::DNS::RR::Classes.default = "CH"
instance = Net::DNS::RR::Classes.new(nil)
- assert_equal("3", instance.to_str)
- assert_equal("CH", instance.to_s)
+ assert_equal 3, instance.to_i
+ assert_equal "CH", instance.to_s
Net::DNS::RR::Classes.default = "IN"
instance = Net::DNS::RR::Classes.new(nil)
- assert_equal("1", instance.to_str)
- assert_equal("IN", instance.to_s)
+ assert_equal 1, instance.to_i
+ assert_equal "IN", instance.to_s
end
- def test_classes
- @classes.each do |key,num|
- instance_from_string = Net::DNS::RR::Classes.new(key)
- instance_from_num = Net::DNS::RR::Classes.new(num)
- assert_equal(key, instance_from_string.to_s)
- assert_equal(num.to_s, instance_from_string.to_str)
- assert_equal(key, instance_from_num.to_s)
- assert_equal(num.to_s, instance_from_num.to_str)
- end
- assert_raise(ArgumentError) do
- Net::DNS::RR::Classes.new(Hash.new)
- end
+ def test_self_valid?
+ assert Net::DNS::RR::Classes.valid?("IN")
+ assert Net::DNS::RR::Classes.valid?(1)
+ assert !Net::DNS::RR::Classes.valid?("Q")
+ assert !Net::DNS::RR::Classes.valid?(256)
+ assert_raise(ArgumentError) { Net::DNS::RR::Classes.valid?(Hash.new) }
end
- def test_regexp
- assert_equal(@regexp_string, Net::DNS::RR::Classes.regexp)
- end
-
- def test_valid
- assert_equal(true, Net::DNS::RR::Classes.valid?("IN"))
- assert_equal(true, Net::DNS::RR::Classes.valid?(1))
- assert_equal(false, Net::DNS::RR::Classes.valid?("Q"))
- assert_equal(false, Net::DNS::RR::Classes.valid?(256))
- assert_raise(ArgumentError) do
- Net::DNS::RR::Classes.valid? Hash.new
- end
- end
-
- def test_to_str
- assert_equal("IN", Net::DNS::RR::Classes.to_str(1))
- assert_raise(ArgumentError) do
- Net::DNS::RR::Classes.to_str(256)
- end
- assert_raise(ArgumentError) do
- Net::DNS::RR::Classes.to_str("string")
- end
+ def test_self_regexp
+ assert_equal @regexp_string, Net::DNS::RR::Classes.regexp
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.