Commit bbbf7911 by Mariusz Kupidura Committed by GitHub

Refactor tests (#332)

* Move tests to tests/ directory
* Remove unused `tox.ini` file
* Add flake8 compliance
* Add `lint` target to `tests` target in the Makefile
parent 550bcf44
...@@ -10,4 +10,4 @@ if [[ "$(uname -s)" == "Darwin" ]]; then ...@@ -10,4 +10,4 @@ if [[ "$(uname -s)" == "Darwin" ]]; then
source ~/.venv/bin/activate source ~/.venv/bin/activate
fi fi
make tests lint make lint tests
\ No newline at end of file \ No newline at end of file
...@@ -11,7 +11,7 @@ run: ...@@ -11,7 +11,7 @@ run:
docker run -it --rm $(RSF_IMAGE) docker run -it --rm $(RSF_IMAGE)
lint: lint:
flake8 --exclude=__init__.py --ignore=$(FLAKE8_IGNORED_RULES) $(MODULES) flake8 --exclude=__init__.py --ignore=$(FLAKE8_IGNORED_RULES) tests $(MODULES)
tests: clean tests: clean
ifeq ($(MODULES), routersploit) ifeq ($(MODULES), routersploit)
......
from routersploit.test.test_case import RoutersploitTestCase
import unittest
import logging import logging
import unittest
from routersploit.utils import NonStringIterable from routersploit.utils import NonStringIterable
logging.getLogger().addHandler(logging.NullHandler()) logging.getLogger().addHandler(logging.NullHandler())
...@@ -17,7 +16,8 @@ class RoutersploitTestCase(unittest.TestCase): ...@@ -17,7 +16,8 @@ class RoutersploitTestCase(unittest.TestCase):
self.assertIn( self.assertIn(
decorator_name, decorator_name,
decorator_list, decorator_list,
msg="'{}' method should be decorated with 'module_required'".format(function.__name__) msg="'{}' method should be decorated "
"with 'module_required'".format(function.__name__)
) )
def assertIsSubset(self, subset, container): def assertIsSubset(self, subset, container):
......
import unittest
import os import os
import unittest
import pexpect import pexpect
from routersploit.test import RoutersploitTestCase from tests.test_case import RoutersploitTestCase
class RoutersploitCompleterTest(RoutersploitTestCase): class RoutersploitCompleterTest(RoutersploitTestCase):
def __init__(self, methodName='runTest'): def __init__(self, methodName='runTest'):
super(RoutersploitCompleterTest, self).__init__(methodName) super(RoutersploitCompleterTest, self).__init__(methodName)
self.cli_path = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir, os.pardir, 'rsf.py')) self.cli_path = os.path.abspath(
os.path.join(__file__, os.pardir, os.pardir, 'rsf.py')
)
self.raw_prompt = "\033[4mrsf\033[0m > " self.raw_prompt = "\033[4mrsf\033[0m > "
self.module_prompt = lambda x: "\033[4mrsf\033[0m (\033[91m{}\033[0m) > ".format(x) self.module_prompt = lambda \
x: "\033[4mrsf\033[0m (\033[91m{}\033[0m) > ".format(x)
def setUp(self): def setUp(self):
self.rsf = pexpect.spawn('python {}'.format(self.cli_path)) self.rsf = pexpect.spawn('python {}'.format(self.cli_path))
...@@ -32,7 +34,9 @@ class RoutersploitCompleterTest(RoutersploitTestCase): ...@@ -32,7 +34,9 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
def test_raw_commands_no_module(self): def test_raw_commands_no_module(self):
self.rsf.send("\t\t") self.rsf.send("\t\t")
self.assertPrompt('exec exit help search show use \r\n', self.raw_prompt) self.assertPrompt(
'exec exit help search show use \r\n',
self.raw_prompt)
def test_complete_use_raw(self): def test_complete_use_raw(self):
self.rsf.send("u\t\t") self.rsf.send("u\t\t")
...@@ -249,7 +253,8 @@ class RoutersploitCompleterTest(RoutersploitTestCase): ...@@ -249,7 +253,8 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.set_module() self.set_module()
self.rsf.send("show \t\t") self.rsf.send("show \t\t")
self.assertPrompt( self.assertPrompt(
'all creds devices exploits info options scanners\r\n', 'all creds devices exploits '
'info options scanners\r\n',
self.module_prompt('FTP Bruteforce') self.module_prompt('FTP Bruteforce')
) )
......
import unittest
import os import os
import unittest
try: import mock
import unittest.mock as mock
except ImportError:
import mock
from routersploit.test import RoutersploitTestCase from routersploit.exploits import Exploit, GLOBAL_OPTS, Option
from routersploit.exploits import Exploit, Option, GLOBAL_OPTS from tests.test_case import RoutersploitTestCase
def suffix(x): def suffix(x):
...@@ -29,8 +26,10 @@ class TestExploitBar(Exploit): ...@@ -29,8 +26,10 @@ class TestExploitBar(Exploit):
class TestExploitWithValidators(Exploit): class TestExploitWithValidators(Exploit):
doo = Option(default="default_value", description="description_three", validators=suffix) doo = Option(default="default_value", description="description_three",
paa = Option(default="default_value", description="description_three", validators=(suffix, SUFFIX)) validators=suffix)
paa = Option(default="default_value", description="description_three",
validators=(suffix, SUFFIX))
class OptionTest(RoutersploitTestCase): class OptionTest(RoutersploitTestCase):
...@@ -66,7 +65,8 @@ class OptionTest(RoutersploitTestCase): ...@@ -66,7 +65,8 @@ class OptionTest(RoutersploitTestCase):
def test_if_validator_is_applied_in_specific_order(self): def test_if_validator_is_applied_in_specific_order(self):
self.exploit_with_validators.paa = "new_value" self.exploit_with_validators.paa = "new_value"
self.assertEqual(self.exploit_with_validators.paa, "new_value_suffix_SUFFIX") self.assertEqual(self.exploit_with_validators.paa,
"new_value_suffix_SUFFIX")
def test_if_exploit_option_is_picked_up_before_global(self): def test_if_exploit_option_is_picked_up_before_global(self):
GLOBAL_OPTS['doo'] = 'global_doo' GLOBAL_OPTS['doo'] = 'global_doo'
...@@ -85,9 +85,11 @@ class OptionTest(RoutersploitTestCase): ...@@ -85,9 +85,11 @@ class OptionTest(RoutersploitTestCase):
self.assertEqual(self.exploit_with_validators.doo, 'global_doo_suffix') self.assertEqual(self.exploit_with_validators.doo, 'global_doo_suffix')
def test_str_representation(self): def test_str_representation(self):
with mock.patch.object(TestExploitFoo, "__module__", new_callable=mock.PropertyMock) as mock_module: with mock.patch.object(TestExploitFoo, "__module__",
new_callable=mock.PropertyMock) as mock_module:
mock_module.return_value = "routersploit.modules.exploits.foo.bar" mock_module.return_value = "routersploit.modules.exploits.foo.bar"
self.assertEqual(str(TestExploitFoo()), os.path.join('exploits', 'foo', 'bar')) self.assertEqual(str(TestExploitFoo()),
os.path.join('exploits', 'foo', 'bar'))
def test_exploit_options_property(self): def test_exploit_options_property(self):
self.assertEqual(self.exploit_bar.options, ['paa', 'target', 'doo']) self.assertEqual(self.exploit_bar.options, ['paa', 'target', 'doo'])
......
import unittest import unittest
from routersploit.utils import iter_modules from routersploit.utils import iter_modules
from routersploit.test import RoutersploitTestCase from tests.test_case import RoutersploitTestCase
class ModuleTest(RoutersploitTestCase): class ModuleTest(RoutersploitTestCase):
...@@ -17,7 +17,8 @@ class ModuleTest(RoutersploitTestCase): ...@@ -17,7 +17,8 @@ class ModuleTest(RoutersploitTestCase):
self.module = module self.module = module
def __str__(self): def __str__(self):
return " ".join([super(ModuleTest, self).__str__(), self.module.__module__]) return " ".join(
[super(ModuleTest, self).__str__(), self.module.__module__])
@property @property
def module_metadata(self): def module_metadata(self):
......
from __future__ import absolute_import
import unittest import unittest
try: import mock
import unittest.mock as mock
except ImportError:
import mock
from .. import utils from routersploit import utils
from . import RoutersploitTestCase from tests.test_case import RoutersploitTestCase
class UtilsTest(RoutersploitTestCase): class UtilsTest(RoutersploitTestCase):
@mock.patch('os.walk') @mock.patch('os.walk')
def test_load_modules_01(self, mock_walk): def test_load_modules_01(self, mock_walk):
mock_walk.return_value = ( mock_walk.return_value = (
('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'], ['__init__.py', '__init__.pyc']), ('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'],
('/Abs/Path/routersploit/routersploit/modules/creds', [], ['__init__.py', '__init__.pyc', 'ftp_bruteforce.py', 'ftp_bruteforce.pyc']), ['__init__.py', '__init__.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [], ['__init__.py', '__init__.pyc', 'asmax_exploit.py', 'asmax_exploit.pyc']), ('/Abs/Path/routersploit/routersploit/modules/creds', [],
['__init__.py', '__init__.pyc', 'ftp_bruteforce.py',
'ftp_bruteforce.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [],
['__init__.py', '__init__.pyc', 'asmax_exploit.py',
'asmax_exploit.pyc']),
) )
path = 'path/to/module' path = 'path/to/module'
...@@ -35,9 +35,14 @@ class UtilsTest(RoutersploitTestCase): ...@@ -35,9 +35,14 @@ class UtilsTest(RoutersploitTestCase):
@mock.patch('os.walk') @mock.patch('os.walk')
def test_load_modules_import_error_02(self, mock_walk): def test_load_modules_import_error_02(self, mock_walk):
mock_walk.return_value = ( mock_walk.return_value = (
('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'], ['__init__.py', '__init__.pyc']), ('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'],
('/Abs/Path/routersploit/routersploit/modules/creds', [], ['__init__.py', '__init__.pyc', 'ftp_bruteforce.py', 'ftp_bruteforce.pyc']), ['__init__.py', '__init__.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [], ['__init__.py', '__init__.pyc', 'asmax_exploit.py', 'asmax_exploit.pyc', 'asmax_multi.py', 'asmax_multi.pyc']), ('/Abs/Path/routersploit/routersploit/modules/creds', [],
['__init__.py', '__init__.pyc', 'ftp_bruteforce.py',
'ftp_bruteforce.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [],
['__init__.py', '__init__.pyc', 'asmax_exploit.py',
'asmax_exploit.pyc', 'asmax_multi.py', 'asmax_multi.pyc']),
) )
path = 'path/to/module' path = 'path/to/module'
......
import unittest import unittest
try: import mock
import unittest.mock as mock
except ImportError:
import mock
from routersploit.test import RoutersploitTestCase
from routersploit import validators from routersploit import validators
from routersploit.exceptions import OptionValidationError from routersploit.exceptions import OptionValidationError
from tests.test_case import RoutersploitTestCase
class ValidatorsTest(RoutersploitTestCase): class ValidatorsTest(RoutersploitTestCase):
...@@ -15,10 +12,12 @@ class ValidatorsTest(RoutersploitTestCase): ...@@ -15,10 +12,12 @@ class ValidatorsTest(RoutersploitTestCase):
self.assertEqual(validators.url("127.0.0.1"), "http://127.0.0.1") self.assertEqual(validators.url("127.0.0.1"), "http://127.0.0.1")
def test_url_already_with_http_prefix(self): def test_url_already_with_http_prefix(self):
self.assertEqual(validators.url("http://127.0.0.1"), "http://127.0.0.1") self.assertEqual(validators.url("http://127.0.0.1"),
"http://127.0.0.1")
def test_url_already_with_https_prefix(self): def test_url_already_with_https_prefix(self):
self.assertEqual(validators.url("https://127.0.0.1"), "https://127.0.0.1") self.assertEqual(validators.url("https://127.0.0.1"),
"https://127.0.0.1")
def test_ipv4_valid_address(self): def test_ipv4_valid_address(self):
address = "127.0.0.1" address = "127.0.0.1"
...@@ -139,7 +138,8 @@ class ValidatorsTest(RoutersploitTestCase): ...@@ -139,7 +138,8 @@ class ValidatorsTest(RoutersploitTestCase):
def test_choice_1(self): def test_choice_1(self):
valid_values = ["test1", "test2"] valid_values = ["test1", "test2"]
selected_value = "test1" selected_value = "test1"
self.assertEqual(validators.choice(valid_values)(selected_value), selected_value) self.assertEqual(validators.choice(valid_values)(selected_value),
selected_value)
def test_choice_2(self): def test_choice_2(self):
valid_values = ["test1", "test2"] valid_values = ["test1", "test2"]
......
[tox]
skipsdist = True
envlist = py27
[testenv]
deps =
mock
pexpect
-r{toxinidir}/requirements.txt
commands=python -m unittest discover
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