Bug 1477046 - Part 3: Add FastBlock redirect tests; r=mayhemer

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Liang-Heng Chen 2018-09-17 14:57:52 +00:00
parent 4168f0d48c
commit b5b7a94343
4 changed files with 67 additions and 16 deletions

View File

@ -13,6 +13,7 @@ support-files =
head.js
threathit.sjs
fastblock.html
redirect_tracker.sjs
fastblock_iframe.html
!/toolkit/components/url-classifier/tests/mochitest/classifierFrame.html
!/toolkit/components/url-classifier/tests/mochitest/cleanWorker.js

View File

@ -7,6 +7,9 @@
<!-- Tracking iframe contains some trackers -->
<iframe id="fastIFrame" data-touched="not sure" src="http://tracking.example.org/chrome/toolkit/components/url-classifier/tests/mochitest/fastblock_iframe.html" onload="this.dataset.touched='yes';" onerror="this.dataset.touched='no';"></iframe>
<!-- A fast tracker that redirects to become a slow tracker -->
<script id="redirectScript"src="http://example.com/chrome/toolkit/components/url-classifier/tests/mochitest/redirect_tracker.sjs" onload="this.dataset.touched='yes';" onerror="this.dataset.touched='no';"></script>
<!-- Tracking URL -->
<script id="goodScript" data-touched="not sure" src="http://tracking.example.com/tests/toolkit/components/url-classifier/tests/mochitest/good.js" onload="this.dataset.touched='yes';" onerror="this.dataset.touched='no';"></script>

View File

@ -0,0 +1,7 @@
const gURL =
"http://trackertest.org/tests/toolkit/components/url-classifier/tests/mochitest/evil.js";
function handleRequest(request, response) {
response.setStatusLine("1.1", 302, "found");
response.setHeader("Location", gURL, false);
}

View File

@ -11,6 +11,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1477046
| goodScript | No | N/A |
| fastScript | Yes | No |
| slowScript | Yes | Yes |
| redirectScript | Yes | Yes |
-------------------+---------+------------
| fastIFrame | Yes | No |
-------------------+---------+------------
@ -62,10 +63,19 @@ const gGoodIFramePage =
"http://example.com/chrome/toolkit/components/url-classifier/tests/mochitest/fastblock_iframe.html";
const gBadIFramePage =
"http://itisatracker.org/chrome/toolkit/components/url-classifier/tests/mochitest/fastblock_iframe.html";
const gValidHosts = [
"example.com",
"itisatracker.org",
"trackertest.org",
"tracking.example.com",
"tracking.example.org",
];
const gSlowTrackers = [
"http://tracking.example.org/tests/toolkit/components/url-classifier/tests/mochitest/trackingRequest.js",
"http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackingRequest.js",
];
const gRedirectTracker =
"http://example.com/chrome/toolkit/components/url-classifier/tests/mochitest/redirect_tracker.sjs";
const gInfiniteTimeout = 300000;
const gDebug = false;
@ -84,21 +94,24 @@ function awaitBrowserLoaded(browser) {
}).then(() => {
return ContentTaskUtils.waitForCondition(() => {
return content.document.readyState === "complete";
}, "page load complete");
}, "page load complete", 100, 600); // wait for ~60s
});
}
return Promise.resolve();
});
}
function testOnWindow(aCallback) {
function testOnWindow(resolve, reject) {
const mainWindow = window.docShell.rootTreeItem.domWindow;
let win = mainWindow.OpenBrowserWindow({remote: true});
whenDelayedStartupFinished(win, function() {
SimpleTest.executeSoon(function() { win.gBrowser.loadURI(gContentPage); });
awaitBrowserLoaded(win.gBrowser.selectedBrowser).then(() => {
aCallback(win);
resolve(win);
}).catch((reason) => {
ok(false, reason);
reject(reason);
});
});
}
@ -135,12 +148,14 @@ function SlowTrackerDetector(aNumNonSlowTargets) {
this.suspendedRequests = [];
this._onBC = this.onBeforeConnect.bind(this);
this._onSR = this.onStopRequest.bind(this);
this._onER = this.onExamineResponse.bind(this);
}
SlowTrackerDetector.prototype = {
async init() {
SpecialPowers.addObserver(this._onBC, "http-on-before-connect");
SpecialPowers.addObserver(this._onSR, "http-on-stop-request");
SpecialPowers.addObserver(this._onER, "http-on-examine-response");
await SpecialPowers.pushPrefEnv({
"set": [
@ -151,6 +166,7 @@ SlowTrackerDetector.prototype = {
async close() {
ok(!this._onBC, "observer removed");
ok(!this._onSR, "observer removed");
ok(!this._onER, "observer removed");
is(this.suspendedRequests.length, 0, "no suspended request");
await SpecialPowers.popPrefEnv();
},
@ -167,7 +183,8 @@ SlowTrackerDetector.prototype = {
onStopRequest(aSubject) {
let channel = aSubject.QueryInterface(Ci.nsIChannel);
log("onStopRequest: " + channel.name);
if (!gSlowTrackers.includes(channel.name)) {
if (gValidHosts.includes(channel.URI.host) &&
!gSlowTrackers.includes(channel.name)) {
--this.numTargets;
if (this.numTargets == 0) {
this.onAllNonSlowTargetsLoaded();
@ -175,18 +192,35 @@ SlowTrackerDetector.prototype = {
}
},
onExamineResponse(aSubject) {
let channel = aSubject.QueryInterface(Ci.nsIChannel);
log("onExamineResponse: " + channel.name);
if (gRedirectTracker == channel.name &&
this.numTargets > 0) {
this.onRedirectTrackerFound(channel);
}
},
onSlowTrackerFound(aChannel) {
log("onSlowTrackerFound: " + aChannel.name);
aChannel.suspend();
this.suspendedRequests.push(aChannel);
},
onRedirectTrackerFound(aChannel) {
log("onRedirectTrackerFound: " + aChannel.name);
let request = aChannel.QueryInterface(Ci.nsIRequest);
request.suspend();
this.suspendedRequests.push(request);
},
onAllNonSlowTargetsLoaded() {
is(this.numTargets, 0, "resources are loaded");
SpecialPowers.removeObserver(this._onBC, "http-on-before-connect");
SpecialPowers.removeObserver(this._onSR, "http-on-stop-request");
this._onBC = this._onSR = null;
SpecialPowers.removeObserver(this._onER, "http-on-examine-response");
this._onBC = this._onSR = this._onER = null;
log("=========== TIMEOUT ===========");
// FastBlock from now on
@ -240,7 +274,7 @@ async function runTest(aPrefs, aFunction, aNumNonSlowTrackers) {
await detector.init();
}
await new Promise(resolve => {
await new Promise((resolve, reject) => {
let monitor = new ChannelUnifier();
monitor.init();
@ -258,7 +292,7 @@ async function runTest(aPrefs, aFunction, aNumNonSlowTrackers) {
await SpecialPowers.popPrefEnv();
}
resolve();
});
}, reject);
});
}
@ -319,6 +353,8 @@ async function addIFrame(aWindow, aUrl, aId) {
}
async function testFastBlock(aWindow) {
log("testFastBlock");
let browser = aWindow.gBrowser.selectedBrowser;
let results = await ContentTask.spawn(browser, {}, () => {
let iframe = content.document.getElementById("fastIFrame").contentDocument;
@ -330,6 +366,7 @@ async function testFastBlock(aWindow) {
goodIFrameScript: iframe.getElementById("goodIFrameScript").dataset.touched,
fastIFrameScript: iframe.getElementById("fastIFrameScript").dataset.touched,
slowIFrameScript: iframe.getElementById("slowIFrameScript").dataset.touched,
redirectScript: content.document.getElementById("redirectScript").dataset.touched,
numTrackersFound: content.document.numTrackersFound,
numTrackersBlocked: content.document.numTrackersBlocked,
};
@ -342,6 +379,7 @@ async function testFastBlock(aWindow) {
goodIFrameScript,
fastIFrameScript,
slowIFrameScript,
redirectScript,
numTrackersFound,
numTrackersBlocked,
} = results;
@ -353,8 +391,9 @@ async function testFastBlock(aWindow) {
is(goodIFrameScript, "yes", "is not a tracker");
is(fastIFrameScript, "yes", "is a fast tracker");
is(slowIFrameScript, "no", "is a blocked tracker");
is(numTrackersFound, 5, "5 trackers found");
is(numTrackersBlocked, 2, "2 tracker blocked");
is(redirectScript, "no", "redirect to blocked tracker");
is(numTrackersFound, 6, "6 trackers found");
is(numTrackersBlocked, 3, "3 tracker blocked");
let badIFrameLoaded = await addIFrame(aWindow, gBadIFramePage, "badIFrame");
ok(!badIFrameLoaded, "tracking iframe is blocked");
@ -376,8 +415,8 @@ async function testFastBlock(aWindow) {
ok(!results.fastIFrameScript, "iframe is not loaded");
ok(!results.slowIFrameScript, "iframe is not loaded");
is(results.numTrackersFound, 6, "6 trackers found");
is(results.numTrackersBlocked, 3, "3 tracker blocked");
is(results.numTrackersFound, 7, "7 trackers found");
is(results.numTrackersBlocked, 4, "4 tracker blocked");
let goodIFrameLoaded = await addIFrame(aWindow, gGoodIFramePage, "goodIFrame");
ok(goodIFrameLoaded, "non tracking iframe is loaded");
@ -399,11 +438,12 @@ async function testFastBlock(aWindow) {
is(results.fastIFrameScript, "no", "is a blocked tracker");
is(results.slowIFrameScript, "no", "is a blocked tracker");
is(results.numTrackersFound, 8, "8 trackers found");
is(results.numTrackersBlocked, 5, "5 tracker blocked");
is(results.numTrackersFound, 9, "9 trackers found");
is(results.numTrackersBlocked, 6, "6 tracker blocked");
}
async function testNoFastBlock(aWindow) {
log("testNoFastBlock");
let browser = aWindow.gBrowser.selectedBrowser;
let results = await ContentTask.spawn(browser, {}, () => {
let iframe = content.document.getElementById("fastIFrame").contentDocument;
@ -438,7 +478,7 @@ async function testNoFastBlock(aWindow) {
is(goodIFrameScript, "yes", "is not a tracker");
is(fastIFrameScript, "yes", "FastBlock is disabled");
is(slowIFrameScript, "yes", "FastBlock is disabled");
is(numTrackersFound, 5, "5 trackers found");
is(numTrackersFound, 6, "6 trackers found");
is(numTrackersBlocked, 0, "no tracker blocked");
let iframeLoaded = await addIFrame(aWindow, gBadIFramePage, "badIFrame");
@ -461,7 +501,7 @@ async function testNoFastBlock(aWindow) {
is(results.fastIFrameScript, "yes", "FastBlock is disabled");
is(results.slowIFrameScript, "yes", "FastBlock is disabled");
is(results.numTrackersFound, 8, "8 trackers found");
is(results.numTrackersFound, 9, "9 trackers found");
is(results.numTrackersBlocked, 0, "0 tracker blocked");
let goodIFrameLoaded = await addIFrame(aWindow, gGoodIFramePage, "goodIFrame");
@ -484,7 +524,7 @@ async function testNoFastBlock(aWindow) {
is(results.fastIFrameScript, "yes", "FastBlock is disabled");
is(results.slowIFrameScript, "yes", "FastBlock is disabled");
is(results.numTrackersFound, 10, "10 trackers found");
is(results.numTrackersFound, 11, "11 trackers found");
is(results.numTrackersBlocked, 0, "0 tracker blocked");
}