summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_SitePermissions_combinations.js
blob: e6267f72cc6c6b07e5856d44aca1badf68e23473 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// This function applies combinations of different permissions and
// checks how they override each other.
async function checkPermissionCombinations(combinations) {
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      "https://example.com"
    );

  await BrowserTestUtils.withNewTab(principal.spec, function (browser) {
    let id = "geo";
    for (let { reverse, states, result } of combinations) {
      let loop = () => {
        for (let [state, scope] of states) {
          SitePermissions.setForPrincipal(principal, id, state, scope, browser);
        }
        Assert.deepEqual(
          SitePermissions.getForPrincipal(principal, id, browser),
          result
        );
        SitePermissions.removeFromPrincipal(principal, id, browser);
      };

      loop();

      if (reverse) {
        states.reverse();
        loop();
      }
    }
  });
}

// Test that passing null as scope becomes SCOPE_PERSISTENT.
add_task(async function testDefaultScope() {
  await checkPermissionCombinations([
    {
      states: [[SitePermissions.ALLOW, null]],
      result: {
        state: SitePermissions.ALLOW,
        scope: SitePermissions.SCOPE_PERSISTENT,
      },
    },
  ]);
});

// Test that "wide" scopes like PERSISTENT always override "narrower" ones like TAB.
add_task(async function testScopeOverrides() {
  await checkPermissionCombinations([
    {
      // The behavior of SCOPE_SESSION is not in line with the general behavior
      // because of the legacy nsIPermissionManager implementation.
      states: [
        [SitePermissions.ALLOW, SitePermissions.SCOPE_PERSISTENT],
        [SitePermissions.BLOCK, SitePermissions.SCOPE_SESSION],
      ],
      result: {
        state: SitePermissions.BLOCK,
        scope: SitePermissions.SCOPE_SESSION,
      },
    },
    {
      states: [
        [SitePermissions.BLOCK, SitePermissions.SCOPE_SESSION],
        [SitePermissions.ALLOW, SitePermissions.SCOPE_PERSISTENT],
      ],
      result: {
        state: SitePermissions.ALLOW,
        scope: SitePermissions.SCOPE_PERSISTENT,
      },
    },
    {
      reverse: true,
      states: [
        [SitePermissions.BLOCK, SitePermissions.SCOPE_TEMPORARY],
        [SitePermissions.ALLOW, SitePermissions.SCOPE_SESSION],
      ],
      result: {
        state: SitePermissions.ALLOW,
        scope: SitePermissions.SCOPE_SESSION,
      },
    },
    {
      reverse: true,
      states: [
        [SitePermissions.BLOCK, SitePermissions.SCOPE_TEMPORARY],
        [SitePermissions.ALLOW, SitePermissions.SCOPE_PERSISTENT],
      ],
      result: {
        state: SitePermissions.ALLOW,
        scope: SitePermissions.SCOPE_PERSISTENT,
      },
    },
  ]);
});

// Test that clearing a temporary permission also removes a
// persistent permission that was set for the same URL.
add_task(async function testClearTempPermission() {
  await checkPermissionCombinations([
    {
      states: [
        [SitePermissions.BLOCK, SitePermissions.SCOPE_TEMPORARY],
        [SitePermissions.ALLOW, SitePermissions.SCOPE_PERSISTENT],
        [SitePermissions.UNKNOWN, SitePermissions.SCOPE_TEMPORARY],
      ],
      result: {
        state: SitePermissions.UNKNOWN,
        scope: SitePermissions.SCOPE_PERSISTENT,
      },
    },
  ]);
});

// Test that states override each other when applied with the same scope.
add_task(async function testStateOverride() {
  await checkPermissionCombinations([
    {
      states: [
        [SitePermissions.ALLOW, SitePermissions.SCOPE_PERSISTENT],
        [SitePermissions.BLOCK, SitePermissions.SCOPE_PERSISTENT],
      ],
      result: {
        state: SitePermissions.BLOCK,
        scope: SitePermissions.SCOPE_PERSISTENT,
      },
    },
    {
      states: [
        [SitePermissions.BLOCK, SitePermissions.SCOPE_PERSISTENT],
        [SitePermissions.ALLOW, SitePermissions.SCOPE_PERSISTENT],
      ],
      result: {
        state: SitePermissions.ALLOW,
        scope: SitePermissions.SCOPE_PERSISTENT,
      },
    },
  ]);
});