예제 #1
0
def test_strange():
    """Tests that in install.rdf-less package is listed as being a
    dictionary type if it has an XPI extension and otherwise passes
    back a None object."""

    err = ErrorBundle(None, True)

    non_langpack = MockXPILangpack()
    langpack = MockXPILangpack(True)

    assert typedetection.detect_type(err, None, langpack) == PACKAGE_DICTIONARY
    assert typedetection.detect_type(err, None, non_langpack) is None
예제 #2
0
def test_strange():
    """Tests that in install.rdf-less package is listed as being a
    dictionary type if it has an XPI extension and otherwise passes
    back a None object."""

    err = ErrorBundle(None, True)

    non_langpack = MockXPILangpack()
    langpack = MockXPILangpack(True)

    assert typedetection.detect_type(err, None, langpack) == PACKAGE_DICTIONARY
    assert typedetection.detect_type(err, None, non_langpack) is None
예제 #3
0
def _load_install_rdf(err, package, expectation):
    try:
        install_rdf = RDFParser(err, package.read("install.rdf"))
    except (RDFException, DefusedXmlException) as ex:
        if isinstance(ex, DefusedXmlException):
            url = "https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors"
            reason = "Malicious XML was detected, see {0}.".format(url)
            line = 0
        else:
            reason = "Try validating your RDF with the W3 validator: " "http://www.w3.org/RDF/Validator/."
            line = ex.line()
        err.error(
            err_id=("main", "test_package", "parse_error"),
            error="Could not parse `install.rdf`.",
            description=(
                "The RDF parser was unable to parse the " "install.rdf file included with this add-on.",
                reason,
            ),
            filename="install.rdf",
            line=line,
        )
        return
    else:
        if install_rdf.rdf is None:
            err.error(
                err_id=("main", "test_package", "cannot_parse_installrdf"),
                error="Cannot read `install.rdf`",
                description="The install.rdf file could not be parsed.",
                filename="install.rdf",
            )
            return
        else:
            err.save_resource("has_install_rdf", True, pushable=True)
            err.save_resource("install_rdf", install_rdf, pushable=True)

    # Load up the results of the type detection
    results = detect_type(err, install_rdf, package)
    if results is None:
        err.error(
            err_id=("main", "test_package", "undeterminable_type"),
            error="Unable to determine add-on type",
            description="The type detection algorithm could not determine " "the type of the add-on.",
        )
        return
    else:
        err.detected_type = results

    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if expectation not in (PACKAGE_ANY, results):
        err.warning(
            err_id=("main", "test_package", "extension_type_mismatch"),
            warning="Extension Type Mismatch",
            description=(
                "We detected that the add-on's type does not match " "the expected type.",
                'Type "%s" expected, found "%s"' % (types[expectation], types[results]),
            ),
        )
예제 #4
0
def _load_install_rdf(err, package, expectation):
    try:
        install_rdf = RDFParser(err, package.read("install.rdf"))
    except (RDFException, DefusedXmlException) as ex:
        if isinstance(ex, DefusedXmlException):
            url = "https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors"
            reason = "Malicious XML was detected, see {0}.".format(url)
            line = 0
        else:
            reason = ("Try validating your RDF with the W3 validator: "
                      "http://www.w3.org/RDF/Validator/.")
            line = ex.line()
        err.error(err_id=("main", "test_package", "parse_error"),
                  error="Could not parse `install.rdf`.",
                  description=[
                      "The RDF parser was unable to parse the "
                      "install.rdf file included with this add-on.", reason
                  ],
                  filename="install.rdf",
                  line=line)
        return
    else:
        if install_rdf.rdf is None:
            err.error(err_id=("main", "test_package",
                              "cannot_parse_installrdf"),
                      error="Cannot read `install.rdf`",
                      description="The install.rdf file could not be parsed.",
                      filename="install.rdf")
            return
        else:
            err.save_resource("has_install_rdf", True, pushable=True)
            err.save_resource("install_rdf", install_rdf, pushable=True)

    # Load up the results of the type detection
    results = detect_type(err, install_rdf, package)
    if results is None:
        err.error(
            err_id=("main", "test_package", "undeterminable_type"),
            error="Unable to determine add-on type",
            description="The type detection algorithm could not determine "
            "the type of the add-on.")
        return
    else:
        err.detected_type = results

    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if not expectation in (PACKAGE_ANY, results):
        err.warning(err_id=("main", "test_package", "extension_type_mismatch"),
                    warning="Extension Type Mismatch",
                    description=[
                        "We detected that the add-on's type does not match "
                        "the expected type.",
                        'Type "%s" expected, found "%s"' %
                        (types[expectation], types[results])
                    ])
예제 #5
0
def _load_install_rdf(err, package, expectation):
    try:
        install_rdf = RDFParser(err, package.read('install.rdf'))
    except (RDFException, DefusedXmlException) as ex:
        if isinstance(ex, DefusedXmlException):
            url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors'
            reason = 'Malicious XML was detected, see {0}.'.format(url)
            line = 0
        else:
            reason = ('Try validating your RDF with the W3 validator: '
                      'http://www.w3.org/RDF/Validator/.')
            line = ex.line()
        err.error(
                err_id=('main', 'test_package', 'parse_error'),
                error='Could not parse `install.rdf`.',
                description=('The RDF parser was unable to parse the '
                             'install.rdf file included with this add-on.',
                             reason),
                filename='install.rdf',
                line=line)
        return
    else:
        if install_rdf.rdf is None:
            err.error(
                    err_id=('main', 'test_package', 'cannot_parse_installrdf'),
                    error='Cannot read `install.rdf`',
                    description='The install.rdf file could not be parsed.',
                    filename='install.rdf')
            return
        else:
            err.save_resource('has_install_rdf', True, pushable=True)
            err.save_resource('install_rdf', install_rdf, pushable=True)

    # Load up the results of the type detection
    results = detect_type(err, install_rdf, package)
    if results is None:
        err.error(
                err_id=('main', 'test_package', 'undeterminable_type'),
                error='Unable to determine add-on type',
                description='The type detection algorithm could not determine '
                            'the type of the add-on.')
        return
    else:
        err.detected_type = results

    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if expectation not in (PACKAGE_ANY, results):
        err.warning(
            err_id=('main', 'test_package', 'extension_type_mismatch'),
            warning='Extension Type Mismatch',
            description=("We detected that the add-on's type does not match "
                         'the expected type.',
                         'Type "%s" expected, found "%s"' %
                         (types[expectation], types[results])))
예제 #6
0
def _load_install_rdf(err, package, expectation):
    try:
        install_rdf = RDFParser(err, package.read('install.rdf'))
    except (RDFException, DefusedXmlException) as ex:
        if isinstance(ex, DefusedXmlException):
            url = 'https://pypi.python.org/pypi/defusedxml/0.3#attack-vectors'
            reason = 'Malicious XML was detected, see {0}.'.format(url)
            line = 0
        else:
            reason = ('Try validating your RDF with the W3 validator: '
                      'http://www.w3.org/RDF/Validator/.')
            line = ex.line()
        err.error(err_id=('main', 'test_package', 'parse_error'),
                  error='Could not parse `install.rdf`.',
                  description=('The RDF parser was unable to parse the '
                               'install.rdf file included with this add-on.',
                               reason),
                  filename='install.rdf',
                  line=line)
        return
    else:
        if install_rdf.rdf is None:
            err.error(err_id=('main', 'test_package',
                              'cannot_parse_installrdf'),
                      error='Cannot read `install.rdf`',
                      description='The install.rdf file could not be parsed.',
                      filename='install.rdf')
            return
        else:
            err.save_resource('has_install_rdf', True, pushable=True)
            err.save_resource('install_rdf', install_rdf, pushable=True)

    # Load up the results of the type detection
    results = detect_type(err, install_rdf, package)
    if results is None:
        err.error(
            err_id=('main', 'test_package', 'undeterminable_type'),
            error='Unable to determine add-on type',
            description='The type detection algorithm could not determine '
            'the type of the add-on.')
        return
    else:
        err.detected_type = results

    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if expectation not in (PACKAGE_ANY, results):
        err.warning(
            err_id=('main', 'test_package', 'extension_type_mismatch'),
            warning='Extension Type Mismatch',
            description=("We detected that the add-on's type does not match "
                         'the expected type.',
                         'Type "%s" expected, found "%s"' %
                         (types[expectation], types[results])))
예제 #7
0
def _load_install_rdf(err, package, expectation):
    # Load up the install.rdf file.
    install_rdf_data = package.read("install.rdf")

    if re.search('<!doctype', install_rdf_data, re.I):
        err.save_resource("bad_install_rdf", True)
        return err.error(("main",
                          "test_package",
                          "doctype_in_installrdf"),
                         "DOCTYPEs are not permitted in install.rdf",
                         "The add-on's install.rdf file contains a DOCTYPE. "
                         "It must be removed before your add-on can be "
                         "validated.",
                         filename="install.rdf")

    install_rdf = RDFParser(install_rdf_data)

    if install_rdf.rdf is None or not install_rdf:
        return err.error(("main",
                          "test_package",
                          "cannot_parse_installrdf"),
                         "Cannot Parse install.rdf",
                         "The install.rdf file could not be parsed.",
                         filename="install.rdf")
    else:
        err.save_resource("has_install_rdf", True, pushable=True)
        err.save_resource("install_rdf", install_rdf, pushable=True)

    # Load up the results of the type detection
    results = detect_type(err, install_rdf, package)

    if results is None:
        return err.error(("main",
                          "test_package",
                          "undeterminable_type"),
                         "Unable to determine add-on type",
                         "The type detection algorithm could not determine "
                         "the type of the add-on.")
    else:
        err.set_type(results)

    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if not expectation in (PACKAGE_ANY, results):
        err.warning(("main",
                     "test_package",
                     "extension_type_mismatch"),
                    "Extension Type Mismatch",
                    ["We detected that the add-on's type does not match the "
                     "expected type.",
                     'Type "%s" expected, found "%s")' %
                         (types[expectation], types[results])])
예제 #8
0
def _load_install_rdf(err, package, expectation):
    try:
        install_rdf = RDFParser(err, package.read("install.rdf"))
    except RDFException as ex:
        err.error(
                err_id=("main", "test_package", "parse_error"),
                error="Could not parse `install.rdf`.",
                description=["The RDF parser was unable to parse the "
                             "install.rdf file included with this add-on.",
                             "Try validating your RDF with the W3 "
                             "validator: http://www.w3.org/RDF/Validator/"],
                filename="install.rdf",
                line=ex.line())
        return
    else:
        if install_rdf.rdf is None:
            err.error(
                    err_id=("main", "test_package", "cannot_parse_installrdf"),
                    error="Cannot read `install.rdf`",
                    description="The install.rdf file could not be parsed.",
                    filename="install.rdf")
            return
        else:
            err.save_resource("has_install_rdf", True, pushable=True)
            err.save_resource("install_rdf", install_rdf, pushable=True)

    # Load up the results of the type detection
    results = detect_type(err, install_rdf, package)
    if results is None:
        err.error(
                err_id=("main", "test_package", "undeterminable_type"),
                error="Unable to determine add-on type",
                description="The type detection algorithm could not determine "
                            "the type of the add-on.")
        return
    else:
        err.detected_type = results

    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if not expectation in (PACKAGE_ANY, results):
        err.warning(
            err_id=("main", "test_package", "extension_type_mismatch"),
            warning="Extension Type Mismatch",
            description=["We detected that the add-on's type does not match "
                         "the expected type.",
                         'Type "%s" expected, found "%s"' %
                             (types[expectation], types[results])])
예제 #9
0
def _load_install_rdf(err, package, expectation):
    # Load up the install.rdf file.
    install_rdf_data = package.read("install.rdf")
    install_rdf = RDFParser(install_rdf_data)
    
    if install_rdf.rdf is None or not install_rdf:
        return err.error(("main",
                          "test_package",
                          "cannot_parse_installrdf"),
                         "Cannot Parse install.rdf",
                         "The install.rdf file could not be parsed.")
    else:
        err.save_resource("has_install_rdf", True)
        err.save_resource("install_rdf", install_rdf)
    
    # Load up the results of the type detection
    results = typedetection.detect_type(err,
                                        install_rdf,
                                        package)
    
    if results is None:
        return err.error(("main",
                          "test_package",
                          "undeterminable_type"),
                         "Unable to determine addon type",
                         """The type detection algorithm could not
                         determine the type of the add-on.""")
    else:
        err.set_type(results)
    
    # Compare the results of the low-level type detection to
    # that of the expectation and the assumption.
    if not expectation in (PACKAGE_ANY, results):
        err.reject = True
        err.warning(("main",
                     "test_package",
                     "extension_type_mismatch"),
                    "Extension Type Mismatch",
                    'Type "%s" expected, found "%s")' % (
                                                    types[expectation],
                                                    types[results]))
예제 #10
0
def _test_type(file_, expectation, failure=False):
    "Tests a file against the expectations"

    err = ErrorBundle(None, True)
    package = XPIManager(open(file_, "rb"), file_)
    contents = package.get_file_data()

    # We need to have an install.rdf.
    assert "install.rdf" in contents

    # Load up the install.rdf into an RDFParser
    install_file = package.read("install.rdf")
    install_rdf = RDFParser(install_file)

    results = typedetection.detect_type(err, install_rdf, package)

    assert results == expectation
    if not failure:
        assert not err.failed()
    else:
        assert err.failed()
예제 #11
0
def _test_type(file_, expectation, failure=False):
    'Tests a file against the expectations'

    err = ErrorBundle(None, True)
    package = XPIManager(open(file_), mode='r', name=file_)
    contents = package.package_contents()

    # We need to have an install.rdf.
    assert 'install.rdf' in contents

    # Load up the install.rdf into an RDFParser
    install_file = package.read('install.rdf')
    install_rdf = RDFParser(err, install_file)

    results = typedetection.detect_type(err, install_rdf, package)

    assert results == expectation
    if not failure:
        assert not err.failed()
    else:
        assert err.failed()

    return err
예제 #12
0
def _test_type(file_, expectation, failure=False):
    'Tests a file against the expectations'

    err = ErrorBundle(None, True)
    package = XPIManager(open(file_), mode='r', name=file_)
    contents = package.package_contents()

    # We need to have an install.rdf.
    assert 'install.rdf' in contents

    # Load up the install.rdf into an RDFParser
    install_file = package.read('install.rdf')
    install_rdf = RDFParser(err, install_file)

    results = typedetection.detect_type(err, install_rdf, package)

    assert results == expectation
    if not failure:
        assert not err.failed()
    else:
        assert err.failed()

    return err