test_permmanager_local_files.js (2443B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that permissions work for file:// URIs (aka local files). 5 6 function getPrincipalFromURIString(uriStr) { 7 let uri = NetUtil.newURI(uriStr); 8 return Services.scriptSecurityManager.createContentPrincipal(uri, {}); 9 } 10 11 function run_test() { 12 let pm = Services.perms; 13 14 // If we add a permission to a file:// URI, the test should return true. 15 let principal = getPrincipalFromURIString("file:///foo/bar"); 16 pm.addFromPrincipal(principal, "test/local-files", pm.ALLOW_ACTION, 0, 0); 17 Assert.equal( 18 pm.testPermissionFromPrincipal(principal, "test/local-files"), 19 pm.ALLOW_ACTION 20 ); 21 22 // Another file:// URI should have the same permission. 23 let witnessPrincipal = getPrincipalFromURIString("file:///bar/foo"); 24 Assert.equal( 25 pm.testPermissionFromPrincipal(witnessPrincipal, "test/local-files"), 26 pm.UNKNOWN_ACTION 27 ); 28 29 // Giving "file:///" a permission shouldn't give it to all file:// URIs. 30 let rootPrincipal = getPrincipalFromURIString("file:///"); 31 pm.addFromPrincipal(rootPrincipal, "test/local-files", pm.ALLOW_ACTION, 0, 0); 32 Assert.equal( 33 pm.testPermissionFromPrincipal(witnessPrincipal, "test/local-files"), 34 pm.UNKNOWN_ACTION 35 ); 36 37 // Giving "file://" a permission shouldn't give it to all file:// URIs. 38 let schemeRootPrincipal = getPrincipalFromURIString("file://"); 39 pm.addFromPrincipal( 40 schemeRootPrincipal, 41 "test/local-files", 42 pm.ALLOW_ACTION, 43 0, 44 0 45 ); 46 Assert.equal( 47 pm.testPermissionFromPrincipal(witnessPrincipal, "test/local-files"), 48 pm.UNKNOWN_ACTION 49 ); 50 51 // Giving 'node' a permission shouldn't give it to its 'children'. 52 let fileInDirPrincipal = getPrincipalFromURIString( 53 "file:///foo/bar/foobar.txt" 54 ); 55 Assert.equal( 56 pm.testPermissionFromPrincipal(fileInDirPrincipal, "test/local-files"), 57 pm.UNKNOWN_ACTION 58 ); 59 60 // Revert "file:///foo/bar" permission and check that it has been correctly taken into account. 61 pm.removeFromPrincipal(principal, "test/local-files"); 62 Assert.equal( 63 pm.testPermissionFromPrincipal(principal, "test/local-files"), 64 pm.UNKNOWN_ACTION 65 ); 66 Assert.equal( 67 pm.testPermissionFromPrincipal(witnessPrincipal, "test/local-files"), 68 pm.UNKNOWN_ACTION 69 ); 70 Assert.equal( 71 pm.testPermissionFromPrincipal(fileInDirPrincipal, "test/local-files"), 72 pm.UNKNOWN_ACTION 73 ); 74 }