Bug 1522412 - P7. A mochitest to test different channel open call sites are classified. r=Ehsan

The goal of this testcase is to provide an easier way to add callsites
to test if it is correctly classified. This is a first step, more callsites should be added to
the testcase(See Bug 1532691)

Flow of the test case:
1. setup the server(trackerFrame.sjs) with the expceted number of request it should receive
2. load the test frame(trackerFrame.html) with cookie restriction off, to ensure all the tracking requests contain cookies
3. server responses a list of tracker's request with cookie after reciving all the requests
4. the list should contain all the trackers in the test frame.
5. enable cookie restriction and load the test frame again.
6. server responses a list of tracker's request without cookie after reciving all the requests
7. the list should contain all the trackers in the test frame.

Differential Revision: https://phabricator.services.mozilla.com/D22116
This commit is contained in:
Dimi Lee
2019-03-25 12:51:58 +00:00
parent c830b2c5d0
commit 606b46098e
4 changed files with 250 additions and 0 deletions

View File

@@ -14,6 +14,8 @@ support-files =
import.css
raptor.jpg
track.html
trackerFrame.html
trackerFrame.sjs
trackingRequest.js
trackingRequest.js^headers^
unwantedWorker.js
@@ -38,6 +40,8 @@ support-files =
skip-if = (os == 'linux' && debug) #Bug 1199778
[test_classifier_match.html]
[test_classifier_worker.html]
[test_classify_by_default.html]
skip-if = (os == 'android') #Bug 1538662
[test_classify_ping.html]
skip-if = (verify && debug && (os == 'win' || os == 'mac'))
[test_classify_track.html]

View File

@@ -0,0 +1,98 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for Bug 1442496</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1442496">Mozilla Bug 1442496</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<iframe id="testFrame1"></iframe>
<iframe id="testFrame2"></iframe>
<script class="testbody" type="text/javascript">
// To add a request to test, add the request in trackerFrame.html
// and the id of query string "?id=xxx" here.
const trackersAll = [
"img-src",
"object-data",
"script-src",
"iframe-src",
"link-rel-stylesheet",
"link-rel-prefetch",
"video-src",
"track-src",
"ping",
"fetch",
"xmlhttprequest",
"send-beacon",
];
const trackerFrame = "http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs";
// This function ask the server to set the cookie
function setup() {
return fetch(trackerFrame + "?init=" + trackersAll.length, {
credentials: "include",
});
}
function loadTestFrame(withcookie, frameId) {
return new Promise(resolve => {
let query = withcookie ? "with-cookie" : "without-cookie";
fetch(trackerFrame + "?callback=" + query, {
credentials: "include",
}).then(r => {
r.text().then((body) => {
let trackers_found = body.split(",");
for (let tracker of trackersAll) {
let description = "Tracker request " + tracker + "received " +
(withcookie ? "with" : "without") + " cookie";
ok(trackers_found.includes(tracker), description);
}
resolve();
});
});
let frame = document.getElementById(frameId);
frame.src = "trackerFrame.html";
});
}
async function runTests() {
await SpecialPowers.pushPrefEnv({set: [
[ "urlclassifier.trackingAnnotationTable.testEntries", "itisatracker.org" ],
[ "network.cookie.cookieBehavior", 4],
[ "privacy.trackingprotection.enabled", false ],
[ "privacy.trackingprotection.annotate_channels", false],
[ "browser.send_pings", true],
]});
await setup();
await loadTestFrame(true, "testFrame1");
await SpecialPowers.pushPrefEnv({set: [
[ "privacy.trackingprotection.annotate_channels", true],
]});
await loadTestFrame(false, "testFrame2");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
runTests();
</script>
</pre>
</body>
</html>

View File

@@ -0,0 +1,72 @@
<!DOCTYPE HTML>
<!-- Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ -->
<html>
<head>
<title></title>
</head>
<body>
<!--NewImageChannel-->
<img src="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=img-src">
<!--nsObjectLoadingContent::OpenChannel-->
<object data="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=object-data"></object>
<!--ScriptLoader::StartLoad-->
<script src="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=script-src"></script>
<!--nsDocShell::DoURILoad-->
<iframe src="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=iframe-src"></iframe>
<!--Loader::LoadSheet-->
<link rel="stylesheet" href="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=link-rel-stylesheet" />
<!--nsPrefetchNode::OpenChannel-->
<link rel="prefetch" href="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=link-rel-prefetch" />
<!--HTMLMediaElement::ChannelLoader::LoadInternal-->
<video src="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=video-src">
</video>
<video src="http://mochi.test:8888/basic.vtt", crossorigin=use-credentials>
<track src="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=track-src" ></track>
</video>
<!--SendPing-->
<a ping="http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=ping" id="a-ping" href="#"></a>
<script>
(function() {
document.getElementById("a-ping").click();
})();
</script>
<script>
// FetchDriver::HttpFetch
(function() {
try {
fetch(new Request("http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=fetch"), {
credentials: "include",
});
} catch (err) {
console.log(err);
}
})();
// XMLHttpRequestMainThread::CreateChannel
(function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=xmlhttprequest");
xhr.withCredentials = true;
xhr.send();
})();
// Navigator::SendBeaconInternal
(function() {
navigator.sendBeacon("http://itisatracker.org/tests/toolkit/components/url-classifier/tests/mochitest/trackerFrame.sjs?id=send-beacon");
})();
</script>
</body>
</html>

View File

@@ -0,0 +1,76 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Components.utils.importGlobalProperties(["URLSearchParams"]);
const stateTotalRequests = "total-request";
const stateCallback = "callback-response";
const stateTrackersWithCookie = "trackers-with-cookie";
const stateTrackersWithoutCookie = "trackers-without-cookie";
const stateReceivedTrackers = "received-trackers";
const stateResponseType = "response-tracker-with-cookie";
function reset()
{
setState(stateCallback, "");
setState(stateTrackersWithCookie, "");
setState(stateTrackersWithoutCookie, "");
setState(stateReceivedTrackers, "");
setState(stateResponseType, "");
}
function handleRequest(aRequest, aResponse)
{
let params = new URLSearchParams(aRequest.queryString);
// init the server and tell the server the total number requests to process
// server set the cookie
if (params.has("init")) {
setState(stateTotalRequests, params.get("init"));
aResponse.setStatusLine(aRequest.httpVersion, 200);
aResponse.setHeader("Content-Type", "text/plain", false);
// Prepare the cookie
aResponse.setHeader("Set-Cookie", "cookie=1234");
aResponse.setHeader("Access-Control-Allow-Origin", aRequest.getHeader("Origin"), false);
aResponse.setHeader("Access-Control-Allow-Credentials","true", false);
aResponse.write("begin-test");
// register the callback response, the response will be fired after receiving
// all the request
} else if (params.has("callback")) {
aResponse.processAsync();
aResponse.setHeader("Content-Type", "text/plain", false);
aResponse.setHeader("Access-Control-Allow-Origin", aRequest.getHeader("Origin"), false);
aResponse.setHeader("Access-Control-Allow-Credentials","true", false);
setState(stateResponseType, params.get("callback"));
setObjectState(stateCallback, aResponse);
} else {
let count = parseInt(getState(stateReceivedTrackers) || 0) + 1;
setState(stateReceivedTrackers, count.toString());
let state = "";
if (aRequest.hasHeader("Cookie")) {
state = stateTrackersWithCookie;
} else {
state = stateTrackersWithoutCookie;
}
let ids = params.get("id").concat(",", getState(state));
setState(state, ids);
if (getState(stateTotalRequests) == getState(stateReceivedTrackers)) {
getObjectState(stateCallback, r => {
if (getState(stateResponseType) == "with-cookie") {
r.write(getState(stateTrackersWithCookie));
} else {
r.write(getState(stateTrackersWithoutCookie));
}
r.finish();
reset();
});
}
}
}