diff --git a/lib/net/dns/rr/txt.rb b/lib/net/dns/rr/txt.rb index d9f4ca7..1696b6e 100644 --- a/lib/net/dns/rr/txt.rb +++ b/lib/net/dns/rr/txt.rb @@ -19,6 +19,10 @@ def build_pack @rdlength = @txt_pack.size end + def get_inspect + "\"#{@txt}\"" + end + def get_data @txt_pack end @@ -33,18 +37,24 @@ def subclass_new_from_hash(args) def subclass_new_from_string(str) @txt = str.strip + if @txt[0] == '"' and @txt[-1] == '"' + @txt = @txt[1, @txt.length-2] + else + raise ArgumentError, "TXT RR data must be enclosed in \"quotes\"" + end end def subclass_new_from_binary(data,offset) off_end = offset + @rdlength - @txt = "" + rs = [] while offset < off_end len = data.unpack("@#{offset} C")[0] offset += 1 str = data[offset..offset+len-1] offset += len - @txt << str << " " + rs << str end + @txt = rs.join(" ") return offset end diff --git a/test/rr/txt_test.rb b/test/rr/txt_test.rb new file mode 100644 index 0000000..d5449e4 --- /dev/null +++ b/test/rr/txt_test.rb @@ -0,0 +1,85 @@ +require 'test_helper' +require 'net/dns/rr' + +class RRTXTTest < Test::Unit::TestCase + + def setup + @rr_name = "example.com." + @rr_type = "TXT" + @rr_cls = "IN" + @rr_ttl = 10800 + @rr_txt = "This is an example TXT record." + + @rr_output = "example.com. 10800 IN TXT \"This is an example TXT record.\"" + + @rr = Net::DNS::RR::TXT.new(:name => "example.com.", :txt => "This is an example TXT record.", :ttl => @rr_ttl) + end + + + def test_initialize_from_hash + @record = Net::DNS::RR::TXT.new(:name => "example.com.", :txt => "This is an example TXT record.") + 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_txt, @record.txt + end + + def test_initialize_from_string + @record = Net::DNS::RR::TXT.new("example.com. 10800 IN TXT \"This is an example TXT record.\"") + 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_txt, @record.txt + end + + def test_parse + data = "\007example\003com\000\000\020\000\001\000\000*0\000\037\036This is an example TXT record." + @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_txt, @record.txt + end + + + InvalidArguments = [ + { :name => "example.com" }, + Object.new, + Array.new(7), + "10800 IN A", + ] + + InvalidArguments.each_with_index do |arguments, index| + define_method "test_initialize_should_raise_with_invalid_arguments_#{index}" do + assert_raises(ArgumentError) { Net::DNS::RR::TXT.new(arguments) } + end + end + + + def test_value + assert_equal "\"This is an example TXT record.\"", @rr.value + end + + + def test_inspect + assert_equal "example.com. 10800 IN TXT \"This is an example TXT record.\"", + @rr.inspect + end + + def test_to_s + assert_equal "example.com. 10800 IN TXT \"This is an example TXT record.\"", + @rr.to_s + end + + def test_to_a + assert_equal ["example.com.", 10800, "IN", "TXT", "\"This is an example TXT record.\""], + @rr.to_a + end + +end diff --git a/test/rr_test.rb b/test/rr_test.rb index 1c7bda8..01c157f 100644 --- a/test/rr_test.rb +++ b/test/rr_test.rb @@ -87,7 +87,7 @@ def test_rdata assert_equal @mx.preference, 10 assert_equal @mx.exchange, "mailhost.example.com." assert_equal @cname.cname, "www1.example.com" - assert_equal @txt.txt, '"text record"' + assert_equal @txt.txt, 'text record' assert_equal @a_binary.address.to_s, "10.1.2.3" assert_equal @mx_binary.preference, 10 assert_equal @mx_binary.exchange, "mailhost.example.com."