test_https_first.py (2746B)
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 # This file contains tests that verify various HTTPS-First upgrade scenarios 6 # by loading requested URLs into the browser's location bar. 7 # 8 # These tests allow nonlocal connections to real websites, ensuring that the 9 # feature works as intended. 10 # 11 # Additional HTTPS-First tests can be found at: dom/security/test/https-first 12 13 from marionette_driver import By, Keys, Wait 14 from marionette_driver.errors import JavascriptException 15 from marionette_harness import MarionetteTestCase, WindowManagerMixin 16 17 18 class TestHTTPSFirst(WindowManagerMixin, MarionetteTestCase): 19 def setUp(self): 20 super().setUp() 21 22 self.http_url = "http://example.org/" 23 self.https_url = "https://example.org/" 24 self.schemeless_url = "example.org" 25 26 self.http_only_url = "http://http.badssl.com/" 27 28 def tearDown(self): 29 with self.marionette.using_context("chrome"): 30 self.marionette.execute_script("Services.perms.removeAll();") 31 32 super().tearDown() 33 34 def test_no_upgrade_with_http_only_site(self): 35 self.navigate_in_urlbar(self.http_only_url) 36 self.wait_for_page_navigated( 37 self.http_only_url, "Expected no HTTPS-First upgrade for HTTP-only site" 38 ) 39 40 def test_no_upgrade_with_http_scheme(self): 41 self.navigate_in_urlbar(self.http_url) 42 self.wait_for_page_navigated( 43 self.http_url, f"Expected no HTTPS-First upgrade for {self.http_url}" 44 ) 45 46 def test_upgrade_with_schemeless_url(self): 47 self.navigate_in_urlbar(self.schemeless_url) 48 self.wait_for_page_navigated( 49 self.https_url, f"Expected HTTPS-First upgrade to {self.https_url}" 50 ) 51 52 def navigate_in_urlbar(self, url): 53 with self.marionette.using_context("chrome"): 54 urlbar = self.marionette.find_element(By.ID, "urlbar-input") 55 urlbar.clear() 56 urlbar.send_keys(url + Keys.ENTER) 57 58 def wait_for_page_navigated(self, target_url, message): 59 def navigated(m): 60 return self.marionette.execute_script( 61 """ 62 const [url] = arguments; 63 64 return ["interactive", "complete"].includes(document.readyState) && 65 window.location.href == url; 66 """, 67 script_args=[target_url], 68 ) 69 70 Wait( 71 self.marionette, 72 ignored_exceptions=[JavascriptException], 73 timeout=self.marionette.session_capabilities["timeouts"]["pageLoad"], 74 ).until(navigated, message=message)