test_permmanager_remove_add_update.js (2528B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 function check_enumerator(principal, permissions) { 5 let perms = Services.perms.getAllForPrincipal(principal); 6 for (let [type, capability, expireType] of permissions) { 7 let perm = perms.shift(); 8 Assert.notEqual(perm, null); 9 Assert.equal(perm.type, type); 10 Assert.equal(perm.capability, capability); 11 Assert.equal(perm.expireType, expireType); 12 } 13 Assert.ok(!perms.length); 14 } 15 16 add_task(async function test() { 17 Services.prefs.setCharPref("permissions.manager.defaultsUrl", ""); 18 19 // setup a profile directory 20 do_get_profile(); 21 22 // We need to execute a pm method to be sure that the DB is fully 23 // initialized. 24 var pm = Services.perms; 25 Assert.strictEqual(pm.all.length, 0); 26 27 let principal = 28 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 29 "http://example.com" 30 ); 31 32 info("From session to persistent"); 33 pm.addFromPrincipal( 34 principal, 35 "test/foo", 36 pm.ALLOW_ACTION, 37 pm.EXPIRE_SESSION 38 ); 39 40 check_enumerator(principal, [ 41 ["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_SESSION], 42 ]); 43 44 pm.addFromPrincipal(principal, "test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER); 45 46 check_enumerator(principal, [["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER]]); 47 48 // Let's reload the DB. 49 Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk"); 50 51 Assert.strictEqual(pm.all.length, 1); 52 check_enumerator(principal, [["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER]]); 53 54 info("From persistent to session"); 55 pm.addFromPrincipal( 56 principal, 57 "test/foo", 58 pm.ALLOW_ACTION, 59 pm.EXPIRE_SESSION 60 ); 61 62 check_enumerator(principal, [ 63 ["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_SESSION], 64 ]); 65 66 // Let's reload the DB. 67 Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk"); 68 Assert.strictEqual(pm.all.length, 0); 69 70 info("From persistent to persistent"); 71 pm.addFromPrincipal(principal, "test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER); 72 pm.addFromPrincipal(principal, "test/foo", pm.DENY_ACTION, pm.EXPIRE_NEVER); 73 74 check_enumerator(principal, [["test/foo", pm.DENY_ACTION, pm.EXPIRE_NEVER]]); 75 76 // Let's reload the DB. 77 Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk"); 78 Assert.strictEqual(pm.all.length, 1); 79 check_enumerator(principal, [["test/foo", pm.DENY_ACTION, pm.EXPIRE_NEVER]]); 80 81 info("Cleanup"); 82 pm.removeAll(); 83 check_enumerator(principal, []); 84 });