Bug 989137 - Part 13: Ability to ignore hashes of downloaded experiments. r=gfritzsche
authorGeorg Fritzsche <georg.fritzsche@googlemail.com>
Thu, 17 Apr 2014 15:47:36 +0200
changeset 179570 9d5d5da02bf259b6301d81e9c4e752c65cfa81e8
parent 179569 cb9a5809f6737c3ce4a0f1d63adf93b9f4aaac89
child 179571 27e2a7c2086e29fd457c475818a7737691ee9c38
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersgfritzsche
bugs989137
milestone31.0a1
Bug 989137 - Part 13: Ability to ignore hashes of downloaded experiments. r=gfritzsche
browser/experiments/Experiments.jsm
browser/experiments/test/xpcshell/test_activate.js
--- a/browser/experiments/Experiments.jsm
+++ b/browser/experiments/Experiments.jsm
@@ -258,16 +258,20 @@ let Experiments = {
  * The policy object allows us to inject fake enviroment data from the
  * outside by monkey-patching.
  */
 
 Experiments.Policy = function () {
   this._log = Log.repository.getLoggerWithMessagePrefix(
     "Browser.Experiments.Policy",
     "Policy #" + gPolicyCounter++ + "::");
+
+  // Set to true to ignore hash verification on downloaded XPIs. This should
+  // not be used outside of testing.
+  this.ignoreHashes = false;
 };
 
 Experiments.Policy.prototype = {
   now: function () {
     return new Date();
   },
 
   random: function () {
@@ -1493,18 +1497,19 @@ Experiments.ExperimentEntry.prototype = 
     this._enabled = true;
     return yield this.reconcileAddonState();
   }),
 
   // Async install of the addon for this experiment, part of the start task above.
   _installAddon: Task.async(function* () {
     let deferred = Promise.defer();
 
-    let install = yield addonInstallForURL(this._manifestData.xpiURL,
-                                           this._manifestData.xpiHash);
+    let hash = this._policy.ignoreHashes ? null : this._manifestData.xpiHash;
+
+    let install = yield addonInstallForURL(this._manifestData.xpiURL, hash);
     gActiveInstallURLs.add(install.sourceURI.spec);
 
     let failureHandler = (install, handler) => {
       let message = "AddonInstall " + handler + " for " + this.id + ", state=" +
                    (install.state || "?") + ", error=" + install.error;
       this._log.error("_installAddon() - " + message);
       this._failedStart = true;
       gActiveInstallURLs.delete(install.sourceURI.spec);
--- a/browser/experiments/test/xpcshell/test_activate.js
+++ b/browser/experiments/test/xpcshell/test_activate.js
@@ -136,9 +136,28 @@ add_task(function* test_startStop() {
   defineNow(gPolicy, futureDate(endDate, MS_IN_ONE_DAY));
   result = yield experiment.shouldStop();
   Assert.equal(result.shouldStop, true, "shouldStop should now be true.");
   changes = yield experiment.stop();
   Assert.equal(changes, experiment.ADDON_CHANGE_UNINSTALL, "Add-on should be uninstalled.");
   Assert.equal(experiment.enabled, false, "Experiment should be disabled.");
   addons = yield getExperimentAddons();
   Assert.equal(addons.length, 0, "Experiment add-on is uninstalled.");
+
+  // Ensure hash validation works.
+  // We set an incorrect hash and expect the install to fail.
+  experiment._manifestData.xpiHash = "sha1:41014dcc66b4dcedcd973491a1530a32f0517d8a";
+  let errored = false;
+  try {
+    yield experiment.start();
+  } catch (ex) {
+    errored = true;
+  }
+  Assert.ok(experiment._failedStart, "Experiment failed to start.");
+  Assert.ok(errored, "start() threw an exception.");
+
+  // Make sure "ignore hashes" mode works.
+  gPolicy.ignoreHashes = true;
+  let changes = yield experiment.start();
+  Assert.equal(changes, experiment.ADDON_CHANGE_INSTALL);
+  yield experiment.stop();
+  gPolicy.ignoreHashes = false;
 });