def test_metadata_serialization(self): import time modelobject = ModelObject() modelobject.updateID = lambda *args: 'ModelObjectID' metadata = Metadata('') # {'create_time': 1417205460.253131, # 'creator': 'ninja_owner', # 'owner': 'ninja_owner', # 'update_time': 1417205460.253132, # 'update_user': '******'} modelobjectserialized = self.mapper.serialize(modelobject) metadataserialized = modelobjectserialized['metadata'] # if serialization fails, returns None self.assertNotEqual(modelobjectserialized, None, "Serialized ModelObejct shouldn't be None") # we check the cmd attributes self.assertEquals(modelobjectserialized.get("_id"), modelobject.getID(), "Serialized ID is not the same as metadata ID") self.assertEquals( metadataserialized.get("creator"), metadata.__getattribute__("creator"), "Serialized owner is not the same as metadata creator") self.assertEquals( metadataserialized.get("owner"), metadata.__getattribute__("owner"), "Serialized owner is not the same as metadata owner") self.assertTrue( isinstance(metadataserialized.get("create_time"), float), "Serialized create_time is not int") self.assertTrue( isinstance(metadataserialized.get("update_time"), float), "Serialized update_time is not int")
def test_metadata_serialization(self): import time modelobject = ModelObject() modelobject.updateID = lambda *args : 'ModelObjectID' metadata = Metadata('') # {'create_time': 1417205460.253131, # 'creator': 'ninja_owner', # 'owner': 'ninja_owner', # 'update_time': 1417205460.253132, # 'update_user': '******'} modelobjectserialized = self.mapper.serialize(modelobject) metadataserialized = modelobjectserialized['metadata'] # if serialization fails, returns None self.assertNotEqual( modelobjectserialized, None, "Serialized ModelObejct shouldn't be None") # we check the cmd attributes self.assertEquals( modelobjectserialized.get("_id"), modelobject.getID(), "Serialized ID is not the same as metadata ID") self.assertEquals( metadataserialized.get("creator"), metadata.__getattribute__("creator"), "Serialized owner is not the same as metadata creator") self.assertEquals( metadataserialized.get("owner"), metadata.__getattribute__("owner"), "Serialized owner is not the same as metadata owner") self.assertTrue( isinstance(metadataserialized.get("create_time"), float), "Serialized create_time is not int") self.assertTrue( isinstance(metadataserialized.get("update_time"), float), "Serialized update_time is not int")
def unserialize(self, mobj, doc): self.children = self.findChildren(mobj.getID()) mobj.setName(doc.get("name")) mobj.setOwned(doc.get("owned")) if doc.get("parent", None): mobj.setParent(self.mapper_manager.find(doc.get("parent"))) mobj.setOwner(doc.get("owner")) mobj.setDescription(doc.get("description")) mobj.setMetadata(Metadata('').fromDict(mobj.getMetadata().__dict__)) if self.children: self.setNotes(mobj) self.setVulns(mobj) self.setCreds(mobj) return mobj
def fromDict(self, dict): dict.setdefault("") self.id = dict["_id"] self.name = dict["name"] owned = True if dict.get("owned", "").upper() == "TRUE" else False self.setOwned(owned) parent_id = dict["parent"] self.owner = dict["owner"] self.operating_system = dict["os"] self._default_gateway = dict["default_gateway"].split(",") self.description = dict["description"] self._metadata = Metadata("").fromDict(dict["metadata"]) self.categories = [] for category in dict["categories"]: self.categories.append(category) interfaces = dict["interfaces"] for id, interface in interfaces.items(): ints = Interface() ints.setParent(self) ints.fromDict(interface) self.addInterface(ints) applications = dict["applications"] for id, application in applications.items(): app = HostApplication("") app.setParent(self) app.fromDict(application) self.addApplication(app) for note in dict["notes"]: n = ModelObjectNote("") self.setParent(self) n.fromDict(note) self.addNote(n) for vuln in dict["vulnerabilities"]: v = ModelObjectVuln("") self.setParent(self) v.fromDict(vuln) self.addVuln(v) return True
def unserialize(self, mobj, doc): mobj_type = mobj.class_signature self.children = self.findChildren(mobj.getID()) mobj.setName(doc.get("name")) mobj.setOwned(doc.get("owned")) if doc.get("parent", None): mobj.setParent(self.mapper_manager.find(doc.get("parent"))) mobj.setOwner(doc.get("owner")) # NOTE: Vulnerability and VulnerabilityWeb, when modified from the web, # have a 'desc' key, not a description key, which is already handled # by their specific unserialize method if mobj_type != 'Vulnerability' and mobj_type != 'VulnerabilityWeb': mobj.setDescription(doc.get("description")) mobj.setMetadata(Metadata('').fromDict(mobj.getMetadata().__dict__)) if self.children: self.setNotes(mobj) self.setVulns(mobj) self.setCreds(mobj) return mobj