Rename targets to devices in the legal keys test. Use utils.iter_modules instead…

Rename targets to devices in the legal keys test. Use utils.iter_modules instead of interpreter internals to load module tests. Flatten the load_tests code to use fewer language features/utilities. Grab module metadata in the same way the interpreter was doing it, but locally in the test case.
parent 38e2e0b4
from inspect import getmodule from inspect import getmodule
from itertools import chain
from unittest import main, TestCase, TestSuite from unittest import main, TestCase, TestSuite
from routersploit.exploits import Exploit from routersploit.exploits import Exploit
from routersploit.interpreter import RoutersploitInterpreter from routersploit.utils import iter_modules
class ModuleTest(TestCase): class ModuleTest(TestCase):
...@@ -25,7 +24,7 @@ class ModuleTest(TestCase): ...@@ -25,7 +24,7 @@ class ModuleTest(TestCase):
legal_keys = set([ legal_keys = set([
"name", "name",
"description", "description",
"targets", "devices",
"authors", "authors",
"references"]) "references"])
...@@ -35,31 +34,26 @@ class ModuleTest(TestCase): ...@@ -35,31 +34,26 @@ class ModuleTest(TestCase):
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
"""Map every module to a test case, and group them into a suite.""" """Map every module to a test case, and group them into a suite."""
def tests(): suite = TestSuite()
# let interpreter load the modules
interpreter = RoutersploitInterpreter()
for module_path in interpreter.modules: for m in iter_modules():
# use the given module class ParametrizedModuleTest(ModuleTest):
interpreter.command_use(module_path)
class ParametrizedModuleTest(ModuleTest): # bind module
module = m()
# bind module and metadata @property
module = interpreter.current_module def metadata(self):
metadata = interpreter.module_metadata return getattr(self.module, "_{}__info__".format(self.module.__class__.__name__))
def shortDescription(self): def shortDescription(self):
# provide the module name in the test description # provide the module name in the test description
return getmodule(self.module).__name__ return getmodule(self.module).__name__
# yield the tests from this test case # add the tests from this test case
yield loader.loadTestsFromTestCase(ParametrizedModuleTest) suite.addTests(loader.loadTestsFromTestCase(ParametrizedModuleTest))
suite = TestSuite()
suite.addTests(chain(*tests()))
return suite return suite
if __name__ == '__main__': if __name__ == '__main__':
......
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