Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | let Cu = Components.utils; |
michael@0 | 5 | let Cc = Components.classes; |
michael@0 | 6 | let Ci = Components.interfaces; |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://gre/modules/Services.jsm", this); |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); |
michael@0 | 10 | Cu.import("resource://gre/modules/ThirdPartyCookieProbe.jsm", this); |
michael@0 | 11 | Cu.import("resource://gre/modules/Promise.jsm", this); |
michael@0 | 12 | Cu.import("resource://gre/modules/TelemetryPing.jsm", this); |
michael@0 | 13 | |
michael@0 | 14 | let TOPIC_ACCEPTED = "third-party-cookie-accepted"; |
michael@0 | 15 | let TOPIC_REJECTED = "third-party-cookie-rejected"; |
michael@0 | 16 | |
michael@0 | 17 | let FLUSH_MILLISECONDS = 1000 * 60 * 60 * 24 / 2; /*Half a day, for testing purposes*/ |
michael@0 | 18 | |
michael@0 | 19 | const NUMBER_OF_REJECTS = 30; |
michael@0 | 20 | const NUMBER_OF_ACCEPTS = 17; |
michael@0 | 21 | const NUMBER_OF_REPEATS = 5; |
michael@0 | 22 | |
michael@0 | 23 | let gCookieService; |
michael@0 | 24 | let gThirdPartyCookieProbe; |
michael@0 | 25 | |
michael@0 | 26 | let gHistograms = { |
michael@0 | 27 | clear: function() { |
michael@0 | 28 | this.sitesAccepted.clear(); |
michael@0 | 29 | this.requestsAccepted.clear(); |
michael@0 | 30 | this.sitesRejected.clear(); |
michael@0 | 31 | this.requestsRejected.clear(); |
michael@0 | 32 | } |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | function run_test() { |
michael@0 | 36 | do_print("Initializing environment"); |
michael@0 | 37 | do_get_profile(); |
michael@0 | 38 | createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); |
michael@0 | 39 | gCookieService = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService); |
michael@0 | 40 | |
michael@0 | 41 | do_print("Initializing ThirdPartyCookieProbe.jsm"); |
michael@0 | 42 | gThirdPartyCookieProbe = new ThirdPartyCookieProbe(); |
michael@0 | 43 | gThirdPartyCookieProbe.init(); |
michael@0 | 44 | |
michael@0 | 45 | do_print("Acquiring histograms"); |
michael@0 | 46 | gHistograms.sitesAccepted = |
michael@0 | 47 | Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_ACCEPTED"); |
michael@0 | 48 | gHistograms.sitesRejected = |
michael@0 | 49 | Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_SITES_BLOCKED"), |
michael@0 | 50 | gHistograms.requestsAccepted = |
michael@0 | 51 | Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_ACCEPTED"); |
michael@0 | 52 | gHistograms.requestsRejected = |
michael@0 | 53 | Services.telemetry.getHistogramById("COOKIES_3RDPARTY_NUM_ATTEMPTS_BLOCKED"), |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | run_next_test(); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * Utility function: try to set a cookie with the given document uri and referrer uri. |
michael@0 | 61 | * |
michael@0 | 62 | * @param obj An object with the following fields |
michael@0 | 63 | * - {string} request The uri of the request setting the cookie. |
michael@0 | 64 | * - {string} referrer The uri of the referrer for this request. |
michael@0 | 65 | */ |
michael@0 | 66 | function tryToSetCookie(obj) { |
michael@0 | 67 | let requestURI = Services.io.newURI(obj.request, null, null); |
michael@0 | 68 | let referrerURI = Services.io.newURI(obj.referrer, null, null); |
michael@0 | 69 | let requestChannel = Services.io.newChannelFromURI(requestURI); |
michael@0 | 70 | gCookieService.setCookieString(referrerURI, null, "Is there a cookie in my jar?", requestChannel); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function wait(ms) { |
michael@0 | 74 | let deferred = Promise.defer(); |
michael@0 | 75 | do_timeout(ms, () => deferred.resolve()); |
michael@0 | 76 | return deferred.promise; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function oneTest(tld, flushUptime, check) { |
michael@0 | 80 | gHistograms.clear(); |
michael@0 | 81 | do_print("Testing with tld " + tld); |
michael@0 | 82 | |
michael@0 | 83 | do_print("Adding rejected entries"); |
michael@0 | 84 | Services.prefs.setIntPref("network.cookie.cookieBehavior", |
michael@0 | 85 | 1 /*reject third-party cookies*/); |
michael@0 | 86 | |
michael@0 | 87 | for (let i = 0; i < NUMBER_OF_REJECTS; ++i) { |
michael@0 | 88 | for (let j = 0; j < NUMBER_OF_REPEATS; ++j) { |
michael@0 | 89 | for (let prefix of ["http://", "https://"]) { |
michael@0 | 90 | // Histogram sitesRejected should only count |
michael@0 | 91 | // NUMBER_OF_REJECTS entries. |
michael@0 | 92 | // Histogram requestsRejected should count |
michael@0 | 93 | // NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 |
michael@0 | 94 | tryToSetCookie({ |
michael@0 | 95 | request: prefix + "echelon" + tld, |
michael@0 | 96 | referrer: prefix + "domain" + i + tld |
michael@0 | 97 | }); |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | do_print("Adding accepted entries"); |
michael@0 | 103 | Services.prefs.setIntPref("network.cookie.cookieBehavior", |
michael@0 | 104 | 0 /*accept third-party cookies*/); |
michael@0 | 105 | |
michael@0 | 106 | for (let i = 0; i < NUMBER_OF_ACCEPTS; ++i) { |
michael@0 | 107 | for (let j = 0; j < NUMBER_OF_REPEATS; ++j) { |
michael@0 | 108 | for (let prefix of ["http://", "https://"]) { |
michael@0 | 109 | // Histogram sitesAccepted should only count |
michael@0 | 110 | // NUMBER_OF_ACCEPTS entries. |
michael@0 | 111 | // Histogram requestsAccepted should count |
michael@0 | 112 | // NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 |
michael@0 | 113 | tryToSetCookie({ |
michael@0 | 114 | request: prefix + "prism" + tld, |
michael@0 | 115 | referrer: prefix + "domain" + i + tld |
michael@0 | 116 | }); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | do_print("Checking that the histograms have not changed before ping()"); |
michael@0 | 122 | do_check_eq(gHistograms.sitesAccepted.snapshot().sum, 0); |
michael@0 | 123 | do_check_eq(gHistograms.sitesRejected.snapshot().sum, 0); |
michael@0 | 124 | do_check_eq(gHistograms.requestsAccepted.snapshot().sum, 0); |
michael@0 | 125 | do_check_eq(gHistograms.requestsRejected.snapshot().sum, 0); |
michael@0 | 126 | |
michael@0 | 127 | do_print("Checking that the resulting histograms are correct"); |
michael@0 | 128 | if (flushUptime != null) { |
michael@0 | 129 | let now = Date.now(); |
michael@0 | 130 | let before = now - flushUptime; |
michael@0 | 131 | gThirdPartyCookieProbe._latestFlush = before; |
michael@0 | 132 | gThirdPartyCookieProbe.flush(now); |
michael@0 | 133 | } else { |
michael@0 | 134 | gThirdPartyCookieProbe.flush(); |
michael@0 | 135 | } |
michael@0 | 136 | check(); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | add_task(function() { |
michael@0 | 140 | // To ensure that we work correctly with eTLD, test with several suffixes |
michael@0 | 141 | for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) { |
michael@0 | 142 | oneTest(tld, FLUSH_MILLISECONDS, function() { |
michael@0 | 143 | do_check_eq(gHistograms.sitesAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * 2); |
michael@0 | 144 | do_check_eq(gHistograms.sitesRejected.snapshot().sum, NUMBER_OF_REJECTS * 2); |
michael@0 | 145 | do_check_eq(gHistograms.requestsAccepted.snapshot().sum, NUMBER_OF_ACCEPTS * NUMBER_OF_REPEATS * 2 * 2); |
michael@0 | 146 | do_check_eq(gHistograms.requestsRejected.snapshot().sum, NUMBER_OF_REJECTS * NUMBER_OF_REPEATS * 2 * 2); |
michael@0 | 147 | }); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // Check that things still work with default uptime management |
michael@0 | 151 | for (let tld of [".com", ".com.ar", ".co.uk", ".gouv.fr"]) { |
michael@0 | 152 | yield wait(1000); // Ensure that uptime is at least one second |
michael@0 | 153 | oneTest(tld, null, function() { |
michael@0 | 154 | do_check_true(gHistograms.sitesAccepted.snapshot().sum > 0); |
michael@0 | 155 | do_check_true(gHistograms.sitesRejected.snapshot().sum > 0); |
michael@0 | 156 | do_check_true(gHistograms.requestsAccepted.snapshot().sum > 0); |
michael@0 | 157 | do_check_true(gHistograms.requestsRejected.snapshot().sum > 0); |
michael@0 | 158 | }); |
michael@0 | 159 | } |
michael@0 | 160 | }); |
michael@0 | 161 | |
michael@0 | 162 | add_task(function() { |
michael@0 | 163 | gThirdPartyCookieProbe.dispose(); |
michael@0 | 164 | }); |
michael@0 | 165 |