Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / test / unit / test_trackingProtection_annotateChannels.js
blobcbcc13cb4d7c675da82eeaf7b71297ba8a114e54
1 "use strict";
3 const { UrlClassifierTestUtils } = ChromeUtils.import(
4   "resource://testing-common/UrlClassifierTestUtils.jsm"
5 );
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) {
26   do_get_profile();
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) {
39     Assert.equal(
40       request
41         .QueryInterface(Ci.nsIClassifiedChannel)
42         .isThirdPartyTrackingResource(),
43       this._tracking,
44       "tracking flag"
45     );
46     Assert.equal(
47       request.QueryInterface(Ci.nsISupportsPriority).priority,
48       this._priority,
49       "channel priority"
50     );
51     if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT && this._tracking) {
52       Assert.equal(
53         !!(
54           request.QueryInterface(Ci.nsIClassOfService).classFlags &
55           Ci.nsIClassOfService.Throttleable
56         ),
57         this._throttleable,
58         "throttleable flag"
59       );
60     }
61     request.cancel(Cr.NS_ERROR_ABORT);
62     this._nextTest();
63   },
64   onDataAvailable: (request, stream, offset, count) => {},
65   onStopRequest: (request, status) => {},
68 var httpServer;
69 var normalOrigin, trackingOrigin;
70 var testPriorityMap;
71 var currentTest;
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();
84   httpServer.start(-1);
85   httpServer.identity.setPrimary(
86     "http",
87     "tracking.example.org",
88     httpServer.identity.primaryPort
89   );
90   httpServer.identity.add(
91     "http",
92     "example.org",
93     httpServer.identity.primaryPort
94   );
95   normalOrigin = "http://localhost:" + httpServer.identity.primaryPort;
96   trackingOrigin =
97     "http://tracking.example.org:" + httpServer.identity.primaryPort;
99   if (runtime.processType == runtime.PROCESS_TYPE_CONTENT) {
100     if (
101       Services.prefs.getBoolPref(
102         "privacy.trackingprotection.annotate_channels"
103       ) &&
104       Services.prefs.getBoolPref(
105         "privacy.trackingprotection.lower_network_priority"
106       )
107     ) {
108       skipNormalPriority = true;
109     } else {
110       skipLowestPriority = true;
111     }
112   }
114   runTests();
117 function doPriorityTest() {
118   if (!testPriorityMap.length) {
119     runTests();
120     return;
121   }
123   currentTest = testPriorityMap.shift();
125   // Let's be explicit about what we're testing!
126   Assert.ok(
127     "loadingPrincipal" in currentTest,
128     "check for incomplete test case"
129   );
130   Assert.ok("topWindowURI" in currentTest, "check for incomplete test case");
132   var channel = makeChannel(
133     currentTest.path,
134     currentTest.loadingPrincipal,
135     currentTest.topWindowURI
136   );
137   channel.asyncOpen(
138     new listener(
139       currentTest.expectedTracking,
140       currentTest.expectedPriority,
141       currentTest.expectedThrottleable,
142       doPriorityTest
143     )
144   );
147 function makeChannel(path, loadingPrincipal, topWindowURI) {
148   var chan;
150   if (loadingPrincipal) {
151     chan = NetUtil.newChannel({
152       uri: path,
153       loadingPrincipal,
154       securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
155       contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
156     });
157   } else {
158     chan = NetUtil.newChannel({
159       uri: path,
160       loadUsingSystemPrincipal: true,
161     });
162   }
163   chan.QueryInterface(Ci.nsIHttpChannel);
164   chan.requestMethod = "GET";
165   if (topWindowURI) {
166     chan
167       .QueryInterface(Ci.nsIHttpChannelInternal)
168       .setTopWindowURIIfUnknown(topWindowURI);
169   }
170   return chan;
173 var tests = [
174   // Create the HTTP server.
175   setup_test,
177   // Add the test table into tracking protection table.
178   function addTestTrackers() {
179     if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
180       UrlClassifierTestUtils.addTestTrackers().then(() => {
181         runTests();
182       });
183     } else {
184       runTests();
185     }
186   },
188   // Annotations OFF, normal loading principal, topWinURI of example.com
189   // => trackers should not be de-prioritized
190   function setupAnnotationsOff() {
191     if (skipNormalPriority) {
192       runTests();
193       return;
194     }
195     if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
196       Services.prefs.setBoolPref(
197         "privacy.trackingprotection.annotate_channels",
198         false
199       );
200       Services.prefs.setBoolPref(
201         "privacy.trackingprotection.lower_network_priority",
202         false
203       );
204     }
205     var principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
206       normalOrigin
207     );
208     testPriorityMap = [
209       {
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
216       },
217       {
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
224       },
225       {
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
232       },
233       {
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
240       },
241     ];
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);
245     runTests();
246   },
248   // Annotations ON, normal loading principal, topWinURI of example.com
249   // => trackers should be de-prioritized
250   function setupAnnotationsOn() {
251     if (skipLowestPriority) {
252       runTests();
253       return;
254     }
255     if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
256       Services.prefs.setBoolPref(
257         "privacy.trackingprotection.annotate_channels",
258         true
259       );
260       Services.prefs.setBoolPref(
261         "privacy.trackingprotection.lower_network_priority",
262         true
263       );
264     }
265     var principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
266       normalOrigin
267     );
268     testPriorityMap = [
269       {
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
276       },
277       {
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
284       },
285       {
286         path: trackingOrigin + "/evil.css",
287         loadingPrincipal: principal,
288         topWindowURI: defaultTopWindowURI,
289         expectedTracking: true,
290         expectedPriority: Ci.nsISupportsPriority.PRIORITY_LOWEST,
291         expectedThrottleable: true,
292       },
293       {
294         path: trackingOrigin + "/evil.js",
295         loadingPrincipal: principal,
296         topWindowURI: defaultTopWindowURI,
297         expectedTracking: true,
298         expectedPriority: Ci.nsISupportsPriority.PRIORITY_LOWEST,
299         expectedThrottleable: true,
300       },
301     ];
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);
305     runTests();
306   },
308   // Annotations ON, system loading principal, topWinURI of example.com
309   // => trackers should not be de-prioritized
310   function setupAnnotationsOnSystemPrincipal() {
311     if (skipLowestPriority) {
312       runTests();
313       return;
314     }
315     if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
316       Services.prefs.setBoolPref(
317         "privacy.trackingprotection.annotate_channels",
318         true
319       );
320       Services.prefs.setBoolPref(
321         "privacy.trackingprotection.lower_network_priority",
322         true
323       );
324     }
325     testPriorityMap = [
326       {
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
333       },
334       {
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
341       },
342       {
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,
349       },
350       {
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,
357       },
358     ];
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);
362     runTests();
363   },
365   function cleanUp() {
366     httpServer.stop(do_test_finished);
367     if (runtime.processType == runtime.PROCESS_TYPE_DEFAULT) {
368       UrlClassifierTestUtils.cleanupTestTrackers();
369     }
370     runTests();
371   },
374 function runTests() {
375   if (!tests.length) {
376     do_test_finished();
377     return;
378   }
380   var test = tests.shift();
381   test();
384 function run_test() {
385   runTests();
386   do_test_pending();