michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: let Cu = Components.utils; michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm", this); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); michael@0: Cu.import("resource://gre/modules/ThirdPartyCookieProbe.jsm", this); michael@0: Cu.import("resource://gre/modules/Promise.jsm", this); michael@0: Cu.import("resource://gre/modules/TelemetryPing.jsm", this); michael@0: michael@0: let TOPIC_ACCEPTED = "third-party-cookie-accepted"; michael@0: let TOPIC_REJECTED = "third-party-cookie-rejected"; michael@0: michael@0: let FLUSH_MILLISECONDS = 1000 * 60 * 60 * 24 / 2; /*Half a day, for testing purposes*/ michael@0: michael@0: const NUMBER_OF_REJECTS = 30; michael@0: const NUMBER_OF_ACCEPTS = 17; michael@0: const NUMBER_OF_REPEATS = 5; michael@0: michael@0: let gCookieService; michael@0: let gThirdPartyCookieProbe; michael@0: michael@0: let gHistograms = { michael@0: clear: function() { michael@0: this.sitesAccepted.clear(); michael@0: this.requestsAccepted.clear(); michael@0: this.sitesRejected.clear(); michael@0: this.requestsRejected.clear(); michael@0: } michael@0: }; michael@0: michael@0: function run_test() { michael@0: do_print("Initializing environment"); michael@0: do_get_profile(); michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); michael@0: gCookieService = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService); michael@0: michael@0: do_print("Initializing ThirdPartyCookieProbe.jsm"); michael@0: gThirdPartyCookieProbe = new ThirdPartyCookieProbe(); michael@0: gThirdPartyCookieProbe.init(); michael@0: michael@0: do_print("Acquiring histograms"); michael@0: gHistograms.sitesAccepted = michael@0: Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_ACCEPTED"); michael@0: gHistograms.sitesRejected = michael@0: Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_BLOCKED"), michael@0: gHistograms.requestsAccepted = michael@0: Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_ACCEPTED"); michael@0: gHistograms.requestsRejected = michael@0: Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_BLOCKED"), michael@0: michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: /** michael@0: * Utility function: try to set a cookie with the given document uri and referrer uri. michael@0: * michael@0: * @param obj An object with the following fields michael@0: * - {string} request The uri of the request setting the cookie. michael@0: * - {string} referrer The uri of the referrer for this request. michael@0: */ michael@0: function tryToSetCookie(obj) { michael@0: let requestURI = Services.io.newURI(obj.request, null, null); michael@0: let referrerURI = Services.io.newURI(obj.referrer, null, null); michael@0: let requestChannel = Services.io.newChannelFromURI(requestURI); michael@0: gCookieService.setCookieString(referrerURI, null, "Is there a cookie in my jar?", requestChannel); michael@0: } michael@0: michael@0: function wait(ms) { michael@0: let deferred = Promise.defer(); michael@0: do_timeout(ms, () => deferred.resolve()); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function oneTest(tld, flushUptime, check) { michael@0: gHistograms.clear(); michael@0: do_print("Testing with tld " + tld); michael@0: michael@0: do_print("Adding rejected entries"); michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", michael@0: 1 /*reject third-party cookies*/); michael@0: michael@0: for (let i = 0; i < NUMBER_OF_REJECTS; ++i) { michael@0: for (let j = 0; j < NUMBER_OF_REPEATS; ++j) { michael@0: for (let prefix of ["http://", "https://"]) { michael@0: // Histogram sitesRejected should only count michael@0: // NUMBER_OF_REJECTS entries. michael@0: // Histogram requestsRejected should count michael@0: // NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 michael@0: tryToSetCookie({ michael@0: request: prefix + "echelon" + tld, michael@0: referrer: prefix + "domain" + i + tld michael@0: }); michael@0: } michael@0: } michael@0: } michael@0: michael@0: do_print("Adding accepted entries"); michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", michael@0: 0 /*accept third-party cookies*/); michael@0: michael@0: for (let i = 0; i < NUMBER_OF_ACCEPTS; ++i) { michael@0: for (let j = 0; j < NUMBER_OF_REPEATS; ++j) { michael@0: for (let prefix of ["http://", "https://"]) { michael@0: // Histogram sitesAccepted should only count michael@0: // NUMBER_OF_ACCEPTS entries. michael@0: // Histogram requestsAccepted should count michael@0: // NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 michael@0: tryToSetCookie({ michael@0: request: prefix + "prism" + tld, michael@0: referrer: prefix + "domain" + i + tld michael@0: }); michael@0: } michael@0: } michael@0: } michael@0: michael@0: do_print("Checking that the histograms have not changed before ping()"); michael@0: do_check_eq(gHistograms.sitesAccepted.snapshot().sum, 0); michael@0: do_check_eq(gHistograms.sitesRejected.snapshot().sum, 0); michael@0: do_check_eq(gHistograms.requestsAccepted.snapshot().sum, 0); michael@0: do_check_eq(gHistograms.requestsRejected.snapshot().sum, 0); michael@0: michael@0: do_print("Checking that the resulting histograms are correct"); michael@0: if (flushUptime != null) { michael@0: let now = Date.now(); michael@0: let before = now - flushUptime; michael@0: gThirdPartyCookieProbe._latestFlush = before; michael@0: gThirdPartyCookieProbe.flush(now); michael@0: } else { michael@0: gThirdPartyCookieProbe.flush(); michael@0: } michael@0: check(); michael@0: } michael@0: michael@0: add_task(function() { michael@0: // To ensure that we work correctly with eTLD, test with several suffixes michael@0: for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) { michael@0: oneTest(tld, FLUSH_MILLISECONDS, function() { michael@0: do_check_eq(gHistograms.sitesAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * 2); michael@0: do_check_eq(gHistograms.sitesRejected.snapshot().sum, NUMBER_OF_REJECTS * 2); michael@0: do_check_eq(gHistograms.requestsAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 * 2); michael@0: do_check_eq(gHistograms.requestsRejected.snapshot().sum, NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 * 2); michael@0: }); michael@0: } michael@0: michael@0: // Check that things still work with default uptime management michael@0: for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) { michael@0: yield wait(1000); // Ensure that uptime is at least one second michael@0: oneTest(tld, null, function() { michael@0: do_check_true(gHistograms.sitesAccepted.snapshot().sum > 0); michael@0: do_check_true(gHistograms.sitesRejected.snapshot().sum > 0); michael@0: do_check_true(gHistograms.requestsAccepted.snapshot().sum > 0); michael@0: do_check_true(gHistograms.requestsRejected.snapshot().sum > 0); michael@0: }); michael@0: } michael@0: }); michael@0: michael@0: add_task(function() { michael@0: gThirdPartyCookieProbe.dispose(); michael@0: }); michael@0: