summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_removesince.js
blob: c33d02b08be84323e33dc45dbca87d26161a42c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that removing permissions since a specified time behaves as expected.

var test_generator = do_run_test();

function run_test() {
  do_test_pending();
  test_generator.next();
}

function continue_test() {
  do_run_generator(test_generator);
}

function* do_run_test() {
  let pm = Services.perms;

  // to help with testing edge-cases, we will arrange for .removeAllSince to
  // remove *all* permissions from one principal and one permission from another.
  let permURI1 = NetUtil.newURI("http://example.com");
  let principal1 = Services.scriptSecurityManager.createContentPrincipal(
    permURI1,
    {}
  );

  let permURI2 = NetUtil.newURI("http://example.org");
  let principal2 = Services.scriptSecurityManager.createContentPrincipal(
    permURI2,
    {}
  );

  // add a permission now - this isn't going to be removed.
  pm.addFromPrincipal(principal1, "test/remove-since", 1);

  // sleep briefly, then record the time - we'll remove all since then.
  do_timeout(20, continue_test);
  yield;

  let since = Number(Date.now());

  // *sob* - on Windows at least, the now recorded by PermissionManager.cpp
  // might be a couple of ms *earlier* than what JS sees.  So another sleep
  // to ensure our |since| is greater than the time of the permissions we
  // are now adding.  Sadly this means we'll never be able to test when since
  // exactly equals the modTime, but there you go...
  do_timeout(20, continue_test);
  yield;

  // add another item - this second one should get nuked.
  pm.addFromPrincipal(principal1, "test/remove-since-2", 1);

  // add 2 items for the second principal - both will be removed.
  pm.addFromPrincipal(principal2, "test/remove-since", 1);
  pm.addFromPrincipal(principal2, "test/remove-since-2", 1);

  // do the removal.
  pm.removeAllSince(since);

  // principal1 - the first one should remain.
  Assert.equal(
    1,
    pm.testPermissionFromPrincipal(principal1, "test/remove-since")
  );
  // but the second should have been removed.
  Assert.equal(
    0,
    pm.testPermissionFromPrincipal(principal1, "test/remove-since-2")
  );

  // principal2 - both should have been removed.
  Assert.equal(
    0,
    pm.testPermissionFromPrincipal(principal2, "test/remove-since")
  );
  Assert.equal(
    0,
    pm.testPermissionFromPrincipal(principal2, "test/remove-since-2")
  );

  do_finish_generator_test(test_generator);
}