Skip to content

Commit 9207ed6

Browse files
committed
Msf::Ui::Console::CommandDispatcher::Core#search_modules_sql spec
[#47979793]
1 parent 24b9713 commit 9207ed6

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,9 +1455,13 @@ def cmd_search(*args)
14551455

14561456
end
14571457

1458-
def search_modules_sql(match)
1458+
# Prints table of modules matching the search_string.
1459+
#
1460+
# @param (see Msf::DBManager#search_modules)
1461+
# @return [void]
1462+
def search_modules_sql(search_string)
14591463
tbl = generate_module_table("Matching Modules")
1460-
framework.db.search_modules(match).each do |o|
1464+
framework.db.search_modules(search_string).each do |o|
14611465
tbl << [ o.fullname, o.disclosure_date.to_s, RankingName[o.rank].to_s, o.name ]
14621466
end
14631467
print_line(tbl.to_s)

spec/factories/mdm/module_details.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
end
4545

4646
sequence :mdm_module_detail_rank do |n|
47-
(100 * n)
47+
100 * (n % 7)
4848
end
4949

5050
stances = ['active', 'passive']
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
require 'spec_helper'
2+
3+
require 'msf/ui'
4+
require 'msf/ui/console/module_command_dispatcher'
5+
require 'msf/ui/console/command_dispatcher/core'
6+
7+
describe Msf::Ui::Console::CommandDispatcher::Core do
8+
include_context 'Msf::DBManager'
9+
10+
let(:driver) do
11+
mock(
12+
'Driver',
13+
:framework => framework
14+
).tap { |driver|
15+
driver.stub(:on_command_proc=).with(kind_of(Proc))
16+
driver.stub(:print_line).with(kind_of(String))
17+
}
18+
end
19+
20+
subject(:core) do
21+
described_class.new(driver)
22+
end
23+
24+
context '#search_modules_sql' do
25+
def search_modules_sql
26+
core.search_modules_sql(match)
27+
end
28+
29+
let(:match) do
30+
''
31+
end
32+
33+
it 'should generate Matching Modules table' do
34+
core.should_receive(:generate_module_table).with('Matching Modules').and_call_original
35+
36+
search_modules_sql
37+
end
38+
39+
it 'should call Msf::DBManager#search_modules' do
40+
db_manager.should_receive(:search_modules).with(match).and_return([])
41+
42+
search_modules_sql
43+
end
44+
45+
context 'with matching Mdm::ModuleDetails' do
46+
let(:match) do
47+
module_detail.fullname
48+
end
49+
50+
let!(:module_detail) do
51+
FactoryGirl.create(:mdm_module_detail)
52+
end
53+
54+
context 'printed table' do
55+
def cell(table, row, column)
56+
row_line_number = 6 + row
57+
line_number = 0
58+
59+
cell = nil
60+
61+
table.each_line do |line|
62+
if line_number == row_line_number
63+
# strip prefix and postfix
64+
padded_cells = line[3...-1]
65+
cells = padded_cells.split(/\s{2,}/)
66+
67+
cell = cells[column]
68+
break
69+
end
70+
71+
line_number += 1
72+
end
73+
74+
cell
75+
end
76+
77+
let(:printed_table) do
78+
table = ''
79+
80+
core.stub(:print_line) do |string|
81+
table = string
82+
end
83+
84+
search_modules_sql
85+
86+
table
87+
end
88+
89+
it 'should have fullname in first column' do
90+
cell(printed_table, 0, 0).should include(module_detail.fullname)
91+
end
92+
93+
it 'should have disclosure date in second column' do
94+
cell(printed_table, 0, 1).should include(module_detail.disclosure_date.to_s)
95+
end
96+
97+
it 'should have rank name in third column' do
98+
cell(printed_table, 0, 2).should include(Msf::RankingName[module_detail.rank])
99+
end
100+
101+
it 'should have name in fourth column' do
102+
cell(printed_table, 0, 3).should include(module_detail.name)
103+
end
104+
end
105+
end
106+
end
107+
end

0 commit comments

Comments
 (0)