Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/net/dns/rr/txt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def build_pack
@rdlength = @txt_pack.size
end

def get_inspect
"\"#{@txt}\""
end

def get_data
@txt_pack
end
Expand All @@ -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

Expand Down
85 changes: 85 additions & 0 deletions test/rr/txt_test.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/rr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down