test_permmanager_removesince.js (2492B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that removing permissions since a specified time behaves as expected. 5 6 var test_generator = do_run_test(); 7 8 function run_test() { 9 do_test_pending(); 10 test_generator.next(); 11 } 12 13 function continue_test() { 14 do_run_generator(test_generator); 15 } 16 17 function* do_run_test() { 18 let pm = Services.perms; 19 20 // to help with testing edge-cases, we will arrange for .removeAllSince to 21 // remove *all* permissions from one principal and one permission from another. 22 let permURI1 = NetUtil.newURI("http://example.com"); 23 let principal1 = Services.scriptSecurityManager.createContentPrincipal( 24 permURI1, 25 {} 26 ); 27 28 let permURI2 = NetUtil.newURI("http://example.org"); 29 let principal2 = Services.scriptSecurityManager.createContentPrincipal( 30 permURI2, 31 {} 32 ); 33 34 // add a permission now - this isn't going to be removed. 35 pm.addFromPrincipal(principal1, "test/remove-since", 1); 36 37 // sleep briefly, then record the time - we'll remove all since then. 38 do_timeout(20, continue_test); 39 yield; 40 41 let since = Number(Date.now()); 42 43 // *sob* - on Windows at least, the now recorded by PermissionManager.cpp 44 // might be a couple of ms *earlier* than what JS sees. So another sleep 45 // to ensure our |since| is greater than the time of the permissions we 46 // are now adding. Sadly this means we'll never be able to test when since 47 // exactly equals the modTime, but there you go... 48 do_timeout(20, continue_test); 49 yield; 50 51 // add another item - this second one should get nuked. 52 pm.addFromPrincipal(principal1, "test/remove-since-2", 1); 53 54 // add 2 items for the second principal - both will be removed. 55 pm.addFromPrincipal(principal2, "test/remove-since", 1); 56 pm.addFromPrincipal(principal2, "test/remove-since-2", 1); 57 58 // do the removal. 59 pm.removeAllSince(since); 60 61 // principal1 - the first one should remain. 62 Assert.equal( 63 1, 64 pm.testPermissionFromPrincipal(principal1, "test/remove-since") 65 ); 66 // but the second should have been removed. 67 Assert.equal( 68 0, 69 pm.testPermissionFromPrincipal(principal1, "test/remove-since-2") 70 ); 71 72 // principal2 - both should have been removed. 73 Assert.equal( 74 0, 75 pm.testPermissionFromPrincipal(principal2, "test/remove-since") 76 ); 77 Assert.equal( 78 0, 79 pm.testPermissionFromPrincipal(principal2, "test/remove-since-2") 80 ); 81 82 do_finish_generator_test(test_generator); 83 }