def test_reverse_lookup():
    """Test that the chrome reverse lookup function works properly."""

    c = ChromeManifest("""
    content ns1 /dir1/
    content ns2 /dir2/foo/
    content nsbad1 /dir3
    content ns3 jar:foo.jar!/subdir1/
    content ns3 jar:zap.jar!/altdir1/
    content ns4 jar:bar.jar!/subdir2
    """, "chrome.manifest")

    eq_(c.reverse_lookup(MockPackStack(), "random.js"), None)
    eq_(c.reverse_lookup(MockPackStack(), "/dir1/x.js"),
        "chrome://ns1/x.js")
    eq_(c.reverse_lookup(MockPackStack(), "/dir2/x.js"), None)
    eq_(c.reverse_lookup(MockPackStack(), "/dir2/foo/x.js"),
        "chrome://ns2/x.js")
    eq_(c.reverse_lookup(MockPackStack(), "/dir3/x.js"),
        "chrome://nsbad1/x.js")
    eq_(c.reverse_lookup(MockPackStack(["foo.jar"]), "/x.js"),
        "chrome://ns3/subdir1/x.js")
    eq_(c.reverse_lookup(MockPackStack(["foo.jar"]), "/zap/x.js"),
        "chrome://ns3/subdir1/zap/x.js")
    eq_(c.reverse_lookup(MockPackStack(["bar.jar"]), "/x.js"),
        "chrome://ns4/subdir2/x.js")
    eq_(c.reverse_lookup(MockPackStack(["zap.jar"]), "/x.js"),
        "chrome://ns3/altdir1/x.js")
def test_reverse_lookup():
    """Test that the chrome reverse lookup function works properly."""

    c = ChromeManifest("""
    content ns1 /dir1/
    content ns2 /dir2/foo/
    content nsbad1 /dir3
    content ns3 jar:foo.jar!/subdir1/
    content ns3 jar:zap.jar!/altdir1/
    content ns4 jar:bar.jar!/subdir2
    """, 'chrome.manifest')

    assert c.reverse_lookup(MockPackStack(), 'random.js') is None
    assert c.reverse_lookup(MockPackStack(),
                            '/dir1/x.js') == 'chrome://ns1/x.js'
    assert c.reverse_lookup(MockPackStack(), '/dir2/x.js') is None
    assert c.reverse_lookup(MockPackStack(),
                            '/dir2/foo/x.js') == 'chrome://ns2/x.js'
    assert c.reverse_lookup(MockPackStack(),
                            '/dir3/x.js') == 'chrome://nsbad1/x.js'
    assert c.reverse_lookup(MockPackStack(['foo.jar']),
                            '/x.js') == 'chrome://ns3/subdir1/x.js'
    assert c.reverse_lookup(MockPackStack(['foo.jar']),
                            '/zap/x.js') == 'chrome://ns3/subdir1/zap/x.js'
    assert c.reverse_lookup(MockPackStack(['bar.jar']),
                            '/x.js') == 'chrome://ns4/subdir2/x.js'
    assert c.reverse_lookup(MockPackStack(['zap.jar']),
                            '/x.js') == 'chrome://ns3/altdir1/x.js'
def _get_locales(err, xpi_package):
    "Returns a list of locales from the chrome.manifest file."
    
    # Retrieve the chrome.manifest if it's cached.
    if err is not None and \
       err.get_resource("chrome.manifest"): # pragma: no cover
        chrome = err.get_resource("chrome.manifest")
    else:
        chrome_data = xpi_package.read("chrome.manifest")
        chrome = ChromeManifest(chrome_data)
        if err is not None:
            err.save_resource("chrome.manifest", chrome)
        
    pack_locales = chrome.get_triples("locale")
    locales = {}
    # Find all of the locales referenced in the chrome.manifest file.
    for locale in pack_locales:
        locale_jar = locale["object"].split()

        location = locale_jar[-1]
        if not location.startswith("jar:"):
            continue
        full_location = location[4:].split("!")
        locale_desc = {"predicate": locale["predicate"],
                       "path": full_location[0],
                       "target": full_location[1],
                       "name": locale_jar[0]}
        locale_name = "%s:%s" % (locale["predicate"], locale_jar[0])
        if locale_name not in locales:
            locales[locale_name] = locale_desc
    
    return locales
def _list_locales(err, xpi_package=None):
    'Returns a raw list of locales from chrome.manifest'

    chrome = None
    if xpi_package is not None:
        # Handle a reference XPI
        chrome = ChromeManifest(xpi_package, 'chrome.manifest', err)
    else:
        # Handle the current XPI
        chrome = err.get_resource('chrome.manifest')
    if not chrome:
        return None

    return list(chrome.get_entries('locale'))
def test_duplicate_subjects():
    """Test that two triplets with the same subject can be retrieved."""

    c = ChromeManifest("""
    foo bar abc
    foo bar def
    foo bam test
    oof rab cba
    """, 'chrome.manifest')

    assert len(list(c.get_triples(subject='foo'))) == 3
    assert len(list(c.get_triples(subject='foo', predicate='bar'))) == 2
    assert len(list(c.get_triples(subject='foo',
                                  predicate='bar',
                                  object_='abc'))) == 1
def _list_locales(err, xpi_package=None):
    "Returns a raw list of locales from chrome.manifest"

    chrome = None
    if xpi_package is not None:
        # Handle a reference XPI
        chrome = ChromeManifest(xpi_package.read("chrome.manifest"), "chrome.manifest")
    else:
        # Handle the current XPI
        chrome = err.get_resource("chrome.manifest")
    if not chrome:
        return None

    pack_locales = chrome.get_triples("locale")
    return list(pack_locales)
def test_lines():
    """Test that the correct line numbers are given in a chrome.manifest."""

    c = ChromeManifest("""
    zero foo bar
    one bar foo
    two abc def
    #comment
    four def abc
    """.strip(), 'chrome.manifest')

    assert list(c.get_triples(subject='zero'))[0]['line'] == 1
    assert list(c.get_triples(subject='one'))[0]['line'] == 2
    assert list(c.get_triples(subject='two'))[0]['line'] == 3
    assert list(c.get_triples(subject='four'))[0]['line'] == 5
def test_lines():
    """Test that the correct line numbers are given in a chrome.manifest."""

    c = ChromeManifest("""
    zero foo bar
    one bar foo
    two abc def
    #comment
    four def abc
    """.strip(), "chrome.manifest")

    eq_(list(c.get_triples(subject="zero"))[0]["line"], 1)
    eq_(list(c.get_triples(subject="one"))[0]["line"], 2)
    eq_(list(c.get_triples(subject="two"))[0]["line"], 3)
    eq_(list(c.get_triples(subject="four"))[0]["line"], 5)
def test_duplicate_subjects():
    """Test that two triplets with the same subject can be retrieved."""

    c = ChromeManifest("""
    foo bar abc
    foo bar def
    foo bam test
    oof rab cba
    """, "chrome.manifest")

    assert len(list(c.get_triples(subject="foo"))) == 3
    assert len(list(c.get_triples(subject="foo", predicate="bar"))) == 2
    assert len(list(c.get_triples(subject="foo",
                                  predicate="bar",
                                  object_="abc"))) == 1
def test_open():
    """Open a chrome file and ensure that data can be pulled from it."""

    xpi = MockXPI({
        'chrome.manifest': 'tests/resources/chromemanifest/chrome.manifest'})

    manifest = ChromeManifest(xpi, 'chrome.manifest')
    assert manifest is not None

    g_obj = list(manifest.get_entries('subject', 'predicate'))

    assert len(g_obj) == 1
    assert g_obj[0]['args'][1] == 'object'

    sub_locale = list(manifest.get_entries('locale'))
    assert len(sub_locale) == 2
Beispiel #11
0
def get_translation_xpi(request, project_slug, lang_code):
    """ Compile project's XPI in given language
    """
    project = get_object_or_404(Project, slug=project_slug)
    language = get_object_or_404(Language, code=lang_code)
    xpi = get_object_or_404(XpiFile, project=project)

    zip_orig = zipfile.ZipFile(os.path.join(settings.XPI_DIR,xpi.filename), "r")

    zip_buffer = StringIO()
    zip_file = zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED)

    # copy all the contents from original file except META-INF,
    # to make it unsigned
    for item in zip_orig.infolist():
        if not (item.filename.startswith('META-INF') or
                item.filename == 'chrome.manifest'):
            fn = item.filename
            data = zip_orig.read(item.filename)
            zip_file.writestr(item, data)

    # write our localization
    for resource in Resource.objects.filter(project=project):
        template = _compile_translation_template(resource, language)
        zip_file.writestr("tx-locale/%s/%s" % (lang_code, resource.name), template)

    chrome_str = zip_orig.read("chrome.manifest")
    manifest = ChromeManifest(chrome_str, "manifest")

    zip_file.writestr("chrome.manifest", chrome_str +\
        "\nlocale %(predicate)s %(code)s tx-locale/%(code)s/\n" % {
            'predicate': list(manifest.get_triples("locale"))[0]['predicate'],
            'code': lang_code,
        })

    zip_file.close()
    zip_buffer.flush()
    zip_contents = zip_buffer.getvalue()

    response = HttpResponse(mimetype='application/x-xpinstall')
    response['Content-Disposition'] = 'filename=%s.xpi' % project_slug
    response.write(zip_contents)
    return response
Beispiel #12
0
def test_marking_overlays_root_package():
    """
    Tests that '/' resolves correctly as a chrome content package.
    """

    err = ErrorBundle()
    err.supported_versions = {}
    manifest = ChromeManifest("""
    content ns1 /
    overlay chrome://foo chrome://ns1/content/main.xul
    """, 'chrome.manifest')
    err.save_resource('chrome.manifest', manifest)
    err.save_resource('chrome.manifest_nopush', manifest)

    xpi = MockXPI({'main.xul': 'tests/resources/content/script_list.xul'})

    content.test_packed_packages(err, xpi)
    assert not err.failed()

    marked_scripts = err.get_resource('marked_scripts')

    eq_(marked_scripts, set(['chrome://ns1/foo.js',
                             'chrome://ns1/bar.js',
                             'chrome://asdf/foo.js']))
Beispiel #13
0
def test_marking_overlays():
    """
    Mark an overlay, then test that it marks the scripts within the overlay.
    """

    err = ErrorBundle()
    err.supported_versions = {}
    c = ChromeManifest("""
    content ns1 foo/
    overlay chrome://foo chrome://ns1/content/main.xul
    """, 'chrome.manifest')
    err.save_resource('chrome.manifest', c)
    err.save_resource('chrome.manifest_nopush', c)

    xpi = MockXPI({'foo/main.xul': 'tests/resources/content/script_list.xul'})

    content.test_packed_packages(err, xpi)
    assert not err.failed()

    marked_scripts = err.get_resource('marked_scripts')

    eq_(marked_scripts, set(['chrome://ns1/foo.js',
                             'chrome://ns1/bar.js',
                             'chrome://asdf/foo.js']))
Beispiel #14
0
def test_open():
    """Open a chrome file and ensure that data can be pulled from it."""

    chrome = open("tests/resources/chromemanifest/chrome.manifest")
    chrome_data = chrome.read()

    manifest = ChromeManifest(chrome_data, "chrome.manifest")
    assert manifest is not None

    assert manifest.get_value("locale", "basta")["object"] == "resource"

    g_obj = list(manifest.get_objects("subject", "predicate"))

    assert len(g_obj) == 1
    assert g_obj[0] == "object"

    obj_resource = list(manifest.get_triples(None, None, "resource"))
    assert len(obj_resource) == 2

    pred_pred = list(manifest.get_triples(None, "predicate", None))
    assert len(pred_pred) == 2

    sub_locale = list(manifest.get_triples("locale", None, None))
    assert len(sub_locale) == 2
def test_open():
    """Open a chrome file and ensure that data can be pulled from it."""

    chrome = open('tests/resources/chromemanifest/chrome.manifest')
    chrome_data = chrome.read()

    manifest = ChromeManifest(chrome_data, 'chrome.manifest')
    assert manifest is not None

    assert manifest.get_value('locale', 'basta')['object'] == 'resource'

    g_obj = list(manifest.get_objects('subject', 'predicate'))

    assert len(g_obj) == 1
    assert g_obj[0] == 'object'

    obj_resource = list(manifest.get_triples(None, None, 'resource'))
    assert len(obj_resource) == 2

    pred_pred = list(manifest.get_triples(None, 'predicate', None))
    assert len(pred_pred) == 2

    sub_locale = list(manifest.get_triples('locale', None, None))
    assert len(sub_locale) == 2
def test_overlay_object():
    """Test that overlay instructions have all its properties."""

    err = ErrorBundle()
    c = ChromeManifest("""
    content namespace /foo/bar
    overlay namespace /uri/goes/here
    """, "chrome.manifest")
    err.save_resource("chrome.manifest", c)
    c.get_applicable_overlays(err)
    assert not err.failed()
    assert not err.notices

    err = ErrorBundle()
    c = ChromeManifest("""
    content namespace /foo/bar
    overlay /uri/goes/here
    """, "chrome.manifest")
    err.save_resource("chrome.manifest", c)
    c.get_applicable_overlays(err)
    assert err.failed()
    assert not err.notices
def test_open():
    """Open a chrome file and ensure that data can be pulled from it."""

    chrome = open("tests/resources/chromemanifest/chrome.manifest")
    chrome_data = chrome.read()

    manifest = ChromeManifest(chrome_data, "chrome.manifest")
    assert manifest is not None

    assert manifest.get_value("locale", "basta")["object"] == "resource"

    g_obj = list(manifest.get_objects("subject", "predicate"))

    assert len(g_obj) == 1
    assert g_obj[0] == "object"

    obj_resource = list(manifest.get_triples(None, None, "resource"))
    assert len(obj_resource) == 2

    pred_pred = list(manifest.get_triples(None, "predicate", None))
    assert len(pred_pred) == 2

    sub_locale = list(manifest.get_triples("locale", None, None))
    assert len(sub_locale) == 2
def test_open():
    """Open a chrome file and ensure that data can be pulled from it."""

    chrome = open('tests/resources/chromemanifest/chrome.manifest')
    chrome_data = chrome.read()

    manifest = ChromeManifest(chrome_data, 'chrome.manifest')
    assert manifest is not None

    assert manifest.get_value('locale', 'basta')['object'] == 'resource'

    g_obj = list(manifest.get_objects('subject', 'predicate'))

    assert len(g_obj) == 1
    assert g_obj[0] == 'object'

    obj_resource = list(manifest.get_triples(None, None, 'resource'))
    assert len(obj_resource) == 2

    pred_pred = list(manifest.get_triples(None, 'predicate', None))
    assert len(pred_pred) == 2

    sub_locale = list(manifest.get_triples('locale', None, None))
    assert len(sub_locale) == 2
def test_reverse_lookup():
    """Test that the chrome reverse lookup function works properly."""

    c = ChromeManifest(
        """
    content ns1 /dir1/
    content ns2 /dir2/foo/
    content nsbad1 /dir3
    content ns3 jar:foo.jar!/subdir1/
    content ns3 jar:zap.jar!/altdir1/
    content ns4 jar:bar.jar!/subdir2
    """, 'chrome.manifest')

    assert c.reverse_lookup(MockPackStack(), 'random.js') is None
    assert c.reverse_lookup(MockPackStack(),
                            '/dir1/x.js') == 'chrome://ns1/x.js'
    assert c.reverse_lookup(MockPackStack(), '/dir2/x.js') is None
    assert c.reverse_lookup(MockPackStack(),
                            '/dir2/foo/x.js') == 'chrome://ns2/x.js'
    assert c.reverse_lookup(MockPackStack(),
                            '/dir3/x.js') == 'chrome://nsbad1/x.js'
    assert c.reverse_lookup(MockPackStack(['foo.jar']),
                            '/x.js') == 'chrome://ns3/subdir1/x.js'
    assert c.reverse_lookup(MockPackStack(['foo.jar']),
                            '/zap/x.js') == 'chrome://ns3/subdir1/zap/x.js'
    assert c.reverse_lookup(MockPackStack(['bar.jar']),
                            '/x.js') == 'chrome://ns4/subdir2/x.js'
    assert c.reverse_lookup(MockPackStack(['zap.jar']),
                            '/x.js') == 'chrome://ns3/altdir1/x.js'
Beispiel #20
0
def chrome_manifest(string):
    """Returns a mock ChromeManifest object for the given string."""
    from validator.chromemanifest import ChromeManifest

    xpi = OtherMockXPI({'chrome.manifest': string})
    return ChromeManifest(xpi, 'chrome.manifest')
Beispiel #21
0
def test_conduittoolbar(err, package_contents=None, xpi_manager=None):
    "Find and blacklist Conduit toolbars"

    # Ignore non-extension types
    if err.detected_type in (PACKAGE_ANY, PACKAGE_THEME, PACKAGE_SEARCHPROV):
        return None

    # Tests regarding the install.rdf file.
    if err.get_resource("has_install_rdf"):

        # Go out and fetch the install.rdf instance object
        install = err.get_resource("install_rdf")

        # Define a list of specifications to search for Conduit with
        parameters = {
            "http://www.conduit.com/": install.uri("homepageURL"),
            "Conduit Ltd.": install.uri("creator"),
            "More than just a toolbar.": install.uri("description"),
        }

        # Iterate each specification and test for it.
        for k, uri_reference in parameters.items():
            # Retrieve the value
            results = install.get_object(None, uri_reference)
            # If the value exists, test for the appropriate content
            if results == k:
                err.reject = True
                err_mesg = "Conduit value (%s) found in install.rdf" % k
                return err.warning(
                    ("testcases_conduit", "test_conduittoolbar", "detected_rdf"),
                    "Detected Conduit toolbar.",
                    err_mesg,
                    "install.rdf",
                )

        # Also test for the update URL
        update_url_value = "https://ffupdate.conduit-services.com/"

        results = install.get_object(None, install.uri("updateURL"))
        if results and results.startswith(update_url_value):
            err.reject = True
            return err.warning(
                ("testcases_conduit", "test_conduittoolbar", "detected_updateurl"),
                "Detected Conduit toolbar.",
                "Conduit update URL found in install.rdf.",
                "install.rdf",
            )

    # Do some matching on the files in the package
    conduit_files = ("components/Conduit*", "searchplugin/conduit*")
    for file_ in package_contents:
        for bad_file in conduit_files:
            # If there's a matching file, it's Conduit
            if fnmatch.fnmatch(file_, bad_file):
                err.reject = True
                return err.warning(
                    ("testcases_conduit", "test_conduittoolbar", "detected_files"),
                    "Detected Conduit toolbar.",
                    "Conduit directory (%s) found." % bad_file,
                )

    # Do some tests on the chrome.manifest file if it exists
    if "chrome.manifest" in package_contents:
        # Grab the chrome manifest
        if err.get_resource("chrome.manifest"):  # pragma: no cover
            # It's cached in the error bundler
            chrome = err.get_resource("chrome.manifest")
        else:
            # Not cached, so we grab it.
            chrome_data = xpi_manager.read("chrome.manifest")
            chrome = ChromeManifest(chrome_data)
            err.save_resource("chrome.manifest", chrome)

        # Get all styles for customizing the toolbar
        data = chrome.get_value("style", "chrome://global/content/customizeToolbar.xul")

        # If the style exists and it contains "ebtoolbarstyle"...
        if data is not None and data["object"].count("ebtoolbarstyle") > 0:
            err.reject = True
            return err.warning(
                ("testcases_conduit", "test_conduittoolbar", "detected_chrome_manifest"),
                "Detected Conduit toolbar.",
                "'ebtoolbarstyle' found in chrome.manifest",
                "chrome.manifest",
                line=data["line"],
                context=chrome.context,
            )
Beispiel #22
0
def populate_chrome_manifest(err, xpi_package):
    "Loads the chrome.manifest if it's present"

    if 'chrome.manifest' in xpi_package:
        chrome_data = xpi_package.read('chrome.manifest')
        chrome = ChromeManifest(chrome_data, 'chrome.manifest')

        chrome_recursion_buster = set()

        # Handle the case of manifests linked from the manifest.
        def get_linked_manifest(path, from_path, from_chrome, from_triple):

            if path in chrome_recursion_buster:
                err.warning(
                    err_id=('submain', 'populate_chrome_manifest',
                            'recursion'),
                    warning='Linked manifest recursion detected.',
                    description='A chrome registration file links back to '
                    'itself. This can cause a multitude of '
                    'issues.',
                    filename=path)
                return

            # Make sure the manifest is properly linked
            if path not in xpi_package:
                err.notice(
                    err_id=('submain', 'populate_chrome_manifest', 'linkerr'),
                    notice='Linked manifest could not be found.',
                    description=('A linked manifest file could not be found '
                                 'in the package.', 'Path: %s' % path),
                    filename=from_path,
                    line=from_triple['line'],
                    context=from_chrome.context)
                return

            chrome_recursion_buster.add(path)

            manifest = ChromeManifest(xpi_package.read(path), path)
            for triple in manifest.triples:
                yield triple

                if triple['subject'] == 'manifest':
                    subpath = triple['predicate']
                    # If the path is relative, make it relative to the current
                    # file.
                    if not subpath.startswith('/'):
                        subpath = '%s/%s' % ('/'.join(
                            path.split('/')[:-1]), subpath)

                    subpath = subpath.lstrip('/')

                    for subtriple in get_linked_manifest(
                            subpath, path, manifest, triple):
                        yield subtriple

            chrome_recursion_buster.discard(path)

        chrome_recursion_buster.add('chrome.manifest')

        # Search for linked manifests in the base manifest.
        for extra_manifest in chrome.get_triples(subject='manifest'):
            # When one is found, add its triples to our own.
            for triple in get_linked_manifest(extra_manifest['predicate'],
                                              'chrome.manifest', chrome,
                                              extra_manifest):
                chrome.triples.append(triple)

        chrome_recursion_buster.discard('chrome.manifest')

        # Create a reference so we can get the chrome manifest later, but make
        # it pushable so we don't run chrome manifests in JAR files.
        err.save_resource('chrome.manifest', chrome, pushable=True)
        # Create a non-pushable reference for tests that need to access the
        # chrome manifest from within JAR files.
        err.save_resource('chrome.manifest_nopush', chrome, pushable=False)
Beispiel #23
0
    def __init__(self, filename, project=None, release=None, name=None):
        """
        Fills in a list of locales from the chrome.manifest file.
        """
        Bundle.__init__(self, project, release)
        self.xpi = XPIManager(filename, name=name)
         # here we will store managers for jarfiles
        self.jarfiles = {}
        chrome = ChromeManifest(self.xpi.read("chrome.manifest"), "manifest")
        locales = list(chrome.get_triples("locale"))

        if not locales:
            return None

        # read the list
        for locale in locales:
            code, location = locale["object"].split()

            # finding out the language of the locale
            try:
                lang = self._get_lang(code)
            except Language.DoesNotExist:
                self.log("Locale %s SKIPPED" % code, "font-weight:bold")
                continue

            # Locales can be bundled in JARs
            jarred = location.startswith("jar:")
            if jarred:
                # We just care about the JAR path
                location = location[4:]
                split_location = location.split("!", 2)
                # Ignore malformed JAR URIs.
                if len(split_location) < 2:
                    continue
                jarname, location = split_location

                # missing file mentioned
                if jarname not in self.xpi:
                    continue
                # may be we have already read this one
                if jarname in self.jarfiles:
                    package = self.jarfiles[jarname]
                else:
                    jar = StringIO(self.xpi.read(jarname))
                    package = XPIManager(jar, mode="r", name=jarname)
            else:
                package = self.xpi

            # and now we read files from there
            location = location.strip('/')
            result = {}
            for f in package.package_contents():
                f = f.strip("/")
                if f.startswith(location) and f != location:
                    result[f.split("/")[-1]] = package.read(f)

            # file with same name in different jars can get overwritten
            if lang not in self.locales:
                self.locales[lang] = result
            else:
                self.locales[lang].update(result)
def test_incomplete_triplets():
    """Test that incomplete triplets are ignored."""

    c = ChromeManifest('foo\nbar', 'chrome.manifest')
    assert not c.triples
def populate_chrome_manifest(err, xpi_package):
    "Loads the chrome.manifest if it's present"

    if 'chrome.manifest' in xpi_package:
        chrome_data = xpi_package.read('chrome.manifest')
        chrome = ChromeManifest(chrome_data, 'chrome.manifest')

        chrome_recursion_buster = set()

        # Handle the case of manifests linked from the manifest.
        def get_linked_manifest(path, from_path, from_chrome, from_triple):

            if path in chrome_recursion_buster:
                err.warning(
                    err_id=('submain', 'populate_chrome_manifest',
                            'recursion'),
                    warning='Linked manifest recursion detected.',
                    description='A chrome registration file links back to '
                                'itself. This can cause a multitude of '
                                'issues.',
                    filename=path)
                return

            # Make sure the manifest is properly linked
            if path not in xpi_package:
                err.notice(
                    err_id=('submain', 'populate_chrome_manifest', 'linkerr'),
                    notice='Linked manifest could not be found.',
                    description=('A linked manifest file could not be found '
                                 'in the package.',
                                 'Path: %s' % path),
                    filename=from_path,
                    line=from_triple['line'],
                    context=from_chrome.context)
                return

            chrome_recursion_buster.add(path)

            manifest = ChromeManifest(xpi_package.read(path), path)
            for triple in manifest.triples:
                yield triple

                if triple['subject'] == 'manifest':
                    subpath = triple['predicate']
                    # If the path is relative, make it relative to the current
                    # file.
                    if not subpath.startswith('/'):
                        subpath = '%s/%s' % (
                            '/'.join(path.split('/')[:-1]), subpath)

                    subpath = subpath.lstrip('/')

                    for subtriple in get_linked_manifest(
                            subpath, path, manifest, triple):
                        yield subtriple

            chrome_recursion_buster.discard(path)

        chrome_recursion_buster.add('chrome.manifest')

        # Search for linked manifests in the base manifest.
        for extra_manifest in chrome.get_triples(subject='manifest'):
            # When one is found, add its triples to our own.
            for triple in get_linked_manifest(extra_manifest['predicate'],
                                              'chrome.manifest', chrome,
                                              extra_manifest):
                chrome.triples.append(triple)

        chrome_recursion_buster.discard('chrome.manifest')

        # Create a reference so we can get the chrome manifest later, but make
        # it pushable so we don't run chrome manifests in JAR files.
        err.save_resource('chrome.manifest', chrome, pushable=True)
        # Create a non-pushable reference for tests that need to access the
        # chrome manifest from within JAR files.
        err.save_resource('chrome.manifest_nopush', chrome, pushable=False)
Beispiel #26
0
def populate_chrome_manifest(err, xpi_package):
    "Loads the chrome.manifest if it's present"

    if "chrome.manifest" in xpi_package:
        chrome_data = xpi_package.read("chrome.manifest")
        chrome = ChromeManifest(chrome_data, "chrome.manifest")

        chrome_recursion_buster = set()

        # Handle the case of manifests linked from the manifest.
        def get_linked_manifest(path, from_path, from_chrome, from_triple):

            if path in chrome_recursion_buster:
                err.warning(
                    err_id=("submain", "populate_chrome_manifest",
                            "recursion"),
                    warning="Linked manifest recursion detected.",
                    description="A chrome registration file links back to "
                    "itself. This can cause a multitude of "
                    "issues.",
                    filename=path)
                return

            # Make sure the manifest is properly linked
            if path not in xpi_package:
                err.notice(err_id=("submain", "populate_chrome_manifest",
                                   "linkerr"),
                           notice="Linked manifest could not be found.",
                           description=[
                               "A linked manifest file could not be found "
                               "in the package.",
                               "Path: %s" % path
                           ],
                           filename=from_path,
                           line=from_triple["line"],
                           context=from_chrome.context)
                return

            chrome_recursion_buster.add(path)

            manifest = ChromeManifest(xpi_package.read(path), path)
            for triple in manifest.triples:
                yield triple

                if triple["subject"] == "manifest":
                    subpath = triple["predicate"]
                    # If the path is relative, make it relative to the current
                    # file.
                    if not subpath.startswith("/"):
                        subpath = "%s/%s" % ("/".join(
                            path.split("/")[:-1]), subpath)

                    subpath = subpath.lstrip("/")

                    for subtriple in get_linked_manifest(
                            subpath, path, manifest, triple):
                        yield subtriple

            chrome_recursion_buster.discard(path)

        chrome_recursion_buster.add("chrome.manifest")

        # Search for linked manifests in the base manifest.
        for extra_manifest in chrome.get_triples(subject="manifest"):
            # When one is found, add its triples to our own.
            for triple in get_linked_manifest(extra_manifest["predicate"],
                                              "chrome.manifest", chrome,
                                              extra_manifest):
                chrome.triples.append(triple)

        chrome_recursion_buster.discard("chrome.manifest")

        # Create a reference so we can get the chrome manifest later, but make
        # it pushable so we don't run chrome manifests in JAR files.
        err.save_resource("chrome.manifest", chrome, pushable=True)
        # Create a non-pushable reference for tests that need to access the
        # chrome manifest from within JAR files.
        err.save_resource("chrome.manifest_nopush", chrome, pushable=False)
Beispiel #27
0
def populate_chrome_manifest(err, xpi_package):
    "Loads the chrome.manifest if it's present"

    if "chrome.manifest" in xpi_package:
        chrome_data = xpi_package.read("chrome.manifest")
        chrome = ChromeManifest(chrome_data, "chrome.manifest")

        chrome_recursion_buster = set()

        # Handle the case of manifests linked from the manifest.
        def get_linked_manifest(path, from_path, from_chrome, from_triple):

            if path in chrome_recursion_buster:
                err.warning(
                    err_id=("submain", "populate_chrome_manifest",
                            "recursion"),
                    warning="Linked manifest recursion detected.",
                    description="A chrome registration file links back to "
                                "itself. This can cause a multitude of "
                                "issues.",
                    filename=path)
                return

            # Make sure the manifest is properly linked
            if path not in xpi_package:
                err.notice(
                    err_id=("submain", "populate_chrome_manifest", "linkerr"),
                    notice="Linked manifest could not be found.",
                    description=["A linked manifest file could not be found "
                                 "in the package.",
                                 "Path: %s" % path],
                    filename=from_path,
                    line=from_triple["line"],
                    context=from_chrome.context)
                return

            chrome_recursion_buster.add(path)

            manifest = ChromeManifest(xpi_package.read(path), path)
            for triple in manifest.triples:
                yield triple

                if triple["subject"] == "manifest":
                    for subtriple in get_linked_manifest(
                            triple["predicate"], path, manifest, triple):
                        yield subtriple

            chrome_recursion_buster.discard(path)

        chrome_recursion_buster.add("chrome.manifest")

        # Search for linked manifests in the base manifest.
        for extra_manifest in chrome.get_triples(subject="manifest"):
            # When one is found, add its triples to our own.
            for triple in get_linked_manifest(extra_manifest["predicate"],
                                              "chrome.manifest", chrome,
                                              extra_manifest):
                chrome.triples.append(triple)

        chrome_recursion_buster.discard("chrome.manifest")

        # Create a reference so we can get the chrome manifest later, but make
        # it pushable so we don't run chrome manifests in JAR files.
        err.save_resource("chrome.manifest", chrome, pushable=True)
        # Create a non-pushable reference for tests that need to access the
        # chrome manifest from within JAR files.
        err.save_resource("chrome.manifest_nopush", chrome, pushable=False)
Beispiel #28
0
    def __init__(self, filename, project=None, release=None, name=None):
        """
        Fills in a list of locales from the chrome.manifest file.
        """
        Bundle.__init__(self, project, release)
        self.xpi = XPIManager(filename, name=name)
        # here we will store managers for jarfiles
        self.jarfiles = {}
        chrome = ChromeManifest(self.xpi.read("chrome.manifest"), "manifest")
        locales = list(chrome.get_triples("locale"))

        if not locales:
            return None

        # read the list
        for locale in locales:
            code, location = locale["object"].split()

            # finding out the language of the locale
            try:
                lang = self._get_lang(code)
            except Language.DoesNotExist:
                self.log("Locale %s SKIPPED" % code, "font-weight:bold")
                continue

            # Locales can be bundled in JARs
            jarred = location.startswith("jar:")
            if jarred:
                # We just care about the JAR path
                location = location[4:]
                split_location = location.split("!", 2)
                # Ignore malformed JAR URIs.
                if len(split_location) < 2:
                    continue
                jarname, location = split_location

                # missing file mentioned
                if jarname not in self.xpi:
                    continue
                # may be we have already read this one
                if jarname in self.jarfiles:
                    package = self.jarfiles[jarname]
                else:
                    jar = StringIO(self.xpi.read(jarname))
                    package = XPIManager(jar, mode="r", name=jarname)
            else:
                package = self.xpi

            # and now we read files from there
            location = location.strip('/')
            result = {}
            for f in package.package_contents():
                f = f.strip("/")
                if f.startswith(location) and f != location:
                    result[f.split("/")[-1]] = package.read(f)

            # file with same name in different jars can get overwritten
            if lang not in self.locales:
                self.locales[lang] = result
            else:
                self.locales[lang].update(result)