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

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

mercurial