feature_manager_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # -*- coding:binary -*-
  2. require 'spec_helper'
  3. RSpec.describe Msf::FeatureManager do
  4. let(:mock_features) do
  5. [
  6. {
  7. name: 'filtered_options',
  8. description: 'Add option filtering functionality to Metasploit',
  9. default_value: false
  10. },
  11. {
  12. name: 'new_search_capabilities',
  13. description: 'Add new search capabilities to Metasploit',
  14. default_value: true
  15. }
  16. ]
  17. end
  18. let(:subject) { described_class.send(:new) }
  19. before(:each) do
  20. stub_const('Msf::FeatureManager::DEFAULTS', mock_features)
  21. end
  22. describe '#all' do
  23. let(:expected_features) do
  24. [
  25. {
  26. name: 'filtered_options',
  27. description: 'Add option filtering functionality to Metasploit',
  28. enabled: false
  29. },
  30. {
  31. name: 'new_search_capabilities',
  32. description: 'Add new search capabilities to Metasploit',
  33. enabled: true
  34. }
  35. ]
  36. end
  37. it { expect(subject.all).to eql expected_features }
  38. end
  39. describe '#enabled?' do
  40. it { expect(subject.enabled?('missing_option')).to be false }
  41. it { expect(subject.enabled?('filtered_options')).to be false }
  42. it { expect(subject.enabled?('new_search_capabilities')).to be true }
  43. end
  44. describe '#exists?' do
  45. it { expect(subject.exists?('missing_option')).to be false }
  46. it { expect(subject.exists?('filtered_options')).to be true }
  47. it { expect(subject.exists?('new_search_capabilities')).to be true }
  48. end
  49. describe 'names' do
  50. it { expect(subject.names).to eq ['filtered_options', 'new_search_capabilities'] }
  51. end
  52. describe '#set' do
  53. context 'when a flag is enabled' do
  54. before(:each) do
  55. subject.set('filtered_options', true)
  56. end
  57. it { expect(subject.enabled?('missing_option')).to be false }
  58. it { expect(subject.enabled?('filtered_options')).to be true }
  59. it { expect(subject.enabled?('new_search_capabilities')).to be true }
  60. end
  61. context 'when a flag is disabled' do
  62. before(:each) do
  63. subject.set('new_search_capabilities', false)
  64. end
  65. it { expect(subject.enabled?('missing_option')).to be false }
  66. it { expect(subject.enabled?('filtered_options')).to be false }
  67. it { expect(subject.enabled?('new_search_capabilities')).to be false }
  68. end
  69. context 'when the flag does not exist' do
  70. before(:each) do
  71. subject.set('missing_option', false)
  72. end
  73. it { expect(subject.enabled?('missing_option')).to be false }
  74. it { expect(subject.enabled?('filtered_options')).to be false }
  75. it { expect(subject.enabled?('new_search_capabilities')).to be true }
  76. end
  77. end
  78. describe "#load_config" do
  79. before(:each) do
  80. allow(Msf::Config).to receive(:load).and_return(Rex::Parser::Ini.from_s(config))
  81. subject.load_config
  82. end
  83. context 'when the config file is empty' do
  84. let(:config) do
  85. <<~CONFIG
  86. CONFIG
  87. end
  88. let(:expected_features) do
  89. [
  90. {
  91. name: 'filtered_options',
  92. description: 'Add option filtering functionality to Metasploit',
  93. enabled: false
  94. },
  95. {
  96. name: 'new_search_capabilities',
  97. description: 'Add new search capabilities to Metasploit',
  98. enabled: true
  99. }
  100. ]
  101. end
  102. it { expect(subject.all).to eql expected_features }
  103. end
  104. context 'when there are valid and invalid flags' do
  105. let(:config) do
  106. <<~CONFIG
  107. [framework/features]
  108. new_search_capabilities=false
  109. missing_feature=true
  110. CONFIG
  111. end
  112. let(:expected_features) do
  113. [
  114. {
  115. name: 'filtered_options',
  116. description: 'Add option filtering functionality to Metasploit',
  117. enabled: false
  118. },
  119. {
  120. name: 'new_search_capabilities',
  121. description: 'Add new search capabilities to Metasploit',
  122. enabled: false
  123. }
  124. ]
  125. end
  126. it { expect(subject.all).to eql expected_features }
  127. end
  128. end
  129. describe '#save_config' do
  130. before(:each) do
  131. allow(Msf::Config).to receive(:load).and_return(Rex::Parser::Ini.from_s(config))
  132. allow(Msf::Config).to receive(:save)
  133. end
  134. context 'when there is no existing configuration' do
  135. before(:each) do
  136. subject.save_config
  137. end
  138. let(:config) do
  139. <<~CONFIG
  140. [framework/features]
  141. CONFIG
  142. end
  143. let(:expected_config) do
  144. {
  145. "framework/features" => {}
  146. }
  147. end
  148. it { expect(Msf::Config).to have_received(:save).with(expected_config) }
  149. end
  150. context 'when there is only a missing feature' do
  151. before(:each) do
  152. subject.save_config
  153. end
  154. let(:config) do
  155. <<~CONFIG
  156. [framework/features]
  157. missing_feature=true
  158. CONFIG
  159. end
  160. let(:expected_config) do
  161. {
  162. "framework/features" => { "missing_feature" => "true" }
  163. }
  164. end
  165. it { expect(Msf::Config).to have_received(:save).with(expected_config) }
  166. end
  167. context 'when there are user preferences set' do
  168. before(:each) do
  169. subject.set('new_search_capabilities', true)
  170. subject.save_config
  171. end
  172. let(:config) do
  173. <<~CONFIG
  174. [framework/features]
  175. new_search_capabilities=false
  176. missing_feature=true
  177. CONFIG
  178. end
  179. let(:expected_config) do
  180. {
  181. "framework/features" => { "missing_feature" => "true", "new_search_capabilities"=>"true" }
  182. }
  183. end
  184. it { expect(Msf::Config).to have_received(:save).with(expected_config) }
  185. end
  186. end
  187. end