Commit ac339a95 by fwkz

Refactoring 'show' command.

parent a2e5282a
......@@ -352,46 +352,53 @@ class RoutersploitInterpreter(BaseInterpreter):
else:
yield opt_key, opt_value, opt_description
@utils.module_required
def command_show(self, *args, **kwargs):
info, options, devices = 'info', 'options', 'devices'
sub_command = args[0]
if sub_command == info:
utils.pprint_dict_in_order(
def _show_info(self, *args, **kwargs):
utils.pprint_dict_in_order(
self.module_metadata,
("name", "description", "devices", "authors", "references"),
)
utils.print_info()
elif sub_command == options:
target_opts = {'port', 'target'}
module_opts = set(self.current_module.options) - target_opts
headers = ("Name", "Current settings", "Description")
utils.print_info('\nTarget options:')
utils.print_table(headers, *self.get_opts(*target_opts))
if module_opts:
utils.print_info('\nModule options:')
utils.print_table(headers, *self.get_opts(*module_opts))
utils.print_info()
elif sub_command == devices:
if devices in self.current_module._Exploit__info__.keys():
devices = self.current_module._Exploit__info__['devices']
print("\nTarget devices:")
i = 0
for device in devices:
if isinstance(device, dict):
print(" {} - {}".format(i, device['name']))
else:
print(" {} - {}".format(i, device))
i += 1
print()
else:
print("\nTarget devices are not defined")
else:
print("Unknown command 'show {}'. You want to 'show {}' or 'show {}'?".format(sub_command, info, options))
utils.print_info()
def _show_options(self, *args, **kwargs):
target_opts = {'port', 'target'}
module_opts = set(self.current_module.options) - target_opts
headers = ("Name", "Current settings", "Description")
utils.print_info('\nTarget options:')
utils.print_table(headers, *self.get_opts(*target_opts))
if module_opts:
utils.print_info('\nModule options:')
utils.print_table(headers, *self.get_opts(*module_opts))
utils.print_info()
def _show_devices(self, *args, **kwargs): # TODO: cover with tests
try:
devices = self.current_module._Exploit__info__['devices']
print("\nTarget devices:")
i = 0
for device in devices:
if isinstance(device, dict):
print(" {} - {}".format(i, device['name']))
else:
print(" {} - {}".format(i, device))
i += 1
print()
except KeyError:
print("\nTarget devices are not defined")
@utils.module_required
def command_show(self, *args, **kwargs):
sub_commands = ('info', 'options', 'devices')
sub_command = args[0]
try:
getattr(self, "_show_{}".format(sub_command))(*args, **kwargs)
except AttributeError:
utils.print_error("Unknown 'show' sub-command '{}'. "
"What do you want to show?\n"
"Possible choices are: {}".format(sub_command, sub_commands))
@utils.stop_after(2)
def complete_show(self, text, *args, **kwargs):
......
......@@ -356,7 +356,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.assertEqual(self.interpreter.current_module, None)
@mock.patch('__builtin__.print')
def test_command_show_info(self, mock_print):
def test_show_info(self, mock_print):
metadata = {
'devices': 'target_desc',
'authors': 'authors_desc',
......@@ -368,7 +368,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.interpreter.current_module.__doc__ = description
self.interpreter.current_module._MagicMock__info__ = metadata
self.interpreter.command_show('info')
self.interpreter._show_info()
self.assertEqual(
mock_print.mock_calls,
[
......@@ -392,7 +392,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.interpreter.current_module.__doc__ = description
self.interpreter.current_module._MagicMock__info__ = metadata
self.interpreter.command_show('info')
self.interpreter._show_info()
self.assertEqual(
mock_print.mock_calls,
[
......@@ -400,7 +400,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
)
@mock.patch('__builtin__.print')
def test_command_show_options(self, mock_print):
def test_show_options(self, mock_print):
exploit_attributes = {
'target': 'target_desc',
'port': 'port_desc',
......@@ -417,7 +417,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.interpreter.current_module.target = '127.0.0.1'
self.interpreter.current_module.port = 22
self.interpreter.command_show('options')
self.interpreter._show_options()
self.assertEqual(
mock_print.mock_calls,
[
......@@ -452,7 +452,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.interpreter.current_module.target = '127.0.0.1'
self.interpreter.current_module.port = 22
self.interpreter.command_show('options')
self.interpreter._show_options()
self.assertEqual(
mock_print.mock_calls,
[
......@@ -467,15 +467,17 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
]
)
@mock.patch('__builtin__.print')
def test_command_show_unknown_sub_command(self, mock_print):
help_text = "Unknown command 'show unknown_sub_command'. You want to 'show info' or 'show options'?"
def test_command_show(self):
with mock.patch.object(self.interpreter, "_show_options") as mock_show_options:
self.interpreter.command_show("options")
mock_show_options.assert_called_once_with("options")
@mock.patch('routersploit.utils.print_error')
def test_command_show_unknown_sub_command(self, mock_print_error):
self.interpreter.command_show('unknown_sub_command')
self.assertEqual(
mock_print.mock_calls,
[mock.call(help_text)]
)
mock_print_error.assert_called_once_with("Unknown 'show' sub-command 'unknown_sub_command'. "
"What do you want to show?\n"
"Possible choices are: ('info', 'options', 'devices')")
def test_if_command_run_has_module_required_decorator(self):
self.assertIsDecorated(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment