Skip to content
Merged
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
18 changes: 12 additions & 6 deletions ext/win32/resolv/lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def get_info

unless nvdom.empty?
search = [ nvdom ]
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution', dword: true)
if udmnd != 0
if /^\w+\./ =~ nvdom
devo = $'
Expand Down Expand Up @@ -126,17 +126,23 @@ def get_info
[ search.uniq, nameserver.uniq ]
end

def get_item_property(path, name, expand: false)
def get_item_property(path, name, expand: false, dword: false)
if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
expand ? reg.read_s_expand(name) : reg.read_s(name)
begin
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
if dword
reg.read_i(name)
else
expand ? reg.read_s_expand(name) : reg.read_s(name)
end
end
rescue Registry::Error
""
dword ? 0 : ""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty '#{name}'"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip
dword ? output.strip.to_i : output.strip
end
end
end
Expand Down
104 changes: 104 additions & 0 deletions test/resolv/test_win32_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# frozen_string_literal: true

require 'test/unit'
require 'resolv'

class TestWin32Config < Test::Unit::TestCase
def setup
omit 'Win32::Resolv tests only run on Windows' unless RUBY_PLATFORM =~ /mswin|mingw|cygwin/
end

def test_get_item_property_string
# Test reading a string registry value
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'DataBasePath')

# Should return a string (empty or with a path)
assert_instance_of String, result
end

def test_get_item_property_with_expand
# Test reading an expandable string registry value
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'DataBasePath',
expand: true)

# Should return a string with environment variables expanded
assert_instance_of String, result
end

def test_get_item_property_dword
# Test reading a DWORD registry value
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'UseDomainNameDevolution',
dword: true)

# Should return an integer (0 or 1 typically)
assert_kind_of Integer, result
end

def test_get_item_property_nonexistent_key
# Test reading a non-existent registry key
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'NonExistentKeyThatShouldNotExist')

# Should return empty string for non-existent string values
assert_equal '', result
end

def test_get_item_property_nonexistent_key_dword
# Test reading a non-existent registry key as DWORD
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'NonExistentKeyThatShouldNotExist',
dword: true)

# Should return 0 for non-existent DWORD values
assert_equal 0, result
end

def test_get_item_property_search_list
# Test reading SearchList which may exist in the registry
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'SearchList')

# Should return a string (may be empty if not configured)
assert_instance_of String, result
end

def test_get_item_property_nv_domain
# Test reading NV Domain which may exist in the registry
result = Win32::Resolv.send(:get_item_property,
Win32::Resolv::TCPIP_NT,
'NV Domain')

# Should return a string (may be empty if not configured)
assert_instance_of String, result
end

def test_get_item_property_with_invalid_path
# Test with an invalid registry path
result = Win32::Resolv.send(:get_item_property,
'SYSTEM\NonExistent\Path',
'SomeKey')

# Should return empty string for invalid path
assert_equal '', result
end

def test_get_item_property_with_invalid_path_dword
# Test with an invalid registry path as DWORD
result = Win32::Resolv.send(:get_item_property,
'SYSTEM\NonExistent\Path',
'SomeKey',
dword: true)

# Should return 0 for invalid path
assert_equal 0, result
end
end