extensions/cookie/test/unit/test_permmanager_subdomains.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/test/unit/test_permmanager_subdomains.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function getPrincipalFromURI(uri) {
     1.8 +  return Cc["@mozilla.org/scriptsecuritymanager;1"]
     1.9 +           .getService(Ci.nsIScriptSecurityManager)
    1.10 +           .getNoAppCodebasePrincipal(NetUtil.newURI(uri));
    1.11 +}
    1.12 +
    1.13 +function run_test() {
    1.14 +  var pm = Cc["@mozilla.org/permissionmanager;1"].
    1.15 +           getService(Ci.nsIPermissionManager);
    1.16 +
    1.17 +  // Adds a permission to a sub-domain. Checks if it is working.
    1.18 +  let sub1Principal = getPrincipalFromURI("http://sub1.example.com");
    1.19 +  pm.addFromPrincipal(sub1Principal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
    1.20 +  do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.ALLOW_ACTION);
    1.21 +
    1.22 +  // A sub-sub-domain should get the permission.
    1.23 +  let subsubPrincipal = getPrincipalFromURI("http://sub.sub1.example.com");
    1.24 +  do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
    1.25 +
    1.26 +  // Another sub-domain shouldn't get the permission.
    1.27 +  let sub2Principal = getPrincipalFromURI("http://sub2.example.com");
    1.28 +  do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.29 +
    1.30 +  // Remove current permissions.
    1.31 +  pm.removeFromPrincipal(sub1Principal, "test/subdomains");
    1.32 +  do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.33 +
    1.34 +  // Adding the permission to the main domain. Checks if it is working.
    1.35 +  let mainPrincipal = getPrincipalFromURI("http://example.com");
    1.36 +  pm.addFromPrincipal(mainPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
    1.37 +  do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
    1.38 +
    1.39 +  // All sub-domains should have the permission now.
    1.40 +  do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.ALLOW_ACTION);
    1.41 +  do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.ALLOW_ACTION);
    1.42 +  do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
    1.43 +
    1.44 +  // Remove current permissions.
    1.45 +  pm.removeFromPrincipal(mainPrincipal, "test/subdomains");
    1.46 +  do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.47 +  do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.48 +  do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.49 +  do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.50 +
    1.51 +  // A sanity check that the previous implementation wasn't passing...
    1.52 +  let crazyPrincipal = getPrincipalFromURI("http://com");
    1.53 +  pm.addFromPrincipal(crazyPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
    1.54 +  do_check_eq(pm.testPermissionFromPrincipal(crazyPrincipal, "test/subdomains"),  pm.ALLOW_ACTION);
    1.55 +  do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"),   pm.UNKNOWN_ACTION);
    1.56 +  do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"),   pm.UNKNOWN_ACTION);
    1.57 +  do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"),   pm.UNKNOWN_ACTION);
    1.58 +  do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
    1.59 +}
    1.60 \ No newline at end of file

mercurial