Skip to content

Commit 564240a

Browse files
authored
Merge pull request #100 from orgads/no-broken
Fix invalid "Broken registry" warning for UseDomainNameDevolution
2 parents f5ebf38 + bf00ed8 commit 564240a

File tree

2 files changed

+116
-6
lines changed

2 files changed

+116
-6
lines changed

ext/win32/resolv/lib/resolv.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def get_info
8383

8484
unless nvdom.empty?
8585
search = [ nvdom ]
86-
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i
86+
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution', dword: true)
8787
if udmnd != 0
8888
if /^\w+\./ =~ nvdom
8989
devo = $'
@@ -126,17 +126,23 @@ def get_info
126126
[ search.uniq, nameserver.uniq ]
127127
end
128128

129-
def get_item_property(path, name, expand: false)
129+
def get_item_property(path, name, expand: false, dword: false)
130130
if defined?(Win32::Registry)
131-
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
132-
expand ? reg.read_s_expand(name) : reg.read_s(name)
131+
begin
132+
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
133+
if dword
134+
reg.read_i(name)
135+
else
136+
expand ? reg.read_s_expand(name) : reg.read_s(name)
137+
end
138+
end
133139
rescue Registry::Error
134-
""
140+
dword ? 0 : ""
135141
end
136142
else
137143
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty '#{name}'"
138144
output, _ = Open3.capture2('powershell', '-Command', cmd)
139-
output.strip
145+
dword ? output.strip.to_i : output.strip
140146
end
141147
end
142148
end

test/resolv/test_win32_config.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# frozen_string_literal: true
2+
3+
require 'test/unit'
4+
require 'resolv'
5+
6+
class TestWin32Config < Test::Unit::TestCase
7+
def setup
8+
omit 'Win32::Resolv tests only run on Windows' unless RUBY_PLATFORM =~ /mswin|mingw|cygwin/
9+
end
10+
11+
def test_get_item_property_string
12+
# Test reading a string registry value
13+
result = Win32::Resolv.send(:get_item_property,
14+
Win32::Resolv::TCPIP_NT,
15+
'DataBasePath')
16+
17+
# Should return a string (empty or with a path)
18+
assert_instance_of String, result
19+
end
20+
21+
def test_get_item_property_with_expand
22+
# Test reading an expandable string registry value
23+
result = Win32::Resolv.send(:get_item_property,
24+
Win32::Resolv::TCPIP_NT,
25+
'DataBasePath',
26+
expand: true)
27+
28+
# Should return a string with environment variables expanded
29+
assert_instance_of String, result
30+
end
31+
32+
def test_get_item_property_dword
33+
# Test reading a DWORD registry value
34+
result = Win32::Resolv.send(:get_item_property,
35+
Win32::Resolv::TCPIP_NT,
36+
'UseDomainNameDevolution',
37+
dword: true)
38+
39+
# Should return an integer (0 or 1 typically)
40+
assert_kind_of Integer, result
41+
end
42+
43+
def test_get_item_property_nonexistent_key
44+
# Test reading a non-existent registry key
45+
result = Win32::Resolv.send(:get_item_property,
46+
Win32::Resolv::TCPIP_NT,
47+
'NonExistentKeyThatShouldNotExist')
48+
49+
# Should return empty string for non-existent string values
50+
assert_equal '', result
51+
end
52+
53+
def test_get_item_property_nonexistent_key_dword
54+
# Test reading a non-existent registry key as DWORD
55+
result = Win32::Resolv.send(:get_item_property,
56+
Win32::Resolv::TCPIP_NT,
57+
'NonExistentKeyThatShouldNotExist',
58+
dword: true)
59+
60+
# Should return 0 for non-existent DWORD values
61+
assert_equal 0, result
62+
end
63+
64+
def test_get_item_property_search_list
65+
# Test reading SearchList which may exist in the registry
66+
result = Win32::Resolv.send(:get_item_property,
67+
Win32::Resolv::TCPIP_NT,
68+
'SearchList')
69+
70+
# Should return a string (may be empty if not configured)
71+
assert_instance_of String, result
72+
end
73+
74+
def test_get_item_property_nv_domain
75+
# Test reading NV Domain which may exist in the registry
76+
result = Win32::Resolv.send(:get_item_property,
77+
Win32::Resolv::TCPIP_NT,
78+
'NV Domain')
79+
80+
# Should return a string (may be empty if not configured)
81+
assert_instance_of String, result
82+
end
83+
84+
def test_get_item_property_with_invalid_path
85+
# Test with an invalid registry path
86+
result = Win32::Resolv.send(:get_item_property,
87+
'SYSTEM\NonExistent\Path',
88+
'SomeKey')
89+
90+
# Should return empty string for invalid path
91+
assert_equal '', result
92+
end
93+
94+
def test_get_item_property_with_invalid_path_dword
95+
# Test with an invalid registry path as DWORD
96+
result = Win32::Resolv.send(:get_item_property,
97+
'SYSTEM\NonExistent\Path',
98+
'SomeKey',
99+
dword: true)
100+
101+
# Should return 0 for invalid path
102+
assert_equal 0, result
103+
end
104+
end

0 commit comments

Comments
 (0)