tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_chrome_manifest.py (6775B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import os
      6 import unittest
      7 
      8 import mozunit
      9 
     10 from mozpack.chrome.manifest import (
     11    MANIFESTS_TYPES,
     12    Manifest,
     13    ManifestBinaryComponent,
     14    ManifestCategory,
     15    ManifestComponent,
     16    ManifestContent,
     17    ManifestContract,
     18    ManifestInterfaces,
     19    ManifestLocale,
     20    ManifestOverlay,
     21    ManifestOverride,
     22    ManifestResource,
     23    ManifestSkin,
     24    ManifestStyle,
     25    parse_manifest,
     26    parse_manifest_line,
     27 )
     28 from mozpack.errors import AccumulatedErrors, errors
     29 from test_errors import TestErrors
     30 
     31 
     32 class TestManifest(unittest.TestCase):
     33    def test_parse_manifest(self):
     34        manifest = [
     35            "content global content/global/",
     36            "content global content/global/ application=foo application=bar"
     37            + " platform",
     38            "locale global en-US content/en-US/",
     39            "locale global en-US content/en-US/ application=foo",
     40            "skin global classic/1.0 content/skin/classic/",
     41            "skin global classic/1.0 content/skin/classic/ application=foo"
     42            + " os=WINNT",
     43            "",
     44            "manifest pdfjs/chrome.manifest",
     45            "resource gre-resources toolkit/res/",
     46            "override chrome://global/locale/netError.dtd"
     47            + " chrome://browser/locale/netError.dtd",
     48            "# Comment",
     49            "component {b2bba4df-057d-41ea-b6b1-94a10a8ede68} foo.js",
     50            "contract @mozilla.org/foo;1" + " {b2bba4df-057d-41ea-b6b1-94a10a8ede68}",
     51            "interfaces foo.xpt",
     52            "binary-component bar.so",
     53            "category command-line-handler m-browser"
     54            + " @mozilla.org/browser/clh;1"
     55            + " application={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
     56            "style chrome://global/content/viewSource.xul" + " chrome://browser/skin/",
     57            "overlay chrome://global/content/viewSource.xul"
     58            + " chrome://browser/content/viewSourceOverlay.xul",
     59        ]
     60        other_manifest = ["content global content/global/"]
     61        expected_result = [
     62            ManifestContent("", "global", "content/global/"),
     63            ManifestContent(
     64                "",
     65                "global",
     66                "content/global/",
     67                "application=foo",
     68                "application=bar",
     69                "platform",
     70            ),
     71            ManifestLocale("", "global", "en-US", "content/en-US/"),
     72            ManifestLocale("", "global", "en-US", "content/en-US/", "application=foo"),
     73            ManifestSkin("", "global", "classic/1.0", "content/skin/classic/"),
     74            ManifestSkin(
     75                "",
     76                "global",
     77                "classic/1.0",
     78                "content/skin/classic/",
     79                "application=foo",
     80                "os=WINNT",
     81            ),
     82            Manifest("", "pdfjs/chrome.manifest"),
     83            ManifestResource("", "gre-resources", "toolkit/res/"),
     84            ManifestOverride(
     85                "",
     86                "chrome://global/locale/netError.dtd",
     87                "chrome://browser/locale/netError.dtd",
     88            ),
     89            ManifestComponent("", "{b2bba4df-057d-41ea-b6b1-94a10a8ede68}", "foo.js"),
     90            ManifestContract(
     91                "", "@mozilla.org/foo;1", "{b2bba4df-057d-41ea-b6b1-94a10a8ede68}"
     92            ),
     93            ManifestInterfaces("", "foo.xpt"),
     94            ManifestBinaryComponent("", "bar.so"),
     95            ManifestCategory(
     96                "",
     97                "command-line-handler",
     98                "m-browser",
     99                "@mozilla.org/browser/clh;1",
    100                "application=" + "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
    101            ),
    102            ManifestStyle(
    103                "", "chrome://global/content/viewSource.xul", "chrome://browser/skin/"
    104            ),
    105            ManifestOverlay(
    106                "",
    107                "chrome://global/content/viewSource.xul",
    108                "chrome://browser/content/viewSourceOverlay.xul",
    109            ),
    110        ]
    111        with mozunit.MockedOpen({
    112            "manifest": "\n".join(manifest),
    113            "other/manifest": "\n".join(other_manifest),
    114        }):
    115            # Ensure we have tests for all types of manifests.
    116            self.assertEqual(
    117                set(type(e) for e in expected_result), set(MANIFESTS_TYPES.values())
    118            )
    119            self.assertEqual(
    120                list(parse_manifest(os.curdir, "manifest")), expected_result
    121            )
    122            self.assertEqual(
    123                list(parse_manifest(os.curdir, "other/manifest")),
    124                [ManifestContent("other", "global", "content/global/")],
    125            )
    126 
    127    def test_manifest_rebase(self):
    128        m = parse_manifest_line("chrome", "content global content/global/")
    129        m = m.rebase("")
    130        self.assertEqual(str(m), "content global chrome/content/global/")
    131        m = m.rebase("chrome")
    132        self.assertEqual(str(m), "content global content/global/")
    133 
    134        m = parse_manifest_line("chrome/foo", "content global content/global/")
    135        m = m.rebase("chrome")
    136        self.assertEqual(str(m), "content global foo/content/global/")
    137        m = m.rebase("chrome/foo")
    138        self.assertEqual(str(m), "content global content/global/")
    139 
    140        m = parse_manifest_line("modules/foo", "resource foo ./")
    141        m = m.rebase("modules")
    142        self.assertEqual(str(m), "resource foo foo/")
    143        m = m.rebase("modules/foo")
    144        self.assertEqual(str(m), "resource foo ./")
    145 
    146        m = parse_manifest_line("chrome", "content browser browser/content/")
    147        m = m.rebase("chrome/browser").move("jar:browser.jar!").rebase("")
    148        self.assertEqual(str(m), "content browser jar:browser.jar!/content/")
    149 
    150 
    151 class TestManifestErrors(TestErrors, unittest.TestCase):
    152    def test_parse_manifest_errors(self):
    153        manifest = [
    154            "skin global classic/1.0 content/skin/classic/ platform",
    155            "",
    156            "binary-component bar.so",
    157            "unsupported foo",
    158        ]
    159        with mozunit.MockedOpen({"manifest": "\n".join(manifest)}):
    160            with self.assertRaises(AccumulatedErrors):
    161                with errors.accumulate():
    162                    list(parse_manifest(os.curdir, "manifest"))
    163            out = self.get_output()
    164            # Expecting 2 errors
    165            self.assertEqual(len(out), 2)
    166            path = os.path.abspath("manifest")
    167            # First on line 1
    168            self.assertTrue(out[0].startswith("error: %s:1: " % path))
    169            # Second on line 4
    170            self.assertTrue(out[1].startswith("error: %s:4: " % path))
    171 
    172 
    173 if __name__ == "__main__":
    174    mozunit.main()