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

function getPrincipalFromURI(aURI) {
  let ssm = Services.scriptSecurityManager;
  let uri = NetUtil.newURI(aURI);
  return ssm.createContentPrincipal(uri, {});
}

function run_test() {
  var pm = Services.perms;

  // Adds a permission to a sub-domain. Checks if it is working.
  let sub1Principal = getPrincipalFromURI("http://sub1.example.com");
  pm.addFromPrincipal(sub1Principal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
  Assert.equal(
    pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"),
    pm.ALLOW_ACTION
  );

  // A sub-sub-domain should get the permission.
  let subsubPrincipal = getPrincipalFromURI("http://sub.sub1.example.com");
  Assert.equal(
    pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"),
    pm.ALLOW_ACTION
  );

  // Another sub-domain shouldn't get the permission.
  let sub2Principal = getPrincipalFromURI("http://sub2.example.com");
  Assert.equal(
    pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );

  // Remove current permissions.
  pm.removeFromPrincipal(sub1Principal, "test/subdomains");
  Assert.equal(
    pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );

  // Adding the permission to the main domain. Checks if it is working.
  let mainPrincipal = getPrincipalFromURI("http://example.com");
  pm.addFromPrincipal(mainPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
  Assert.equal(
    pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"),
    pm.ALLOW_ACTION
  );

  // All sub-domains should have the permission now.
  Assert.equal(
    pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"),
    pm.ALLOW_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"),
    pm.ALLOW_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"),
    pm.ALLOW_ACTION
  );

  // Remove current permissions.
  pm.removeFromPrincipal(mainPrincipal, "test/subdomains");
  Assert.equal(
    pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );

  // A sanity check that the previous implementation wasn't passing...
  let crazyPrincipal = getPrincipalFromURI("http://com");
  pm.addFromPrincipal(crazyPrincipal, "test/subdomains", pm.ALLOW_ACTION, 0, 0);
  Assert.equal(
    pm.testPermissionFromPrincipal(crazyPrincipal, "test/subdomains"),
    pm.ALLOW_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(mainPrincipal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(sub1Principal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(sub2Principal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
  Assert.equal(
    pm.testPermissionFromPrincipal(subsubPrincipal, "test/subdomains"),
    pm.UNKNOWN_ACTION
  );
}