test_browserGlue_migration_removeContentBlockerPerms.js (2306B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; 5 const TOPICDATA_BROWSERGLUE_TEST = "force-ui-migration"; 6 const UI_VERSION = 147; 7 const CONTENT_BLOCKER_PERM_TYPES = [ 8 "other", 9 "script", 10 "image", 11 "stylesheet", 12 "object", 13 "document", 14 "subdocument", 15 "refresh", 16 "xbl", 17 "ping", 18 "xmlhttprequest", 19 "objectsubrequest", 20 "dtd", 21 "font", 22 "websocket", 23 "csp_report", 24 "xslt", 25 "beacon", 26 "fetch", 27 "manifest", 28 "speculative", 29 ]; 30 31 const gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService( 32 Ci.nsIObserver 33 ); 34 35 // Test to check if migration resets default permissions properly. 36 add_task(async function test_removeContentBlockerPerms() { 37 registerCleanupFunction(() => { 38 Services.prefs.clearUserPref("browser.migration.version"); 39 Services.perms.removeAll(); 40 }); 41 42 Services.perms.removeAll(); 43 Services.prefs.setIntPref("browser.migration.version", UI_VERSION); 44 45 let pm = Services.perms; 46 47 // Add a permission for each nsContentBlocker type 48 CONTENT_BLOCKER_PERM_TYPES.forEach(type => { 49 pm.addFromPrincipal( 50 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 51 "https://www.mozilla.org" 52 ), 53 type, 54 pm.ALLOW_ACTION 55 ); 56 }); 57 58 // Check all permissions were added 59 let remaining_perms = pm.getAllByTypes(CONTENT_BLOCKER_PERM_TYPES); 60 Assert.equal( 61 remaining_perms.length, 62 CONTENT_BLOCKER_PERM_TYPES.length, 63 `Number permissions added vs. expected` 64 ); 65 66 // Check default permissions are present 67 let nonCBPerms = pm.all.length - CONTENT_BLOCKER_PERM_TYPES.length; 68 Assert.greater( 69 nonCBPerms, 70 0, 71 "Check at least one non-content blocker permission is present" 72 ); 73 74 // Simulate a migration. 75 gBrowserGlue.observe( 76 null, 77 TOPIC_BROWSERGLUE_TEST, 78 TOPICDATA_BROWSERGLUE_TEST 79 ); 80 81 // Check all permissions were deleted 82 remaining_perms = pm.getAllByTypes(CONTENT_BLOCKER_PERM_TYPES); 83 Assert.equal( 84 remaining_perms.length, 85 0, 86 `All content blocker permissions should be deleted` 87 ); 88 89 // Check rest of permissions were not deleted 90 Assert.equal( 91 pm.all.length, 92 nonCBPerms, 93 "Default permissions should not be deleted" 94 ); 95 });