tor-browser

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

test_execute_sandboxes.py (3246B)


      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 from marionette_driver.errors import JavascriptException
      6 
      7 from marionette_harness import MarionetteTestCase
      8 
      9 
     10 class TestExecuteSandboxes(MarionetteTestCase):
     11    def setUp(self):
     12        super(TestExecuteSandboxes, self).setUp()
     13 
     14    def test_execute_system_sandbox(self):
     15        # Test that "system" sandbox has elevated privileges in execute_script
     16        result = self.marionette.execute_script(
     17            "return Components.interfaces.nsIPermissionManager.ALLOW_ACTION",
     18            sandbox="system",
     19        )
     20        self.assertEqual(result, 1)
     21 
     22    def test_execute_async_system_sandbox(self):
     23        # Test that "system" sandbox has elevated privileges in
     24        # execute_async_script.
     25        result = self.marionette.execute_async_script(
     26            """
     27            let result = Ci.nsIPermissionManager.ALLOW_ACTION;
     28            arguments[0](result);
     29        """,
     30            sandbox="system",
     31        )
     32        self.assertEqual(result, 1)
     33 
     34    def test_execute_switch_sandboxes(self):
     35        # Test that sandboxes are retained when switching between them
     36        # for execute_script.
     37        self.marionette.execute_script("foo = 1", sandbox="1")
     38        self.marionette.execute_script("foo = 2", sandbox="2")
     39        foo = self.marionette.execute_script(
     40            "return foo", sandbox="1", new_sandbox=False
     41        )
     42        self.assertEqual(foo, 1)
     43        foo = self.marionette.execute_script(
     44            "return foo", sandbox="2", new_sandbox=False
     45        )
     46        self.assertEqual(foo, 2)
     47 
     48    def test_execute_new_sandbox(self):
     49        # test that clearing a sandbox does not affect other sandboxes
     50        self.marionette.execute_script("foo = 1", sandbox="1")
     51        self.marionette.execute_script("foo = 2", sandbox="2")
     52 
     53        # deprecate sandbox 1 by asking explicitly for a fresh one
     54        with self.assertRaises(JavascriptException):
     55            self.marionette.execute_script(
     56                """
     57                return foo
     58            """,
     59                sandbox="1",
     60                new_sandbox=True,
     61            )
     62 
     63        foo = self.marionette.execute_script(
     64            "return foo", sandbox="2", new_sandbox=False
     65        )
     66        self.assertEqual(foo, 2)
     67 
     68    def test_execute_async_switch_sandboxes(self):
     69        # Test that sandboxes are retained when switching between them
     70        # for execute_async_script.
     71        self.marionette.execute_async_script("foo = 1; arguments[0]();", sandbox="1")
     72        self.marionette.execute_async_script("foo = 2; arguments[0]();", sandbox="2")
     73        foo = self.marionette.execute_async_script(
     74            "arguments[0](foo);", sandbox="1", new_sandbox=False
     75        )
     76        self.assertEqual(foo, 1)
     77        foo = self.marionette.execute_async_script(
     78            "arguments[0](foo);", sandbox="2", new_sandbox=False
     79        )
     80        self.assertEqual(foo, 2)
     81 
     82 
     83 class TestExecuteSandboxesChrome(TestExecuteSandboxes):
     84    def setUp(self):
     85        super(TestExecuteSandboxesChrome, self).setUp()
     86        self.marionette.set_context("chrome")