test_permmanager_subdomains.js (3378B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 function getPrincipalFromURI(aURI) { 5 let ssm = Services.scriptSecurityManager; 6 let uri = NetUtil.newURI(aURI); 7 return ssm.createContentPrincipal(uri, {}); 8 } 9 10 function run_test() { 11 var pm = Services.perms; 12 13 // Adds a permission to a sub-domain. Checks if it is working. 14 let sub1Principal = getPrincipalFromURI("http://sub1.example.com"); 15 pm.addFromPrincipal(sub1Principal, "test/subdomains", pm.ALLOW_ACTION, 0, 0); 16 Assert.equal( 17 pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), 18 pm.ALLOW_ACTION 19 ); 20 21 // A sub-sub-domain should get the permission. 22 let subsubPrincipal = getPrincipalFromURI("http://sub.sub1.example.com"); 23 Assert.equal( 24 pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), 25 pm.ALLOW_ACTION 26 ); 27 28 // Another sub-domain shouldn't get the permission. 29 let sub2Principal = getPrincipalFromURI("http://sub2.example.com"); 30 Assert.equal( 31 pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), 32 pm.UNKNOWN_ACTION 33 ); 34 35 // Remove current permissions. 36 pm.removeFromPrincipal(sub1Principal, "test/subdomains"); 37 Assert.equal( 38 pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), 39 pm.UNKNOWN_ACTION 40 ); 41 42 // Adding the permission to the main domain. Checks if it is working. 43 let mainPrincipal = getPrincipalFromURI("http://example.com"); 44 pm.addFromPrincipal(mainPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0); 45 Assert.equal( 46 pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), 47 pm.ALLOW_ACTION 48 ); 49 50 // All sub-domains should have the permission now. 51 Assert.equal( 52 pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), 53 pm.ALLOW_ACTION 54 ); 55 Assert.equal( 56 pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), 57 pm.ALLOW_ACTION 58 ); 59 Assert.equal( 60 pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), 61 pm.ALLOW_ACTION 62 ); 63 64 // Remove current permissions. 65 pm.removeFromPrincipal(mainPrincipal, "test/subdomains"); 66 Assert.equal( 67 pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), 68 pm.UNKNOWN_ACTION 69 ); 70 Assert.equal( 71 pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), 72 pm.UNKNOWN_ACTION 73 ); 74 Assert.equal( 75 pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), 76 pm.UNKNOWN_ACTION 77 ); 78 Assert.equal( 79 pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), 80 pm.UNKNOWN_ACTION 81 ); 82 83 // A sanity check that the previous implementation wasn't passing... 84 let crazyPrincipal = getPrincipalFromURI("http://com"); 85 pm.addFromPrincipal(crazyPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0); 86 Assert.equal( 87 pm.testPermissionFromPrincipal(crazyPrincipal, "test/subdomains"), 88 pm.ALLOW_ACTION 89 ); 90 Assert.equal( 91 pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"), 92 pm.UNKNOWN_ACTION 93 ); 94 Assert.equal( 95 pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"), 96 pm.UNKNOWN_ACTION 97 ); 98 Assert.equal( 99 pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"), 100 pm.UNKNOWN_ACTION 101 ); 102 Assert.equal( 103 pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"), 104 pm.UNKNOWN_ACTION 105 ); 106 }