tor-browser

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

test_marionette.py (5594B)


      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 socket
      7 import time
      8 
      9 from marionette_driver import errors
     10 from marionette_driver.marionette import Marionette
     11 from marionette_harness import MarionetteTestCase, run_if_manage_instance
     12 
     13 
     14 class TestMarionette(MarionetteTestCase):
     15    def test_correct_test_name(self):
     16        """Test that the correct test name gets set."""
     17        expected_test_name = "{module}.py {cls}.{func}".format(
     18            module=__name__,
     19            cls=self.__class__.__name__,
     20            func=self.test_correct_test_name.__name__,
     21        )
     22 
     23        self.assertIn(expected_test_name, self.marionette.test_name)
     24 
     25    @run_if_manage_instance("Only runnable if Marionette manages the instance")
     26    def test_raise_for_port_non_existing_process(self):
     27        """Test that raise_for_port doesn't run into a timeout if instance is not running."""
     28        self.marionette.quit()
     29        self.assertIsNotNone(self.marionette.instance.runner.returncode)
     30        start_time = time.time()
     31        self.assertRaises(socket.timeout, self.marionette.raise_for_port, timeout=5)
     32        self.assertLess(time.time() - start_time, 5)
     33 
     34    @run_if_manage_instance("Only runnable if Marionette manages the instance")
     35    def test_marionette_active_port_file(self):
     36        active_port_file = os.path.join(
     37            self.marionette.instance.profile.profile, "MarionetteActivePort"
     38        )
     39        self.assertTrue(
     40            os.path.exists(active_port_file), "MarionetteActivePort file written"
     41        )
     42        with open(active_port_file, "r") as fp:
     43            lines = fp.readlines()
     44        self.assertEqual(len(lines), 1, "MarionetteActivePort file contains two lines")
     45        self.assertEqual(
     46            int(lines[0]),
     47            self.marionette.port,
     48            "MarionetteActivePort file contains port",
     49        )
     50 
     51        self.marionette.quit()
     52        self.assertFalse(
     53            os.path.exists(active_port_file), "MarionetteActivePort file removed"
     54        )
     55 
     56    def test_single_active_session(self):
     57        self.assertEqual(1, self.marionette.execute_script("return 1"))
     58 
     59        # Use a new Marionette instance for the connection attempt, while there is
     60        # still an active session present.
     61        marionette = Marionette(host=self.marionette.host, port=self.marionette.port)
     62        self.assertRaises(socket.timeout, marionette.raise_for_port, timeout=1.0)
     63 
     64    def test_disable_enable_new_connections(self):
     65        # Do not re-create socket if it already exists
     66        self.marionette._send_message("Marionette:AcceptConnections", {"value": True})
     67 
     68        try:
     69            # Disabling new connections does not affect the existing one.
     70            self.marionette._send_message(
     71                "Marionette:AcceptConnections", {"value": False}
     72            )
     73            self.assertEqual(1, self.marionette.execute_script("return 1"))
     74 
     75            # Delete the current active session to allow new connection attempts.
     76            self.marionette.delete_session()
     77 
     78            # Use a new Marionette instance for the connection attempt, that doesn't
     79            # handle an instance of the application to prevent a connection lost error.
     80            marionette = Marionette(
     81                host=self.marionette.host, port=self.marionette.port
     82            )
     83            self.assertRaises(socket.timeout, marionette.raise_for_port, timeout=1.0)
     84 
     85        finally:
     86            self.marionette.quit(in_app=False)
     87 
     88    def test_client_socket_uses_expected_socket_timeout(self):
     89        current_socket_timeout = self.marionette.socket_timeout
     90 
     91        self.assertEqual(current_socket_timeout, self.marionette.client.socket_timeout)
     92        self.assertEqual(
     93            current_socket_timeout,
     94            self.marionette.client._socket_context._sock.gettimeout(),
     95        )
     96 
     97    def test_application_update_disabled(self):
     98        # Updates of the application should always be disabled by default
     99        with self.marionette.using_context("chrome"):
    100            update_allowed = self.marionette.execute_script(
    101                """
    102              let aus = Cc['@mozilla.org/updates/update-service;1']
    103                        .getService(Ci.nsIApplicationUpdateService);
    104              return aus.canCheckForUpdates;
    105            """
    106            )
    107 
    108        self.assertFalse(update_allowed)
    109 
    110 
    111 class TestContext(MarionetteTestCase):
    112    def setUp(self):
    113        MarionetteTestCase.setUp(self)
    114        self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
    115 
    116    def get_context(self):
    117        return self.marionette._send_message("Marionette:GetContext", key="value")
    118 
    119    def set_context(self, value):
    120        return self.marionette._send_message("Marionette:SetContext", {"value": value})
    121 
    122    def test_set_context(self):
    123        self.assertEqual(self.set_context("content"), {"value": None})
    124        self.assertEqual(self.set_context("chrome"), {"value": None})
    125 
    126        for typ in [True, 42, [], {}, None]:
    127            with self.assertRaises(errors.InvalidArgumentException):
    128                self.set_context(typ)
    129 
    130        with self.assertRaises(errors.MarionetteException):
    131            self.set_context("foo")
    132 
    133    def test_get_context(self):
    134        self.assertEqual(self.get_context(), "content")
    135        self.set_context("chrome")
    136        self.assertEqual(self.get_context(), "chrome")
    137        self.set_context("content")
    138        self.assertEqual(self.get_context(), "content")