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

function run_test() {
  // initialize the permission manager service
  let ssm = Services.scriptSecurityManager;
  let pm = Services.perms;

  function mkPrin(uri, appId, inIsolatedMozBrowser) {
    return ssm.createCodebasePrincipal(Services.io.newURI(uri, null, null),
                                       {appId: appId, inIsolatedMozBrowser: inIsolatedMozBrowser});
  }

  function checkPerms(perms) {
    perms.forEach((perm) => {
      // Look up the expected permission
      do_check_eq(pm.getPermissionObject(mkPrin(perm[0],  perm[1], perm[2]),
                                         perm[3], true).capability,
                  perm[4], "Permission is expected in the permission database");
    });

    // Count the entries
    let count = 0;
    let enumerator = Services.perms.enumerator;
    while (enumerator.hasMoreElements()) { enumerator.getNext(); count++; }

    do_check_eq(count, perms.length, "There should be the right number of permissions in the DB");
  }

  checkPerms([]);

  let permissions = [
    ['http://google.com',  1001, false, 'a', 1],
    ['http://google.com',  1001, false, 'b', 1],
    ['http://mozilla.com', 1001, false, 'b', 1],
    ['http://mozilla.com', 1001, false, 'a', 1],

    ['http://google.com',  1001, true, 'a', 1],
    ['http://google.com',  1001, true, 'b', 1],
    ['http://mozilla.com', 1001, true, 'b', 1],
    ['http://mozilla.com', 1001, true, 'a', 1],

    ['http://google.com',  1011, false, 'a', 1],
    ['http://google.com',  1011, false, 'b', 1],
    ['http://mozilla.com', 1011, false, 'b', 1],
    ['http://mozilla.com', 1011, false, 'a', 1],
  ];

  permissions.forEach((perm) => {
    pm.addFromPrincipal(mkPrin(perm[0], perm[1], perm[2]), perm[3], perm[4]);
  });

  checkPerms(permissions);

  let remove_false_perms = [
    ['http://google.com',  1011, false, 'a', 1],
    ['http://google.com',  1011, false, 'b', 1],
    ['http://mozilla.com', 1011, false, 'b', 1],
    ['http://mozilla.com', 1011, false, 'a', 1],
  ];

  let attrs = { appId: 1001 };
  pm.removePermissionsWithAttributes(JSON.stringify(attrs));
  checkPerms(remove_false_perms);

  let restore = [
    ['http://google.com',  1001, false, 'a', 1],
    ['http://google.com',  1001, false, 'b', 1],
    ['http://mozilla.com', 1001, false, 'b', 1],
    ['http://mozilla.com', 1001, false, 'a', 1],

    ['http://google.com',  1001, true, 'a', 1],
    ['http://google.com',  1001, true, 'b', 1],
    ['http://mozilla.com', 1001, true, 'b', 1],
    ['http://mozilla.com', 1001, true, 'a', 1],
  ];

  restore.forEach((perm) => {
    pm.addFromPrincipal(mkPrin(perm[0],  perm[1], perm[2]), perm[3], perm[4]);
  });
  checkPerms(permissions);

  let remove_true_perms = [
    ['http://google.com',  1001, false, 'a', 1],
    ['http://google.com',  1001, false, 'b', 1],
    ['http://mozilla.com', 1001, false, 'b', 1],
    ['http://mozilla.com', 1001, false, 'a', 1],

    ['http://google.com',  1011, false, 'a', 1],
    ['http://google.com',  1011, false, 'b', 1],
    ['http://mozilla.com', 1011, false, 'b', 1],
    ['http://mozilla.com', 1011, false, 'a', 1],
  ];

  attrs = { appId: 1001,
            inIsolatedMozBrowser: true };
  pm.removePermissionsWithAttributes(JSON.stringify(attrs));
  checkPerms(remove_true_perms);
}