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