Bug 989137 - Part 12: Ability to ignore hashes of downloaded experiments; r=gfritzsche
authorGregory Szorc <gps@mozilla.com>
Thu, 10 Apr 2014 13:18:19 -0700
changeset 179393 1ab6bbd62c76e13a81fc3bc164c3f125e8758b82
parent 179392 a5cee95d219ec9021d5b59d51dde9404293715c2
child 179394 5c3a195a107ee64200bb65d041876ca8ba9d65a9
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersgfritzsche
bugs989137
milestone31.0a1
Bug 989137 - Part 12: Ability to ignore hashes of downloaded experiments; r=gfritzsche We normally validate hashes of XPIs as part of installing experiments. While a useful security feature, this patch makes that optional. This in turn makes automated and manual testing easier.
browser/experiments/Experiments.jsm
browser/experiments/test/xpcshell/test_activate.js
--- a/browser/experiments/Experiments.jsm
+++ b/browser/experiments/Experiments.jsm
@@ -247,16 +247,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 = 
       gPrefs.set(PREF_ACTIVE_EXPERIMENT, true);
     }.bind(this));
   },
 
   // Async install of the addon for this experiment, part of the start task above.
   _installAddon: 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
@@ -135,9 +135,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.");
   maybeStop = yield experiment.maybeStop();
   Assert.equal(maybeStop, true, "Experiment should have been stopped.");
   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;
 });