Pier Angelo Vendrame pushed to branch tor-browser-140.4.0esr-15.0-1 at The Tor Project / Applications / Tor Browser

Commits:

2 changed files:

Changes:

  • testing/tor/marionette.toml
    1 1
     [DEFAULT]
    
    2 2
     
    
    3
    +["test_circuit_isolation.py"]
    
    3 4
     ["test_network_check.py"]

  • testing/tor/test_circuit_isolation.py
    1
    +from ipaddress import ip_address
    
    2
    +
    
    3
    +from marionette_driver import By
    
    4
    +from marionette_driver.errors import NoSuchElementException
    
    5
    +from marionette_harness import MarionetteTestCase
    
    6
    +
    
    7
    +TOR_BOOTSTRAP_TIMEOUT = 30000  # 30s
    
    8
    +
    
    9
    +
    
    10
    +class TestCircuitIsolation(MarionetteTestCase):
    
    11
    +
    
    12
    +    def bootstrap(self):
    
    13
    +        with self.marionette.using_context("chrome"):
    
    14
    +            self.marionette.execute_async_script(
    
    15
    +                """
    
    16
    +                const { TorConnect, TorConnectTopics } = ChromeUtils.importESModule(
    
    17
    +                    "resource://gre/modules/TorConnect.sys.mjs"
    
    18
    +                );
    
    19
    +                const [resolve] = arguments;
    
    20
    +
    
    21
    +                function waitForBootstrap() {
    
    22
    +                    const topic = TorConnectTopics.BootstrapComplete;
    
    23
    +                    Services.obs.addObserver(function observer() {
    
    24
    +                        Services.obs.removeObserver(observer, topic);
    
    25
    +                        resolve();
    
    26
    +                    }, topic);
    
    27
    +                    TorConnect.beginBootstrapping();
    
    28
    +                }
    
    29
    +
    
    30
    +                const stageTopic = TorConnectTopics.StageChange;
    
    31
    +                function stageObserver() {
    
    32
    +                    if (TorConnect.canBeginNormalBootstrap) {
    
    33
    +                        Services.obs.removeObserver(stageObserver, stageTopic);
    
    34
    +                        waitForBootstrap();
    
    35
    +                    }
    
    36
    +                }
    
    37
    +                Services.obs.addObserver(stageObserver, stageTopic);
    
    38
    +                stageObserver();
    
    39
    +                """,
    
    40
    +                script_timeout=TOR_BOOTSTRAP_TIMEOUT,
    
    41
    +            )
    
    42
    +
    
    43
    +    def extract_from_check_tpo(self):
    
    44
    +        # Fetch the IP from check.torproject.org.
    
    45
    +        # In addition to that, since we are loading this page, we
    
    46
    +        # perform some additional sanity checks.
    
    47
    +        self.marionette.navigate("https://check.torproject.org/")
    
    48
    +        # When check.tpo's check succeed (i.e., it thinks we're
    
    49
    +        # connecting through tor), we should be able to find a h1.on,
    
    50
    +        # with some message...
    
    51
    +        on = self.marionette.find_element(By.CLASS_NAME, "on")
    
    52
    +        self.assertIsNotNone(
    
    53
    +            on,
    
    54
    +            "h1.on not found, you might not be connected through tor",
    
    55
    +        )
    
    56
    +        # ... but if it fails, the message is inside a h1.off. We want
    
    57
    +        # to make sure we do not find that either (even though there is
    
    58
    +        # no reason for both of the h1 to be outputted at the moment).
    
    59
    +        self.assertRaises(
    
    60
    +            NoSuchElementException,
    
    61
    +            self.marionette.find_element,
    
    62
    +            By.CLASS_NAME,
    
    63
    +            "off",
    
    64
    +        )
    
    65
    +        ip = self.marionette.find_element(By.TAG_NAME, "strong")
    
    66
    +        return ip_address(ip.text.strip())
    
    67
    +
    
    68
    +    def extract_generic(self, url):
    
    69
    +        # Fetch the IP address from any generic page that only contains
    
    70
    +        # the address.
    
    71
    +        self.marionette.navigate(url)
    
    72
    +        return ip_address(
    
    73
    +            self.marionette.execute_script(
    
    74
    +                "return document.documentElement.textContent"
    
    75
    +            ).strip()
    
    76
    +        )
    
    77
    +
    
    78
    +    def test_circuit_isolation(self):
    
    79
    +        self.bootstrap()
    
    80
    +        ips = [
    
    81
    +            self.extract_from_check_tpo(),
    
    82
    +            self.extract_generic("https://am.i.mullvad.net/ip"),
    
    83
    +            self.extract_generic("https://test1.ifconfig.me/ip"),
    
    84
    +        ]
    
    85
    +        self.logger.info(f"Found the following IP addresses: {ips}")
    
    86
    +        unique_ips = set(ips)
    
    87
    +        self.logger.info(f"Found the following unique IP addresses: {unique_ips}")
    
    88
    +        self.assertEqual(
    
    89
    +            len(ips),
    
    90
    +            len(unique_ips),
    
    91
    +            "Some of the IP addresses we got are not unique.",
    
    92
    +        )
    
    93
    +        duplicate = self.extract_generic("https://test2.ifconfig.me/ip")
    
    94
    +        self.assertEqual(
    
    95
    +            ips[-1],
    
    96
    +            duplicate,
    
    97
    +            "Two IPs that were expected to be equal are different, we might be over isolating!",
    
    98
    +        )