diff --git a/lib/resolv.rb b/lib/resolv.rb index edca92689d..099c7406f3 100644 --- a/lib/resolv.rb +++ b/lib/resolv.rb @@ -451,6 +451,8 @@ def each_name(address) case address when Name ptr = address + when IPv4, IPv6 + ptr = address.to_name when IPv4::Regex ptr = IPv4.create(address).to_name when IPv6::Regex diff --git a/test/resolv/test_dns.rb b/test/resolv/test_dns.rb index 669d86bd83..fe1ab6ce79 100644 --- a/test/resolv/test_dns.rb +++ b/test/resolv/test_dns.rb @@ -265,4 +265,28 @@ def test_no_fd_leak_connected def test_no_fd_leak_unconnected assert_no_fd_leak {Resolv::DNS.new} end + + def test_each_name + dns = Resolv::DNS.new + def dns.each_resource(name, typeclass) + yield typeclass.new(name) + end + + dns.each_name('127.0.0.1') do |ptr| + assert_equal('1.0.0.127.in-addr.arpa', ptr.to_s) + end + dns.each_name(Resolv::IPv4.create('127.0.0.1')) do |ptr| + assert_equal('1.0.0.127.in-addr.arpa', ptr.to_s) + end + dns.each_name('::1') do |ptr| + assert_equal('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa', ptr.to_s) + end + dns.each_name(Resolv::IPv6.create('::1')) do |ptr| + assert_equal('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa', ptr.to_s) + end + dns.each_name(Resolv::DNS::Name.create('1.0.0.127.in-addr.arpa.')) do |ptr| + assert_equal('1.0.0.127.in-addr.arpa', ptr.to_s) + end + assert_raise(Resolv::ResolvError) { dns.each_name('example.com') } + end end