summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_proxies.js
blob: 2f40147f83496051ad9bea4e89d7462e8f8640b2 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests the semantics of extension proxy files and symlinks

var ADDONS = [
  {
    id: "proxy1@tests.mozilla.org",
    dirId: "proxy1@tests.mozilla.com",
    type: "proxy",
  },
  {
    id: "proxy2@tests.mozilla.org",
    type: "proxy",
  },
  {
    id: "symlink1@tests.mozilla.org",
    dirId: "symlink1@tests.mozilla.com",
    type: "symlink",
  },
  {
    id: "symlink2@tests.mozilla.org",
    type: "symlink",
  },
];

const gHaveSymlinks = AppConstants.platform != "win";

function createSymlink(aSource, aDest) {
  if (aSource instanceof Ci.nsIFile) {
    aSource = aSource.path;
  }
  if (aDest instanceof Ci.nsIFile) {
    aDest = aDest.path;
  }

  const ln = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  ln.initWithPath("/bin/ln");

  const lnProcess = Cc["@mozilla.org/process/util;1"].createInstance(
    Ci.nsIProcess
  );
  lnProcess.init(ln);

  const args = ["-s", aSource, aDest];
  lnProcess.run(true, args, args.length);
  Assert.equal(lnProcess.exitValue, 0);
}

async function promiseWriteFile(aFile, aData) {
  if (!(await IOUtils.exists(aFile.parent.path))) {
    await IOUtils.makeDirectory(aFile.parent.path);
  }

  return IOUtils.writeUTF8(aFile.path, aData);
}

function checkAddonsExist() {
  for (let addon of ADDONS) {
    let file = addon.directory.clone();
    file.append("manifest.json");
    Assert.ok(file.exists());
  }
}

const profileDir = gProfD.clone();
profileDir.append("extensions");

function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2", "2");

  // Unpacked extensions are never signed, so this can only run with
  // signature checks disabled.
  Services.prefs.setBoolPref(PREF_XPI_SIGNATURES_REQUIRED, false);

  add_task(run_proxy_tests);

  if (gHaveSymlinks) {
    add_task(run_symlink_tests);
  }

  run_next_test();
}

async function run_proxy_tests() {
  if (!gHaveSymlinks) {
    ADDONS = ADDONS.filter(a => a.type != "symlink");
  }

  for (let addon of ADDONS) {
    addon.directory = gTmpD.clone();
    addon.directory.append(addon.id);

    addon.proxyFile = profileDir.clone();
    addon.proxyFile.append(addon.dirId || addon.id);

    let files = ExtensionTestCommon.generateFiles({
      manifest: {
        name: addon.id,
        browser_specific_settings: { gecko: { id: addon.id } },
      },
    });
    let path = PathUtils.join(gTmpD.path, addon.id);
    await AddonTestUtils.promiseWriteFilesToDir(path, files);

    if (addon.type == "proxy") {
      await promiseWriteFile(addon.proxyFile, addon.directory.path);
    } else if (addon.type == "symlink") {
      await createSymlink(addon.directory, addon.proxyFile);
    }
  }

  await promiseStartupManager();

  // Check that all add-ons original sources still exist after invalid
  // add-ons have been removed at startup.
  checkAddonsExist();

  let addons = await AddonManager.getAddonsByIDs(ADDONS.map(addon => addon.id));
  try {
    for (let [i, addon] of addons.entries()) {
      // Ensure that valid proxied add-ons were installed properly on
      // platforms that support the installation method.
      print(
        ADDONS[i].id,
        ADDONS[i].dirId,
        ADDONS[i].dirId != null,
        ADDONS[i].type == "symlink"
      );
      Assert.equal(addon == null, ADDONS[i].dirId != null);

      if (addon != null) {
        let fixURL = url => {
          if (AppConstants.platform == "macosx") {
            return url.replace(RegExp(`^file:///private/`), "file:///");
          }
          return url;
        };

        // Check that proxied add-ons do not have upgrade permissions.
        Assert.equal(addon.permissions & AddonManager.PERM_CAN_UPGRADE, 0);

        // Check that getResourceURI points to the right place.
        Assert.equal(
          Services.io.newFileURI(ADDONS[i].directory).spec,
          fixURL(addon.getResourceURI().spec),
          `Base resource URL resolves as expected`
        );

        let file = ADDONS[i].directory.clone();
        file.append("manifest.json");

        Assert.equal(
          Services.io.newFileURI(file).spec,
          fixURL(addon.getResourceURI("manifest.json").spec),
          `Resource URLs resolve as expected`
        );

        await addon.uninstall();
      }
    }

    // Check that original sources still exist after explicit uninstall.
    await promiseRestartManager();
    checkAddonsExist();

    await promiseShutdownManager();

    // Check that all of the proxy files have been removed and remove
    // the original targets.
    for (let addon of ADDONS) {
      equal(
        addon.proxyFile.exists(),
        addon.dirId != null,
        `Proxy file ${addon.proxyFile.path} should exist?`
      );
      addon.directory.remove(true);
      try {
        addon.proxyFile.remove(false);
      } catch (e) {}
    }
  } catch (e) {
    do_throw(e);
  }
}

// Check that symlinks are not followed out of a directory tree
// when deleting an add-on.
async function run_symlink_tests() {
  const ID = "unpacked@test.mozilla.org";

  let tempDirectory = gTmpD.clone();
  tempDirectory.append(ID);

  let tempFile = tempDirectory.clone();
  tempFile.append("test.txt");
  tempFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);

  let addonDirectory = profileDir.clone();
  addonDirectory.append(ID);

  let files = ExtensionTestCommon.generateFiles({
    manifest: { browser_specific_settings: { gecko: { id: ID } } },
  });
  await AddonTestUtils.promiseWriteFilesToDir(addonDirectory.path, files);

  let symlink = addonDirectory.clone();
  symlink.append(tempDirectory.leafName);
  await createSymlink(tempDirectory, symlink);

  // Make sure that the symlink was created properly.
  let file = symlink.clone();
  file.append(tempFile.leafName);
  file.normalize();
  Assert.equal(file.path.replace(/^\/private\//, "/"), tempFile.path);

  await promiseStartupManager();

  let addon = await AddonManager.getAddonByID(ID);
  Assert.notEqual(addon, null);

  await addon.uninstall();

  await promiseRestartManager();
  await promiseShutdownManager();

  // Check that the install directory is gone.
  Assert.ok(!addonDirectory.exists());

  // Check that the temp file is not gone.
  Assert.ok(tempFile.exists());

  tempDirectory.remove(true);
}