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

function check_enumerator(uri, permissions) {
  let pm = Cc["@mozilla.org/permissionmanager;1"]
           .getService(Ci.nsIPermissionManager);

  let enumerator = pm.getAllForURI(uri);
  for ([type, capability] of permissions) {
    let perm = enumerator.getNext();
    do_check_true(perm != null);
    do_check_true(perm.principal.URI.equals(uri));
    do_check_eq(perm.type, type);
    do_check_eq(perm.capability, capability);
    do_check_eq(perm.expireType, pm.EXPIRE_NEVER);
  }
  do_check_false(enumerator.hasMoreElements());
}

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

  let uri = NetUtil.newURI("http://example.com");
  let sub = NetUtil.newURI("http://sub.example.com");

  check_enumerator(uri, [ ]);

  pm.add(uri, "test/getallforuri", pm.ALLOW_ACTION);
  check_enumerator(uri, [
    [ "test/getallforuri", pm.ALLOW_ACTION ]
  ]);

  // check that uris are matched exactly
  check_enumerator(sub, [ ]);

  pm.add(sub, "test/getallforuri", pm.PROMPT_ACTION);
  pm.add(sub, "test/getallforuri2", pm.DENY_ACTION);

  check_enumerator(sub, [
    [ "test/getallforuri", pm.PROMPT_ACTION ],
    [ "test/getallforuri2", pm.DENY_ACTION ]
  ]);

  // check that the original uri list has not changed
  check_enumerator(uri, [
    [ "test/getallforuri", pm.ALLOW_ACTION ]
  ]);

  // check that UNKNOWN_ACTION permissions are ignored
  pm.add(uri, "test/getallforuri2", pm.UNKNOWN_ACTION);
  pm.add(uri, "test/getallforuri3", pm.DENY_ACTION);

  check_enumerator(uri, [
    [ "test/getallforuri", pm.ALLOW_ACTION ],
    [ "test/getallforuri3", pm.DENY_ACTION ]
  ]);

  // check that permission updates are reflected
  pm.add(uri, "test/getallforuri", pm.PROMPT_ACTION);

  check_enumerator(uri, [
    [ "test/getallforuri", pm.PROMPT_ACTION ],
    [ "test/getallforuri3", pm.DENY_ACTION ]
  ]);

  // check that permission removals are reflected
  pm.remove(uri, "test/getallforuri");

  check_enumerator(uri, [
    [ "test/getallforuri3", pm.DENY_ACTION ]
  ]);

  pm.removeAll();
  check_enumerator(uri, [ ]);
  check_enumerator(sub, [ ]);
}