diff options
Diffstat (limited to 'toolkit/components/cleardata/tests/marionette')
3 files changed, 209 insertions, 0 deletions
diff --git a/toolkit/components/cleardata/tests/marionette/manifest.toml b/toolkit/components/cleardata/tests/marionette/manifest.toml new file mode 100644 index 0000000000..b9344786c8 --- /dev/null +++ b/toolkit/components/cleardata/tests/marionette/manifest.toml @@ -0,0 +1,5 @@ +[DEFAULT] + +["test_moved_origin_directory_cleanup.py"] + +["test_service_worker_at_shutdown.py"] diff --git a/toolkit/components/cleardata/tests/marionette/test_moved_origin_directory_cleanup.py b/toolkit/components/cleardata/tests/marionette/test_moved_origin_directory_cleanup.py new file mode 100644 index 0000000000..876f86cd32 --- /dev/null +++ b/toolkit/components/cleardata/tests/marionette/test_moved_origin_directory_cleanup.py @@ -0,0 +1,142 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +from pathlib import Path + +from marionette_driver import Wait +from marionette_harness import MarionetteTestCase + + +class MovedOriginDirectoryCleanupTestCase(MarionetteTestCase): + def setUp(self): + super().setUp() + self.marionette.enforce_gecko_prefs( + { + "privacy.sanitize.sanitizeOnShutdown": True, + "privacy.clearOnShutdown.offlineApps": True, + "dom.quotaManager.backgroundTask.enabled": False, + } + ) + self.moved_origin_directory = ( + Path(self.marionette.profile_path) / "storage" / "to-be-removed" / "foo" + ) + self.moved_origin_directory.mkdir(parents=True, exist_ok=True) + self.to_be_removed_directory = ( + Path(self.marionette.profile_path) / "storage" / "to-be-removed" + ) + + # Add a cookie to get a principal to be cleaned up + with self.marionette.using_context("chrome"): + self.marionette.execute_script( + """ + Services.cookies.add( + "example.local", + "path", + "name", + "value", + false, + false, + false, + Math.floor(Date.now() / 1000) + 24 * 60 * 60, + {}, + Ci.nsICookie.SAMESITE_NONE, + Ci.nsICookie.SCHEME_UNSET + ); + """ + ) + + def removeAllCookies(self): + with self.marionette.using_context("chrome"): + self.marionette.execute_script( + """ + Services.cookies.removeAll(); + """ + ) + + def test_ensure_cleanup_by_quit(self): + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must exist", + ) + + # Cleanup happens via Sanitizer.sanitizeOnShutdown + self.marionette.quit() + + self.assertFalse( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must disappear", + ) + + def test_ensure_cleanup_at_crashed_restart(self): + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must exist", + ) + + # Pending sanitization is added via Sanitizer.onStartup + # "offlineApps" corresponds to CLEAR_DOM_STORAGES + Wait(self.marionette).until( + lambda _: ( + "offlineApps" in self.marionette.get_pref("privacy.sanitize.pending"), + ), + message="privacy.sanitize.pending must include offlineApps", + ) + + # Cleanup happens via Sanitizer.onStartup after restart + self.marionette.restart(in_app=False) + + # Wait longer for 30 sec for the restart to finish, given bug 1814281. + Wait(self.marionette, timeout=30).until( + lambda _: not self.moved_origin_directory.exists(), + message="to-be-removed subdirectory must disappear", + ) + + def test_ensure_cleanup_by_quit_with_background_task(self): + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must exist", + ) + + self.marionette.set_pref("dom.quotaManager.backgroundTask.enabled", True) + + # Cleanup happens via Sanitizer.sanitizeOnShutdown + self.marionette.quit() + + Wait(self.marionette).until( + lambda _: not self.moved_origin_directory.exists(), + message="to-be-removed subdirectory must disappear", + ) + self.assertTrue( + self.to_be_removed_directory.exists(), + "to-be-removed parent directory should still be alive", + ) + + def test_ensure_no_cleanup_when_disabled(self): + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must exist", + ) + + self.marionette.set_pref("privacy.sanitize.sanitizeOnShutdown", False) + self.marionette.quit() + + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must not disappear", + ) + + def test_ensure_no_cleanup_when_no_cookie(self): + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must exist", + ) + + self.removeAllCookies() + + self.marionette.quit() + + self.assertTrue( + self.moved_origin_directory.exists(), + "to-be-removed subdirectory must not disappear", + ) diff --git a/toolkit/components/cleardata/tests/marionette/test_service_worker_at_shutdown.py b/toolkit/components/cleardata/tests/marionette/test_service_worker_at_shutdown.py new file mode 100644 index 0000000000..9738c0ea17 --- /dev/null +++ b/toolkit/components/cleardata/tests/marionette/test_service_worker_at_shutdown.py @@ -0,0 +1,62 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +from marionette_driver import Wait +from marionette_harness import MarionetteTestCase + + +class ServiceWorkerAtShutdownTestCase(MarionetteTestCase): + def setUp(self): + super(ServiceWorkerAtShutdownTestCase, self).setUp() + self.install_service_worker() + self.set_pref_to_delete_site_data_on_shutdown() + + def tearDown(self): + self.marionette.restart(in_app=False, clean=True) + super(ServiceWorkerAtShutdownTestCase, self).tearDown() + + def install_service_worker(self): + install_url = self.marionette.absolute_url( + "serviceworker/install_serviceworker.html" + ) + self.marionette.navigate(install_url) + # Make sure 'install_url' is not loaded on startup, it would reinstall the service worker + dummy_url = self.marionette.absolute_url("dummy.html") + self.marionette.navigate(dummy_url) + Wait(self.marionette).until(lambda _: self.is_service_worker_registered) + + def set_pref_to_delete_site_data_on_shutdown(self): + self.marionette.set_pref("privacy.sanitize.sanitizeOnShutdown", True) + self.marionette.set_pref("privacy.clearOnShutdown.offlineApps", True) + + def test_unregistering_service_worker_when_clearing_data(self): + self.marionette.restart(clean=False, in_app=True) + self.assertFalse(self.is_service_worker_registered) + + @property + def is_service_worker_registered(self): + with self.marionette.using_context("chrome"): + return self.marionette.execute_script( + """ + let serviceWorkerManager = Cc["@mozilla.org/serviceworkers/manager;1"].getService( + Ci.nsIServiceWorkerManager + ); + + let principal = + Services.scriptSecurityManager.createContentPrincipalFromOrigin(arguments[0]); + + let serviceWorkers = serviceWorkerManager.getAllRegistrations(); + for (let i = 0; i < serviceWorkers.length; i++) { + let sw = serviceWorkers.queryElementAt( + i, + Ci.nsIServiceWorkerRegistrationInfo + ); + if (sw.principal.origin == principal.origin) { + return true; + } + } + return false; + """, + script_args=(self.marionette.absolute_url(""),), + ) |