3 const { UrlClassifierTestUtils } = ChromeUtils.import(
4 "resource://testing-common/UrlClassifierTestUtils.jsm"
6 const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
8 // This test supports both e10s and non-e10s mode. In non-e10s mode, this test
9 // drives itself by creating a profile directory, setting up the URL classifier
10 // test tables and adjusting the prefs which are necessary to do the testing.
11 // In e10s mode however, some of these operations such as creating a profile
12 // directory, setting up the URL classifier test tables and setting prefs
13 // aren't supported in the content process, so we split the test into two
14 // parts, the part testing the normal priority case by setting both prefs to
15 // false (test_trackingProtection_annotateChannels_wrap1.js), and the part
16 // testing the lowest priority case by setting both prefs to true
17 // (test_trackingProtection_annotateChannels_wrap2.js). These wrapper scripts
18 // are also in charge of creating the profile directory and setting up the URL
19 // classifier test tables.
21 // Below where we need to take different actions based on the process type we're
22 // in, we use runtime.processType to take the correct actions.
24 const runtime = Services.appinfo;
25 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
29 const defaultTopWindowURI = NetUtil.newURI("http://www.example.com/");
31 function listener(tracking, priority, throttleable, nextTest) {
32 this._tracking = tracking;
33 this._priority = priority;
34 this._throttleable = throttleable;
35 this._nextTest = nextTest;
37 listener.prototype = {
38 onStartRequest(request) {
41 .QueryInterface(Ci.nsIClassifiedChannel)
42 .isThirdPartyTrackingResource(),
47 request.QueryInterface(Ci.nsISupportsPriority).priority,
51 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT && this._tracking) {
54 request.QueryInterface(Ci.nsIClassOfService).classFlags &
55 Ci.nsIClassOfService.Throttleable
61 request.cancel(Cr.NS_ERROR_ABORT);
64 onDataAvailable: (request, stream, offset, count) => {},
65 onStopRequest: (request, status) => {},
69 var normalOrigin, trackingOrigin;
72 // When this test is running in e10s mode, the parent process is in charge of
73 // setting the prefs for us, so here we merely read our prefs, and if they have
74 // been set we skip the normal priority test and only test the lowest priority
75 // case, and if it they have not been set we skip the lowest priority test and
76 // only test the normal priority case.
77 // In non-e10s mode, both of these will remain false and we adjust the prefs
78 // ourselves and test both of the cases in one go.
79 var skipNormalPriority = false,
80 skipLowestPriority = false;
82 function setup_test() {
83 httpServer = new HttpServer();
85 httpServer.identity.setPrimary(
87 "tracking.example.org",
88 httpServer.identity.primaryPort
90 httpServer.identity.add(
93 httpServer.identity.primaryPort
95 normalOrigin = "http://localhost:" + httpServer.identity.primaryPort;
97 "http://tracking.example.org:" + httpServer.identity.primaryPort;
99 if (runtime.processType == runtime.PROCESS_TYPE_CONTENT) {
101 Services.prefs.getBoolPref(
102 "privacy.trackingprotection.annotate_channels"
104 Services.prefs.getBoolPref(
105 "privacy.trackingprotection.lower_network_priority"
108 skipNormalPriority = true;
110 skipLowestPriority = true;
117 function doPriorityTest() {
118 if (!testPriorityMap.length) {
123 currentTest = testPriorityMap.shift();
125 // Let's be explicit about what we're testing!
127 "loadingPrincipal" in currentTest,
128 "check for incomplete test case"
130 Assert.ok("topWindowURI" in currentTest, "check for incomplete test case");
132 var channel = makeChannel(
134 currentTest.loadingPrincipal,
135 currentTest.topWindowURI
139 currentTest.expectedTracking,
140 currentTest.expectedPriority,
141 currentTest.expectedThrottleable,
147 function makeChannel(path, loadingPrincipal, topWindowURI) {
150 if (loadingPrincipal) {
151 chan = NetUtil.newChannel({
154 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
155 contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
158 chan = NetUtil.newChannel({
160 loadUsingSystemPrincipal: true,
163 chan.QueryInterface(Ci.nsIHttpChannel);
164 chan.requestMethod = "GET";
167 .QueryInterface(Ci.nsIHttpChannelInternal)
168 .setTopWindowURIIfUnknown(topWindowURI);
174 // Create the HTTP server.
177 // Add the test table into tracking protection table.
178 function addTestTrackers() {
179 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
180 UrlClassifierTestUtils.addTestTrackers().then(() => {
188 // Annotations OFF, normal loading principal, topWinURI of example.com
189 // => trackers should not be de-prioritized
190 function setupAnnotationsOff() {
191 if (skipNormalPriority) {
195 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
196 Services.prefs.setBoolPref(
197 "privacy.trackingprotection.annotate_channels",
200 Services.prefs.setBoolPref(
201 "privacy.trackingprotection.lower_network_priority",
205 var principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
210 path: normalOrigin + "/innocent.css",
211 loadingPrincipal: principal,
212 topWindowURI: defaultTopWindowURI,
213 expectedTracking: false,
214 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
215 expectedThrottleable: false, // ignored since tracking==false
218 path: normalOrigin + "/innocent.js",
219 loadingPrincipal: principal,
220 topWindowURI: defaultTopWindowURI,
221 expectedTracking: false,
222 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
223 expectedThrottleable: false, // ignored since tracking==false
226 path: trackingOrigin + "/evil.css",
227 loadingPrincipal: principal,
228 topWindowURI: defaultTopWindowURI,
229 expectedTracking: false,
230 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
231 expectedThrottleable: false, // ignored since tracking==false
234 path: trackingOrigin + "/evil.js",
235 loadingPrincipal: principal,
236 topWindowURI: defaultTopWindowURI,
237 expectedTracking: false,
238 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
239 expectedThrottleable: false, // ignored since tracking==false
242 // We add the doPriorityTest test here so that it only gets injected in the
243 // test list if we're not skipping over this test.
244 tests.unshift(doPriorityTest);
248 // Annotations ON, normal loading principal, topWinURI of example.com
249 // => trackers should be de-prioritized
250 function setupAnnotationsOn() {
251 if (skipLowestPriority) {
255 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
256 Services.prefs.setBoolPref(
257 "privacy.trackingprotection.annotate_channels",
260 Services.prefs.setBoolPref(
261 "privacy.trackingprotection.lower_network_priority",
265 var principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
270 path: normalOrigin + "/innocent.css",
271 loadingPrincipal: principal,
272 topWindowURI: defaultTopWindowURI,
273 expectedTracking: false,
274 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
275 expectedThrottleable: false, // ignored since tracking==false
278 path: normalOrigin + "/innocent.js",
279 loadingPrincipal: principal,
280 topWindowURI: defaultTopWindowURI,
281 expectedTracking: false,
282 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
283 expectedThrottleable: false, // ignored since tracking==false
286 path: trackingOrigin + "/evil.css",
287 loadingPrincipal: principal,
288 topWindowURI: defaultTopWindowURI,
289 expectedTracking: true,
290 expectedPriority: Ci.nsISupportsPriority.PRIORITY_LOWEST,
291 expectedThrottleable: true,
294 path: trackingOrigin + "/evil.js",
295 loadingPrincipal: principal,
296 topWindowURI: defaultTopWindowURI,
297 expectedTracking: true,
298 expectedPriority: Ci.nsISupportsPriority.PRIORITY_LOWEST,
299 expectedThrottleable: true,
302 // We add the doPriorityTest test here so that it only gets injected in the
303 // test list if we're not skipping over this test.
304 tests.unshift(doPriorityTest);
308 // Annotations ON, system loading principal, topWinURI of example.com
309 // => trackers should not be de-prioritized
310 function setupAnnotationsOnSystemPrincipal() {
311 if (skipLowestPriority) {
315 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
316 Services.prefs.setBoolPref(
317 "privacy.trackingprotection.annotate_channels",
320 Services.prefs.setBoolPref(
321 "privacy.trackingprotection.lower_network_priority",
327 path: normalOrigin + "/innocent.css",
328 loadingPrincipal: null, // system principal
329 topWindowURI: defaultTopWindowURI,
330 expectedTracking: false,
331 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
332 expectedThrottleable: false, // ignored since tracking==false
335 path: normalOrigin + "/innocent.js",
336 loadingPrincipal: null, // system principal
337 topWindowURI: defaultTopWindowURI,
338 expectedTracking: false,
339 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
340 expectedThrottleable: false, // ignored since tracking==false
343 path: trackingOrigin + "/evil.css",
344 loadingPrincipal: null, // system principal
345 topWindowURI: defaultTopWindowURI,
346 expectedTracking: false,
347 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
348 expectedThrottleable: false,
351 path: trackingOrigin + "/evil.js",
352 loadingPrincipal: null, // system principal
353 topWindowURI: defaultTopWindowURI,
354 expectedTracking: false,
355 expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL,
356 expectedThrottleable: true,
359 // We add the doPriorityTest test here so that it only gets injected in the
360 // test list if we're not skipping over this test.
361 tests.unshift(doPriorityTest);
366 httpServer.stop(do_test_finished);
367 if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
368 UrlClassifierTestUtils.cleanupTestTrackers();
374 function runTests() {
380 var test = tests.shift();
384 function run_test() {