def get_etree_root(doc): """Returns an instance of lxml.etree._Element for the given `doc` input. Args: doc: The input XML document. Can be an instance of ``lxml.etree._Element``, ``lxml.etree._ElementTree``, a file-like object, or a string filename. Returns: An ``lxml.etree._Element`` instance for `doc`. Raises: .ValidationError: If `doc` cannot be found or is not a well-formed XML document. """ try: if isinstance(doc, etree._Element): # noqa root = doc elif isinstance(doc, etree._ElementTree): # noqa root = doc.getroot() else: parser = get_xml_parser() if isinstance(doc, StringIO): tree = etree.parse(BytesIO(doc.getvalue().encode()), parser=parser) else: tree = etree.parse(doc, parser=parser) root = tree.getroot() except Exception as ex: raise errors.ValidationError(str(ex)) return root
def test_identity_from_xml(self): obj = self.klass.from_dict(self._full_dict) sp = STIXPackage() sp.add(obj) s = BytesIO(sp.to_xml()) pkg = STIXPackage.from_xml(s) self.assertTrue("CIQIdentity3.0InstanceType" in text_type(pkg.to_xml()))
def from_dict(cls, d): if not d: return None return_obj = super(OpenIOCTestMechanism, cls).from_dict(d) if 'ioc' in d: parser = mixbox.xml.get_xml_parser() return_obj.ioc = etree.parse(BytesIO(d['ioc']), parser=parser) return return_obj
def _test_xml(self, obj): xml = obj.to_xml() parser = mixbox.xml.get_xml_parser() tree = etree.parse(BytesIO(xml), parser=parser) root = tree.getroot() xpath = "//cyboxCommon:Type" nodes = root.xpath( xpath, namespaces={'cyboxCommon': 'http://cybox.mitre.org/common-2'}) self.assertTrue(nodes is not None) self.assertEqual(len(nodes), 1) self.assertEqual(nodes[0].text, "MD5")
def test_utf16_roundtrip(self): sh = STIXHeader() sh.title = UNICODE_STR sp = STIXPackage() sp.stix_header = sh # serialize as utf-16 xml16 = sp.to_xml(encoding='utf-16') # deserialize as utf-16 sp2 = STIXPackage.from_xml(BytesIO(xml16), encoding='utf-16') sh2 = sp2.stix_header # check that the titles align self.assertEqual(sh.title, sh2.title)
def test_deepcopy(self): """Test copy.deepcopy() against parsed document. Previous versions of python-stix would cause an exception with copy.deepcopy() when applied to a parsed STIX component which contained timestamp information. This was due to the lack of a __reduce__ function defined on the bindings._FixedTZOffset class. """ package = core.STIXPackage.from_xml( BytesIO(core.STIXPackage().to_xml())) copied = copy.deepcopy(package) self.assertEqual(package.timestamp, copied.timestamp)
def _test_xml(self, obj): xml = obj.to_xml() parser = mixbox.xml.get_xml_parser() tree = lxml.etree.parse(BytesIO(xml), parser=parser) root = tree.getroot() xpath = "//stix-openioc:ioc//description" nodes = root.xpath( xpath, namespaces={ 'stix-openioc': 'http://stix.mitre.org/extensions/TestMechanism#OpenIOC2010-1' }) self.assertTrue(nodes is not None) self.assertEqual(len(nodes), 1) self.assertEqual(nodes[0].text, self.DESCRIPTION)
def from_dict(cls, d, return_obj=None): if not d: return None d = d.copy() maec = d.get('maec') if maec is None: pass elif isinstance(maec, dict): d['maec'] = cls._maec_from_dict(maec) elif isinstance(maec, binary_type): d['maec'] = mixbox.xml.get_etree_root(BytesIO(maec)) else: raise TypeError("Unknown type for 'maec' entry.") return_obj = super(MAECInstance, cls).from_dict(d) return return_obj
def test_from_xml_default_encoded(self): utf8_xml = XML.encode('utf-8') sio = BytesIO(utf8_xml) sp = STIXPackage.from_xml(sio) header = sp.stix_header self.assertEqual(header.title, UNICODE_STR)
def test_from_xml_utf16_encoded(self): utf16_xml = XML.encode('utf-16') sio = BytesIO(utf16_xml) sp = STIXPackage.from_xml(sio, encoding='utf-16') header = sp.stix_header self.assertEqual(header.title, UNICODE_STR)