diff --git a/lib/msf/core/opt_enum.rb b/lib/msf/core/opt_enum.rb index 344b45b3a5f6..214cfd324506 100644 --- a/lib/msf/core/opt_enum.rb +++ b/lib/msf/core/opt_enum.rb @@ -20,13 +20,22 @@ def initialize(in_name, attrs = [], def valid?(value = self.value, check_empty: true) return false if check_empty && empty_required_value?(value) return true if value.nil? && !required? + return false if value.nil? - !value.nil? && enums.include?(value.to_s) + if case_sensitive? + enums.include?(value.to_s) + else + enums.map(&:downcase).include?(value.to_s.downcase) + end end def normalize(value = self.value) if valid?(value) && !value.nil? - value.to_s + if case_sensitive? + value.to_s + else + enums.find { |e| e.casecmp? value } + end else nil end @@ -44,6 +53,10 @@ def desc protected + def case_sensitive? + enums.map(&:downcase).uniq.length != enums.uniq.length + end + attr_accessor :desc_string # :nodoc: end end diff --git a/spec/lib/msf/core/opt_enum_spec.rb b/spec/lib/msf/core/opt_enum_spec.rb index fac6e72fd898..de0d02cbbd15 100644 --- a/spec/lib/msf/core/opt_enum_spec.rb +++ b/spec/lib/msf/core/opt_enum_spec.rb @@ -17,6 +17,10 @@ expect(required_optenum.valid?('Bar')).to eq true end + it 'should return true for a value in the list with alternative casing' do + expect(required_optenum.valid?('bar')).to eq true + end + it 'should return false for a nil value' do expect(required_optenum.valid?(nil)).to eq false end @@ -31,6 +35,10 @@ expect(not_required_optenum.valid?('Bar')).to eq true end + it 'should return true for a value in the list with alternative casing' do + expect(not_required_optenum.valid?('bar')).to eq true + end + it 'should return false for a value not in the list' do expect(not_required_optenum.valid?('Snap')).to eq false end @@ -45,6 +53,10 @@ expect(required_optenum.normalize('Bar')).to eq 'Bar' end + it 'should return the value string for a value with alternative casing' do + expect(required_optenum.normalize('bar')).to eq 'Bar' + end + it 'should return nil for a value not in the list' do expect(required_optenum.normalize('Snap')).to eq nil end @@ -59,6 +71,10 @@ expect(not_required_optenum.normalize('Bar')).to eq 'Bar' end + it 'should return the value string for a value with alternative casing' do + expect(not_required_optenum.normalize('bar')).to eq 'Bar' + end + it 'should return nil for a value not in the list' do expect(not_required_optenum.normalize('Snap')).to eq nil end