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 getSystemPrincipal() { michael@0: return Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Ci.nsIScriptSecurityManager) michael@0: .getSystemPrincipal(); 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: do_check_null(pm.getPermissionObject(getSystemPrincipal(), "test/pobject", false)); michael@0: michael@0: let principal = getPrincipalFromURI("http://example.com"); michael@0: let subPrincipal = getPrincipalFromURI("http://sub.example.com"); michael@0: let subSubPrincipal = getPrincipalFromURI("http://sub.sub.example.com"); michael@0: michael@0: do_check_null(pm.getPermissionObject(principal, "test/pobject", false)); michael@0: do_check_null(pm.getPermissionObject(principal, "test/pobject", true)); michael@0: michael@0: pm.addFromPrincipal(principal, "test/pobject", pm.ALLOW_ACTION); michael@0: var rootPerm = pm.getPermissionObject(principal, "test/pobject", false); michael@0: do_check_true(rootPerm != null); michael@0: do_check_eq(rootPerm.host, "example.com"); michael@0: do_check_eq(rootPerm.type, "test/pobject"); michael@0: do_check_eq(rootPerm.capability, pm.ALLOW_ACTION); michael@0: do_check_eq(rootPerm.expireType, pm.EXPIRE_NEVER); michael@0: michael@0: var rootPerm2 = pm.getPermissionObject(principal, "test/pobject", true); michael@0: do_check_true(rootPerm != null); michael@0: do_check_eq(rootPerm.host, "example.com"); michael@0: michael@0: var subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", true); michael@0: do_check_null(subPerm); michael@0: subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false); michael@0: do_check_true(subPerm != null); michael@0: do_check_eq(subPerm.host, "example.com"); michael@0: do_check_eq(subPerm.type, "test/pobject"); michael@0: do_check_eq(subPerm.capability, pm.ALLOW_ACTION); michael@0: michael@0: subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", true); michael@0: do_check_null(subPerm); michael@0: subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", false); michael@0: do_check_true(subPerm != null); michael@0: do_check_eq(subPerm.host, "example.com"); michael@0: michael@0: pm.addFromPrincipal(principal, "test/pobject", pm.DENY_ACTION, pm.EXPIRE_SESSION); michael@0: michael@0: // make sure permission objects are not dynamic michael@0: do_check_eq(rootPerm.capability, pm.ALLOW_ACTION); michael@0: michael@0: // but do update on change michael@0: rootPerm = pm.getPermissionObject(principal, "test/pobject", true); michael@0: do_check_eq(rootPerm.capability, pm.DENY_ACTION); michael@0: do_check_eq(rootPerm.expireType, pm.EXPIRE_SESSION); michael@0: michael@0: subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false); michael@0: do_check_eq(subPerm.host, "example.com"); michael@0: do_check_eq(subPerm.capability, pm.DENY_ACTION); michael@0: do_check_eq(subPerm.expireType, pm.EXPIRE_SESSION); michael@0: michael@0: pm.addFromPrincipal(subPrincipal, "test/pobject", pm.PROMPT_ACTION); michael@0: rootPerm = pm.getPermissionObject(principal, "test/pobject", true); michael@0: do_check_eq(rootPerm.host, "example.com"); michael@0: do_check_eq(rootPerm.capability, pm.DENY_ACTION); michael@0: michael@0: subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", true); michael@0: do_check_eq(subPerm.host, "sub.example.com"); michael@0: do_check_eq(subPerm.capability, pm.PROMPT_ACTION); michael@0: michael@0: subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false); michael@0: do_check_eq(subPerm.host, "sub.example.com"); michael@0: do_check_eq(subPerm.capability, pm.PROMPT_ACTION); michael@0: michael@0: subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", true); michael@0: do_check_null(subPerm); michael@0: michael@0: subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", false); michael@0: do_check_eq(subPerm.host, "sub.example.com"); michael@0: do_check_eq(subPerm.capability, pm.PROMPT_ACTION); michael@0: michael@0: pm.removeFromPrincipal(principal, "test/pobject"); michael@0: michael@0: rootPerm = pm.getPermissionObject(principal, "test/pobject", true); michael@0: do_check_null(rootPerm); michael@0: }