1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/cookie/test/unit/test_permmanager_cleardata.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +let pm; 1.8 + 1.9 +// Create a principal based on the { origin, appId, browserElement }. 1.10 +function createPrincipal(aOrigin, aAppId, aBrowserElement) 1.11 +{ 1.12 + return Services.scriptSecurityManager.getAppCodebasePrincipal(NetUtil.newURI(aOrigin), aAppId, aBrowserElement); 1.13 +} 1.14 + 1.15 +// Return the subject required by 'webapps-clear-data' notification. 1.16 +function getSubject(aAppId, aBrowserOnly) 1.17 +{ 1.18 + return { 1.19 + appId: aAppId, 1.20 + browserOnly: aBrowserOnly, 1.21 + QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams]) 1.22 + }; 1.23 +} 1.24 + 1.25 +// Use aEntries to create principals, add permissions to them and check that they have them. 1.26 +// Then, it is notifying 'webapps-clear-data' with the given aSubject and check if the permissions 1.27 +// of principals[i] matches the permission in aResults[i]. 1.28 +function test(aEntries, aSubject, aResults) 1.29 +{ 1.30 + let principals = []; 1.31 + 1.32 + for (entry of aEntries) { 1.33 + principals.push(createPrincipal(entry.origin, entry.appId, entry.browserElement)); 1.34 + } 1.35 + 1.36 + for (principal of principals) { 1.37 + do_check_eq(pm.testPermissionFromPrincipal(principal, "test/webapps-clear"), pm.UNKNOWN_ACTION); 1.38 + pm.addFromPrincipal(principal, "test/webapps-clear", pm.ALLOW_ACTION, pm.EXPIRE_NEVER, 0); 1.39 + do_check_eq(pm.testPermissionFromPrincipal(principal, "test/webapps-clear"), pm.ALLOW_ACTION); 1.40 + } 1.41 + 1.42 + Services.obs.notifyObservers(aSubject, 'webapps-clear-data', null); 1.43 + 1.44 + var length = aEntries.length; 1.45 + for (let i=0; i<length; ++i) { 1.46 + do_check_eq(pm.testPermissionFromPrincipal(principals[i], 'test/webapps-clear'), aResults[i]); 1.47 + 1.48 + // Remove allowed actions. 1.49 + if (aResults[i] == pm.ALLOW_ACTION) { 1.50 + pm.removeFromPrincipal(principals[i], 'test/webapps-clear'); 1.51 + } 1.52 + } 1.53 +} 1.54 + 1.55 +function run_test() 1.56 +{ 1.57 + do_get_profile(); 1.58 + 1.59 + pm = Cc["@mozilla.org/permissionmanager;1"] 1.60 + .getService(Ci.nsIPermissionManager); 1.61 + 1.62 + let entries = [ 1.63 + { origin: 'http://example.com', appId: 1, browserElement: false }, 1.64 + { origin: 'http://example.com', appId: 1, browserElement: true }, 1.65 + { origin: 'http://example.com', appId: Ci.nsIScriptSecurityManager.NO_APPID, browserElement: false }, 1.66 + { origin: 'http://example.com', appId: 2, browserElement: false }, 1.67 + ]; 1.68 + 1.69 + // In that case, all permissions from app 1 should be removed but not the other ones. 1.70 + test(entries, getSubject(1, false), [ pm.UNKNOWN_ACTION, pm.UNKNOWN_ACTION, pm.ALLOW_ACTION, pm.ALLOW_ACTION ]); 1.71 + 1.72 + // In that case, only the permissions of app 1 related to a browserElement should be removed. 1.73 + // All the other permissions should stay. 1.74 + test(entries, getSubject(1, true), [ pm.ALLOW_ACTION, pm.UNKNOWN_ACTION, pm.ALLOW_ACTION, pm.ALLOW_ACTION ]); 1.75 +}