fune/toolkit/components/antitracking/test/browser/browser_imageCache.js
Francois Marier e98e918836 Bug 1461515 - Fix and expand tracking annotation test. r=dimi
Here's a summary of things that were wrong about this test:

1. It was setting urlclassifier.trackingTable only to be overwritten
   later by addTestTrackers().
2. It was using an http event which fires before the classification has
   been done.
3. It didn't disable tailing, which interferes with lowering the priority of
   XHRs.
4. It was not testing that non-annotated or whitelisted resources would not
   have their priority lowered.

I added more test cases both to ensure that the correct list
(urlclassifier.trackingAnnotationTable) is used but also to ensure that
whitelisted or non-blacklisted URLs preserve the normal priority (point #4 above).

I found that XHRs do not get their priority lowered because of this flag:

  https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/netwerk/base/nsChannelClassifier.cpp#221

which gets set here:

  https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/dom/xhr/XMLHttpRequestMainThread.cpp#2548

and so I had to disable tailing in the test (point #3 above).

There was also a problem where the test was resetting the prefs too early
because we were not actually waiting for the classification to finish.

We would wait for the following event: http-on-opening-request

  https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/netwerk/protocol/http/nsIHttpProtocolHandler.idl#85

whereas maybe a more appropriate one would be http-on-before-connect:

  https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/netwerk/protocol/http/nsIHttpProtocolHandler.idl#103

since that is triggerred after annotations (point #2 above):

  https://searchfox.org/mozilla-central/rev/d47c829065767b6f36d29303d650bffb7c4f94a3/netwerk/protocol/http/nsHttpChannel.cpp#6614

Differential Revision: https://phabricator.services.mozilla.com/D2485

--HG--
extra : moz-landing-system : lando
2018-08-01 11:52:03 +00:00

52 lines
1.9 KiB
JavaScript

ChromeUtils.import("resource://gre/modules/Services.jsm");
AntiTracking.runTest("Image cache - should load the image twice.",
// blocking callback
async _ => {
// Let's load the image twice here.
let img = document.createElement("img");
document.body.appendChild(img);
img.src = "https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/image.sjs",
await new Promise(resolve => { img.onload = resolve; });
ok(true, "Image 1 loaded");
img = document.createElement("img");
document.body.appendChild(img);
img.src = "https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/image.sjs",
await new Promise(resolve => { img.onload = resolve; });
ok(true, "Image 2 loaded");
},
// non-blocking callback
async _ => {
// Let's load the image twice here as well.
let img = document.createElement("img");
document.body.appendChild(img);
img.src = "https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/image.sjs",
await new Promise(resolve => { img.onload = resolve; });
ok(true, "Image 3 loaded");
img = document.createElement("img");
document.body.appendChild(img);
img.src = "https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/image.sjs",
await new Promise(resolve => { img.onload = resolve; });
ok(true, "Image 4 loaded");
},
null, // cleanup function
null, // no extra prefs
false, // no window open test
false // no user-interaction test
);
// We still want to see just 2 requests.
add_task(async _ => {
await fetch("https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/image.sjs?result")
.then(r => r.text())
.then(text => {
is(text, 2, "The image should be loaded correctly.");
});
await new Promise(resolve => {
Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
});
});