summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_PermissionsUtils.js
blob: adf148ad661219ae31cdbca01f23b9c882ffa937 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests that PerrmissionsUtils.jsm works as expected, including:
// * PermissionsUtils.importfromPrefs()
//      <ROOT>.[whitelist|blacklist].add preferences are emptied when
//       converted into permissions on startup.

const PREF_ROOT = "testpermissions.";
const TEST_PERM = "test-permission";

const { PermissionsUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PermissionsUtils.sys.mjs"
);
const { PermissionTestUtils } = ChromeUtils.import(
  "resource://testing-common/PermissionTestUtils.jsm"
);

function run_test() {
  test_importfromPrefs();
}

function test_importfromPrefs() {
  // Create own preferences to test
  Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", "");
  Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ",");
  Services.prefs.setCharPref(
    PREF_ROOT + "whitelist.add.TEST",
    "http://whitelist.example.com"
  );
  Services.prefs.setCharPref(
    PREF_ROOT + "whitelist.add.TEST2",
    "https://whitelist2-1.example.com,http://whitelist2-2.example.com:8080,about:home"
  );
  Services.prefs.setCharPref(
    PREF_ROOT + "whitelist.add.TEST3",
    "whitelist3-1.example.com,about:config"
  ); // legacy style - host only
  Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", "");
  Services.prefs.setCharPref(
    PREF_ROOT + "blacklist.add.TEST",
    "http://blacklist.example.com,"
  );
  Services.prefs.setCharPref(
    PREF_ROOT + "blacklist.add.TEST2",
    ",https://blacklist2-1.example.com,http://blacklist2-2.example.com:8080,about:mozilla"
  );
  Services.prefs.setCharPref(
    PREF_ROOT + "blacklist.add.TEST3",
    "blacklist3-1.example.com,about:preferences"
  ); // legacy style - host only

  // Check they are unknown in the permission manager prior to importing.
  let whitelisted = [
    "http://whitelist.example.com",
    "https://whitelist2-1.example.com",
    "http://whitelist2-2.example.com:8080",
    "http://whitelist3-1.example.com",
    "https://whitelist3-1.example.com",
    "about:config",
    "about:home",
  ];
  let blacklisted = [
    "http://blacklist.example.com",
    "https://blacklist2-1.example.com",
    "http://blacklist2-2.example.com:8080",
    "http://blacklist3-1.example.com",
    "https://blacklist3-1.example.com",
    "about:preferences",
    "about:mozilla",
  ];
  let untouched = [
    "https://whitelist.example.com",
    "https://blacklist.example.com",
    "http://whitelist2-1.example.com",
    "http://blacklist2-1.example.com",
    "https://whitelist2-2.example.com:8080",
    "https://blacklist2-2.example.com:8080",
  ];
  let unknown = whitelisted.concat(blacklisted).concat(untouched);
  for (let url of unknown) {
    let uri = Services.io.newURI(url);
    Assert.equal(
      PermissionTestUtils.testPermission(uri, TEST_PERM),
      Services.perms.UNKNOWN_ACTION
    );
  }

  // Import them
  PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM);

  // Get list of preferences to check
  let preferences = Services.prefs.getChildList(PREF_ROOT);

  // Check preferences were emptied
  for (let pref of preferences) {
    Assert.equal(Services.prefs.getCharPref(pref), "");
  }

  // Check they were imported into the permissions manager
  for (let url of whitelisted) {
    let uri = Services.io.newURI(url);
    Assert.equal(
      PermissionTestUtils.testPermission(uri, TEST_PERM),
      Services.perms.ALLOW_ACTION
    );
  }
  for (let url of blacklisted) {
    let uri = Services.io.newURI(url);
    Assert.equal(
      PermissionTestUtils.testPermission(uri, TEST_PERM),
      Services.perms.DENY_ACTION
    );
  }
  for (let url of untouched) {
    let uri = Services.io.newURI(url);
    Assert.equal(
      PermissionTestUtils.testPermission(uri, TEST_PERM),
      Services.perms.UNKNOWN_ACTION
    );
  }
}