From f8523cb3e2aeadeba2f2f26f83f570377aa4d0bb Mon Sep 17 00:00:00 2001 From: Alan Foster Date: Tue, 18 Aug 2020 16:10:28 +0100 Subject: [PATCH] Add additional tests for edge cases --- lib/msf/core/modules/metadata/search.rb | 1 + spec/lib/msf/core/modules/metadata/search_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/msf/core/modules/metadata/search.rb b/lib/msf/core/modules/metadata/search.rb index c2ef5d4c52..a811392d57 100644 --- a/lib/msf/core/modules/metadata/search.rb +++ b/lib/msf/core/modules/metadata/search.rb @@ -25,6 +25,7 @@ module Msf::Modules::Metadata::Search # {"platform"=>[[], ["android"]]} will exclude modules targeting the android platform # def self.parse_search_string(search_string) + search_string ||= '' search_string += ' ' # Split search terms by space, but allow quoted strings diff --git a/spec/lib/msf/core/modules/metadata/search_spec.rb b/spec/lib/msf/core/modules/metadata/search_spec.rb index 31af466bc4..53d9bb2378 100644 --- a/spec/lib/msf/core/modules/metadata/search_spec.rb +++ b/spec/lib/msf/core/modules/metadata/search_spec.rb @@ -30,6 +30,17 @@ RSpec.describe Msf::Modules::Metadata::Search do allow(subject).to receive(:get_metadata).and_return([mock_module]) end + describe '#parse_search_string' do + it { expect(described_class.parse_search_string(nil)).to eq({}) } + it { expect(described_class.parse_search_string(" ")).to eq({}) } + it { expect(described_class.parse_search_string("os:osx os:windows")).to eq({"os"=>[["osx", "windows"], []]}) } + it { expect(described_class.parse_search_string("postgres login")).to eq({"text"=>[["postgres", "login"], []]}) } + it { expect(described_class.parse_search_string("platform:android")).to eq({"platform"=>[["android"], []]}) } + it { expect(described_class.parse_search_string("platform:-android")).to eq({"platform"=>[[], ["android"]]}) } + it { expect(described_class.parse_search_string("author:egypt arch:x64")).to eq({"author"=>[["egypt"], []], "arch"=>[["x64"], []]}) } + it { expect(described_class.parse_search_string(" author:egypt arch:x64 ")).to eq({"author"=>[["egypt"], []], "arch"=>[["x64"], []]}) } + end + describe '#find' do REF_TYPES = %w(CVE BID EDB) @@ -188,6 +199,10 @@ RSpec.describe Msf::Modules::Metadata::Search do it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo) end + context 'on nil and empty input' do + it_should_behave_like 'search_filter', :accept => [nil, '', ' '], :test_inverse => false + end + context 'when filtering by module #type' do all_module_types = Msf::MODULE_TYPES all_module_types.each do |mtype|