1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
<!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(
"resource://testing-common/UrlClassifierTestUtils.sys.mjs"
);
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]],
});
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["webRequest", "webRequestBlocking", "proxy", "<all_urls>"],
},
background() {
let expected = {
"http://tracking.example.org/": {first: "tracking", thirdParty: false, },
"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, },
};
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;
}
browser.proxy.onRequest.addListener(details => {
browser.test.log(`proxy.onRequest ${JSON.stringify(details)}`);
testRequest(details);
}, {urls: ["http://mochi.test/tests/*", "http://tracking.example.org/*", "http://example.net/*"]});
browser.webRequest.onBeforeRequest.addListener(async (details) => {
browser.test.log(`webRequest.onBeforeRequest ${JSON.stringify(details)}`);
testRequest(details);
}, {urls: ["http://mochi.test/tests/*", "http://tracking.example.org/*", "http://example.net/*"]}, ["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: ["http://mochi.test/tests/*", "http://tracking.example.org/*", "http://example.net/*"]});
},
});
await extension.startup();
// Test first party tracking classification.
let url = "http://tracking.example.org/";
let win = window.open(url);
is(await extension.awaitMessage("classification"), url, "request completed");
win.close();
// Test third party tracking classification, expecting two results.
url = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=tracking.example.org";
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.
url = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=example.net";
win = window.open(url);
is(await extension.awaitMessage("classification"), url);
is(await extension.awaitMessage("classification"),
"http://example.net/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png",
"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(
"resource://testing-common/UrlClassifierTestUtils.sys.mjs"
);
await UrlClassifierTestUtils.cleanupTestTrackers();
sendAsyncMessage("trackersUnloaded");
});
await chromeScript.promiseOneMessage("trackersUnloaded");
chromeScript.destroy();
});
</script>
</body>
</html>
|