1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/telemetry/tests/unit/test_ThirdPartyCookieProbe.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +let Cu = Components.utils; 1.8 +let Cc = Components.classes; 1.9 +let Ci = Components.interfaces; 1.10 + 1.11 +Cu.import("resource://gre/modules/Services.jsm", this); 1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); 1.13 +Cu.import("resource://gre/modules/ThirdPartyCookieProbe.jsm", this); 1.14 +Cu.import("resource://gre/modules/Promise.jsm", this); 1.15 +Cu.import("resource://gre/modules/TelemetryPing.jsm", this); 1.16 + 1.17 +let TOPIC_ACCEPTED = "third-party-cookie-accepted"; 1.18 +let TOPIC_REJECTED = "third-party-cookie-rejected"; 1.19 + 1.20 +let FLUSH_MILLISECONDS = 1000 * 60 * 60 * 24 / 2; /*Half a day, for testing purposes*/ 1.21 + 1.22 +const NUMBER_OF_REJECTS = 30; 1.23 +const NUMBER_OF_ACCEPTS = 17; 1.24 +const NUMBER_OF_REPEATS = 5; 1.25 + 1.26 +let gCookieService; 1.27 +let gThirdPartyCookieProbe; 1.28 + 1.29 +let gHistograms = { 1.30 + clear: function() { 1.31 + this.sitesAccepted.clear(); 1.32 + this.requestsAccepted.clear(); 1.33 + this.sitesRejected.clear(); 1.34 + this.requestsRejected.clear(); 1.35 + } 1.36 +}; 1.37 + 1.38 +function run_test() { 1.39 + do_print("Initializing environment"); 1.40 + do_get_profile(); 1.41 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); 1.42 + gCookieService = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService); 1.43 + 1.44 + do_print("Initializing ThirdPartyCookieProbe.jsm"); 1.45 + gThirdPartyCookieProbe = new ThirdPartyCookieProbe(); 1.46 + gThirdPartyCookieProbe.init(); 1.47 + 1.48 + do_print("Acquiring histograms"); 1.49 + gHistograms.sitesAccepted = 1.50 + Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_ACCEPTED"); 1.51 + gHistograms.sitesRejected = 1.52 + Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_BLOCKED"), 1.53 + gHistograms.requestsAccepted = 1.54 + Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_ACCEPTED"); 1.55 + gHistograms.requestsRejected = 1.56 + Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_BLOCKED"), 1.57 + 1.58 + 1.59 + run_next_test(); 1.60 +} 1.61 + 1.62 +/** 1.63 + * Utility function: try to set a cookie with the given document uri and referrer uri. 1.64 + * 1.65 + * @param obj An object with the following fields 1.66 + * - {string} request The uri of the request setting the cookie. 1.67 + * - {string} referrer The uri of the referrer for this request. 1.68 + */ 1.69 +function tryToSetCookie(obj) { 1.70 + let requestURI = Services.io.newURI(obj.request, null, null); 1.71 + let referrerURI = Services.io.newURI(obj.referrer, null, null); 1.72 + let requestChannel = Services.io.newChannelFromURI(requestURI); 1.73 + gCookieService.setCookieString(referrerURI, null, "Is there a cookie in my jar?", requestChannel); 1.74 +} 1.75 + 1.76 +function wait(ms) { 1.77 + let deferred = Promise.defer(); 1.78 + do_timeout(ms, () => deferred.resolve()); 1.79 + return deferred.promise; 1.80 +} 1.81 + 1.82 +function oneTest(tld, flushUptime, check) { 1.83 + gHistograms.clear(); 1.84 + do_print("Testing with tld " + tld); 1.85 + 1.86 + do_print("Adding rejected entries"); 1.87 + Services.prefs.setIntPref("network.cookie.cookieBehavior", 1.88 + 1 /*reject third-party cookies*/); 1.89 + 1.90 + for (let i = 0; i < NUMBER_OF_REJECTS; ++i) { 1.91 + for (let j = 0; j < NUMBER_OF_REPEATS; ++j) { 1.92 + for (let prefix of ["http://", "https://"]) { 1.93 + // Histogram sitesRejected should only count 1.94 + // NUMBER_OF_REJECTS entries. 1.95 + // Histogram requestsRejected should count 1.96 + // NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 1.97 + tryToSetCookie({ 1.98 + request: prefix + "echelon" + tld, 1.99 + referrer: prefix + "domain" + i + tld 1.100 + }); 1.101 + } 1.102 + } 1.103 + } 1.104 + 1.105 + do_print("Adding accepted entries"); 1.106 + Services.prefs.setIntPref("network.cookie.cookieBehavior", 1.107 + 0 /*accept third-party cookies*/); 1.108 + 1.109 + for (let i = 0; i < NUMBER_OF_ACCEPTS; ++i) { 1.110 + for (let j = 0; j < NUMBER_OF_REPEATS; ++j) { 1.111 + for (let prefix of ["http://", "https://"]) { 1.112 + // Histogram sitesAccepted should only count 1.113 + // NUMBER_OF_ACCEPTS entries. 1.114 + // Histogram requestsAccepted should count 1.115 + // NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 1.116 + tryToSetCookie({ 1.117 + request: prefix + "prism" + tld, 1.118 + referrer: prefix + "domain" + i + tld 1.119 + }); 1.120 + } 1.121 + } 1.122 + } 1.123 + 1.124 + do_print("Checking that the histograms have not changed before ping()"); 1.125 + do_check_eq(gHistograms.sitesAccepted.snapshot().sum, 0); 1.126 + do_check_eq(gHistograms.sitesRejected.snapshot().sum, 0); 1.127 + do_check_eq(gHistograms.requestsAccepted.snapshot().sum, 0); 1.128 + do_check_eq(gHistograms.requestsRejected.snapshot().sum, 0); 1.129 + 1.130 + do_print("Checking that the resulting histograms are correct"); 1.131 + if (flushUptime != null) { 1.132 + let now = Date.now(); 1.133 + let before = now - flushUptime; 1.134 + gThirdPartyCookieProbe._latestFlush = before; 1.135 + gThirdPartyCookieProbe.flush(now); 1.136 + } else { 1.137 + gThirdPartyCookieProbe.flush(); 1.138 + } 1.139 + check(); 1.140 +} 1.141 + 1.142 +add_task(function() { 1.143 + // To ensure that we work correctly with eTLD, test with several suffixes 1.144 + for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) { 1.145 + oneTest(tld, FLUSH_MILLISECONDS, function() { 1.146 + do_check_eq(gHistograms.sitesAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * 2); 1.147 + do_check_eq(gHistograms.sitesRejected.snapshot().sum, NUMBER_OF_REJECTS * 2); 1.148 + do_check_eq(gHistograms.requestsAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 * 2); 1.149 + do_check_eq(gHistograms.requestsRejected.snapshot().sum, NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 * 2); 1.150 + }); 1.151 + } 1.152 + 1.153 + // Check that things still work with default uptime management 1.154 + for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) { 1.155 + yield wait(1000); // Ensure that uptime is at least one second 1.156 + oneTest(tld, null, function() { 1.157 + do_check_true(gHistograms.sitesAccepted.snapshot().sum > 0); 1.158 + do_check_true(gHistograms.sitesRejected.snapshot().sum > 0); 1.159 + do_check_true(gHistograms.requestsAccepted.snapshot().sum > 0); 1.160 + do_check_true(gHistograms.requestsRejected.snapshot().sum > 0); 1.161 + }); 1.162 + } 1.163 +}); 1.164 + 1.165 +add_task(function() { 1.166 + gThirdPartyCookieProbe.dispose(); 1.167 +}); 1.168 +