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