1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
from routersploit.utils import iter_modules
from tests.test_case import RoutersploitTestCase
class ModuleTest(RoutersploitTestCase):
"""A test case that every module must pass.
Attributes:
module (Exploit): The exploit instance of the module being tested.
metadata (Dict): The info associated with the module.
"""
def __init__(self, methodName='runTest', module=None):
super(ModuleTest, self).__init__(methodName)
self.module = module
def __str__(self):
return " ".join(
[super(ModuleTest, self).__str__(), self.module.__module__])
@property
def module_metadata(self):
return getattr(self.module, "_{}__info__".format(self.module.__name__))
def test_required_metadata(self):
required_metadata = (
"name",
"description",
"devices",
"authors",
"references"
)
self.assertIsSubset(required_metadata, self.module_metadata.keys())
def test_metadata_type(self):
self.assertIsSequence(self.module_metadata['authors'])
self.assertIsSequence(self.module_metadata['references'])
self.assertIsSequence(self.module_metadata['devices'])
def load_tests(loader, tests, pattern):
""" Map every module to a test case, and group them into a suite. """
suite = unittest.TestSuite()
test_names = loader.getTestCaseNames(ModuleTest)
for module in iter_modules():
suite.addTests([ModuleTest(name, module) for name in test_names])
return suite
if __name__ == '__main__':
unittest.main()