tor-browser

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

test_notification.py (5552B)


      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 time
      6 
      7 from marionette_driver import By, Wait, expected
      8 from marionette_harness import MarionetteTestCase, WindowManagerMixin
      9 
     10 
     11 class TestSafeBrowsingNotificationBar(WindowManagerMixin, MarionetteTestCase):
     12    def setUp(self):
     13        super().setUp()
     14 
     15        self.test_data = [
     16            # Unwanted software URL
     17            {"unsafe_page": "https://www.itisatrap.org/firefox/unwanted.html"},
     18            # Phishing URL info
     19            {"unsafe_page": "https://www.itisatrap.org/firefox/its-a-trap.html"},
     20            # Malware URL object
     21            {"unsafe_page": "https://www.itisatrap.org/firefox/its-an-attack.html"},
     22        ]
     23 
     24        self.default_homepage = self.marionette.get_pref("browser.startup.homepage")
     25 
     26        self.marionette.set_pref("browser.safebrowsing.phishing.enabled", True)
     27        self.marionette.set_pref("browser.safebrowsing.malware.enabled", True)
     28 
     29        # Give the browser a little time, because SafeBrowsing.sys.mjs takes a while
     30        # between start up and adding the example urls to the db.
     31        # hg.mozilla.org/mozilla-central/file/46aebcd9481e/browser/base/content/browser.js#l1194
     32        time.sleep(3)
     33 
     34        # Run this test in a new tab.
     35        new_tab = self.open_tab()
     36        self.marionette.switch_to_window(new_tab)
     37 
     38    def tearDown(self):
     39        try:
     40            self.marionette.clear_pref("browser.safebrowsing.phishing.enabled")
     41            self.marionette.clear_pref("browser.safebrowsing.malware.enabled")
     42 
     43            self.remove_permission("https://www.itisatrap.org", "safe-browsing")
     44            self.close_all_tabs()
     45        finally:
     46            super().tearDown()
     47 
     48    def test_notification_bar(self):
     49        for item in self.test_data:
     50            unsafe_page = item["unsafe_page"]
     51 
     52            # Return to the unsafe page
     53            # Check "ignore warning" link then notification bar's "get me out" button
     54            self.marionette.navigate(unsafe_page)
     55            # Wait for the DOM to receive events for about:blocked
     56            time.sleep(1)
     57            self.check_ignore_warning_link(unsafe_page)
     58            self.check_get_me_out_of_here_button()
     59 
     60            # Return to the unsafe page
     61            # Check "ignore warning" link then notification bar's "X" button
     62            self.marionette.navigate(unsafe_page)
     63            # Wait for the DOM to receive events for about:blocked
     64            time.sleep(1)
     65            self.check_ignore_warning_link(unsafe_page)
     66            self.check_x_button()
     67 
     68    def get_final_url(self, url):
     69        self.marionette.navigate(url)
     70        return self.marionette.get_url()
     71 
     72    def remove_permission(self, host, permission):
     73        with self.marionette.using_context("chrome"):
     74            self.marionette.execute_script(
     75                """
     76              let uri = Services.io.newURI(arguments[0], null, null);
     77              let principal = Services.scriptSecurityManager.createContentPrincipal(uri, {});
     78              Services.perms.removeFromPrincipal(principal, arguments[1]);
     79            """,
     80                script_args=[host, permission],
     81            )
     82 
     83    def check_ignore_warning_link(self, unsafe_page):
     84        button = self.marionette.find_element(By.ID, "seeDetailsButton")
     85        button.click()
     86        time.sleep(1)
     87        link = self.marionette.find_element(By.ID, "ignore_warning_link")
     88        link.click()
     89 
     90        Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
     91            expected.element_present(By.ID, "main-feature"),
     92            message='Expected target element "#main-feature" has not been found',
     93        )
     94        self.assertEqual(self.marionette.get_url(), self.get_final_url(unsafe_page))
     95 
     96        # Clean up here since the permission gets set in this function
     97        self.remove_permission("https://www.itisatrap.org", "safe-browsing")
     98 
     99    def check_get_me_out_of_here_button(self):
    100        with self.marionette.using_context("chrome"):
    101            notification_box = self.marionette.find_element(
    102                By.CSS_SELECTOR, 'vbox.notificationbox-stack[slot="selected"]'
    103            )
    104            message = notification_box.find_element(
    105                By.CSS_SELECTOR, "notification-message"
    106            )
    107            button_container = message.get_property("buttonContainer")
    108            button = button_container.find_element(
    109                By.CSS_SELECTOR, 'button[label="Get me out of here!"]'
    110            )
    111            button.click()
    112 
    113        Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
    114            lambda mn: self.default_homepage in mn.get_url(),
    115            message="The default home page has not been loaded",
    116        )
    117 
    118    def check_x_button(self):
    119        with self.marionette.using_context("chrome"):
    120            notification_box = self.marionette.find_element(
    121                By.CSS_SELECTOR, 'vbox.notificationbox-stack[slot="selected"]'
    122            )
    123            message = notification_box.find_element(
    124                By.CSS_SELECTOR, "notification-message[value=blocked-badware-page]"
    125            )
    126            button = message.get_property("closeButton")
    127            button.click()
    128 
    129            Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
    130                expected.element_stale(button),
    131                message="The notification bar has not been closed",
    132            )