Ejemplo n.º 1
0
def validate_profile_string(profile_str,
                            image_dir=None,
                            resolve_entities=True,
                            dtd_validation=False,
                            warn_if_dtd_missing=False):
    ''' Given the profile contained in a string variable, validate
    Args:
        profile_str - profile in string format
        image_dir - path of service image, used to locate service_bundle
            if None, only unit test against local service_bundle(4)
        resolve_entities - if True, ask XML parser to resolve all entities
        dtd_validation - if True, validate against a DTD in the profile
        warn_if_dtd_missing - if True, raise an exception if the DTD not found
    Returns: profile as string with any inclusions
    Exceptions: etree.XMLSyntaxError
    '''
    import lxml.etree as etree
    from StringIO import StringIO

    # create an XMLParser object with settings
    parser = etree.XMLParser(
        # always read DTD for XInclude namespace xi in service_bundle(4)
        load_dtd=True,
        resolve_entities=resolve_entities,
        dtd_validation=False)
    root = etree.parse(StringIO(profile_str), parser)
    if not resolve_entities:  # just check basic XML, no inclusions
        return profile_str
    # validate against DTD if provided
    if dtd_validation and \
            (root.docinfo.externalDTD is not None or
                    root.docinfo.internalDTD is not None):
        # check for service_bundle(4)
        if root.docinfo.system_url is not None and \
                root.docinfo.system_url.find('/service_bundle.dtd.') == -1:
            print >> sys.stderr, _(
                "Warning:  DOCTYPE %s specified instead of service_bundle(4). "
                "The file might not be a profile.") % root.docinfo.system_url
    if image_dir is None:  # unit testing only
        err = validate_profile_external_dtd(profile_str)
        if err:
            raise etree.XMLSyntaxError(err, '', '', '')
        return profile_str
    dtd_file = os.path.join(image_dir, 'auto_install', 'service_bundle.dtd.1')
    # if warning only on DTD missing, and DTD is indeed missing
    if root.docinfo.system_url is not None and warn_if_dtd_missing and \
        not os.path.exists(dtd_file):
        print >> sys.stderr, _(
            "Warning:  DTD %s not found.  Cannot validate completely.") % \
            dtd_file
        return etree.tostring(root)
    # parse, validating against external DTD
    err = validate_profile_external_dtd(profile_str, dtd_file)
    if err:
        raise etree.XMLSyntaxError(err, '', '', '')
    return profile_str
Ejemplo n.º 2
0
    def find_framerate(self, itt):
        """
        Parse itt files to find the framerate
        :return:
        """
        parser = etree.XMLParser(remove_blank_text=True, encoding="utf-8")
        nsmap = {"ttp": "http://www.w3.org/ns/ttml#parameter"}

        with open(os.path.join(self.root_path + os.sep, itt),
                  "r",
                  encoding="utf-8") as itt_file:
            try:
                sub = etree.parse(itt_file, parser=parser)

            except etree.XMLSyntaxError as e:
                raise etree.XMLSyntaxError(e)

            else:
                sub_root = sub.getroot()
                attributes = sub_root.attrib
                framerate = attributes["{%s}frameRate" % nsmap["ttp"]]
                multiplier = attributes["{%s}frameRateMultiplier" %
                                        nsmap["ttp"]]
                drop = attributes["{%s}dropMode" % nsmap["ttp"]]

                return framerate, multiplier, drop
Ejemplo n.º 3
0
def test_parse_handles_bad_xml(mocker, test_nodl_path):
    mocker.patch(
        'nodl._parsing._parsing.etree.parse',
        side_effect=etree.XMLSyntaxError('', '', 404, 404),
    )

    with pytest.raises(nodl.errors.InvalidXMLError):
        nodl._parsing._parsing.parse(mocker.Mock())
Ejemplo n.º 4
0
 def test_validate_throw(self, get_parser, fromstring):
     get_parser.return_value = "parser"
     fromstring.side_effect = etree.XMLSyntaxError("XML Error", None, None,
                                                   None)
     self.assertRaisesRegexp(etree.XMLSyntaxError, "XML Error",
                             xsd._validate, "xml", "schema_name")
     get_parser.assert_called_once_with("schema_name")
     fromstring.assert_called_once_with("xml", "parser")
Ejemplo n.º 5
0
 def test_put_invalid(self, passthrough_put, validate):
     self.request.body = "dodgy_xml_body"
     validate.side_effect = etree.XMLSyntaxError("XML Error", None, None,
                                                 None)
     self.assertRaisesRegexp(HTTPError,
                             "HTTP 400: Bad Request \(XML Error\)",
                             self.handler.put, "arg1")
     validate.assert_called_once_with("dodgy_xml_body", self.schema_path)
Ejemplo n.º 6
0
def gccxml_parse(filename, includes=(), defines=('XDRESS',), undefines=(),
                 extra_parser_args=(), verbose=False, debug=False, builddir='build',
                 clang_includes=()):
    """Use GCC-XML to parse a file. This function is automatically memoized.

    Parameters
    ----------
    filename : str
        The path to the file.
    includes : list of str, optional
        The list of extra include directories to search for header files.
    defines : list of str, optional
        The list of extra macro definitions to apply.
    undefines : list of str, optional
        The list of extra macro undefinitions to apply.
    extra_parser_args : list of str, optional
        Further command line arguments to pass to the parser.
    verbose : bool, optional
        Flag to display extra information while describing the class.
    debug : bool, optional
        Flag to enable/disable debug mode.
    builddir : str, optional
        Location of -- often temporary -- build files.
    clang_includes : ignored

    Returns
    -------
    root : XML etree
        An in memory tree representing the parsed file.
    """
    drive, xmlname = os.path.splitdrive(filename)
    if len(drive) > 0:
        # Windows drive handling, 'C:' -> 'C_'
        xmlname = drive.replace(':', '_') + xmlname
    xmlname = xmlname.replace(os.path.sep, '_').rsplit('.', 1)[0] + '.xml'
    xmlname = os.path.join(builddir, xmlname)
    cmd = ['gccxml', filename, '-fxml=' + xmlname]
    cmd += ['-I' + i for i in includes]
    cmd += ['-D' + d for d in defines]
    cmd += ['-U' + u for u in undefines]
    cmd += extra_parser_args
    if verbose:
        print(" ".join(cmd))
    if os.path.isfile(xmlname):
        f = io.open(xmlname, 'r+b')
    else:
        ensuredirs(xmlname)
        f = io.open(xmlname, 'w+b')
        subprocess.call(cmd)
    f.seek(0)
    try:
        root = etree.parse(f)
    except etree.XMLSyntaxError:
        raise etree.XMLSyntaxError("failed to parse GCC-XML results, this likely "
                                   "means that the C/C++ code is not valid. please "
                                   "see the top most build error.")
    f.close()
    return root
Ejemplo n.º 7
0
    def test_normalize_xml_syntax_error(self, parse_mock):
        '''
        Unit test for method : format_xml.normalize_xml to cover XMLSyntaxError
        '''

        builtins.t = MagicMock()
        builtins.t.log = MagicMock()

        parse_mock.side_effect = etree.XMLSyntaxError("Error Message", 0, 0, 0)

        with self.assertRaises(SystemExit):
            normalize_xml(self.VMX_CHASSIS_INFO_XML)
Ejemplo n.º 8
0
 def test_invalid_xml(self, xml_fromstring_mock):
     reason = "custom reason"
     xml_fromstring_mock.side_effect = etree.XMLSyntaxError(reason, 1, 1, 1)
     xml = "<invalid><xml />"
     assert_raise_library_error(
         lambda: lib.get_cib(xml),
         fixture.error(
             report_codes.CIB_LOAD_ERROR_BAD_FORMAT,
             reason=f"{reason} (line 1)",
         ),
     )
     xml_fromstring_mock.assert_called_once_with(xml)
Ejemplo n.º 9
0
 def test_decorator_error(self, passthrough_put, validate):
     decorator = xsd.validate("schema_path")
     func = mock.MagicMock()
     decorated = decorator(func)
     self.request = mock.MagicMock()
     self.request.body = "dodgy_xml_body"
     validate.side_effect = etree.XMLSyntaxError("XML Error", None, None, None)
     self.assertRaisesRegexp(HTTPError, "HTTP 400: Bad Request \(XML Error\)",
                             decorated,
                             self, "arg1")
     validate.assert_called_once_with("dodgy_xml_body", "schema_path")
     self.assertFalse(func.called)
Ejemplo n.º 10
0
 def test_validation_should_fail_if_lxml_raise_an_exception(
         self, mk_xmlvalidator):
     mk_xmlvalidator.parse.side_effect = etree.XMLSyntaxError(
         "some error", 1, 1, 1, "fake_path/file.xml")
     result = validation.validate_article_xml("fake_path/file.xml")
     self.assertEqual(
         {
             "some error (file.xml, line 1)": {
                 "count": 1,
                 "lineno": [1],
                 "message": ["some error (file.xml, line 1)"],
                 "filename": {"fake_path/file.xml"},
             }
         },
         result,
     )
Ejemplo n.º 11
0
 def _side_effect(arg):
     if arg == "<body></body>":
         return b"<body></body>"
     raise etree.XMLSyntaxError("Test Error - READING XML", 1, 1, 1)
Ejemplo n.º 12
0
 def test_apply_xslt_stylesheets_nonXML(self, mock_parse):
     mock_parse.side_effect = etree.XMLSyntaxError('not-XML', '<', 1, 1)
     with raises(KiwiConfigFileFormatNotSupported):
         self.markup.apply_xslt_stylesheets(
             'artificial_and_invalid_XML_markup'
         )
Ejemplo n.º 13
0
 def _close(self):
     # because we choose a subparser after seeing the root element, the only
     # way we'll get here is if the document is empty
     raise etree.XMLSyntaxError("Empty file", None, 1, 1)