Skip to content

Commit

Permalink
Merge branch 'integracion' of git.assembla.com:faraday into integracion
Browse files Browse the repository at this point in the history
  • Loading branch information
dioh committed May 14, 2014
2 parents e4dc901 + 6904add commit 1f0e6a4
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 105 deletions.
101 changes: 0 additions & 101 deletions test_cases/integration_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,107 +121,6 @@ def test_delete_service(self):
self.assertNotIn(service1.getID(), services_ids, \
"Service not deleted")

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))
self.workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch)
self.wm.setActiveWorkspace(self.workspace)
WorkspacePersister.stopThreads()

def tearDown(self):
self.wm.removeWorkspace(self.workspace.name)

def test_switch_workspace_with_objects(self):
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.wm.setActiveWorkspace(workspace2)
WorkspacePersister.stopThreads()

self.assertNotIn(host1, self.model_controller.getAllHosts(),
"Host in controller, should be removed when \
switching workspaces")

self.wm.setActiveWorkspace(self.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!")


# clean up
self.wm.removeWorkspace(workspace2.name)


def test_remove_active_workspace(self):
host1 = create_host(self, "coquito")

workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch)
self.wm.setActiveWorkspace(workspace)
WorkspacePersister.stopThreads()

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.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")


# After clear down
self.wm.removeWorkspace(workspace2.name)


if __name__ == '__main__':
unittest.main()

219 changes: 215 additions & 4 deletions test_cases/workspace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from mockito import mock
from model import api
from model.hosts import Host, Interface, Service
from model.common import (ModelObjectVuln, ModelObjectVulnWeb,
ModelObjectNote, ModelObjectCred)
from model.workspace import WorkspaceOnCouch, WorkspaceManager, WorkspaceOnFS
import random
from persistence.orm import WorkspacePersister
Expand All @@ -33,14 +35,15 @@ def create_host(self, host_name="pepito", os="linux"):
return host


def create_interface(self, host, iname="coqiuto", mac="00:03:00:03:04:04"):
interface = Interface(name=iname, mac=mac)
def create_interface(self, host, iname="coquito", mac="00:03:00:03:04:04",
ip="127.0.0.1"):
interface = Interface(name=iname, mac=mac, ipv4_address=ip)
self.model_controller.addInterfaceSYNC(host.getName(), interface)
return interface


def create_service(self, host, interface, service_name="coquito"):
service = Service(service_name)
def create_service(self, host, interface, service_name="coquito", ports=999):
service = Service(service_name, ports=ports)
self.model_controller.addServiceToInterfaceSYNC(host.getID(),
interface.getID(),
service)
Expand Down Expand Up @@ -154,5 +157,213 @@ def test_remove_another_workspace(self):
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='user', password='pass')

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")


if __name__ == '__main__':
unittest.main()

0 comments on commit 1f0e6a4

Please sign in to comment.