option_group_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding:binary -*-
  2. # frozen_string_literal: true
  3. require 'spec_helper'
  4. RSpec.describe Msf::OptionGroup do
  5. subject { described_class.new(name: 'name', description: 'description') }
  6. describe '#add_option' do
  7. let(:option_name) { 'option_name' }
  8. context 'when the option group is empty' do
  9. it 'adds the option' do
  10. subject.add_option(option_name)
  11. expect(subject.option_names.length).to eql(1)
  12. expect(subject.option_names).to include(option_name)
  13. end
  14. end
  15. context 'when the option group contains options' do
  16. subject { described_class.new(name: 'name', description: 'description', option_names: ['existing_option']) }
  17. it 'adds the option' do
  18. subject.add_option(option_name)
  19. expect(subject.option_names.length).to eql(2)
  20. expect(subject.option_names).to include(option_name)
  21. end
  22. end
  23. end
  24. describe '#add_options' do
  25. let(:option_names) { %w[option_name1 option_name2] }
  26. context 'when the option group is empty' do
  27. it 'adds the option' do
  28. subject.add_options(option_names)
  29. expect(subject.option_names.length).to eql(2)
  30. expect(subject.option_names).to include(*option_names)
  31. end
  32. end
  33. context 'when the option group contains options' do
  34. subject { described_class.new(name: 'name', description: 'description', option_names: ['existing_option']) }
  35. it 'adds the option' do
  36. subject.add_options(option_names)
  37. expect(subject.option_names.length).to eql(3)
  38. expect(subject.option_names).to include(*option_names)
  39. end
  40. end
  41. end
  42. describe '#validate' do
  43. let(:required_option_name) { 'required_name' }
  44. let(:option_names) { ['not_required_name', required_option_name] }
  45. let(:required_names) { [required_option_name] }
  46. let(:options) { instance_double(Msf::OptionContainer) }
  47. let(:datastore) { instance_double(Msf::DataStoreWithFallbacks) }
  48. context 'when there are no required options' do
  49. subject { described_class.new(name: 'name', description: 'description', option_names: option_names) }
  50. context 'when no values are set for the options' do
  51. before(:each) do
  52. allow(options).to receive(:[]).and_return(instance_double(Msf::OptBase))
  53. allow(datastore).to receive(:[]).and_return(nil)
  54. end
  55. it 'validates the options in the group' do
  56. expect { subject.validate(options, datastore) }.not_to raise_error
  57. end
  58. end
  59. context 'when values are set for the options' do
  60. before(:each) do
  61. allow(options).to receive(:[]).and_return(instance_double(Msf::OptBase))
  62. allow(datastore).to receive(:[]).and_return('OptionValue')
  63. end
  64. it 'validates the options in the group' do
  65. expect { subject.validate(options, datastore) }.not_to raise_error
  66. end
  67. end
  68. context 'when the options have not been registered' do
  69. before(:each) do
  70. allow(options).to receive(:[]).and_return(nil)
  71. end
  72. it 'does not attempt to validate the options' do
  73. expect { subject.validate(options, datastore) }.not_to raise_error
  74. end
  75. end
  76. end
  77. context 'when there is a required option' do
  78. subject { described_class.new(name: 'name', description: 'description', option_names: option_names, required_options: required_names) }
  79. let(:error_message) { "The following options failed to validate: #{required_option_name}." }
  80. context 'when no values are set for the options' do
  81. before(:each) do
  82. allow(options).to receive(:[]).and_return(instance_double(Msf::OptBase))
  83. allow(datastore).to receive(:[]).and_return(nil)
  84. end
  85. it 'raises an error only for the required option' do
  86. expect { subject.validate(options, datastore) }.to raise_error(Msf::OptionValidateError).with_message(error_message)
  87. end
  88. end
  89. context 'when values are set for the options' do
  90. before(:each) do
  91. allow(options).to receive(:[]).and_return(instance_double(Msf::OptBase))
  92. allow(datastore).to receive(:[]).and_return('OptionValue')
  93. end
  94. it 'validates the options in the group' do
  95. expect { subject.validate(options, datastore) }.not_to raise_error
  96. end
  97. end
  98. context 'when the options have not been registered' do
  99. before(:each) do
  100. allow(options).to receive(:[]).and_return(nil)
  101. end
  102. it 'does not attempt to validate the options' do
  103. expect { subject.validate(options, datastore) }.not_to raise_error
  104. end
  105. end
  106. end
  107. end
  108. end