toolkit/mozapps/extensions/test/xpcshell/test_filepointer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_filepointer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,403 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// Tests that various operations with file pointers work and do not affect the
     1.9 +// source files
    1.10 +
    1.11 +var addon1 = {
    1.12 +  id: "addon1@tests.mozilla.org",
    1.13 +  version: "1.0",
    1.14 +  name: "Test 1",
    1.15 +  targetApplications: [{
    1.16 +    id: "xpcshell@tests.mozilla.org",
    1.17 +    minVersion: "1",
    1.18 +    maxVersion: "1"
    1.19 +  }]
    1.20 +};
    1.21 +
    1.22 +var addon1_2 = {
    1.23 +  id: "addon1@tests.mozilla.org",
    1.24 +  version: "2.0",
    1.25 +  name: "Test 1",
    1.26 +  targetApplications: [{
    1.27 +    id: "xpcshell@tests.mozilla.org",
    1.28 +    minVersion: "1",
    1.29 +    maxVersion: "1"
    1.30 +  }]
    1.31 +};
    1.32 +
    1.33 +var addon2 = {
    1.34 +  id: "addon2@tests.mozilla.org",
    1.35 +  version: "1.0",
    1.36 +  name: "Test 2",
    1.37 +  targetApplications: [{
    1.38 +    id: "xpcshell@tests.mozilla.org",
    1.39 +    minVersion: "1",
    1.40 +    maxVersion: "1"
    1.41 +  }]
    1.42 +};
    1.43 +
    1.44 +const profileDir = gProfD.clone();
    1.45 +profileDir.append("extensions");
    1.46 +profileDir.create(AM_Ci.nsIFile.DIRECTORY_TYPE, 0755);
    1.47 +
    1.48 +const sourceDir = gProfD.clone();
    1.49 +sourceDir.append("source");
    1.50 +
    1.51 +Components.utils.import("resource://testing-common/httpd.js");
    1.52 +var testserver;
    1.53 +
    1.54 +function writePointer(aId, aName) {
    1.55 +  let file = profileDir.clone();
    1.56 +  file.append(aName ? aName : aId);
    1.57 +
    1.58 +  let target = sourceDir.clone();
    1.59 +  target.append(do_get_expected_addon_name(aId));
    1.60 +
    1.61 +  var fos = AM_Cc["@mozilla.org/network/file-output-stream;1"].
    1.62 +            createInstance(AM_Ci.nsIFileOutputStream);
    1.63 +  fos.init(file,
    1.64 +           FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE,
    1.65 +           FileUtils.PERMS_FILE, 0);
    1.66 +  fos.write(target.path, target.path.length);
    1.67 +  fos.close();
    1.68 +}
    1.69 +
    1.70 +function writeRelativePointer(aId, aName) {
    1.71 +  let file = profileDir.clone();
    1.72 +  file.append(aName ? aName : aId);
    1.73 +
    1.74 +  let absTarget = sourceDir.clone();
    1.75 +  absTarget.append(do_get_expected_addon_name(aId));
    1.76 +
    1.77 +  var relTarget = absTarget.getRelativeDescriptor(profileDir);
    1.78 +
    1.79 +  var fos = AM_Cc["@mozilla.org/network/file-output-stream;1"].
    1.80 +            createInstance(AM_Ci.nsIFileOutputStream);
    1.81 +  fos.init(file,
    1.82 +           FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE,
    1.83 +           FileUtils.PERMS_FILE, 0);
    1.84 +  fos.write(relTarget, relTarget.length);
    1.85 +  fos.close();
    1.86 +}
    1.87 +
    1.88 +function run_test() {
    1.89 +  // pointer files only work with unpacked directories
    1.90 +  if (Services.prefs.getBoolPref("extensions.alwaysUnpack") == false)
    1.91 +    return;
    1.92 +
    1.93 +  do_test_pending();
    1.94 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
    1.95 +
    1.96 +  // Create and configure the HTTP server.
    1.97 +  testserver = new HttpServer();
    1.98 +  testserver.registerDirectory("/data/", do_get_file("data"));
    1.99 +  testserver.registerDirectory("/addons/", do_get_file("addons"));
   1.100 +  testserver.start(-1);
   1.101 +  gPort = testserver.identity.primaryPort;
   1.102 +
   1.103 +  run_test_1();
   1.104 +}
   1.105 +
   1.106 +function end_test() {
   1.107 +  testserver.stop(do_test_finished);
   1.108 +}
   1.109 +
   1.110 +// Tests that installing a new add-on by pointer works
   1.111 +function run_test_1() {
   1.112 +  writeInstallRDFForExtension(addon1, sourceDir);
   1.113 +  writePointer(addon1.id);
   1.114 +
   1.115 +  startupManager();
   1.116 +
   1.117 +  AddonManager.getAddonByID(addon1.id, function(a1) {
   1.118 +    do_check_neq(a1, null);
   1.119 +    do_check_eq(a1.version, "1.0");
   1.120 +
   1.121 +    let file = a1.getResourceURI().QueryInterface(AM_Ci.nsIFileURL).file;
   1.122 +    do_check_eq(file.parent.path, sourceDir.path);
   1.123 +
   1.124 +    let rootUri = do_get_addon_root_uri(sourceDir, addon1.id);
   1.125 +    let uri = a1.getResourceURI("/");
   1.126 +    do_check_eq(uri.spec, rootUri);
   1.127 +    uri = a1.getResourceURI("install.rdf");
   1.128 +    do_check_eq(uri.spec, rootUri + "install.rdf");
   1.129 +    
   1.130 +    // Check that upgrade is disabled for addons installed by file-pointers.
   1.131 +    do_check_eq(a1.permissions & AddonManager.PERM_CAN_UPGRADE, 0);
   1.132 +    run_test_2();
   1.133 +  });
   1.134 +}
   1.135 +
   1.136 +// Tests that installing the addon from some other source doesn't clobber
   1.137 +// the original sources
   1.138 +function run_test_2() {
   1.139 +  prepare_test({}, [
   1.140 +    "onNewInstall",
   1.141 +  ]);
   1.142 +
   1.143 +  let url = "http://localhost:" + gPort + "/addons/test_filepointer.xpi";
   1.144 +  AddonManager.getInstallForURL(url, function(install) {
   1.145 +    ensure_test_completed();
   1.146 +
   1.147 +    prepare_test({
   1.148 +      "addon1@tests.mozilla.org": [
   1.149 +        "onInstalling"
   1.150 +      ]
   1.151 +    }, [
   1.152 +      "onDownloadStarted",
   1.153 +      "onDownloadEnded",
   1.154 +      "onInstallStarted",
   1.155 +      "onInstallEnded"
   1.156 +    ], callback_soon(check_test_2));
   1.157 +
   1.158 +    install.install();
   1.159 +  }, "application/x-xpinstall");
   1.160 +}
   1.161 +
   1.162 +function check_test_2() {
   1.163 +  restartManager();
   1.164 +
   1.165 +  AddonManager.getAddonByID(addon1.id, function(a1) {
   1.166 +    do_check_neq(a1, null);
   1.167 +    do_check_eq(a1.version, "2.0");
   1.168 +
   1.169 +    let file = a1.getResourceURI().QueryInterface(AM_Ci.nsIFileURL).file;
   1.170 +    do_check_eq(file.parent.path, profileDir.path);
   1.171 +
   1.172 +    let rootUri = do_get_addon_root_uri(profileDir, addon1.id);
   1.173 +    let uri = a1.getResourceURI("/");
   1.174 +    do_check_eq(uri.spec, rootUri);
   1.175 +    uri = a1.getResourceURI("install.rdf");
   1.176 +    do_check_eq(uri.spec, rootUri + "install.rdf");
   1.177 +
   1.178 +    let source = sourceDir.clone();
   1.179 +    source.append(addon1.id);
   1.180 +    do_check_true(source.exists());
   1.181 +
   1.182 +    a1.uninstall();
   1.183 +
   1.184 +    do_execute_soon(run_test_3);
   1.185 +  });
   1.186 +}
   1.187 +
   1.188 +// Tests that uninstalling doesn't clobber the original sources
   1.189 +function run_test_3() {
   1.190 +  restartManager();
   1.191 +
   1.192 +  writePointer(addon1.id);
   1.193 +
   1.194 +  restartManager();
   1.195 +
   1.196 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) {
   1.197 +    do_check_neq(a1, null);
   1.198 +    do_check_eq(a1.version, "1.0");
   1.199 +
   1.200 +    a1.uninstall();
   1.201 +
   1.202 +    restartManager();
   1.203 +
   1.204 +    let source = sourceDir.clone();
   1.205 +    source.append(addon1.id);
   1.206 +    do_check_true(source.exists());
   1.207 +
   1.208 +    do_execute_soon(run_test_4);
   1.209 +  }));
   1.210 +}
   1.211 +
   1.212 +// Tests that misnaming a pointer doesn't clobber the sources
   1.213 +function run_test_4() {
   1.214 +  writePointer("addon2@tests.mozilla.org", addon1.id);
   1.215 +
   1.216 +  restartManager();
   1.217 +
   1.218 +  AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org",
   1.219 +                               "addon2@tests.mozilla.org"], function([a1, a2]) {
   1.220 +    do_check_eq(a1, null);
   1.221 +    do_check_eq(a2, null);
   1.222 +
   1.223 +    let source = sourceDir.clone();
   1.224 +    source.append(addon1.id);
   1.225 +    do_check_true(source.exists());
   1.226 +
   1.227 +    let pointer = profileDir.clone();
   1.228 +    pointer.append("addon2@tests.mozilla.org");
   1.229 +    do_check_false(pointer.exists());
   1.230 +
   1.231 +    do_execute_soon(run_test_5);
   1.232 +  });
   1.233 +}
   1.234 +
   1.235 +// Tests that changing the ID of an existing add-on doesn't clobber the sources
   1.236 +function run_test_5() {
   1.237 +  var dest = writeInstallRDFForExtension(addon1, sourceDir);
   1.238 +  // Make sure the modification time changes enough to be detected.
   1.239 +  setExtensionModifiedTime(dest, dest.lastModifiedTime - 5000);
   1.240 +  writePointer(addon1.id);
   1.241 +
   1.242 +  restartManager();
   1.243 +
   1.244 +  AddonManager.getAddonByID(addon1.id, callback_soon(function(a1) {
   1.245 +    do_check_neq(a1, null);
   1.246 +    do_check_eq(a1.version, "1.0");
   1.247 +
   1.248 +    writeInstallRDFForExtension(addon2, sourceDir, addon1.id);
   1.249 +
   1.250 +    restartManager();
   1.251 +
   1.252 +    AddonManager.getAddonsByIDs(["addon1@tests.mozilla.org",
   1.253 +                                 "addon2@tests.mozilla.org"], function([a1, a2]) {
   1.254 +      do_check_eq(a1, null);
   1.255 +      do_check_eq(a2, null);
   1.256 +
   1.257 +      let source = sourceDir.clone();
   1.258 +      source.append(addon1.id);
   1.259 +      do_check_true(source.exists());
   1.260 +
   1.261 +      let pointer = profileDir.clone();
   1.262 +      pointer.append(addon1.id);
   1.263 +      do_check_false(pointer.exists());
   1.264 +
   1.265 +      do_execute_soon(run_test_6);
   1.266 +    });
   1.267 +  }));
   1.268 +}
   1.269 +
   1.270 +// Removing the pointer file should uninstall the add-on
   1.271 +function run_test_6() {
   1.272 +  var dest = writeInstallRDFForExtension(addon1, sourceDir);
   1.273 +  // Make sure the modification time changes enough to be detected in run_test_8.
   1.274 +  setExtensionModifiedTime(dest, dest.lastModifiedTime - 5000);
   1.275 +  writePointer(addon1.id);
   1.276 +
   1.277 +  restartManager();
   1.278 +
   1.279 +  AddonManager.getAddonByID(addon1.id, callback_soon(function(a1) {
   1.280 +    do_check_neq(a1, null);
   1.281 +    do_check_eq(a1.version, "1.0");
   1.282 +
   1.283 +    let pointer = profileDir.clone();
   1.284 +    pointer.append(addon1.id);
   1.285 +    pointer.remove(false);
   1.286 +
   1.287 +    restartManager();
   1.288 +
   1.289 +    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
   1.290 +      do_check_eq(a1, null);
   1.291 +
   1.292 +      do_execute_soon(run_test_7);
   1.293 +    });
   1.294 +  }));
   1.295 +}
   1.296 +
   1.297 +// Removing the pointer file and replacing it with a directory should work
   1.298 +function run_test_7() {
   1.299 +  writePointer(addon1.id);
   1.300 +
   1.301 +  restartManager();
   1.302 +
   1.303 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) {
   1.304 +    do_check_neq(a1, null);
   1.305 +    do_check_eq(a1.version, "1.0");
   1.306 +
   1.307 +    let pointer = profileDir.clone();
   1.308 +    pointer.append(addon1.id);
   1.309 +    pointer.remove(false);
   1.310 +
   1.311 +    writeInstallRDFForExtension(addon1_2, profileDir);
   1.312 +
   1.313 +    restartManager();
   1.314 +
   1.315 +    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
   1.316 +      do_check_neq(a1, null);
   1.317 +      do_check_eq(a1.version, "2.0");
   1.318 +
   1.319 +      a1.uninstall();
   1.320 +
   1.321 +      do_execute_soon(run_test_8);
   1.322 +    });
   1.323 +  }));
   1.324 +}
   1.325 +
   1.326 +// Changes to the source files should be detected
   1.327 +function run_test_8() {
   1.328 +  restartManager();
   1.329 +
   1.330 +  writePointer(addon1.id);
   1.331 +
   1.332 +  restartManager();
   1.333 +
   1.334 +  AddonManager.getAddonByID("addon1@tests.mozilla.org", callback_soon(function(a1) {
   1.335 +    do_check_neq(a1, null);
   1.336 +    do_check_eq(a1.version, "1.0");
   1.337 +
   1.338 +    writeInstallRDFForExtension(addon1_2, sourceDir);
   1.339 +
   1.340 +    restartManager();
   1.341 +
   1.342 +    AddonManager.getAddonByID("addon1@tests.mozilla.org", function(a1) {
   1.343 +      do_check_neq(a1, null);
   1.344 +      do_check_eq(a1.version, "2.0");
   1.345 +
   1.346 +      a1.uninstall();
   1.347 +
   1.348 +      do_execute_soon(run_test_9);
   1.349 +    });
   1.350 +  }));
   1.351 +}
   1.352 +
   1.353 +// Removing the add-on the pointer file points at should uninstall the add-on
   1.354 +function run_test_9() {
   1.355 +  restartManager();
   1.356 +
   1.357 +  var dest = writeInstallRDFForExtension(addon1, sourceDir);
   1.358 +  writePointer(addon1.id);
   1.359 +
   1.360 +  restartManager();
   1.361 +
   1.362 +  AddonManager.getAddonByID(addon1.id, callback_soon(function(a1) {
   1.363 +    do_check_neq(a1, null);
   1.364 +    do_check_eq(a1.version, "1.0");
   1.365 +
   1.366 +    dest.remove(true);
   1.367 +
   1.368 +    restartManager();
   1.369 +
   1.370 +    AddonManager.getAddonByID(addon1.id, function(a1) {
   1.371 +      do_check_eq(a1, null);
   1.372 +
   1.373 +      let pointer = profileDir.clone();
   1.374 +      pointer.append(addon1.id);
   1.375 +      do_check_false(pointer.exists());
   1.376 +
   1.377 +      do_execute_soon(run_test_10);
   1.378 +    });
   1.379 +  }));
   1.380 +}
   1.381 +
   1.382 +// Tests that installing a new add-on by pointer with a relative path works
   1.383 +function run_test_10() {
   1.384 +  writeInstallRDFForExtension(addon1, sourceDir);
   1.385 +  writeRelativePointer(addon1.id);
   1.386 +
   1.387 +  restartManager();
   1.388 +
   1.389 +  AddonManager.getAddonByID(addon1.id, function(a1) {
   1.390 +    do_check_neq(a1, null);
   1.391 +    do_check_eq(a1.version, "1.0");
   1.392 +
   1.393 +    let file = a1.getResourceURI().QueryInterface(AM_Ci.nsIFileURL).file;
   1.394 +    do_check_eq(file.parent.path, sourceDir.path);
   1.395 +
   1.396 +    let rootUri = do_get_addon_root_uri(sourceDir, addon1.id);
   1.397 +    let uri = a1.getResourceURI("/");
   1.398 +    do_check_eq(uri.spec, rootUri);
   1.399 +    uri = a1.getResourceURI("install.rdf");
   1.400 +    do_check_eq(uri.spec, rootUri + "install.rdf");
   1.401 +    
   1.402 +    // Check that upgrade is disabled for addons installed by file-pointers.
   1.403 +    do_check_eq(a1.permissions & AddonManager.PERM_CAN_UPGRADE, 0);
   1.404 +    end_test();
   1.405 +  });
   1.406 +}

mercurial