summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_remove_add_update.js
blob: 6ec7b1f6e57b83683bf5169a2ec76a70041633b4 (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(principal, permissions) {
  let perms = Services.perms.getAllForPrincipal(principal);
  for (let [type, capability, expireType] of permissions) {
    let perm = perms.shift();
    Assert.ok(perm != null);
    Assert.equal(perm.type, type);
    Assert.equal(perm.capability, capability);
    Assert.equal(perm.expireType, expireType);
  }
  Assert.ok(!perms.length);
}

add_task(async function test() {
  Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");

  // setup a profile directory
  do_get_profile();

  // We need to execute a pm method to be sure that the DB is fully
  // initialized.
  var pm = Services.perms;
  Assert.ok(pm.all.length === 0);

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

  info("From session to persistent");
  pm.addFromPrincipal(
    principal,
    "test/foo",
    pm.ALLOW_ACTION,
    pm.EXPIRE_SESSION
  );

  check_enumerator(principal, [
    ["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_SESSION],
  ]);

  pm.addFromPrincipal(principal, "test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER);

  check_enumerator(principal, [["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER]]);

  // Let's reload the DB.
  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");

  Assert.ok(pm.all.length === 1);
  check_enumerator(principal, [["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER]]);

  info("From persistent to session");
  pm.addFromPrincipal(
    principal,
    "test/foo",
    pm.ALLOW_ACTION,
    pm.EXPIRE_SESSION
  );

  check_enumerator(principal, [
    ["test/foo", pm.ALLOW_ACTION, pm.EXPIRE_SESSION],
  ]);

  // Let's reload the DB.
  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
  Assert.ok(pm.all.length === 0);

  info("From persistent to persistent");
  pm.addFromPrincipal(principal, "test/foo", pm.ALLOW_ACTION, pm.EXPIRE_NEVER);
  pm.addFromPrincipal(principal, "test/foo", pm.DENY_ACTION, pm.EXPIRE_NEVER);

  check_enumerator(principal, [["test/foo", pm.DENY_ACTION, pm.EXPIRE_NEVER]]);

  // Let's reload the DB.
  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
  Assert.ok(pm.all.length === 1);
  check_enumerator(principal, [["test/foo", pm.DENY_ACTION, pm.EXPIRE_NEVER]]);

  info("Cleanup");
  pm.removeAll();
  check_enumerator(principal, []);
});