Bug 837326 - Unit tests for third party addon telemetry;r=froydnj,ehsan
☠☠ backed out by 8ce7857353a6 ☠ ☠
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Fri, 21 Jun 2013 21:19:00 +0200
changeset 148348 65804eae62b149a597b4c01e6189ac778dc5c21b
parent 148347 a0b216d3be12bfa6304484b45f32ea5997f1b85d
child 148349 8ce7857353a6880f79dad8e7be150850d41bc750
push id368
push userbbajaj@mozilla.com
push dateMon, 09 Sep 2013 22:57:58 +0000
treeherdermozilla-release@5a4f47ae1217 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, ehsan
bugs837326
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 837326 - Unit tests for third party addon telemetry;r=froydnj,ehsan
toolkit/components/telemetry/tests/unit/head.js
toolkit/components/telemetry/tests/unit/test_TelemetryPing.js
toolkit/components/telemetry/tests/unit/test_ThirdPartyCookieProbe.js
toolkit/components/telemetry/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/unit/head.js
@@ -0,0 +1,52 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+
+// copied from toolkit/mozapps/extensions/test/xpcshell/head_addons.js
+const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
+const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
+
+function createAppInfo(id, name, version, platformVersion) {
+  gAppInfo = {
+    // nsIXULAppInfo
+    vendor: "Mozilla",
+    name: name,
+    ID: id,
+    version: version,
+    appBuildID: "2007010101",
+    platformVersion: platformVersion,
+    platformBuildID: "2007010101",
+
+    // nsIXULRuntime
+    inSafeMode: false,
+    logConsoleErrors: true,
+    OS: "XPCShell",
+    XPCOMABI: "noarch-spidermonkey",
+    invalidateCachesOnRestart: function invalidateCachesOnRestart() {
+      // Do nothing
+    },
+
+    // nsICrashReporter
+    annotations: {},
+
+    annotateCrashReport: function(key, data) {
+      this.annotations[key] = data;
+    },
+
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo,
+                                           Ci.nsIXULRuntime,
+                                           Ci.nsICrashReporter,
+                                           Ci.nsISupports])
+  };
+
+  var XULAppInfoFactory = {
+    createInstance: function (outer, iid) {
+      if (outer != null)
+        throw Components.results.NS_ERROR_NO_AGGREGATION;
+      return gAppInfo.QueryInterface(iid);
+    }
+  };
+  var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
+  registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
+                            XULAPPINFO_CONTRACTID, XULAppInfoFactory);
+}
--- a/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js
@@ -352,65 +352,16 @@ function runOldPingFileTest() {
   do_check_true(histogramsFile.exists());
 
   let mtime = histogramsFile.lastModifiedTime;
   histogramsFile.lastModifiedTime = mtime - 8 * 24 * 60 * 60 * 1000; // 8 days.
   TelemetryPing.testLoadHistograms(histogramsFile, true);
   do_check_false(histogramsFile.exists());
 }
 
-// copied from toolkit/mozapps/extensions/test/xpcshell/head_addons.js
-const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
-const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
-
-function createAppInfo(id, name, version, platformVersion) {
-  gAppInfo = {
-    // nsIXULAppInfo
-    vendor: "Mozilla",
-    name: name,
-    ID: id,
-    version: version,
-    appBuildID: "2007010101",
-    platformVersion: platformVersion,
-    platformBuildID: "2007010101",
-
-    // nsIXULRuntime
-    inSafeMode: false,
-    logConsoleErrors: true,
-    OS: "XPCShell",
-    XPCOMABI: "noarch-spidermonkey",
-    invalidateCachesOnRestart: function invalidateCachesOnRestart() {
-      // Do nothing
-    },
-
-    // nsICrashReporter
-    annotations: {},
-
-    annotateCrashReport: function(key, data) {
-      this.annotations[key] = data;
-    },
-
-    QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo,
-                                           Ci.nsIXULRuntime,
-                                           Ci.nsICrashReporter,
-                                           Ci.nsISupports])
-  };
-
-  var XULAppInfoFactory = {
-    createInstance: function (outer, iid) {
-      if (outer != null)
-        throw Components.results.NS_ERROR_NO_AGGREGATION;
-      return gAppInfo.QueryInterface(iid);
-    }
-  };
-  var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
-  registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
-                            XULAPPINFO_CONTRACTID, XULAppInfoFactory);
-}
-
 function dummyTheme(id) {
   return {
     id: id,
     name: Math.random().toString(),
     headerURL: "http://lwttest.invalid/a.png",
     footerURL: "http://lwttest.invalid/b.png",
     textcolor: Math.random().toString(),
     accentcolor: Math.random().toString()
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/unit/test_ThirdPartyCookieProbe.js
@@ -0,0 +1,164 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+let Cu = Components.utils;
+let Cc = Components.classes;
+let Ci = Components.interfaces;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/ThirdPartyCookieProbe.jsm");
+Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
+
+let TOPIC_ACCEPTED = "third-party-cookie-accepted";
+let TOPIC_REJECTED = "third-party-cookie-rejected";
+
+let FLUSH_MILLISECONDS = 1000 * 60 * 60 * 24 / 2; /*Half a day, for testing purposes*/
+
+const NUMBER_OF_REJECTS = 30;
+const NUMBER_OF_ACCEPTS = 17;
+const NUMBER_OF_REPEATS = 5;
+
+const TelemetryPing = Cc["@mozilla.org/base/telemetry-ping;1"].getService(Ci.nsITelemetryPing);
+
+let gCookieService;
+let gThirdPartyCookieProbe;
+
+let gHistograms = {
+  clear: function() {
+    this.sitesAccepted.clear();
+    this.requestsAccepted.clear();
+    this.sitesRejected.clear();
+    this.requestsRejected.clear();
+  }
+};
+
+function run_test() {
+  do_print("Initializing environment");
+  do_get_profile();
+  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
+  gCookieService = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
+
+  do_print("Initializing ThirdPartyCookieProbe.jsm");
+  gThirdPartyCookieProbe = new ThirdPartyCookieProbe();
+  gThirdPartyCookieProbe.init();
+
+  do_print("Acquiring histograms");
+  gHistograms.sitesAccepted =
+    Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_ACCEPTED");
+  gHistograms.sitesRejected =
+    Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_BLOCKED"),
+  gHistograms.requestsAccepted =
+    Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_ACCEPTED");
+  gHistograms.requestsRejected =
+    Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_BLOCKED"),
+
+
+  run_next_test();
+}
+
+/**
+ * Utility function: try to set a cookie with the given document uri and referrer uri.
+ *
+ * @param obj An object with the following fields
+ * - {string} request The uri of the request setting the cookie.
+ * - {string} referrer The uri of the referrer for this request.
+ */
+function tryToSetCookie(obj) {
+  let requestURI = Services.io.newURI(obj.request, null, null);
+  let referrerURI = Services.io.newURI(obj.referrer, null, null);
+  let requestChannel = Services.io.newChannelFromURI(requestURI);
+  gCookieService.setCookieString(referrerURI, null, "Is there a cookie in my jar?", requestChannel);
+}
+
+function wait(ms) {
+  let deferred = Promise.defer();
+  do_timeout(ms, () => deferred.resolve());
+  return deferred.promise;
+}
+
+function oneTest(tld, flushUptime, check) {
+  gHistograms.clear();
+  do_print("Testing with tld " + tld);
+
+  do_print("Adding rejected entries");
+  Services.prefs.setIntPref("network.cookie.cookieBehavior",
+                          1 /*reject third-party cookies*/);
+
+  for (let i = 0; i < NUMBER_OF_REJECTS; ++i) {
+    for (let j = 0; j < NUMBER_OF_REPEATS; ++j) {
+      for (let prefix of ["http://", "https://"]) {
+        // Histogram sitesRejected should only count
+        // NUMBER_OF_REJECTS entries.
+        // Histogram requestsRejected should count
+        // NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2
+        tryToSetCookie({
+          request: prefix + "echelon" + tld,
+          referrer: prefix + "domain" + i + tld
+        });
+      }
+    }
+  }
+
+  do_print("Adding accepted entries");
+  Services.prefs.setIntPref("network.cookie.cookieBehavior",
+                            0 /*accept third-party cookies*/);
+
+  for (let i = 0; i < NUMBER_OF_ACCEPTS; ++i) {
+    for (let j = 0; j < NUMBER_OF_REPEATS; ++j) {
+      for (let prefix of ["http://", "https://"]) {
+        // Histogram sitesAccepted should only count
+        // NUMBER_OF_ACCEPTS entries.
+        // Histogram requestsAccepted should count
+        // NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2
+        tryToSetCookie({
+            request: prefix + "prism" + tld,
+            referrer: prefix + "domain" + i + tld
+          });
+      }
+    }
+  }
+
+  do_print("Checking that the histograms have not changed before ping()");
+  do_check_eq(gHistograms.sitesAccepted.snapshot().sum, 0);
+  do_check_eq(gHistograms.sitesRejected.snapshot().sum, 0);
+  do_check_eq(gHistograms.requestsAccepted.snapshot().sum, 0);
+  do_check_eq(gHistograms.requestsRejected.snapshot().sum, 0);
+
+  do_print("Checking that the resulting histograms are correct");
+  if (flushUptime != null) {
+    let now = Date.now();
+    let before = now - flushUptime;
+    gThirdPartyCookieProbe._latestFlush = before;
+  }
+  gThirdPartyCookieProbe.flush();
+  check();
+}
+
+add_task(function() {
+  // To ensure that we work correctly with eTLD, test with several suffixes
+  for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) {
+    oneTest(tld, FLUSH_MILLISECONDS, function() {
+      do_check_eq(gHistograms.sitesAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * 2);
+      do_check_eq(gHistograms.sitesRejected.snapshot().sum, NUMBER_OF_REJECTS * 2);
+      do_check_eq(gHistograms.requestsAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 * 2);
+      do_check_eq(gHistograms.requestsRejected.snapshot().sum, NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 * 2);
+    });
+  }
+
+  // Check that things still work with default uptime management
+  for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) {
+    yield wait(1000); // Ensure that uptime is at least one second
+    oneTest(tld, null, function() {
+      do_check_true(gHistograms.sitesAccepted.snapshot().sum > 0);
+      do_check_true(gHistograms.sitesRejected.snapshot().sum > 0);
+      do_check_true(gHistograms.requestsAccepted.snapshot().sum > 0);
+      do_check_true(gHistograms.requestsRejected.snapshot().sum > 0);
+    });
+  }
+});
+
+add_task(function() {
+  gThirdPartyCookieProbe.dispose();
+});
+
--- a/toolkit/components/telemetry/tests/unit/xpcshell.ini
+++ b/toolkit/components/telemetry/tests/unit/xpcshell.ini
@@ -1,13 +1,14 @@
 [DEFAULT]
-head = 
+head = head.js
 tail = 
 
 [test_nsITelemetry.js]
 [test_TelemetryLateWrites.js]
 [test_TelemetryLockCount.js]
 [test_TelemetryPing.js]
 # Bug 676989: test fails consistently on Android
 # fail-if = os == "android"
 [test_TelemetryPing_idle.js]
 [test_TelemetryStopwatch.js]
 [test_TelemetryPingBuildID.js]
+[test_ThirdPartyCookieProbe.js]