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