|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import unittest |
| 4 | +import os |
| 5 | +from libnmap import NmapParser, NmapReport |
| 6 | +from libnmap.plugins.backendplugin import NmapBackendPlugin |
| 7 | +from libnmap.plugins.backendpluginFactory import BackendPluginFactory |
| 8 | + |
| 9 | +#define all plugin that need test |
| 10 | +from libnmap.plugins.mongodb import NmapMongoPlugin |
| 11 | +from libnmap.plugins.sqlite import NmapSqlitePlugin |
| 12 | + |
| 13 | + |
| 14 | +class TestNmapBackendPlugin(unittest.TestCase): |
| 15 | + """ |
| 16 | + This testing class will create a testsuite for each plugins |
| 17 | + The following test need to be done : |
| 18 | + - test the factory |
| 19 | + - test all the method of the class NmapBackendPlugin: |
| 20 | + - Verify implmented/notImplemented |
| 21 | + - Verify the behaviour (ie insert must insert) |
| 22 | + Sample of code to implement for a new plugin: |
| 23 | +
|
| 24 | + def %plugintest_factory(self): |
| 25 | + pass |
| 26 | +
|
| 27 | + def %plugintest_insert(self): |
| 28 | + pass |
| 29 | +
|
| 30 | + def %plugintest_update(self): |
| 31 | + pass |
| 32 | +
|
| 33 | + def %plugintest_delete(self): |
| 34 | + pass |
| 35 | +
|
| 36 | + def %plugintest_get(self): |
| 37 | + pass |
| 38 | +
|
| 39 | + def %plugintest_getall(self): |
| 40 | + pass |
| 41 | +
|
| 42 | + def %plugintest_find(self): |
| 43 | + pass |
| 44 | + """ |
| 45 | + def setUp(self): |
| 46 | + fdir = os.path.dirname(os.path.realpath(__file__)) |
| 47 | + self.flist_full = [{'file': "%s/%s" % (fdir, 'files/2_hosts.xml'), |
| 48 | + 'hosts': 2}, |
| 49 | + {'file': "%s/%s" % (fdir, 'files/1_hosts.xml'), |
| 50 | + 'hosts': 1}, |
| 51 | + {'file': "%s/%s" % (fdir, |
| 52 | + 'files/1_hosts_banner_ports_notsyn.xml'), |
| 53 | + 'hosts': 1}, |
| 54 | + {'file': "%s/%s" % (fdir, |
| 55 | + 'files/1_hosts_banner_ports.xml'), |
| 56 | + 'hosts': 1}, |
| 57 | + {'file': "%s/%s" % (fdir, |
| 58 | + 'files/1_hosts_banner.xml'), |
| 59 | + 'hosts': 1}, |
| 60 | + {'file': "%s/%s" % (fdir, |
| 61 | + 'files/2_hosts_version.xml'), |
| 62 | + 'hosts': 2}, |
| 63 | + {'file': "%s/%s" % (fdir, |
| 64 | + 'files/2_tcp_hosts.xml'), |
| 65 | + 'hosts': 2}, |
| 66 | + {'file': "%s/%s" % (fdir, |
| 67 | + 'files/1_hosts_nohostname.xml'), |
| 68 | + 'hosts': 1}] |
| 69 | + self.flist = self.flist_full |
| 70 | + |
| 71 | + def mongo_test_factory(self): |
| 72 | + """Invoke factory and test that the object is of the right classes""" |
| 73 | + #create the backend factory object |
| 74 | + factory = BackendPluginFactory() |
| 75 | + mongodb = factory.create(plugin_name="mongodb") |
| 76 | + self.assertEqual(isinstance(mongodb, NmapBackendPlugin), True) |
| 77 | + self.assertEqual(isinstance(mongodb, NmapMongoPlugin), True) |
| 78 | + self.assertEqual(isinstance(mongodb, NmapSqlitePlugin), False) |
| 79 | + |
| 80 | + def mongo_test_insert(self): |
| 81 | + """"best way to insert is to call save() of nmapreport""" |
| 82 | + for testfile in self.flist: |
| 83 | + fd = open(testfile['file'], 'r') |
| 84 | + s = fd.read() |
| 85 | + fd.close() |
| 86 | + |
| 87 | + nr = NmapReport(NmapParser.parse(s)) |
| 88 | + #create the backend factory object |
| 89 | + factory = BackendPluginFactory() |
| 90 | + mongodb = factory.create(plugin_name="mongodb") |
| 91 | + self.assertNotEqual(nr.save(mongodb),None) |
| 92 | + |
| 93 | + def mongo_test_update(self): |
| 94 | + pass |
| 95 | + |
| 96 | + def mongo_test_delete(self): |
| 97 | + pass |
| 98 | + |
| 99 | + def mongo_test_get(self): |
| 100 | + pass |
| 101 | + |
| 102 | + def mongo_test_getall(self): |
| 103 | + pass |
| 104 | + |
| 105 | + def mongo_test_find(self): |
| 106 | + pass |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + template_test_suite = ['test_factory', |
| 110 | + 'test_insert', |
| 111 | + 'test_update', |
| 112 | + 'test_delete', |
| 113 | + 'test_get', |
| 114 | + 'test_getall', |
| 115 | + 'test_find'] |
| 116 | + |
| 117 | + test_suite = ['mongo_test_factory', 'mongo_test_insert'] |
| 118 | + suite = unittest.TestSuite(map(TestNmapBackendPlugin, test_suite)) |
| 119 | + test_result = unittest.TextTestRunner(verbosity=5).run(suite) |
0 commit comments