summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_getAllWithTypePrefix.js
blob: e3708cf4451bf12b594c22cead08370233b2c760 (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
84
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function check_enumerator(prefix, permissions) {
  let pm = Services.perms;

  let array = pm.getAllWithTypePrefix(prefix);
  for (let [principal, type, capability] of permissions) {
    let perm = array.shift();
    Assert.ok(perm != null);
    Assert.ok(perm.principal.equals(principal));
    Assert.equal(perm.type, type);
    Assert.equal(perm.capability, capability);
    Assert.equal(perm.expireType, pm.EXPIRE_NEVER);
  }
  Assert.equal(array.length, 0);
}

function run_test() {
  let pm = Services.perms;

  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      "http://example.com"
    );
  let subPrincipal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      "http://sub.example.com"
    );

  check_enumerator("test/", []);

  pm.addFromPrincipal(principal, "test/getallwithtypeprefix", pm.ALLOW_ACTION);
  pm.addFromPrincipal(
    subPrincipal,
    "other-test/getallwithtypeprefix",
    pm.PROMPT_ACTION
  );
  check_enumerator("test/", [
    [principal, "test/getallwithtypeprefix", pm.ALLOW_ACTION],
  ]);

  pm.addFromPrincipal(
    subPrincipal,
    "test/getallwithtypeprefix",
    pm.PROMPT_ACTION
  );
  check_enumerator("test/", [
    [subPrincipal, "test/getallwithtypeprefix", pm.PROMPT_ACTION],
    [principal, "test/getallwithtypeprefix", pm.ALLOW_ACTION],
  ]);

  check_enumerator("test/getallwithtypeprefix", [
    [subPrincipal, "test/getallwithtypeprefix", pm.PROMPT_ACTION],
    [principal, "test/getallwithtypeprefix", pm.ALLOW_ACTION],
  ]);

  // check that UNKNOWN_ACTION permissions are ignored
  pm.addFromPrincipal(
    principal,
    "test/getallwithtypeprefix2",
    pm.UNKNOWN_ACTION
  );
  check_enumerator("test/", [
    [subPrincipal, "test/getallwithtypeprefix", pm.PROMPT_ACTION],
    [principal, "test/getallwithtypeprefix", pm.ALLOW_ACTION],
  ]);

  // check that permission updates are reflected
  pm.addFromPrincipal(principal, "test/getallwithtypeprefix", pm.PROMPT_ACTION);
  check_enumerator("test/", [
    [subPrincipal, "test/getallwithtypeprefix", pm.PROMPT_ACTION],
    [principal, "test/getallwithtypeprefix", pm.PROMPT_ACTION],
  ]);

  // check that permission removals are reflected
  pm.removeFromPrincipal(principal, "test/getallwithtypeprefix");
  check_enumerator("test/", [
    [subPrincipal, "test/getallwithtypeprefix", pm.PROMPT_ACTION],
  ]);

  pm.removeAll();
  check_enumerator("test/", []);
}