class TestWorkspacesManagement(unittest.TestCase): def setUp(self): self.couch_uri = CONF.getCouchURI() self.cdm = CouchdbManager(uri=self.couch_uri) wpath = os.path.expanduser("~/.faraday/persistence/") self.fsm = FSManager(wpath) self.wm = WorkspaceManager(mock(ModelController), mock(PluginController)) self._fs_workspaces = [] self._couchdb_workspaces = [] def tearDown(self): self.cleanCouchDatabases() self.cleanFSWorkspaces() # pass def new_random_workspace_name(self): return ("aworkspace" + "".join(random.sample([chr(i) for i in range(65, 90)], 10))).lower() def cleanFSWorkspaces(self): import shutil basepath = os.path.expanduser("~/.faraday/persistence/") for d in self._fs_workspaces: wpath = os.path.join(basepath, d) if os.path.isdir(wpath): shutil.rmtree(wpath) def cleanCouchDatabases(self): try: for wname in self._couchdb_workspaces: self.cdm.removeWorkspace(wname) except Exception as e: print e def test_create_fs_workspace(self): """ Verifies the creation of a filesystem workspace """ wname = self.new_random_workspace_name() self._fs_workspaces.append(wname) self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) self.assertFalse(self.cdm.existWorkspace(wname)) wpath = os.path.expanduser("~/.faraday/persistence/%s" % wname) self.assertTrue(os.path.exists(wpath)) self.assertEquals(WorkspaceOnFS.__name__, self.wm.getWorkspaceType(wname)) def test_create_couch_workspace(self): """ Verifies the creation of a couch workspace """ wname = self.new_random_workspace_name() self._couchdb_workspaces.append(wname) self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnCouch) self.assertTrue(self.cdm.existWorkspace(wname)) wpath = os.path.expanduser("~/.faraday/persistence/%s" % wname) self.assertFalse(os.path.exists(wpath)) self.assertEquals(WorkspaceOnCouch.__name__, self.wm.getWorkspaceType(wname)) def test_delete_couch_workspace(self): """ Verifies the deletion of a couch workspace """ wname = self.new_random_workspace_name() self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnCouch) self.assertTrue(self.cdm.existWorkspace(wname)) #Delete workspace self.wm.removeWorkspace(wname) self.assertFalse(self.cdm.existWorkspace(wname)) def test_delete_fs_workspace(self): """ Verifies the deletion of a filesystem workspace """ wname = self.new_random_workspace_name() self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) wpath = os.path.expanduser("~/.faraday/persistence/%s" % wname) self.assertTrue(os.path.exists(wpath)) #Delete workspace self.wm.removeWorkspace(wname) self.assertFalse(os.path.exists(wpath)) def test_list_workspaces(self): """ Lists FS workspaces and Couch workspaces """ # First create workspaces manually wnamefs = self.new_random_workspace_name() wnamecouch = self.new_random_workspace_name() # FS self.fsm.addWorkspace(wnamefs) # Couch self.cdm.addWorkspace(wnamecouch) # When loading workspaces self.wm.loadWorkspaces() self.assertIn(wnamefs, self.wm.getWorkspacesNames(), 'FS Workspace not loaded') self.assertIn(wnamecouch, self.wm.getWorkspacesNames(), 'Couch Workspace not loaded') self.assertEquals(self.wm.getWorkspaceType(wnamefs), WorkspaceOnFS.__name__, 'Workspace type bad defined') self.assertEquals(self.wm.getWorkspaceType(wnamecouch), WorkspaceOnCouch.__name__, 'Workspace type bad defined') def test_get_workspace(self): """ Create a workspace, now ask for it """ # When wname = self.new_random_workspace_name() workspace = self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) added_workspace = self.wm.getWorkspace(wname) # Then self.assertIsNotNone(workspace, 'Workspace added should not be none') self.assertEquals(workspace, added_workspace, 'Workspace created and added diffier') def test_get_existent_couch_workspace(self): """ Create a workspace in the backend, now ask for it """ # When wname = self.new_random_workspace_name() workspace = self.cdm.addWorkspace(wname) self.wm.loadWorkspaces() added_workspace = self.wm.getWorkspace(wname) # Then self.assertIsNotNone(added_workspace, 'Workspace added should not be none') def test_get_existent_fs_workspace(self): """ Create a workspace in the backend, now ask for it """ # When wname = self.new_random_workspace_name() workspace = self.fsm.addWorkspace(wname) self.wm.loadWorkspaces() added_workspace = self.wm.getWorkspace(wname) # Then self.assertIsNotNone(added_workspace, 'Workspace added should not be none') def test_get_non_existent_workspace(self): """ Retrieve a non existent workspace """ added_workspace = self.wm.getWorkspace('inventado') # Then self.assertIsNone(added_workspace, 'Workspace added should not be none') def test_set_active_workspace(self): ''' create a workspace through the backend, then set it as active ''' wname = self.new_random_workspace_name() workspace = self.fsm.addWorkspace(wname) self.wm.loadWorkspaces() added_workspace = self.wm.getWorkspace(wname) # when self.wm.setActiveWorkspace(added_workspace) self.assertEquals(added_workspace, self.wm.getActiveWorkspace(), 'Active workspace diffiers with expected workspace') self.assertTrue(self.wm.isActive(added_workspace.name), 'Workspace is active flag not set') def test_remove_fs_workspace(self): # First wname = self.new_random_workspace_name() added_workspace = self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) # When self.wm.removeWorkspace(wname) # Then self.assertNotIn(wname, self.fsm.getWorkspacesNames()) def test_remove_couch_workspace(self): # First wname = self.new_random_workspace_name() added_workspace = self.wm.createWorkspace( wname, workspaceClass=WorkspaceOnCouch) # When self.wm.removeWorkspace(wname) # Then self.assertNotIn(wname, self.cdm.getWorkspacesNames()) def test_remove_non_existent_workspace(self): # When self.wm.removeWorkspace('invented') # Then self.assertNotIn('invented', self.cdm.getWorkspacesNames())
class TestWorkspacesManagement(unittest.TestCase): def setUp(self): self.couch_uri = CONF.getCouchURI() self.cdm = CouchdbManager(uri=self.couch_uri) wpath = os.path.expanduser("~/.faraday/persistence/" ) self.fsm = FSManager(wpath) self.wm = WorkspaceManager(mock(ModelController), mock(PluginController)) self._fs_workspaces = [] self._couchdb_workspaces = [] def tearDown(self): self.cleanCouchDatabases() self.cleanFSWorkspaces() # pass def new_random_workspace_name(self): return ("aworkspace" + "".join(random.sample( [chr(i) for i in range(65, 90)], 10))).lower() def cleanFSWorkspaces(self): import shutil basepath = os.path.expanduser("~/.faraday/persistence/") for d in self._fs_workspaces: wpath = os.path.join(basepath, d) if os.path.isdir(wpath): shutil.rmtree(wpath) def cleanCouchDatabases(self): try: for wname in self._couchdb_workspaces: self.cdm.removeWorkspace(wname) except Exception as e: print e def test_create_fs_workspace(self): """ Verifies the creation of a filesystem workspace """ wname = self.new_random_workspace_name() self._fs_workspaces.append(wname) self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) self.assertFalse(self.cdm.existWorkspace(wname)) wpath = os.path.expanduser("~/.faraday/persistence/%s" % wname) self.assertTrue(os.path.exists(wpath)) self.assertEquals(WorkspaceOnFS.__name__, self.wm.getWorkspaceType(wname)) def test_create_couch_workspace(self): """ Verifies the creation of a couch workspace """ wname = self.new_random_workspace_name() self._couchdb_workspaces.append(wname) self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnCouch) self.assertTrue(self.cdm.existWorkspace(wname)) wpath = os.path.expanduser("~/.faraday/persistence/%s" % wname) self.assertFalse(os.path.exists(wpath)) self.assertEquals(WorkspaceOnCouch.__name__, self.wm.getWorkspaceType(wname)) def test_delete_couch_workspace(self): """ Verifies the deletion of a couch workspace """ wname = self.new_random_workspace_name() self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnCouch) self.assertTrue(self.cdm.existWorkspace(wname)) #Delete workspace self.wm.removeWorkspace(wname) self.assertFalse(self.cdm.existWorkspace(wname)) def test_delete_fs_workspace(self): """ Verifies the deletion of a filesystem workspace """ wname = self.new_random_workspace_name() self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) wpath = os.path.expanduser("~/.faraday/persistence/%s" % wname) self.assertTrue(os.path.exists(wpath)) #Delete workspace self.wm.removeWorkspace(wname) self.assertFalse(os.path.exists(wpath)) def test_list_workspaces(self): """ Lists FS workspaces and Couch workspaces """ # First create workspaces manually wnamefs = self.new_random_workspace_name() wnamecouch = self.new_random_workspace_name() # FS self.fsm.addWorkspace(wnamefs) # Couch self.cdm.addWorkspace(wnamecouch) # When loading workspaces self.wm.loadWorkspaces() self.assertIn(wnamefs, self.wm.getWorkspacesNames(), 'FS Workspace not loaded') self.assertIn(wnamecouch, self.wm.getWorkspacesNames(), 'Couch Workspace not loaded') self.assertEquals(self.wm.getWorkspaceType(wnamefs), WorkspaceOnFS.__name__, 'Workspace type bad defined' ) self.assertEquals(self.wm.getWorkspaceType(wnamecouch), WorkspaceOnCouch.__name__, 'Workspace type bad defined') def test_get_workspace(self): """ Create a workspace, now ask for it """ # When wname = self.new_random_workspace_name() workspace = self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) added_workspace = self.wm.getWorkspace(wname) # Then self.assertIsNotNone(workspace, 'Workspace added should not be none') self.assertEquals(workspace, added_workspace, 'Workspace created and added diffier') def test_get_existent_couch_workspace(self): """ Create a workspace in the backend, now ask for it """ # When wname = self.new_random_workspace_name() workspace = self.cdm.addWorkspace(wname) self.wm.loadWorkspaces() added_workspace = self.wm.getWorkspace(wname) # Then self.assertIsNotNone(added_workspace, 'Workspace added should not be none') def test_get_existent_fs_workspace(self): """ Create a workspace in the backend, now ask for it """ # When wname = self.new_random_workspace_name() workspace = self.fsm.addWorkspace(wname) self.wm.loadWorkspaces() added_workspace = self.wm.getWorkspace(wname) # Then self.assertIsNotNone(added_workspace, 'Workspace added should not be none') def test_get_non_existent_workspace(self): """ Retrieve a non existent workspace """ added_workspace = self.wm.getWorkspace('inventado') # Then self.assertIsNone(added_workspace, 'Workspace added should not be none') def test_set_active_workspace(self): ''' create a workspace through the backend, then set it as active ''' wname = self.new_random_workspace_name() workspace = self.fsm.addWorkspace(wname) self.wm.loadWorkspaces() added_workspace = self.wm.getWorkspace(wname) # when self.wm.setActiveWorkspace(added_workspace) self.assertEquals(added_workspace, self.wm.getActiveWorkspace(), 'Active workspace diffiers with expected workspace') self.assertTrue(self.wm.isActive(added_workspace.name), 'Workspace is active flag not set') def test_remove_fs_workspace(self): # First wname = self.new_random_workspace_name() added_workspace = self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnFS) # When self.wm.removeWorkspace(wname) # Then self.assertNotIn(wname, self.fsm.getWorkspacesNames()) def test_remove_couch_workspace(self): # First wname = self.new_random_workspace_name() added_workspace = self.wm.createWorkspace(wname, workspaceClass=WorkspaceOnCouch) # When self.wm.removeWorkspace(wname) # Then self.assertNotIn(wname, self.cdm.getWorkspacesNames()) def test_remove_non_existent_workspace(self): # When self.wm.removeWorkspace('invented') # Then self.assertNotIn('invented', self.cdm.getWorkspacesNames())
class TestWorkspaceManager(unittest.TestCase): """docstring for TestWorspace""" @classmethod def setUpClass(cls): cls.model_controller = controller.ModelController(mock()) api.setUpAPIs(cls.model_controller) cls.couch_uri = CONF.getCouchURI() cls.cdm = CouchdbManager(uri=cls.couch_uri) def setUp(self): self._couchdb_workspaces = [] self.wm = WorkspaceManager(self.model_controller, mock(plcore.PluginController)) def tearDown(self): self.cleanCouchDatabases() def cleanCouchDatabases(self): try: for wname in self._couchdb_workspaces: self.cdm.removeWorkspace(wname) except Exception as e: print e def _test_switch_workspace_with_objects(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") interface1 = create_interface(self, host1, iname="pepito") service1 = create_service(self, host1, interface1) self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.assertNotIn( host1, self.model_controller.getAllHosts(), "Host in controller, should be removed when \ switching workspaces") self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") def _test_remove_active_workspace(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) hosts_ids = [h.getID() for h in self.model_controller.getAllHosts()] self.assertNotIn(host1.getID(), hosts_ids, 'Host not removed while removing active workspace') def _test_remove_active_workspace_fs(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnFS) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) self.assertNotIn(host1, self.model_controller.getAllHosts(), 'Host not removed while removing active workspace') def _test_remove_another_workspace(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() create_host(self, "coquito") self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.wm.removeWorkspace(workspace.name) self.assertNotIn(workspace.name, self.wm.getWorkspacesNames(), "Workspace not removed") self.assertIn(workspace2.name, self.wm.getWorkspacesNames(), "Workspace removed while removing another workspace") def _test_load_workspace_on_couch(self): """ This test case creates a host within the Model Controller context adds an interface to it then adds a VulnWeb""" """ We are going to test this structure: host -> interface1 -> service1 -> vuln_web -> vuln -> note -> service2 -> vuln -> vuln -> vuln -> note -> note -> interface2 -> service3 -> note -> credential -> vuln -> vuln """ workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host = create_host(self) interface = create_interface(self, host, ip="127.0.0.1") interface2 = create_interface(self, host, ip="127.0.0.2") service = create_service(self, host, interface, ports=1) service2 = create_service(self, host, interface, ports=2) service3 = create_service(self, host, interface2, ports=3) vulnweb = ModelObjectVulnWeb(name='VulnWebTest', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vulnweb) vuln = ModelObjectVuln(name='VulnTest', desc='TestDescription', severity='high') vuln2 = ModelObjectVuln(name='VulnTest2', desc='TestDescription', severity='high') vuln3 = ModelObjectVuln(name='VulnTest3', desc='TestDescription', severity='high') vuln4 = ModelObjectVuln(name='VulnTest4', desc='TestDescription', severity='high') vuln5 = ModelObjectVuln(name='VulnTest5', desc='TestDescription', severity='high') vuln6 = ModelObjectVuln(name='VulnTest6', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vuln) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln2) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln3) self.model_controller.addVulnToHostSYNC(host.getID(), vuln4) self.model_controller.addVulnToServiceSYNC(host.getID(), service3.getID(), vuln5) self.model_controller.addVulnToInterfaceSYNC(host.getID(), interface2.getID(), vuln6) note = ModelObjectNote(name='NoteTest', text='TestDescription') note2 = ModelObjectNote(name='NoteTest2', text='TestDescription') note3 = ModelObjectNote(name='NoteTest3', text='TestDescription') note4 = ModelObjectNote(name='NoteTest4', text='TestDescription') self.model_controller.addNoteToServiceSYNC(host.getID(), service.getID(), note) self.model_controller.addNoteToHostSYNC(host.getID(), note2) self.model_controller.addNoteToHostSYNC(host.getID(), note3) self.model_controller.addNoteToServiceSYNC(host.getID(), service3.getID(), note4) cred = ModelObjectCred(username='******', password='******') self.model_controller.addCredToServiceSYNC(host.getID(), service3.getID(), cred) # First, we test if the structure was correctly created # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") # So, now we reload the worskpace and check everything again workspace.load() # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") def test_load_workspace_on_fs(self): """ This test case creates a host within the Model Controller context adds an interface to it then adds a VulnWeb""" """ We are going to test this structure: host -> interface1 -> service1 -> vuln_web -> vuln -> note -> service2 -> vuln -> vuln -> vuln -> note -> note -> interface2 -> service3 -> note -> credential -> vuln -> vuln """ workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnFS) #self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host = create_host(self) interface = create_interface(self, host, ip="127.0.0.1") interface2 = create_interface(self, host, ip="127.0.0.2") service = create_service(self, host, interface, ports=1) service2 = create_service(self, host, interface, ports=2) service3 = create_service(self, host, interface2, ports=3) vulnweb = ModelObjectVulnWeb(name='VulnWebTest', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vulnweb) vuln = ModelObjectVuln(name='VulnTest', desc='TestDescription', severity='high') vuln2 = ModelObjectVuln(name='VulnTest2', desc='TestDescription', severity='high') vuln3 = ModelObjectVuln(name='VulnTest3', desc='TestDescription', severity='high') vuln4 = ModelObjectVuln(name='VulnTest4', desc='TestDescription', severity='high') vuln5 = ModelObjectVuln(name='VulnTest5', desc='TestDescription', severity='high') vuln6 = ModelObjectVuln(name='VulnTest6', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vuln) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln2) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln3) self.model_controller.addVulnToHostSYNC(host.getID(), vuln4) self.model_controller.addVulnToServiceSYNC(host.getID(), service3.getID(), vuln5) self.model_controller.addVulnToInterfaceSYNC(host.getID(), interface2.getID(), vuln6) note = ModelObjectNote(name='NoteTest', text='TestDescription') note2 = ModelObjectNote(name='NoteTest2', text='TestDescription') note3 = ModelObjectNote(name='NoteTest3', text='TestDescription') note4 = ModelObjectNote(name='NoteTest4', text='TestDescription') self.model_controller.addNoteToServiceSYNC(host.getID(), service.getID(), note) self.model_controller.addNoteToHostSYNC(host.getID(), note2) self.model_controller.addNoteToHostSYNC(host.getID(), note3) self.model_controller.addNoteToServiceSYNC(host.getID(), service3.getID(), note4) cred = ModelObjectCred(username='******', password='******') self.model_controller.addCredToServiceSYNC(host.getID(), service3.getID(), cred) # First, we test if the structure was correctly created # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") # So, now we reload the worskpace and check everything again print workspace.name workspace.load() # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created")
class TestWorkspaceCRUD(TestCase): """docstring for TestWorspace""" @classmethod def setUpClass(cls): cls.model_controller = controller.ModelController(mock()) api.setUpAPIs(cls.model_controller) def setUp(self): self.wm = WorkspaceManager(self.model_controller, mock(plcore.PluginController)) def test_switch_workspace_with_objects(self): workspace = self.wm.createWorkspace('test_workspace', workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(workspace) host1 = create_host(self, "coquito") interface1 = create_interface(self, host1, iname="pepito") service1 = create_service(self, host1, interface1) self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") workspace2 = self.wm.createWorkspace('test_workspace2', workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(workspace2) self.assertNotIn(host1, self.model_controller.getAllHosts(), "Host in controller, should be removed when \ switching workspaces") self.wm.setActiveWorkspace(workspace) self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") def test_remove_active_workspace(self): workspace = self.wm.createWorkspace('test_workspace', workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(workspace) host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) self.assertNotIn(host1, self.model_controller.getAllHosts(), 'Host not removed while removing active workspace') def test_remove_active_workspace_fs(self): workspace = self.wm.createWorkspace('test_workspace', workspaceClass=WorkspaceOnFS) self.wm.setActiveWorkspace(workspace) host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) self.assertNotIn(host1, self.model_controller.getAllHosts(), 'Host not removed while removing active workspace') def test_remove_another_workspace(self): workspace = self.wm.createWorkspace('test_workspace', workspaceClass=WorkspaceOnCouch) workspace2 = self.wm.createWorkspace('test_workspace2', workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(workspace) create_host(self, "coquito") self.wm.setActiveWorkspace(workspace2) self.wm.removeWorkspace(workspace.name) self.assertNotIn(workspace.name, self.wm.getWorkspacesNames(), "Workspace not removed") self.assertIn(workspace2.name, self.wm.getWorkspacesNames(), "Workspace removed while removing another workspace")
class TestWorkspaceManager(unittest.TestCase): """docstring for TestWorspace""" @classmethod def setUpClass(cls): cls.model_controller = controller.ModelController(mock()) api.setUpAPIs(cls.model_controller) cls.couch_uri = CONF.getCouchURI() cls.cdm = CouchdbManager(uri=cls.couch_uri) def setUp(self): self._couchdb_workspaces = [] self.wm = WorkspaceManager(self.model_controller, mock(plcore.PluginController)) def tearDown(self): self.cleanCouchDatabases() def cleanCouchDatabases(self): try: for wname in self._couchdb_workspaces: self.cdm.removeWorkspace(wname) except Exception as e: print e def _test_switch_workspace_with_objects(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") interface1 = create_interface(self, host1, iname="pepito") service1 = create_service(self, host1, interface1) self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.assertNotIn(host1, self.model_controller.getAllHosts(), "Host in controller, should be removed when \ switching workspaces") self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") def _test_remove_active_workspace(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) hosts_ids = [h.getID() for h in self.model_controller.getAllHosts()] self.assertNotIn(host1.getID(), hosts_ids, 'Host not removed while removing active workspace') def _test_remove_active_workspace_fs(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnFS) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) self.assertNotIn(host1, self.model_controller.getAllHosts(), 'Host not removed while removing active workspace') def _test_remove_another_workspace(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() create_host(self, "coquito") self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.wm.removeWorkspace(workspace.name) self.assertNotIn(workspace.name, self.wm.getWorkspacesNames(), "Workspace not removed") self.assertIn(workspace2.name, self.wm.getWorkspacesNames(), "Workspace removed while removing another workspace") def _test_load_workspace_on_couch(self): """ This test case creates a host within the Model Controller context adds an interface to it then adds a VulnWeb""" """ We are going to test this structure: host -> interface1 -> service1 -> vuln_web -> vuln -> note -> service2 -> vuln -> vuln -> vuln -> note -> note -> interface2 -> service3 -> note -> credential -> vuln -> vuln """ workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host = create_host(self) interface = create_interface(self, host, ip="127.0.0.1") interface2 = create_interface(self, host, ip="127.0.0.2") service = create_service(self, host, interface, ports=1) service2 = create_service(self, host, interface, ports=2) service3 = create_service(self, host, interface2, ports=3) vulnweb = ModelObjectVulnWeb(name='VulnWebTest', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vulnweb) vuln = ModelObjectVuln(name='VulnTest', desc='TestDescription', severity='high') vuln2 = ModelObjectVuln(name='VulnTest2', desc='TestDescription', severity='high') vuln3 = ModelObjectVuln(name='VulnTest3', desc='TestDescription', severity='high') vuln4 = ModelObjectVuln(name='VulnTest4', desc='TestDescription', severity='high') vuln5 = ModelObjectVuln(name='VulnTest5', desc='TestDescription', severity='high') vuln6 = ModelObjectVuln(name='VulnTest6', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vuln) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln2) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln3) self.model_controller.addVulnToHostSYNC(host.getID(), vuln4) self.model_controller.addVulnToServiceSYNC(host.getID(), service3.getID(), vuln5) self.model_controller.addVulnToInterfaceSYNC(host.getID(), interface2.getID(), vuln6) note = ModelObjectNote(name='NoteTest', text='TestDescription') note2 = ModelObjectNote(name='NoteTest2', text='TestDescription') note3 = ModelObjectNote(name='NoteTest3', text='TestDescription') note4 = ModelObjectNote(name='NoteTest4', text='TestDescription') self.model_controller.addNoteToServiceSYNC(host.getID(), service.getID(), note) self.model_controller.addNoteToHostSYNC(host.getID(), note2) self.model_controller.addNoteToHostSYNC(host.getID(), note3) self.model_controller.addNoteToServiceSYNC(host.getID(), service3.getID(), note4) cred = ModelObjectCred(username='******', password='******') self.model_controller.addCredToServiceSYNC(host.getID(), service3.getID(), cred) # First, we test if the structure was correctly created # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") # So, now we reload the worskpace and check everything again workspace.load() # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") def test_load_workspace_on_fs(self): """ This test case creates a host within the Model Controller context adds an interface to it then adds a VulnWeb""" """ We are going to test this structure: host -> interface1 -> service1 -> vuln_web -> vuln -> note -> service2 -> vuln -> vuln -> vuln -> note -> note -> interface2 -> service3 -> note -> credential -> vuln -> vuln """ workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnFS) #self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host = create_host(self) interface = create_interface(self, host, ip="127.0.0.1") interface2 = create_interface(self, host, ip="127.0.0.2") service = create_service(self, host, interface, ports=1) service2 = create_service(self, host, interface, ports=2) service3 = create_service(self, host, interface2, ports=3) vulnweb = ModelObjectVulnWeb(name='VulnWebTest', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vulnweb) vuln = ModelObjectVuln(name='VulnTest', desc='TestDescription', severity='high') vuln2 = ModelObjectVuln(name='VulnTest2', desc='TestDescription', severity='high') vuln3 = ModelObjectVuln(name='VulnTest3', desc='TestDescription', severity='high') vuln4 = ModelObjectVuln(name='VulnTest4', desc='TestDescription', severity='high') vuln5 = ModelObjectVuln(name='VulnTest5', desc='TestDescription', severity='high') vuln6 = ModelObjectVuln(name='VulnTest6', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vuln) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln2) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln3) self.model_controller.addVulnToHostSYNC(host.getID(), vuln4) self.model_controller.addVulnToServiceSYNC(host.getID(), service3.getID(), vuln5) self.model_controller.addVulnToInterfaceSYNC(host.getID(), interface2.getID(), vuln6) note = ModelObjectNote(name='NoteTest', text='TestDescription') note2 = ModelObjectNote(name='NoteTest2', text='TestDescription') note3 = ModelObjectNote(name='NoteTest3', text='TestDescription') note4 = ModelObjectNote(name='NoteTest4', text='TestDescription') self.model_controller.addNoteToServiceSYNC(host.getID(), service.getID(), note) self.model_controller.addNoteToHostSYNC(host.getID(), note2) self.model_controller.addNoteToHostSYNC(host.getID(), note3) self.model_controller.addNoteToServiceSYNC(host.getID(), service3.getID(), note4) cred = ModelObjectCred(username='******', password='******') self.model_controller.addCredToServiceSYNC(host.getID(), service3.getID(), cred) # First, we test if the structure was correctly created # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") # So, now we reload the worskpace and check everything again print workspace.name workspace.load() # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created")