Skip to content
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

Do not return empty strings in hash if option in datastore is nil #19862

Closed
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
2 changes: 2 additions & 0 deletions lib/msf/core/data_store_with_fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def to_s(delim = ' ')
def to_h
datastore_hash = {}
self.keys.each do |k|
next if self[k].nil?

datastore_hash[k.to_s] = self[k].to_s
end
datastore_hash
Expand Down
22 changes: 12 additions & 10 deletions spec/lib/msf/core/data_store_with_fallbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
it 'should have default keyed values' do
expect(subject['foo']).to eq 'foo_value'
expect(subject['bar']).to eq 'bar_value'
expect(subject['empty']).to eq ''
expect(subject['example_nil_opt']).to eq nil
end

it 'should have case-insensitive lookups' do
Expand Down Expand Up @@ -134,7 +136,7 @@
context '#to_h' do
it 'should return a Hash with correct values' do
expected_to_h = opts.fetch(:expected_to_h) do
{ 'foo' => 'foo_value', 'bar' => 'bar_value' }
{ 'foo' => 'foo_value', 'bar' => 'bar_value', 'empty' => '' }
end
expect(subject.to_h).to eq(expected_to_h)
end
Expand Down Expand Up @@ -179,7 +181,7 @@

describe '#import_options_from_hash' do
subject do
hash = { 'foo' => 'foo_value', 'bar' => 'bar_value' }
hash = { 'foo' => 'foo_value', 'bar' => 'bar_value', 'empty' => '' }
s = default_subject
s.import_options_from_hash(hash)
s
Expand All @@ -189,7 +191,7 @@

describe '#import_options_from_s' do
subject do
str = 'foo=foo_value bar=bar_value'
str = 'foo=foo_value bar=bar_value empty='
s = default_subject
s.import_options_from_s(str)
s
Expand Down Expand Up @@ -224,7 +226,8 @@
ini_instance = double group?: true,
:[] => {
'foo' => 'foo_value',
'bar' => 'bar_value'
'bar' => 'bar_value',
'empty' => ''
}
ini_class = double from_file: ini_instance

Expand Down Expand Up @@ -265,14 +268,18 @@
subject['custom_key'] = 'custom_key_value'
subject['OLD_OPTION_NAME'] = 'old_option_name_value'
subject['SMBUser'] = 'smbuser_user'
subject['empty'] = ''
subject['example_nil_value'] = nil
end

it 'should return the set values' do
expected_values = {
'NewOptionName' => 'old_option_name_value',
'custom_key' => 'custom_key_value',
'foo' => 'foo_value',
'SMBUser' => 'smbuser_user'
'SMBUser' => 'smbuser_user',
'empty' => '',
'example_nil_value' => nil
}
expect(subject.user_defined).to eq(expected_values)
end
Expand Down Expand Up @@ -672,8 +679,6 @@
it 'should return a Hash with correct values' do
expected_to_h = {
'SMBDomain' => 'WORKGROUP',
'SMBUser' => '',
'USER_ATTR' => ''
}
expect(subject.to_h).to eq(expected_to_h)
end
Expand All @@ -688,11 +693,8 @@
expected_to_h = {
'NewOptionName' => 'overridden_default_new_option_name',
'SMBDomain' => 'WORKGROUP',
'SMBUser' => '',
'USER_ATTR' => '',
'foo' => 'overridden_default_foo',
'bar' => 'default_bar_value',
'baz' => ''
}
expect(subject.to_h).to eq(expected_to_h)
end
Expand Down