extensions/cookie/test/unit/test_permmanager_subdomains.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 function getPrincipalFromURI(uri) {
michael@0 5 return Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 6 .getService(Ci.nsIScriptSecurityManager)
michael@0 7 .getNoAppCodebasePrincipal(NetUtil.newURI(uri));
michael@0 8 }
michael@0 9
michael@0 10 function run_test() {
michael@0 11 var pm = Cc["@mozilla.org/permissionmanager;1"].
michael@0 12 getService(Ci.nsIPermissionManager);
michael@0 13
michael@0 14 // Adds a permission to a sub-domain. Checks if it is working.
michael@0 15 let sub1Principal = getPrincipalFromURI("http://sub1.example.com");
michael@0 16 pm.addFromPrincipal(sub1Principal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
michael@0 17 do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 18
michael@0 19 // A sub-sub-domain should get the permission.
michael@0 20 let subsubPrincipal = getPrincipalFromURI("http://sub.sub1.example.com");
michael@0 21 do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 22
michael@0 23 // Another sub-domain shouldn't get the permission.
michael@0 24 let sub2Principal = getPrincipalFromURI("http://sub2.example.com");
michael@0 25 do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 26
michael@0 27 // Remove current permissions.
michael@0 28 pm.removeFromPrincipal(sub1Principal, "test/subdomains");
michael@0 29 do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 30
michael@0 31 // Adding the permission to the main domain. Checks if it is working.
michael@0 32 let mainPrincipal = getPrincipalFromURI("http://example.com");
michael@0 33 pm.addFromPrincipal(mainPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
michael@0 34 do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 35
michael@0 36 // All sub-domains should have the permission now.
michael@0 37 do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 38 do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 39 do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 40
michael@0 41 // Remove current permissions.
michael@0 42 pm.removeFromPrincipal(mainPrincipal, "test/subdomains");
michael@0 43 do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 44 do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 45 do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 46 do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 47
michael@0 48 // A sanity check that the previous implementation wasn't passing...
michael@0 49 let crazyPrincipal = getPrincipalFromURI("http://com");
michael@0 50 pm.addFromPrincipal(crazyPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
michael@0 51 do_check_eq(pm.testPermissionFromPrincipal(crazyPrincipal, "test/subdomains"), pm.ALLOW_ACTION);
michael@0 52 do_check_eq(pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 53 do_check_eq(pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 54 do_check_eq(pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 55 do_check_eq(pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), pm.UNKNOWN_ACTION);
michael@0 56 }

mercurial