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