summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_permissions.js
blob: 48fef406f0bfe97c4bf7e45375b7383f79e2f58c (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

Components.utils.import("resource://gre/modules/NetUtil.jsm");

// Checks that permissions set in preferences are correctly imported but can
// be removed by the user.

const XPI_MIMETYPE = "application/x-xpinstall";

function newPrincipal(uri) {
  return Services.scriptSecurityManager.createCodebasePrincipal(NetUtil.newURI(uri), {});
}

function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2", "2");

  Services.prefs.setCharPref("xpinstall.whitelist.add", "https://test1.com,https://test2.com");
  Services.prefs.setCharPref("xpinstall.whitelist.add.36", "https://test3.com,https://www.test4.com");
  Services.prefs.setCharPref("xpinstall.whitelist.add.test5", "https://test5.com");

  Services.perms.add(NetUtil.newURI("https://www.test9.com"), "install",
                     AM_Ci.nsIPermissionManager.ALLOW_ACTION);

  startupManager();

  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("http://test1.com")));
  do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                              newPrincipal("https://test1.com")));
  do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                              newPrincipal("https://www.test2.com")));
  do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                              newPrincipal("https://test3.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://test4.com")));
  do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                              newPrincipal("https://www.test4.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("http://www.test5.com")));
  do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                              newPrincipal("https://www.test5.com")));

  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("http://www.test6.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test6.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://test7.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test8.com")));

  // This should remain unaffected
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("http://www.test9.com")));
  do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                              newPrincipal("https://www.test9.com")));

  Services.perms.removeAll();

  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://test1.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test2.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://test3.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test4.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test5.com")));

  // Upgrade the application and verify that the permissions are still not there
  restartManager("2");

  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://test1.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test2.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://test3.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test4.com")));
  do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
                                               newPrincipal("https://www.test5.com")));
}