Skip to content

Commit 243c856

Browse files
committed
Merge pull request #1 from jhart-r7/landing-4504-jhart
Unit tests for rapid7#4504
2 parents b121e2c + b2e9e43 commit 243c856

File tree

6 files changed

+105
-14
lines changed

6 files changed

+105
-14
lines changed

lib/msf/ui/console/command_dispatcher/core.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@ def cmd_unload_tabs(str, words)
22982298
end
22992299

23002300
def cmd_get_help
2301-
print_line "Usage: get var1 var2 var3"
2301+
print_line "Usage: get var1 [var2 ...]"
23022302
print_line
23032303
print_line "The get command is used to get the value of one or more variables."
23042304
print_line
@@ -2317,22 +2317,20 @@ def cmd_get(*args)
23172317
global = true
23182318
end
23192319

2320+
# No arguments? No cookie.
2321+
if args.empty?
2322+
global ? cmd_getg_help : cmd_get_help
2323+
return false
2324+
end
2325+
23202326
# Determine which data store we're operating on
2321-
if (active_module and global == false)
2327+
if (active_module && !global)
23222328
datastore = active_module.datastore
23232329
else
23242330
datastore = framework.datastore
23252331
end
23262332

2327-
# No arguments? No cookie.
2328-
if (args.length == 0)
2329-
cmd_get_help
2330-
return false
2331-
end
2332-
2333-
while ((val = args.shift))
2334-
print_line("#{val} => #{datastore[val]}")
2335-
end
2333+
args.each { |var| print_line("#{var} => #{datastore[var]}") }
23362334
end
23372335

23382336
#
@@ -2347,7 +2345,6 @@ def cmd_get_tabs(str, words)
23472345
datastore.keys
23482346
end
23492347

2350-
23512348
def cmd_getg_help
23522349
print_line "Usage: getg var1 [var2 ...]"
23532350
print_line
@@ -2375,8 +2372,6 @@ def cmd_getg_tabs(str, words)
23752372
self.framework.datastore.keys
23762373
end
23772374

2378-
alias cmd_getg_help cmd_get_help
2379-
23802375
def cmd_unset_help
23812376
print_line "Usage: unset [-g] var1 var2 var3 ..."
23822377
print_line
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding:binary -*-
2+
require 'spec_helper'
3+
4+
require 'msf/core/rpc/v10/rpc_base'
5+
require 'msf/core/rpc/v10/rpc_core'
6+
require 'msf/core/rpc/v10/service'
7+
8+
describe Msf::RPC::RPC_Core do
9+
include_context 'Msf::Simple::Framework'
10+
11+
let(:service) do
12+
Msf::RPC::Service.new(framework)
13+
end
14+
15+
let(:core) do
16+
Msf::RPC::RPC_Core.new(service)
17+
end
18+
19+
describe '#rpc_getg' do
20+
it 'should show an empty value if the variable is unset' do
21+
expect(core.rpc_getg('FOO')).to eq({'FOO' => ''})
22+
end
23+
it 'should show the correct value if the variable is set' do
24+
core.rpc_setg('FOO', 'BAR')
25+
expect(core.rpc_getg('FOO')).to eq({'FOO' => 'BAR'})
26+
end
27+
end
28+
end

spec/lib/msf/ui/command_dispatcher/core_spec.rb renamed to spec/lib/msf/ui/console/command_dispatcher/core_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,72 @@ def cell(table, row, column)
9595
end
9696
end
9797
end
98+
99+
it { is_expected.to respond_to :cmd_get }
100+
it { is_expected.to respond_to :cmd_getg }
101+
102+
def set_and_test_variable(name, framework_value, module_value, framework_re, module_re)
103+
# set the current module
104+
allow(core).to receive(:active_module).and_return(mod)
105+
# always assume set variables validate (largely irrelevant because ours are random)
106+
allow(driver).to receive(:on_variable_set).and_return(true)
107+
# the specified global value
108+
core.cmd_setg(name, framework_value) if framework_value
109+
# set the specified local value
110+
core.cmd_set(name, module_value) if module_value
111+
112+
# test the global value if specified
113+
if framework_re
114+
@output = []
115+
core.cmd_getg(name)
116+
@output.join.should =~ framework_re
117+
end
118+
119+
# test the local value if specified
120+
if module_re
121+
@output = []
122+
core.cmd_get(name)
123+
@output.join.should =~ module_re
124+
end
125+
end
126+
127+
describe "#cmd_get and #cmd_getg" do
128+
describe "without arguments" do
129+
it "should show the correct help message" do
130+
core.cmd_get
131+
@output.join.should =~ /Usage: get /
132+
@output = []
133+
core.cmd_getg
134+
@output.join.should =~ /Usage: getg /
135+
end
136+
end
137+
138+
describe "with arguments" do
139+
let(:name) { ::Rex::Text.rand_text_alpha(10).upcase }
140+
141+
context "with an active module" do
142+
let(:mod) do
143+
mod = ::Msf::Module.new
144+
mod.send(:initialize, {})
145+
mod
146+
end
147+
148+
it "should show no value if not set in the framework or module" do
149+
set_and_test_variable(name, nil, nil, /^#{name} => $/, /^#{name} => $/)
150+
end
151+
152+
it "should show the correct value when only the module has this variable" do
153+
set_and_test_variable(name, nil, 'MODULE', /^#{name} => $/, /^#{name} => MODULE$/)
154+
end
155+
156+
it "should show the correct value when only the framework has this variable" do
157+
set_and_test_variable(name, 'FRAMEWORK', nil, /^#{name} => FRAMEWORK$/, /^#{name} => $/)
158+
end
159+
160+
it "should show the correct value when both the module and the framework have this variable" do
161+
set_and_test_variable(name, 'FRAMEWORK', 'MODULE', /^#{name} => FRAMEWORK$/, /^#{name} => MODULE$/)
162+
end
163+
end
164+
end
165+
end
98166
end

0 commit comments

Comments
 (0)