Skip to content

Commit

Permalink
Add tests for option groups
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelch-r7 committed Feb 21, 2024
1 parent bbcc763 commit 74a9842
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
59 changes: 59 additions & 0 deletions spec/lib/msf/core/module/options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding:binary -*-
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Msf::Module::Options do
subject(:mod) do
mod = ::Msf::Module.new
mod.extend described_class
mod
end

describe '#register_option_group' do
let(:name) { 'name' }
let(:description) { 'description' }
let(:option_names) { %w[option1 option2] }

context 'there are no registered option groups' do
it 'registers a new option group' do
subject.send(:register_option_group, name: name, description: description, option_names: option_names)
expect(subject.options.groups.length).to eq(1)
expect(subject.options.groups.keys).to include(name)
end
end

context 'there is a registered option group' do
let(:existing_name) { 'existing_name' }
let(:existing_options) { ['existing_option_names'] }
let(:existing_description) { 'existing_description' }
before(:each) do
subject.send(:register_option_group, name: existing_name, description: existing_description, option_names: existing_options)
end

it 'registers a an additional option group' do
subject.send(:register_option_group, name: name, description: description, option_names: option_names)
expect(subject.options.groups.length).to eq(2)
expect(subject.options.groups.keys).to include(name, existing_name)
end

context 'when adding a group with the same name' do
it 'merges the option groups together' do
subject.send(:register_option_group, name: existing_name, description: description, option_names: option_names)
expect(subject.options.groups.length).to eq(1)
expect(subject.options.groups.keys).to include(existing_name)
expect(subject.options.groups[existing_name].option_names).to include(*existing_options, *option_names)
expect(subject.options.groups[existing_name].description).to eq(description)
end

it 'overwrites the existing option group' do
subject.send(:register_option_group, name: existing_name, description: description, option_names: option_names, merge: false)
expect(subject.options.groups.length).to eq(1)
expect(subject.options.groups.keys).to include(existing_name)
expect(subject.options.groups[existing_name].option_names).to include(*option_names)
expect(subject.options.groups[existing_name].description).to eq(description)
end
end
end
end
end
56 changes: 56 additions & 0 deletions spec/lib/msf/core/option_container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,60 @@
end
end
end

describe '#add_group' do
subject { described_class.new }
let(:group) { Msf::OptionGroup.new(name: 'name', description: 'description') }

context 'when the container has no groups' do
it 'adds the group to the container' do
subject.add_group(group)
expect(subject.groups[group.name]).to be(group)
end
end

context 'when the container has existing groups' do
let(:existing_group) { Msf::OptionGroup.new(name: 'existing', description: 'existing') }
before(:each) do
subject.add_group(existing_group)
end
it 'adds an additional group to the container' do
subject.add_group(group)
expect(subject.groups.length).to eql(2)
expect(subject.groups[group.name]).to be(group)
end

context 'when adding a new group with an existing groups name' do
let(:group) { Msf::OptionGroup.new(name: existing_group.name, description: 'description') }
it 'overwrites the existing group' do
expect(subject.groups[group.name]).to be(existing_group)
subject.add_group(group)
expect(subject.groups.length).to eql(1)
expect(subject.groups[group.name]).to be(group)
end
end
end
end

describe '#remove_group' do
subject { described_class.new }

context 'when the container has no groups' do
it 'has no effect' do
expect { subject.remove_group('name') }.not_to raise_error
end
end

context 'when the container has existing groups' do
let(:existing_group) { Msf::OptionGroup.new(name: 'existing', description: 'existing') }
before(:each) do
subject.add_group(existing_group)
end

it 'removes the group' do
subject.remove_group(existing_group.name)
expect(subject.groups).to be_empty
end
end
end
end
50 changes: 50 additions & 0 deletions spec/lib/msf/core/option_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding:binary -*-
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Msf::OptionGroup do
subject { described_class.new(name: 'name', description: 'description') }

describe '#add_option' do
let(:option_name) { 'option_name' }
context 'when the option group is empty' do
it 'adds the option' do
subject.add_option(option_name)
expect(subject.option_names.length).to eql(1)
expect(subject.option_names).to include(option_name)
end
end

context 'when the option group contains options' do
subject { described_class.new(name: 'name', description: 'description', option_names: ['existing_option']) }

it 'adds the option' do
subject.add_option(option_name)
expect(subject.option_names.length).to eql(2)
expect(subject.option_names).to include(option_name)
end
end
end

describe '#add_options' do
let(:option_names) { %w[option_name1 option_name2] }
context 'when the option group is empty' do
it 'adds the option' do
subject.add_options(option_names)
expect(subject.option_names.length).to eql(2)
expect(subject.option_names).to include(*option_names)
end
end

context 'when the option group contains options' do
subject { described_class.new(name: 'name', description: 'description', option_names: ['existing_option']) }

it 'adds the option' do
subject.add_options(option_names)
expect(subject.option_names.length).to eql(3)
expect(subject.option_names).to include(*option_names)
end
end
end
end

0 comments on commit 74a9842

Please sign in to comment.