summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_cleardata.js
blob: 2bd4d113198f4bf18f882b56e9203eca3b954c08 (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
85
86
87
88
89
90
91
92
93
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

var pm;

// Create a principal based on the { origin, originAttributes }.
function createPrincipal(aOrigin, aOriginAttributes) {
  return Services.scriptSecurityManager.createContentPrincipal(
    NetUtil.newURI(aOrigin),
    aOriginAttributes
  );
}

function getData(aPattern) {
  return JSON.stringify(aPattern);
}

// Use aEntries to create principals, add permissions to them and check that they have them.
// Then, it is removing origin attributes with the given aData and check if the permissions
// of principals[i] matches the permission in aResults[i].
function test(aEntries, aData, aResults) {
  let principals = [];

  for (const entry of aEntries) {
    principals.push(createPrincipal(entry.origin, entry.originAttributes));
  }

  for (const principal of principals) {
    Assert.equal(
      pm.testPermissionFromPrincipal(principal, "test/clear-origin"),
      pm.UNKNOWN_ACTION
    );
    pm.addFromPrincipal(
      principal,
      "test/clear-origin",
      pm.ALLOW_ACTION,
      pm.EXPIRE_NEVER,
      0
    );
    Assert.equal(
      pm.testPermissionFromPrincipal(principal, "test/clear-origin"),
      pm.ALLOW_ACTION
    );
  }

  // `clear-origin-attributes-data` notification is removed from permission
  // manager
  pm.removePermissionsWithAttributes(aData);

  var length = aEntries.length;
  for (let i = 0; i < length; ++i) {
    Assert.equal(
      pm.testPermissionFromPrincipal(principals[i], "test/clear-origin"),
      aResults[i]
    );

    // Remove allowed actions.
    if (aResults[i] == pm.ALLOW_ACTION) {
      pm.removeFromPrincipal(principals[i], "test/clear-origin");
    }
  }
}

function run_test() {
  do_get_profile();

  pm = Services.perms;

  let entries = [
    { origin: "http://example.com", originAttributes: {} },
    {
      origin: "http://example.com",
      originAttributes: { inIsolatedMozBrowser: true },
    },
  ];

  // In that case, all permissions should be removed.
  test(entries, getData({}), [
    pm.UNKNOWN_ACTION,
    pm.UNKNOWN_ACTION,
    pm.ALLOW_ACTION,
    pm.ALLOW_ACTION,
  ]);

  // In that case, only the permissions related to a browserElement should be removed.
  // All the other permissions should stay.
  test(entries, getData({ inIsolatedMozBrowser: true }), [
    pm.ALLOW_ACTION,
    pm.UNKNOWN_ACTION,
    pm.ALLOW_ACTION,
    pm.ALLOW_ACTION,
  ]);
}