Permalink
Browse files

Standardized MR record.

  • Loading branch information...
1 parent b197d9a commit e69c2363c75f5a73aa4889a1619e9b59f22daa1c @weppos weppos committed May 25, 2010
Showing with 157 additions and 34 deletions.
  1. +52 −34 lib/net/dns/rr/mr.rb
  2. +105 −0 test/rr/mr_test.rb
View
@@ -3,57 +3,75 @@ module DNS
class RR
- #------------------------------------------------------------
- # RR type MR
- #------------------------------------------------------------
+ #
+ # = Mail Rename Record (MR)
+ #
+ # Class for DNS MR resource records.
+ #
class MR < RR
- attr_reader :newname
- private
-
- def check_name(name)
- unless name =~ /(\w\.?)+\s*$/
- raise ArgumentError, "Name not valid: #{name.inspect}"
- end
- name
+ # Gets the newname value.
+ #
+ # Returns a String.
+ def newname
+ @newname
end
- def build_pack
- @newname_pack = pack_name(@newname)
- @rdlength = @newname_pack.size
+ # Gets the standardized value for this record,
+ # represented by the value of <tt>newname</tt>.
+ #
+ # Returns a String.
+ def value
+ newname.to_s
end
- def get_data
- @newname_pack
- end
- def get_inspect
- "#@newname"
- end
+ private
- def subclass_new_from_hash(args)
- if args.has_key? :newname
- @newname = check_name args[:newname]
- else
- raise ArgumentError, ":newname field is mandatory but missing"
+ def subclass_new_from_hash(options)
+ if options.has_key? :newname
+ @newname = check_name(options[:newname])
+ else
+ raise ArgumentError, ":newname field is mandatory"
+ end
end
- end
- def subclass_new_from_string(str)
- @newname = check_name(str)
- end
+ def subclass_new_from_string(str)
+ @newname = check_name(str)
+ end
- def subclass_new_from_array(data,offset)
- @newname = dn_expand(data,offset)
- return offset
- end
+ def subclass_new_from_binary(data, offset)
+ @newname = dn_expand(data,offset)
+ offset
+ end
- private
def set_type
@type = Net::DNS::RR::Types.new("MR")
end
+ def get_inspect
+ value
+ end
+
+
+ def check_name(input)
+ name = input.to_s
+ unless name =~ /(\w\.?)+\s*$/
+ raise ArgumentError, "Invalid Domain Name `#{name}'"
+ end
+ name
+ end
+
+ def build_pack
+ @newname_pack = pack_name(@newname)
+ @rdlength = @newname_pack.size
+ end
+
+ def get_data
+ @newname_pack
+ end
+
end
end
View
@@ -0,0 +1,105 @@
+require 'test_helper'
+require 'net/dns/rr'
+
+class RRMRTest < Test::Unit::TestCase
+
+ def setup
+ @klass = Net::DNS::RR::MR
+ @rr = @klass.new(:name => "eddie.movie.edu.", :newname => "eddie.bornagain.edu.", :ttl => 9000)
+
+ @rr_name = "eddie.movie.edu."
+ @rr_type = "MR"
+ @rr_cls = "IN"
+ @rr_ttl = 9000
+ @rr_newname = "eddie.bornagain.edu."
+ @rr_value = "eddie.bornagain.edu."
+ @rr_output = "eddie.movie.edu. 9000 IN MR eddie.bornagain.edu."
+ end
+
+
+ def test_initialize_from_hash
+ @record = @klass.new(:name => "eddie.movie.edu.", :newname => "eddie.bornagain.edu.", :ttl => 9000)
+ assert_equal @rr_output, @record.inspect
+ assert_equal @rr_name, @record.name
+ assert_equal @rr_type, @record.type
+ assert_equal @rr_cls, @record.cls
+ assert_equal @rr_ttl, @record.ttl
+ assert_equal @rr_newname, @record.newname
+ end
+
+ def test_initialize_from_string
+ @record = @klass.new("eddie.movie.edu. 9000 IN MR eddie.bornagain.edu.")
+ assert_equal @rr_output, @record.inspect
+ assert_equal @rr_name, @record.name
+ assert_equal @rr_type, @record.type
+ assert_equal @rr_cls, @record.cls
+ assert_equal @rr_ttl, @record.ttl
+ assert_equal @rr_newname, @record.newname
+ end
+
+ # def test_parse
+ # data = "\005eddie\005movie\003edu\000\000\t\000\001\000\000#(\000\025\005eddie\tbornagain\003edu\000"
+ # @record = Net::DNS::RR.parse(data)
+ # assert_equal @rr_output, @record.inspect
+ # assert_equal @rr_name, @record.name
+ # assert_equal @rr_type, @record.type
+ # assert_equal @rr_cls, @record.cls
+ # assert_equal @rr_ttl, @record.ttl
+ # assert_equal @rr_newname, @record.newname
+ # end
+
+
+ InvalidArguments = [
+ # FIXME: { :name => "eddie.movie.edu.", :newname => "foo___bar" },
+ # FIXME: { :name => "eddie.movie.edu.", :newname => "foo$bar" },
+ # FIXME: { :name => "eddie.movie.edu", :newname => "eddie.newname.edu." },
+ Object.new,
+ Array.new(7),
+ "9000 IN MR",
+ ]
+
+ InvalidArguments.each_with_index do |arguments, index|
+ define_method "test_initialize_should_raise_with_invalid_arguments_#{index}" do
+ assert_raise(ArgumentError) { @klass.new(arguments) }
+ end
+ end
+
+ def test_initialize_should_raise_with_missing_newname
+ error = assert_raise(ArgumentError) { @klass.new(:name => "eddie.movie.edu.") }
+ assert_match /:newname/, error.message
+ end
+
+
+ def test_value
+ @rr = @klass.new(:name => "eddie.movie.edu.", :newname => "eddie.newname.edu.")
+ assert_equal "eddie.newname.edu.", @rr.value
+
+ @rr = @klass.new(:name => "eddie.movie.edu.", :newname => "eddie.othername.edu.")
+ assert_equal "eddie.othername.edu.", @rr.value
+ end
+
+ def test_newname
+ @rr = @klass.new(:name => "eddie.movie.edu.", :newname => "eddie.newname.edu.")
+ assert_equal "eddie.newname.edu.", @rr.newname
+
+ @rr = @klass.new(:name => "eddie.movie.edu.", :newname => "eddie.othername.edu.")
+ assert_equal "eddie.othername.edu.", @rr.newname
+ end
+
+
+ def test_inspect
+ assert_equal "eddie.movie.edu. 9000 IN MR eddie.bornagain.edu.",
+ @rr.inspect
+ end
+
+ def test_to_s
+ assert_equal "eddie.movie.edu. 9000 IN MR eddie.bornagain.edu.",
+ @rr.to_s
+ end
+
+ def test_to_a
+ assert_equal ["eddie.movie.edu.", 9000, "IN", "MR", "eddie.bornagain.edu."],
+ @rr.to_a
+ end
+
+end

0 comments on commit e69c236

Please sign in to comment.