michael@0: // tests nsIPermissionManager michael@0: michael@0: var hosts = [ michael@0: // format: [host, type, permission] michael@0: ["mozilla.org", "cookie", 1], michael@0: ["mozilla.org", "image", 2], michael@0: ["mozilla.org", "popup", 3], michael@0: ["mozilla.com", "cookie", 1], michael@0: ["www.mozilla.com", "cookie", 2], michael@0: ["dev.mozilla.com", "cookie", 3] michael@0: ]; michael@0: michael@0: var results = [ michael@0: // format: [host, type, testPermission result, testExactPermission result] michael@0: // test defaults michael@0: ["localhost", "cookie", 0, 0], michael@0: ["spreadfirefox.com", "cookie", 0, 0], michael@0: // test different types michael@0: ["mozilla.org", "cookie", 1, 1], michael@0: ["mozilla.org", "image", 2, 2], michael@0: ["mozilla.org", "popup", 3, 3], michael@0: // test subdomains michael@0: ["www.mozilla.org", "cookie", 1, 0], michael@0: ["www.dev.mozilla.org", "cookie", 1, 0], michael@0: // test different permissions on subdomains michael@0: ["mozilla.com", "cookie", 1, 1], michael@0: ["www.mozilla.com", "cookie", 2, 2], michael@0: ["dev.mozilla.com", "cookie", 3, 3], michael@0: ["www.dev.mozilla.com", "cookie", 3, 0] michael@0: ]; michael@0: michael@0: function run_test() { michael@0: var pm = Components.classes["@mozilla.org/permissionmanager;1"] michael@0: .getService(Components.interfaces.nsIPermissionManager); michael@0: michael@0: var ioService = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: michael@0: var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Components.interfaces.nsIScriptSecurityManager); michael@0: michael@0: // nsIPermissionManager implementation is an extension; don't fail if it's not there michael@0: if (!pm) michael@0: return; michael@0: michael@0: // put a few hosts in michael@0: for (var i = 0; i < hosts.length; ++i) { michael@0: var uri = ioService.newURI("http://" + hosts[i][0], null, null); michael@0: var principal = secMan.getNoAppCodebasePrincipal(uri); michael@0: michael@0: pm.addFromPrincipal(principal, hosts[i][1], hosts[i][2]); michael@0: } michael@0: michael@0: // test the result michael@0: for (var i = 0; i < results.length; ++i) { michael@0: var uri = ioService.newURI("http://" + results[i][0], null, null); michael@0: var principal = secMan.getNoAppCodebasePrincipal(uri); michael@0: michael@0: do_check_eq(pm.testPermissionFromPrincipal(principal, results[i][1]), results[i][2]); michael@0: do_check_eq(pm.testExactPermissionFromPrincipal(principal, results[i][1]), results[i][3]); michael@0: } michael@0: michael@0: // test the enumerator ... michael@0: var j = 0; michael@0: var perms = new Array(); michael@0: var enumerator = pm.enumerator; michael@0: while (enumerator.hasMoreElements()) { michael@0: perms[j] = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission); michael@0: ++j; michael@0: } michael@0: do_check_eq(perms.length, hosts.length); michael@0: michael@0: // ... remove all the hosts ... michael@0: for (var j = 0; j < perms.length; ++j) { michael@0: var uri = ioService.newURI("http://" + perms[j].host, null, null); michael@0: var principal = secMan.getNoAppCodebasePrincipal(uri); michael@0: michael@0: pm.removeFromPrincipal(principal, perms[j].type); michael@0: } michael@0: michael@0: // ... ensure each and every element is equal ... michael@0: for (var i = 0; i < hosts.length; ++i) { michael@0: for (var j = 0; j < perms.length; ++j) { michael@0: if (hosts[i][0] == perms[j].host && michael@0: hosts[i][1] == perms[j].type && michael@0: hosts[i][2] == perms[j].capability) { michael@0: perms.splice(j, 1); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: do_check_eq(perms.length, 0); michael@0: michael@0: // ... and check the permmgr's empty michael@0: do_check_eq(pm.enumerator.hasMoreElements(), false); michael@0: michael@0: // test UTF8 normalization behavior: expect ASCII/ACE host encodings michael@0: var utf8 = "b\u00FCcher.dolske.org"; // "bücher.dolske.org" michael@0: var aceref = "xn--bcher-kva.dolske.org"; michael@0: var uri = ioService.newURI("http://" + utf8, null, null); michael@0: pm.add(uri, "utf8", 1); michael@0: var enumerator = pm.enumerator; michael@0: do_check_eq(enumerator.hasMoreElements(), true); michael@0: var ace = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission); michael@0: do_check_eq(ace.host, aceref); michael@0: do_check_eq(enumerator.hasMoreElements(), false); michael@0: michael@0: // test removeAll() michael@0: pm.removeAll(); michael@0: do_check_eq(pm.enumerator.hasMoreElements(), false); michael@0: }