Skip to content

Fix querying absolute domain name twice #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
17 changes: 6 additions & 11 deletions lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ def lazy_initialize
if /\./ =~ hostname
@search = [Label.split($')]
else
@search = [[]]
@search = []
end
end

Expand Down Expand Up @@ -1084,23 +1084,18 @@ def nameserver_port
end

def generate_candidates(name)
candidates = nil
name = Name.create(name)
if name.absolute?
candidates = [name]
[name]
else
abs = Name.new(name.to_a)
search = @search.map {|domain| Name.new(name.to_a + domain)}
if @ndots <= name.length - 1
candidates = [Name.new(name.to_a)]
[abs, *search]
else
candidates = []
end
candidates.concat(@search.map {|domain| Name.new(name.to_a + domain)})
fname = Name.create("#{name}.")
if !candidates.include?(fname)
candidates << fname
[*search, abs]
end
end
return candidates
end

InitialTimeout = 5
Expand Down
43 changes: 31 additions & 12 deletions test/resolv/test_dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,37 @@ def with_udp(host, port)
end
end

# [ruby-core:65836]
def test_resolve_with_2_ndots
conf = Resolv::DNS::Config.new :nameserver => ['127.0.0.1'], :ndots => 2
assert conf.single?

candidates = []
conf.resolv('example.com') { |candidate, *args|
candidates << candidate
raise Resolv::DNS::Config::NXDomain
}
n = Resolv::DNS::Name.create 'example.com.'
assert_equal n, candidates.last
def test_conf_ndots
# ndots defaults to 1
conf = Resolv::DNS::Config.new(nameserver: '127.0.0.1', search: ['local'])
conf.lazy_initialize
candidates = conf.generate_candidates('example.com')
assert_equal 2, candidates.size
assert_equal 'example.com', candidates[0].to_s
assert_equal 'example.com.local', candidates[1].to_s

# ndots is 2: search path is used first
conf = Resolv::DNS::Config.new(nameserver: '127.0.0.1', search: ['local'], ndots: 2)
conf.lazy_initialize
candidates = conf.generate_candidates('example.com')
assert_equal 2, candidates.size
assert_equal 'example.com.local', candidates[0].to_s
assert_equal 'example.com', candidates[1].to_s
end

def test_conf_search
conf = Resolv::DNS::Config.new(nameserver: '127.0.0.1')
conf.lazy_initialize
candidates = conf.generate_candidates('example.com')
assert_equal candidates.size, candidates.uniq.size
assert_equal 'example.com', candidates[0].to_s

conf = Resolv::DNS::Config.new(nameserver: '127.0.0.1', search: ['local'])
conf.lazy_initialize
candidates = conf.generate_candidates('example.com')
assert_equal 2, candidates.size
assert_equal 'example.com', candidates[0].to_s
assert_equal 'example.com.local', candidates[1].to_s
end

def test_query_ipv4_address
Expand Down