summaryrefslogtreecommitdiffstats
path: root/extensions/cookie/test/unit/test_permmanager_getPermissionObject.js
blob: d01b51923dc63ee6d93a97ba45190dccca7c7dd6 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function getPrincipalFromURI(aURI) {
  let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
              .getService(Ci.nsIScriptSecurityManager);
  let uri = NetUtil.newURI(aURI);
  return ssm.createCodebasePrincipal(uri, {});
}

function getSystemPrincipal() {
  return Cc["@mozilla.org/scriptsecuritymanager;1"]
           .getService(Ci.nsIScriptSecurityManager)
           .getSystemPrincipal();
}

function run_test() {
  var pm = Cc["@mozilla.org/permissionmanager;1"].
           getService(Ci.nsIPermissionManager);

  do_check_null(pm.getPermissionObject(getSystemPrincipal(), "test/pobject", false));

  let principal = getPrincipalFromURI("http://example.com");
  let subPrincipal = getPrincipalFromURI("http://sub.example.com");
  let subSubPrincipal = getPrincipalFromURI("http://sub.sub.example.com");

  do_check_null(pm.getPermissionObject(principal, "test/pobject", false));
  do_check_null(pm.getPermissionObject(principal, "test/pobject", true));

  pm.addFromPrincipal(principal, "test/pobject", pm.ALLOW_ACTION);
  var rootPerm = pm.getPermissionObject(principal, "test/pobject", false);
  do_check_true(rootPerm != null);
  do_check_eq(rootPerm.principal.origin, "http://example.com");
  do_check_eq(rootPerm.type, "test/pobject");
  do_check_eq(rootPerm.capability, pm.ALLOW_ACTION);
  do_check_eq(rootPerm.expireType, pm.EXPIRE_NEVER);

  var rootPerm2 = pm.getPermissionObject(principal, "test/pobject", true);
  do_check_true(rootPerm != null);
  do_check_eq(rootPerm.principal.origin, "http://example.com");

  var subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", true);
  do_check_null(subPerm);
  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false);
  do_check_true(subPerm != null);
  do_check_eq(subPerm.principal.origin, "http://example.com");
  do_check_eq(subPerm.type, "test/pobject");
  do_check_eq(subPerm.capability, pm.ALLOW_ACTION);

  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", true);
  do_check_null(subPerm);
  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", false);
  do_check_true(subPerm != null);
  do_check_eq(subPerm.principal.origin, "http://example.com");

  pm.addFromPrincipal(principal, "test/pobject", pm.DENY_ACTION, pm.EXPIRE_SESSION);

  // make sure permission objects are not dynamic
  do_check_eq(rootPerm.capability, pm.ALLOW_ACTION);

  // but do update on change
  rootPerm = pm.getPermissionObject(principal, "test/pobject", true);
  do_check_eq(rootPerm.capability, pm.DENY_ACTION);
  do_check_eq(rootPerm.expireType, pm.EXPIRE_SESSION);

  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false);
  do_check_eq(subPerm.principal.origin, "http://example.com");
  do_check_eq(subPerm.capability, pm.DENY_ACTION);
  do_check_eq(subPerm.expireType, pm.EXPIRE_SESSION);

  pm.addFromPrincipal(subPrincipal, "test/pobject", pm.PROMPT_ACTION);
  rootPerm = pm.getPermissionObject(principal, "test/pobject", true);
  do_check_eq(rootPerm.principal.origin, "http://example.com");
  do_check_eq(rootPerm.capability, pm.DENY_ACTION);

  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", true);
  do_check_eq(subPerm.principal.origin, "http://sub.example.com");
  do_check_eq(subPerm.capability, pm.PROMPT_ACTION);

  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false);
  do_check_eq(subPerm.principal.origin, "http://sub.example.com");
  do_check_eq(subPerm.capability, pm.PROMPT_ACTION);

  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", true);
  do_check_null(subPerm);

  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", false);
  do_check_eq(subPerm.principal.origin, "http://sub.example.com");
  do_check_eq(subPerm.capability, pm.PROMPT_ACTION);

  pm.removeFromPrincipal(principal, "test/pobject");

  rootPerm = pm.getPermissionObject(principal, "test/pobject", true);
  do_check_null(rootPerm);
}