test_initial_download.py (4899B)
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 os 6 from functools import reduce 7 8 from marionette_driver import Wait 9 from marionette_harness import MarionetteTestCase 10 11 12 class TestSafeBrowsingInitialDownload(MarionetteTestCase): 13 shavar_file_extensions = [ 14 "vlpset", 15 "sbstore", 16 ] 17 18 protobuf_file_extensions = [ 19 "vlpset", 20 "metadata", 21 ] 22 23 prefs_download_lists = [ 24 "urlclassifier.blockedTable", 25 "urlclassifier.downloadAllowTable", 26 "urlclassifier.downloadBlockTable", 27 "urlclassifier.malwareTable", 28 "urlclassifier.phishTable", 29 "urlclassifier.trackingTable", 30 "urlclassifier.trackingWhitelistTable", 31 ] 32 33 prefs_provider_update_time = { 34 # Force an immediate download of the safebrowsing files 35 "browser.safebrowsing.provider.mozilla.nextupdatetime": 1, 36 } 37 38 prefs_safebrowsing = { 39 "services.settings.server": "https://firefox.settings.services.mozilla.com/v1", 40 "browser.safebrowsing.debug": True, 41 "browser.safebrowsing.update.enabled": True, 42 } 43 44 def get_safebrowsing_files(self, is_v4): 45 files = [] 46 47 if is_v4: 48 my_file_extensions = self.protobuf_file_extensions 49 else: # v2 50 my_file_extensions = self.shavar_file_extensions 51 52 for pref_name in self.prefs_download_lists: 53 base_names = self.marionette.get_pref(pref_name).split(",") 54 55 # moztest- lists are not saved to disk 56 # pylint --py3k: W1639 57 base_names = list( 58 filter(lambda x: not x.startswith("moztest-"), base_names) 59 ) 60 61 for ext in my_file_extensions: 62 files.extend([ 63 f"{f}.{ext}" 64 for f in base_names 65 if f and f.endswith("-proto") == is_v4 66 ]) 67 68 return set(sorted(files)) 69 70 def setUp(self): 71 super().setUp() 72 73 self.safebrowsing_shavar_files = self.get_safebrowsing_files(False) 74 if any( 75 f.startswith("goog-") or f.startswith("googpub-") 76 for f in self.safebrowsing_shavar_files 77 ): 78 self.prefs_provider_update_time.update({ 79 "browser.safebrowsing.provider.google.nextupdatetime": 1, 80 }) 81 82 # if V5 is enabled, we use the V5 update time to determine if the files 83 # have been downloaded. Otherwise, we use the V4 update time. 84 is_safebrowsing_v5_enabled = bool( 85 self.marionette.get_pref("browser.safebrowsing.provider.google5.enabled") 86 ) 87 88 self.safebrowsing_protobuf_files = self.get_safebrowsing_files(True) 89 if any( 90 f.startswith("goog-") or f.startswith("googpub-") 91 for f in self.safebrowsing_protobuf_files 92 ): 93 if is_safebrowsing_v5_enabled: 94 self.prefs_provider_update_time.update({ 95 "browser.safebrowsing.provider.google5.nextupdatetime": 1, 96 }) 97 else: 98 self.prefs_provider_update_time.update({ 99 "browser.safebrowsing.provider.google4.nextupdatetime": 1, 100 }) 101 102 # Force the preferences for the new profile 103 enforce_prefs = self.prefs_safebrowsing 104 enforce_prefs.update(self.prefs_provider_update_time) 105 self.marionette.enforce_gecko_prefs(enforce_prefs) 106 107 self.safebrowsing_path = os.path.join( 108 self.marionette.instance.profile.profile, "safebrowsing" 109 ) 110 111 def tearDown(self): 112 try: 113 # Restart with a fresh profile 114 self.marionette.restart(in_app=False, clean=True) 115 finally: 116 super().tearDown() 117 118 def test_safe_browsing_initial_download(self): 119 def check_downloaded(_): 120 return reduce( 121 lambda state, pref: state and int(self.marionette.get_pref(pref)) != 1, 122 list(self.prefs_provider_update_time), 123 True, 124 ) 125 126 try: 127 Wait(self.marionette, timeout=170).until( 128 check_downloaded, 129 message="Not all safebrowsing files have been downloaded", 130 ) 131 finally: 132 files_on_disk_toplevel = os.listdir(self.safebrowsing_path) 133 for f in self.safebrowsing_shavar_files: 134 self.assertIn(f, files_on_disk_toplevel) 135 136 if len(self.safebrowsing_protobuf_files) > 0: 137 files_on_disk_google4 = os.listdir( 138 os.path.join(self.safebrowsing_path, "google4") 139 ) 140 for f in self.safebrowsing_protobuf_files: 141 self.assertIn(f, files_on_disk_google4)