summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_extension_permissions_migration.js
blob: aa05377e0e4245ed6e03678a4d4620f0393c6fe9 (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
107
108
109
110
111
112
"use strict";

const {
  ExtensionPermissions,
  OLD_JSON_FILENAME,
  OLD_RKV_DIRNAME,
  RKV_DIRNAME,
} = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionPermissions.sys.mjs"
);

const GOOD_JSON_FILE = {
  "wikipedia@search.mozilla.org": {
    permissions: ["internal:privateBrowsingAllowed"],
    origins: [],
  },
  "amazon@search.mozilla.org": {
    permissions: ["internal:privateBrowsingAllowed"],
    origins: [],
  },
  "doh-rollout@mozilla.org": {
    permissions: ["internal:privateBrowsingAllowed"],
    origins: [],
  },
};

const BAD_JSON_FILE = {
  "test@example.org": "what",
};

const BAD_FILE = "what is this { } {";

const gOldJSONPath = FileUtils.getDir("ProfD", [OLD_JSON_FILENAME]).path;
const gOldRkvPath = FileUtils.getDir("ProfD", [OLD_RKV_DIRNAME]).path;
const gNewRkvPath = FileUtils.getDir("ProfD", [RKV_DIRNAME]).path;

async function test_file(json, extensionIds, expected, fileDeleted) {
  await ExtensionPermissions._resetVersion();
  await ExtensionPermissions._uninit();

  await IOUtils.writeUTF8(gOldJSONPath, json);

  for (let extensionId of extensionIds) {
    let permissions = await ExtensionPermissions.get(extensionId);
    Assert.deepEqual(permissions, expected, "permissions match");
  }

  Assert.equal(
    await IOUtils.exists(gOldJSONPath),
    !fileDeleted,
    "old file was deleted"
  );

  Assert.ok(
    await IOUtils.exists(gNewRkvPath),
    "found the store at the new rkv path"
  );

  Assert.ok(
    !(await IOUtils.exists(gOldRkvPath)),
    "expect old rkv path to not exist"
  );
}

add_setup(async () => {
  // Bug 1646182: Force ExtensionPermissions to run in rkv mode, because this
  // test does not make sense with the legacy method (which will be removed in
  // the above bug).
  await ExtensionPermissions._uninit();
});

add_task(async function test_migrate_good_json() {
  let expected = {
    permissions: ["internal:privateBrowsingAllowed"],
    origins: [],
  };

  await test_file(
    JSON.stringify(GOOD_JSON_FILE),
    [
      "wikipedia@search.mozilla.org",
      "amazon@search.mozilla.org",
      "doh-rollout@mozilla.org",
    ],
    expected,
    /* fileDeleted */ true
  );
});

add_task(async function test_migrate_bad_json() {
  let expected = { permissions: [], origins: [] };

  await test_file(
    BAD_FILE,
    ["test@example.org"],
    expected,
    /* fileDeleted */ false
  );
  await IOUtils.remove(gOldJSONPath);
});

add_task(async function test_migrate_bad_file() {
  let expected = { permissions: [], origins: [] };

  await test_file(
    JSON.stringify(BAD_JSON_FILE),
    ["test2@example.org"],
    expected,
    /* fileDeleted */ false
  );
  await IOUtils.remove(gOldJSONPath);
});