diff --git a/app/models/condition.rb b/app/models/condition.rb index a53c53db836..3bfbaf762cc 100644 --- a/app/models/condition.rb +++ b/app/models/condition.rb @@ -243,17 +243,15 @@ def self.registry_data(ref, name, ohash) # HKLM\Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders\Common AppData == 'C:\Documents and Settings\All Users\Application Data' # HKLM\Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders : Common AppData == 'C:\Documents and Settings\All Users\Application Data' return nil unless ref.respond_to?("registry_items") + + registry_items = ref.registry_items if ohash[:key_exists] - return ref.registry_items.where("name LIKE ? ESCAPE ''", name + "%").exists? + registry_items.where("name LIKE ? ESCAPE ''", name + "%").exists? elsif ohash[:value_exists] - rec = ref.registry_items.find_by(:name => name) - return !!rec + registry_items.where(:name => name).exists? else - rec = ref.registry_items.find_by(:name => name) + registry_items.find_by(:name => name)&.data end - return nil unless rec - - rec.data end def export_to_array diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index 1238fde4c3e..5b2c8124795 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -172,4 +172,34 @@ expect(result_record).to be(record) end end + + describe ".registry_data" do + let(:name_and_value) { "HKLM\\SOFTWARE\\WindowsFirewall : EnableFirewall" } + let(:registry_item) { FactoryBot.create(:registry_item, :name => name_and_value, :data => "100") } + let(:vm) { FactoryBot.create(:vm_vmware, :registry_items => [registry_item]) } + + it "finds registry item record when key exists" do + expect(described_class.registry_data(vm, 'HKLM', :key_exists => true)).to be_truthy + end + + it "doesn't find registry item record when key exists" do + expect(described_class.registry_data(vm, 'SOFTWARE_X', :key_exists => true)).not_to be_truthy + end + + it "finds registry item record when value exists" do + expect(described_class.registry_data(vm, name_and_value, :value_exists => true)).to be_truthy + end + + it "doesn't find registry item record when value exists" do + expect(described_class.registry_data(vm, name_and_value + "X", :value_exists => true)).not_to be_truthy + end + + it "returns registry data" do + expect(described_class.registry_data(vm, name_and_value, {})).to eq("100") + end + + it "doesn't returns registry data" do + expect(described_class.registry_data(vm, name_and_value + "X", {})).to be_nil + end + end end