Thu, 22 Jan 2015 13:21:57 +0100
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 | // Test that permissions with specific expiry times behave as expected. |
michael@0 | 5 | |
michael@0 | 6 | let test_generator = do_run_test(); |
michael@0 | 7 | |
michael@0 | 8 | function run_test() { |
michael@0 | 9 | do_test_pending(); |
michael@0 | 10 | test_generator.next(); |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | function continue_test() |
michael@0 | 14 | { |
michael@0 | 15 | do_run_generator(test_generator); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | function do_run_test() { |
michael@0 | 19 | // Set up a profile. |
michael@0 | 20 | let profile = do_get_profile(); |
michael@0 | 21 | |
michael@0 | 22 | let pm = Services.perms; |
michael@0 | 23 | let permURI = NetUtil.newURI("http://example.com"); |
michael@0 | 24 | let principal = Services.scriptSecurityManager.getNoAppCodebasePrincipal(permURI); |
michael@0 | 25 | |
michael@0 | 26 | let now = Number(Date.now()); |
michael@0 | 27 | |
michael@0 | 28 | // add a permission with *now* expiration |
michael@0 | 29 | pm.addFromPrincipal(principal, "test/expiration-perm-exp", 1, pm.EXPIRE_TIME, now); |
michael@0 | 30 | pm.addFromPrincipal(principal, "test/expiration-session-exp", 1, pm.EXPIRE_SESSION, now); |
michael@0 | 31 | |
michael@0 | 32 | // add a permission with future expiration (100 milliseconds) |
michael@0 | 33 | pm.addFromPrincipal(principal, "test/expiration-perm-exp2", 1, pm.EXPIRE_TIME, now + 100); |
michael@0 | 34 | pm.addFromPrincipal(principal, "test/expiration-session-exp2", 1, pm.EXPIRE_SESSION, now + 100); |
michael@0 | 35 | |
michael@0 | 36 | // add a permission with future expiration (1000 seconds) |
michael@0 | 37 | pm.addFromPrincipal(principal, "test/expiration-perm-exp3", 1, pm.EXPIRE_TIME, now + 1e6); |
michael@0 | 38 | pm.addFromPrincipal(principal, "test/expiration-session-exp3", 1, pm.EXPIRE_SESSION, now + 1e6); |
michael@0 | 39 | |
michael@0 | 40 | // add a permission without expiration |
michael@0 | 41 | pm.addFromPrincipal(principal, "test/expiration-perm-nexp", 1, pm.EXPIRE_NEVER, 0); |
michael@0 | 42 | |
michael@0 | 43 | // add a permission for renewal |
michael@0 | 44 | pm.addFromPrincipal(principal, "test/expiration-perm-renewable", 1, pm.EXPIRE_TIME, now + 100); |
michael@0 | 45 | pm.addFromPrincipal(principal, "test/expiration-session-renewable", 1, pm.EXPIRE_SESSION, now + 100); |
michael@0 | 46 | |
michael@0 | 47 | // And immediately renew them with longer timeouts |
michael@0 | 48 | pm.updateExpireTime(principal, "test/expiration-perm-renewable", true, now + 100, now + 1e6); |
michael@0 | 49 | pm.updateExpireTime(principal, "test/expiration-session-renewable", true, now + 1e6, now + 100); |
michael@0 | 50 | |
michael@0 | 51 | // check that the second two haven't expired yet |
michael@0 | 52 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-perm-exp3")); |
michael@0 | 53 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-session-exp3")); |
michael@0 | 54 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-perm-nexp")); |
michael@0 | 55 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-perm-renewable")); |
michael@0 | 56 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-session-renewable")); |
michael@0 | 57 | |
michael@0 | 58 | // ... and the first one has |
michael@0 | 59 | do_timeout(10, continue_test); |
michael@0 | 60 | yield; |
michael@0 | 61 | do_check_eq(0, pm.testPermissionFromPrincipal(principal, "test/expiration-perm-exp")); |
michael@0 | 62 | do_check_eq(0, pm.testPermissionFromPrincipal(principal, "test/expiration-session-exp")); |
michael@0 | 63 | |
michael@0 | 64 | // ... and that the short-term one will |
michael@0 | 65 | do_timeout(200, continue_test); |
michael@0 | 66 | yield; |
michael@0 | 67 | do_check_eq(0, pm.testPermissionFromPrincipal(principal, "test/expiration-perm-exp2")); |
michael@0 | 68 | do_check_eq(0, pm.testPermissionFromPrincipal(principal, "test/expiration-session-exp2")); |
michael@0 | 69 | |
michael@0 | 70 | // Check that .getPermission returns a matching result |
michael@0 | 71 | do_check_null(pm.getPermissionObject(principal, "test/expiration-perm-exp", false)); |
michael@0 | 72 | do_check_null(pm.getPermissionObject(principal, "test/expiration-session-exp", false)); |
michael@0 | 73 | do_check_null(pm.getPermissionObject(principal, "test/expiration-perm-exp2", false)); |
michael@0 | 74 | do_check_null(pm.getPermissionObject(principal, "test/expiration-session-exp2", false)); |
michael@0 | 75 | |
michael@0 | 76 | // Check that the renewable permissions actually got renewed |
michael@0 | 77 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-perm-renewable")); |
michael@0 | 78 | do_check_eq(1, pm.testPermissionFromPrincipal(principal, "test/expiration-session-renewable")); |
michael@0 | 79 | |
michael@0 | 80 | do_finish_generator_test(test_generator); |
michael@0 | 81 | } |
michael@0 | 82 |