michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function getPrincipalFromURI(uri) { michael@0: return Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Ci.nsIScriptSecurityManager) michael@0: .getNoAppCodebasePrincipal(NetUtil.newURI(uri)); michael@0: } michael@0: michael@0: function run_test() { michael@0: var pm = Cc["@mozilla.org/permissionmanager;1"]. michael@0: getService(Ci.nsIPermissionManager); michael@0: michael@0: // Adds a permission to a sub-domain. Checks if it is working. michael@0: let sub1Principal = getPrincipalFromURI("http://sub1.example.com"); michael@0: pm.addFromPrincipal(sub1Principal, "test/subdomains", pm.ALLOW_ACTION, 0, 0); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: michael@0: // A sub-sub-domain should get the permission. michael@0: let subsubPrincipal = getPrincipalFromURI("http://sub.sub1.example.com"); michael@0: do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: michael@0: // Another sub-domain shouldn't get the permission. michael@0: let sub2Principal = getPrincipalFromURI("http://sub2.example.com"); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: michael@0: // Remove current permissions. michael@0: pm.removeFromPrincipal(sub1Principal, "test/subdomains"); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: michael@0: // Adding the permission to the main domain. Checks if it is working. michael@0: let mainPrincipal = getPrincipalFromURI("http://example.com"); michael@0: pm.addFromPrincipal(mainPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0); michael@0: do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: michael@0: // All sub-domains should have the permission now. michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: michael@0: // Remove current permissions. michael@0: pm.removeFromPrincipal(mainPrincipal, "test/subdomains"); michael@0: do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: michael@0: // A sanity check that the previous implementation wasn't passing... michael@0: let crazyPrincipal = getPrincipalFromURI("http://com"); michael@0: pm.addFromPrincipal(crazyPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0); michael@0: do_check_eq(pm.testPermissionFromPrincipal(crazyPrincipal, "test/subdomains"), pm.ALLOW_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION); michael@0: }