Commit ac339a95 by fwkz

Refactoring 'show' command.

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