<commit_before>import fnmatch

from validator import decorator
from validator.constants import *

def test_unknown_file(err, filename):
    "Tests some sketchy files that require silly code."
    
    # Extract the file path and the name from the filename
    path = filename.split("/")
    name = path.pop()
    
    if name == "chromelist.txt":
        err.info(("testcases_packagelayout",
                  "test_unknown_file",
                  "deprecated_file"),
                 "Extension contains a deprecated file",
                 """The file "%s" is no longer supported by any
                 modern Mozilla product.""" % filename,
                 filename)
        return True


@decorator.register_test(tier=1)
def test_blacklisted_files(err, package_contents=None, xpi_package=None):
    "Detects blacklisted files and extensions."
    
    # Detect blacklisted files based on their extension.
    blacklisted_extensions = ("dll", "exe", "dylib", "so",
                              "sh", "class", "swf")
    
    pattern = "File '%s' is using a blacklisted file extension (%s)"
    for name, file_ in package_contents.items():
        # Simple test to ensure that the extension isn't blacklisted
        extension = file_["extension"]
        if extension in blacklisted_extensions:
            err.warning(("testcases_packagelayout",
                         "test_blacklisted_files",
                         "disallowed_extension"),
                        "Blacklisted file extension found",
                        ["""The file "%s" uses a blacklisted file
                         extension.""" % name,
                         "The extension %s is disallowed." % extension],
                        name)

@decorator.register_test(tier=1)
def test_layout_all(err, package_contents, xpi_package):
    "Tests the well-formedness of extensions."
    
    # Subpackages don't need to be tested for install.rdf.
    if xpi_package.subpackage:
        return
    
    if not err.get_resource("has_install_rdf"):
        err.error(("testcases_packagelayout",
                  "test_layout_all",
                  "missing_install_rdf"),
                 "Addon missing install.rdf.",
                  "All addons require an install.rdf file.")
        err.reject = True
    

@decorator.register_test(tier=2)
def test_xpcom(err, package_contents, xpi_package):
    "Test to make sure XPCOM is not used for FF4+"
    
    if not err.get_resource("ff4"):
        return None
    
    # Test for XPCOM incompatibility.
    for name, file_ in package_contents.items():
        
        if name.startswith("components/"):
            err.warning(("testcases_packagelayout",
                         "test_xpcom",
                         "incompatible_xpcom_detected"),
                        "XPCOM ties detected",
                        ["""Files were found in the /components/ directory,
                         which generally indicates XPCOM references. Firefox
                         4 no longer supports XPCOM. This interface is
                         subject to change.""",
                         'File "%s" represents XPCOM ties.' % name],
                        name);
            break

@decorator.register_test(tier=2)
def test_emunpack(err, package_contents, xpi_package):

    if err.get_resource("em:unpack") != "true":
        # Covers bug 597255

        # Dictionaries should always be unpacked
        fails = err.detected_type != PACKAGE_DICTIONARY
        if not fails:
            # Search for unpack-worthy files
            for file_ in package_contents:
                if fnmatch.fnmatch(file_, "components/*.exe") or \
                   fnmatch.fnmatch(file_, "*.ico"):
                    fails = True
                    break

        if fails:
            err.warning(("testcases_packagelayout",
                         "test_emunpack",
                         "should_be_true"),
                        "Add-on should set <em:unpack> to true",
                        "The add-on meets criteria that would indicate "
                        "performance issues if <em:unpack> is not set to true "
                        "in the install.rdf file.")

    else:
        # Covers bug 551714

        # This only applies to FF4
        if not err.get_resource("ff4"):
            return

        for file_ in package_contents:
            if fnmatch.fnmatch(file_, ".jar"):
                err.warning(("testcases_packagelayout",
                             "test_emunpack",
                             "should_be_false"),<commit_msg>Added executable types for bug 597255<commit_after>import fnmatch

from validator import decorator
from validator.constants import *

def test_unknown_file(err, filename):
    "Tests some sketchy files that require silly code."
    
    # Extract the file path and the name from the filename
    path = filename.split("/")
    name = path.pop()
    
    if name == "chromelist.txt":
        err.info(("testcases_packagelayout",
                  "test_unknown_file",
                  "deprecated_file"),
                 "Extension contains a deprecated file",
                 """The file "%s" is no longer supported by any
                 modern Mozilla product.""" % filename,
                 filename)
        return True


@decorator.register_test(tier=1)
def test_blacklisted_files(err, package_contents=None, xpi_package=None):
    "Detects blacklisted files and extensions."
    
    # Detect blacklisted files based on their extension.
    blacklisted_extensions = ("dll", "exe", "dylib", "so",
                              "sh", "class", "swf")
    
    pattern = "File '%s' is using a blacklisted file extension (%s)"
    for name, file_ in package_contents.items():
        # Simple test to ensure that the extension isn't blacklisted
        extension = file_["extension"]
        if extension in blacklisted_extensions:
            err.warning(("testcases_packagelayout",
                         "test_blacklisted_files",
                         "disallowed_extension"),
                        "Blacklisted file extension found",
                        ["""The file "%s" uses a blacklisted file
                         extension.""" % name,
                         "The extension %s is disallowed." % extension],
                        name)

@decorator.register_test(tier=1)
def test_layout_all(err, package_contents, xpi_package):
    "Tests the well-formedness of extensions."
    
    # Subpackages don't need to be tested for install.rdf.
    if xpi_package.subpackage:
        return
    
    if not err.get_resource("has_install_rdf"):
        err.error(("testcases_packagelayout",
                  "test_layout_all",
                  "missing_install_rdf"),
                 "Addon missing install.rdf.",
                  "All addons require an install.rdf file.")
        err.reject = True
    

@decorator.register_test(tier=2)
def test_xpcom(err, package_contents, xpi_package):
    "Test to make sure XPCOM is not used for FF4+"
    
    if not err.get_resource("ff4"):
        return None
    
    # Test for XPCOM incompatibility.
    for name, file_ in package_contents.items():
        
        if name.startswith("components/"):
            err.warning(("testcases_packagelayout",
                         "test_xpcom",
                         "incompatible_xpcom_detected"),
                        "XPCOM ties detected",
                        ["""Files were found in the /components/ directory,
                         which generally indicates XPCOM references. Firefox
                         4 no longer supports XPCOM. This interface is
                         subject to change.""",
                         'File "%s" represents XPCOM ties.' % name],
                        name);
            break

@decorator.register_test(tier=2)
def test_emunpack(err, package_contents, xpi_package):

    if err.get_resource("em:unpack") != "true":
        # Covers bug 597255

        # Dictionaries should always be unpacked
        fails = err.detected_type == PACKAGE_DICTIONARY
        if not fails:
            executables = ("exe","dll","so","dylib","exe","bin")
            # Search for unpack-worthy files
            for file_ in package_contents:
                if fnmatch.fnmatch(file_, "*.ico"):
                    fails = True
                    break
                # Executables in /components/ should also be flagged.
                if fnmatch.fnmatch(file_, "components/*") and \
                   [x for x in executables
                      if file_[:-len(x) - 1] == ".%s" % x]:
                    
                    fails = True
                    break

        if fails:
            err.warning(("testcases_packagelayout",
                         "test_emunpack",
                         "should_be_true"),
                        "Add-on should set <em:unpack> to true",
                        "The add-on meets criteria that would indicate "
                        "performance issues if <em:unpack> is not set to true "
                        "in the install.rdf file.")

    else:
        # Covers bug 551714

        # This only applies to FF4
        if not err.get_resource("ff4"):
            return

        for file_ in package_contents:
            if fnmatch.fnmatch(file_, ".jar"):
                err.warning(("testcases_packagelayout",
                             "test_emunpack",
                             "should_be_false"),
{'fraction_non_alphanumeric': 0.08808398950131234, 'fraction_numerical': 0.005144356955380578, 'mean_word_length': 1.6468463462072798, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1770123', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2402, 'n_words': 705}