Commit 5803b213 authored by brizental's avatar brizental Committed by Pier Angelo Vendrame
Browse files

fixup! TB 43817: Add tests for Tor Browser

Bug 43243: Make test bootstrap UI agnostic -- share the code between
Android and Desktop.
parent e2c30270
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -28,5 +28,6 @@ from .runner import (
    TestManifest,
    TestResult,
    TestResultCollection,
    TorBrowserMixin,
    WindowManagerMixin,
)
+4 −1
Original line number Diff line number Diff line
@@ -13,4 +13,7 @@ from .base import (
    TestResult,
    TestResultCollection,
)
from .mixins import WindowManagerMixin
from .mixins import (
    TorBrowserMixin,
    WindowManagerMixin,
)
+1 −0
Original line number Diff line number Diff line
@@ -3,3 +3,4 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from .window_manager import WindowManagerMixin
from .tor_browser import TorBrowserMixin
+73 −0
Original line number Diff line number Diff line
from marionette_driver.errors import ScriptTimeoutException

DEFAULT_BOOTSTRAP_TIMEOUT_MS = 60 * 1000
DEFAULT_BOOTSTRAP_MAX_RETRIES = 3

class TorBrowserMixin:
    def bootstrap(
        self,
        max_retries=DEFAULT_BOOTSTRAP_MAX_RETRIES,
    ):
        """Bootstrap the Tor connection.

        This doesn't fail if already bootstrapped, but will retry a few times if
        a script timeout is hit.

        This function is UI-agnostic, meaning it can be used both on Desktop and Android.
        """

        attempt = 0
        while attempt < max_retries:
            try:
                with self.marionette.using_context("chrome"):
                    self.marionette.execute_async_script(
                        """
                        const { TorConnect, TorConnectStage, TorConnectTopics } = ChromeUtils.importESModule(
                            "resource://gre/modules/TorConnect.sys.mjs"
                        );
                        const [resolve] = arguments;

                        // Only the first test of a suite will need to bootstrap.
                        if (TorConnect.stage.name === TorConnectStage.Bootstrapped) {
                            resolve();
                            return;
                        }

                        function waitForBootstrap() {
                            const topic = TorConnectTopics.BootstrapComplete;
                            Services.obs.addObserver(function observer() {
                                Services.obs.removeObserver(observer, topic);
                                resolve();
                            }, topic);
                            TorConnect.beginBootstrapping();
                        }

                        const stageTopic = TorConnectTopics.StageChange;
                        function stageObserver() {
                            if (TorConnect.canBeginNormalBootstrap) {
                                Services.obs.removeObserver(stageObserver, stageTopic);
                                waitForBootstrap();
                            }
                        }
                        Services.obs.addObserver(stageObserver, stageTopic);
                        stageObserver();
                        """,
                        script_timeout=DEFAULT_BOOTSTRAP_TIMEOUT_MS,
                    )

                return
            except ScriptTimeoutException:
                attempt += 1
                with self.marionette.using_context("chrome"):
                    self.marionette.execute_script(
                        """
                        const { TorConnect } = ChromeUtils.importESModule(
                            "resource://gre/modules/TorConnect.sys.mjs"
                        );

                        TorConnect._makeStageRequest(TorConnectStage.Start, true);
                        """
                    )


        raise RuntimeError("Unable to connect to Tor Network")
+2 −36
Original line number Diff line number Diff line
@@ -2,47 +2,13 @@ from ipaddress import ip_address

from marionette_driver import By
from marionette_driver.errors import NoSuchElementException
from marionette_harness import MarionetteTestCase
from marionette_harness import MarionetteTestCase, TorBrowserMixin

TOR_BOOTSTRAP_TIMEOUT = 30000  # 30s


class TestCircuitIsolation(MarionetteTestCase):
class TestCircuitIsolation(MarionetteTestCase, TorBrowserMixin):
    def tearDown(self):
        self.marionette.restart(in_app=False, clean=True)
        super().tearDown()

    def bootstrap(self):
        with self.marionette.using_context("chrome"):
            self.marionette.execute_async_script(
                """
                const { TorConnect, TorConnectTopics } = ChromeUtils.importESModule(
                    "resource://gre/modules/TorConnect.sys.mjs"
                );
                const [resolve] = arguments;

                function waitForBootstrap() {
                    const topic = TorConnectTopics.BootstrapComplete;
                    Services.obs.addObserver(function observer() {
                        Services.obs.removeObserver(observer, topic);
                        resolve();
                    }, topic);
                    TorConnect.beginBootstrapping();
                }

                const stageTopic = TorConnectTopics.StageChange;
                function stageObserver() {
                    if (TorConnect.canBeginNormalBootstrap) {
                        Services.obs.removeObserver(stageObserver, stageTopic);
                        waitForBootstrap();
                    }
                }
                Services.obs.addObserver(stageObserver, stageTopic);
                stageObserver();
                """,
                script_timeout=TOR_BOOTSTRAP_TIMEOUT,
            )

    def extract_from_check_tpo(self):
        # Fetch the IP from check.torproject.org.
        # In addition to that, since we are loading this page, we
Loading