Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: os == 'android' OR http3 OR http2
- Manifest: toolkit/components/extensions/test/mochitest/mochitest-remote.toml includes toolkit/components/extensions/test/mochitest/mochitest-common.toml
- Manifest: toolkit/components/extensions/test/mochitest/mochitest.toml includes toolkit/components/extensions/test/mochitest/mochitest-common.toml
<!DOCTYPE HTML>
<html>
<head>
<title>Test for WebRequest urlClassification</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(async function setup() {
await SpecialPowers.pushPrefEnv({
set: [["privacy.trackingprotection.enabled", true]],
});
let chromeScript = SpecialPowers.loadChromeScript(async _ => {
/* eslint-env mozilla/chrome-script */
const {UrlClassifierTestUtils} = ChromeUtils.importESModule(
);
await UrlClassifierTestUtils.addTestTrackers();
sendAsyncMessage("trackersLoaded");
});
await chromeScript.promiseOneMessage("trackersLoaded");
chromeScript.destroy();
});
add_task(async function test_urlClassification() {
await SpecialPowers.pushPrefEnv({
set: [["dom.security.https_first", false]],
});
// All URLs below should be distinct from the ones in |expected| below,
// especially the final png URL. If the PNG URL is identical, the image may
// be read from the image cache, instead of through a new request.
const PATH_REDIRECT_TO = "/tests/toolkit/components/extensions/test/mochitest/redirect_to.sjs";
// Cross-origin redirect target. The path of this URL is also in file_third_party.html:
const REDIRECT_TARGET = "http://another-tracking.example.net/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png";
const REDIRECT_TEST = {
TOP_URL: `http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=mochi.test:8888${PATH_REDIRECT_TO}?http://another-tracking.example.net`,
// file_third_party.html embeds http://mochi.test:8888<PATH_REDIRECT_TO>?<REDIRECT_TARGET>, which redirects to REDIRECT_TARGET.
REDIRECT_TARGET,
};
let extension = ExtensionTestUtils.loadExtension({
manifest: {
description: JSON.stringify({ REDIRECT_TEST }),
permissions: ["webRequest", "webRequestBlocking", "proxy", "<all_urls>"],
},
background() {
const { REDIRECT_TEST } = JSON.parse(browser.runtime.getManifest().description);
let expected = {
"http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=tracking.example.org": { thirdParty: false, },
"http://tracking.example.org/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png": {third: "tracking", thirdParty: true, },
"http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=example.net": { thirdParty: false, },
"http://example.net/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png": { thirdParty: true, },
[REDIRECT_TEST.TOP_URL]: { thirdParty: false, },
[REDIRECT_TEST.EMBEDDED_PRE_REDIRECT_URL]: { thirdParty: false, },
[REDIRECT_TEST.REDIRECT_TARGET]: {third: "tracking", thirdParty: true, },
};
function testRequest(details) {
let expect = expected[details.url];
if (expect) {
if (expect.first) {
browser.test.assertTrue(details.urlClassification.firstParty.includes("tracking"), "tracking firstParty");
} else {
browser.test.assertEq(details.urlClassification.firstParty.length, 0, "not tracking firstParty");
}
if (expect.third) {
browser.test.assertTrue(details.urlClassification.thirdParty.includes("tracking"), "tracking thirdParty");
} else {
browser.test.assertEq(details.urlClassification.thirdParty.length, 0, "not tracking thirdParty");
}
browser.test.assertEq(details.thirdParty, expect.thirdParty, "3rd party flag matches");
return true;
}
return false;
}
const urls = [
];
browser.proxy.onRequest.addListener(details => {
browser.test.log(`proxy.onRequest ${JSON.stringify(details)}`);
testRequest(details);
}, {urls});
browser.webRequest.onBeforeRequest.addListener(async (details) => {
browser.test.log(`webRequest.onBeforeRequest ${JSON.stringify(details)}`);
testRequest(details);
}, {urls}, ["blocking"]);
browser.webRequest.onCompleted.addListener(async (details) => {
browser.test.log(`webRequest.onCompleted ${JSON.stringify(details)}`);
if (testRequest(details)) {
browser.test.sendMessage("classification", details.url);
}
}, {urls});
},
});
await extension.startup();
// Test first party tracking classification.
let win = window.open(url);
is(await extension.awaitMessage("classification"), url, "request completed");
win.close();
// Test third party tracking classification, expecting two results.
win = window.open(url);
is(await extension.awaitMessage("classification"), url);
is(await extension.awaitMessage("classification"),
"http://tracking.example.org/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png",
"request completed");
win.close();
// Test third party tracking classification, expecting two results.
win = window.open(url);
is(await extension.awaitMessage("classification"), url);
is(await extension.awaitMessage("classification"),
"request completed");
win.close();
// Test third party tracking classification for redirected requests.
win = window.open(REDIRECT_TEST.TOP_URL);
is(await extension.awaitMessage("classification"), REDIRECT_TEST.TOP_URL);
// Note: REDIRECT_TEST.EMBEDDED_PRE_REDIRECT_URL is not observed because
// onCompleted is not seen for that URL due to a redirect to REDIRECT_TARGET.
is(await extension.awaitMessage("classification"),
REDIRECT_TEST.REDIRECT_TARGET,
"request completed");
win.close();
await extension.unload();
});
add_task(async function teardown() {
let chromeScript = SpecialPowers.loadChromeScript(async _ => {
/* eslint-env mozilla/chrome-script */
// Cleanup cache
await new Promise(resolve => {
Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () => resolve());
});
const {UrlClassifierTestUtils} = ChromeUtils.importESModule(
);
await UrlClassifierTestUtils.cleanupTestTrackers();
sendAsyncMessage("trackersUnloaded");
});
await chromeScript.promiseOneMessage("trackersUnloaded");
chromeScript.destroy();
});
</script>
</body>
</html>