Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | // tests nsIPermissionManager |
michael@0 | 2 | |
michael@0 | 3 | var hosts = [ |
michael@0 | 4 | // format: [host, type, permission] |
michael@0 | 5 | ["mozilla.org", "cookie", 1], |
michael@0 | 6 | ["mozilla.org", "image", 2], |
michael@0 | 7 | ["mozilla.org", "popup", 3], |
michael@0 | 8 | ["mozilla.com", "cookie", 1], |
michael@0 | 9 | ["www.mozilla.com", "cookie", 2], |
michael@0 | 10 | ["dev.mozilla.com", "cookie", 3] |
michael@0 | 11 | ]; |
michael@0 | 12 | |
michael@0 | 13 | var results = [ |
michael@0 | 14 | // format: [host, type, testPermission result, testExactPermission result] |
michael@0 | 15 | // test defaults |
michael@0 | 16 | ["localhost", "cookie", 0, 0], |
michael@0 | 17 | ["spreadfirefox.com", "cookie", 0, 0], |
michael@0 | 18 | // test different types |
michael@0 | 19 | ["mozilla.org", "cookie", 1, 1], |
michael@0 | 20 | ["mozilla.org", "image", 2, 2], |
michael@0 | 21 | ["mozilla.org", "popup", 3, 3], |
michael@0 | 22 | // test subdomains |
michael@0 | 23 | ["www.mozilla.org", "cookie", 1, 0], |
michael@0 | 24 | ["www.dev.mozilla.org", "cookie", 1, 0], |
michael@0 | 25 | // test different permissions on subdomains |
michael@0 | 26 | ["mozilla.com", "cookie", 1, 1], |
michael@0 | 27 | ["www.mozilla.com", "cookie", 2, 2], |
michael@0 | 28 | ["dev.mozilla.com", "cookie", 3, 3], |
michael@0 | 29 | ["www.dev.mozilla.com", "cookie", 3, 0] |
michael@0 | 30 | ]; |
michael@0 | 31 | |
michael@0 | 32 | function run_test() { |
michael@0 | 33 | var pm = Components.classes["@mozilla.org/permissionmanager;1"] |
michael@0 | 34 | .getService(Components.interfaces.nsIPermissionManager); |
michael@0 | 35 | |
michael@0 | 36 | var ioService = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 37 | .getService(Components.interfaces.nsIIOService); |
michael@0 | 38 | |
michael@0 | 39 | var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"] |
michael@0 | 40 | .getService(Components.interfaces.nsIScriptSecurityManager); |
michael@0 | 41 | |
michael@0 | 42 | // nsIPermissionManager implementation is an extension; don't fail if it's not there |
michael@0 | 43 | if (!pm) |
michael@0 | 44 | return; |
michael@0 | 45 | |
michael@0 | 46 | // put a few hosts in |
michael@0 | 47 | for (var i = 0; i < hosts.length; ++i) { |
michael@0 | 48 | var uri = ioService.newURI("http://" + hosts[i][0], null, null); |
michael@0 | 49 | var principal = secMan.getNoAppCodebasePrincipal(uri); |
michael@0 | 50 | |
michael@0 | 51 | pm.addFromPrincipal(principal, hosts[i][1], hosts[i][2]); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | // test the result |
michael@0 | 55 | for (var i = 0; i < results.length; ++i) { |
michael@0 | 56 | var uri = ioService.newURI("http://" + results[i][0], null, null); |
michael@0 | 57 | var principal = secMan.getNoAppCodebasePrincipal(uri); |
michael@0 | 58 | |
michael@0 | 59 | do_check_eq(pm.testPermissionFromPrincipal(principal, results[i][1]), results[i][2]); |
michael@0 | 60 | do_check_eq(pm.testExactPermissionFromPrincipal(principal, results[i][1]), results[i][3]); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | // test the enumerator ... |
michael@0 | 64 | var j = 0; |
michael@0 | 65 | var perms = new Array(); |
michael@0 | 66 | var enumerator = pm.enumerator; |
michael@0 | 67 | while (enumerator.hasMoreElements()) { |
michael@0 | 68 | perms[j] = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission); |
michael@0 | 69 | ++j; |
michael@0 | 70 | } |
michael@0 | 71 | do_check_eq(perms.length, hosts.length); |
michael@0 | 72 | |
michael@0 | 73 | // ... remove all the hosts ... |
michael@0 | 74 | for (var j = 0; j < perms.length; ++j) { |
michael@0 | 75 | var uri = ioService.newURI("http://" + perms[j].host, null, null); |
michael@0 | 76 | var principal = secMan.getNoAppCodebasePrincipal(uri); |
michael@0 | 77 | |
michael@0 | 78 | pm.removeFromPrincipal(principal, perms[j].type); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // ... ensure each and every element is equal ... |
michael@0 | 82 | for (var i = 0; i < hosts.length; ++i) { |
michael@0 | 83 | for (var j = 0; j < perms.length; ++j) { |
michael@0 | 84 | if (hosts[i][0] == perms[j].host && |
michael@0 | 85 | hosts[i][1] == perms[j].type && |
michael@0 | 86 | hosts[i][2] == perms[j].capability) { |
michael@0 | 87 | perms.splice(j, 1); |
michael@0 | 88 | break; |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | do_check_eq(perms.length, 0); |
michael@0 | 93 | |
michael@0 | 94 | // ... and check the permmgr's empty |
michael@0 | 95 | do_check_eq(pm.enumerator.hasMoreElements(), false); |
michael@0 | 96 | |
michael@0 | 97 | // test UTF8 normalization behavior: expect ASCII/ACE host encodings |
michael@0 | 98 | var utf8 = "b\u00FCcher.dolske.org"; // "bücher.dolske.org" |
michael@0 | 99 | var aceref = "xn--bcher-kva.dolske.org"; |
michael@0 | 100 | var uri = ioService.newURI("http://" + utf8, null, null); |
michael@0 | 101 | pm.add(uri, "utf8", 1); |
michael@0 | 102 | var enumerator = pm.enumerator; |
michael@0 | 103 | do_check_eq(enumerator.hasMoreElements(), true); |
michael@0 | 104 | var ace = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission); |
michael@0 | 105 | do_check_eq(ace.host, aceref); |
michael@0 | 106 | do_check_eq(enumerator.hasMoreElements(), false); |
michael@0 | 107 | |
michael@0 | 108 | // test removeAll() |
michael@0 | 109 | pm.removeAll(); |
michael@0 | 110 | do_check_eq(pm.enumerator.hasMoreElements(), false); |
michael@0 | 111 | } |