extensions/cookie/test/unit/test_permmanager_cleardata.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 let pm;
michael@0 5
michael@0 6 // Create a principal based on the { origin, appId, browserElement }.
michael@0 7 function createPrincipal(aOrigin, aAppId, aBrowserElement)
michael@0 8 {
michael@0 9 return Services.scriptSecurityManager.getAppCodebasePrincipal(NetUtil.newURI(aOrigin), aAppId, aBrowserElement);
michael@0 10 }
michael@0 11
michael@0 12 // Return the subject required by 'webapps-clear-data' notification.
michael@0 13 function getSubject(aAppId, aBrowserOnly)
michael@0 14 {
michael@0 15 return {
michael@0 16 appId: aAppId,
michael@0 17 browserOnly: aBrowserOnly,
michael@0 18 QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams])
michael@0 19 };
michael@0 20 }
michael@0 21
michael@0 22 // Use aEntries to create principals, add permissions to them and check that they have them.
michael@0 23 // Then, it is notifying 'webapps-clear-data' with the given aSubject and check if the permissions
michael@0 24 // of principals[i] matches the permission in aResults[i].
michael@0 25 function test(aEntries, aSubject, aResults)
michael@0 26 {
michael@0 27 let principals = [];
michael@0 28
michael@0 29 for (entry of aEntries) {
michael@0 30 principals.push(createPrincipal(entry.origin, entry.appId, entry.browserElement));
michael@0 31 }
michael@0 32
michael@0 33 for (principal of principals) {
michael@0 34 do_check_eq(pm.testPermissionFromPrincipal(principal, "test/webapps-clear"), pm.UNKNOWN_ACTION);
michael@0 35 pm.addFromPrincipal(principal, "test/webapps-clear", pm.ALLOW_ACTION, pm.EXPIRE_NEVER, 0);
michael@0 36 do_check_eq(pm.testPermissionFromPrincipal(principal, "test/webapps-clear"), pm.ALLOW_ACTION);
michael@0 37 }
michael@0 38
michael@0 39 Services.obs.notifyObservers(aSubject, 'webapps-clear-data', null);
michael@0 40
michael@0 41 var length = aEntries.length;
michael@0 42 for (let i=0; i<length; ++i) {
michael@0 43 do_check_eq(pm.testPermissionFromPrincipal(principals[i], 'test/webapps-clear'), aResults[i]);
michael@0 44
michael@0 45 // Remove allowed actions.
michael@0 46 if (aResults[i] == pm.ALLOW_ACTION) {
michael@0 47 pm.removeFromPrincipal(principals[i], 'test/webapps-clear');
michael@0 48 }
michael@0 49 }
michael@0 50 }
michael@0 51
michael@0 52 function run_test()
michael@0 53 {
michael@0 54 do_get_profile();
michael@0 55
michael@0 56 pm = Cc["@mozilla.org/permissionmanager;1"]
michael@0 57 .getService(Ci.nsIPermissionManager);
michael@0 58
michael@0 59 let entries = [
michael@0 60 { origin: 'http://example.com', appId: 1, browserElement: false },
michael@0 61 { origin: 'http://example.com', appId: 1, browserElement: true },
michael@0 62 { origin: 'http://example.com', appId: Ci.nsIScriptSecurityManager.NO_APPID, browserElement: false },
michael@0 63 { origin: 'http://example.com', appId: 2, browserElement: false },
michael@0 64 ];
michael@0 65
michael@0 66 // In that case, all permissions from app 1 should be removed but not the other ones.
michael@0 67 test(entries, getSubject(1, false), [ pm.UNKNOWN_ACTION, pm.UNKNOWN_ACTION, pm.ALLOW_ACTION, pm.ALLOW_ACTION ]);
michael@0 68
michael@0 69 // In that case, only the permissions of app 1 related to a browserElement should be removed.
michael@0 70 // All the other permissions should stay.
michael@0 71 test(entries, getSubject(1, true), [ pm.ALLOW_ACTION, pm.UNKNOWN_ACTION, pm.ALLOW_ACTION, pm.ALLOW_ACTION ]);
michael@0 72 }

mercurial