Bug 1493563 - Part 11: Add tests for the new onSecurityChange progresslistener arguments; r=baku

Differential Revision: https://phabricator.services.mozilla.com/D6601
This commit is contained in:
Ehsan Akhgari 2018-09-23 21:31:59 -04:00
Родитель feb5378a02
Коммит ceebd8b075
2 изменённых файлов: 180 добавлений и 9 удалений

Просмотреть файл

@ -17,7 +17,7 @@ async function onModifyRequest() {
let httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
let spec = httpChannel.URI.spec;
info("Observed channel for " + spec);
if (httpChannel.URI.prePath + "/" != TEST_3RD_PARTY_DOMAIN_TP) {
if (httpChannel.URI.prePath != TEST_3RD_PARTY_DOMAIN_TP) {
return;
}
if (spec.endsWith("empty.js")) {

Просмотреть файл

@ -1,8 +1,8 @@
const TEST_DOMAIN = "http://example.net/";
const TEST_3RD_PARTY_DOMAIN = "https://tracking.example.org/";
const TEST_3RD_PARTY_DOMAIN_TP = "https://tracking.example.com/";
const TEST_DOMAIN = "http://example.net";
const TEST_3RD_PARTY_DOMAIN = "https://tracking.example.org";
const TEST_3RD_PARTY_DOMAIN_TP = "https://tracking.example.com";
const TEST_PATH = "browser/toolkit/components/antitracking/test/browser/";
const TEST_PATH = "/browser/toolkit/components/antitracking/test/browser/";
const TEST_TOP_PAGE = TEST_DOMAIN + TEST_PATH + "page.html";
const TEST_EMBEDDER_PAGE = TEST_DOMAIN + TEST_PATH + "embedder.html";
@ -20,7 +20,7 @@ var gFeatures = undefined;
let {UrlClassifierTestUtils} = ChromeUtils.import("resource://testing-common/UrlClassifierTestUtils.jsm", {});
requestLongerTimeout(3);
requestLongerTimeout(5);
this.AntiTracking = {
runTest(name, callbackTracking, callbackNonTracking, cleanupFunction, extraPrefs,
@ -541,7 +541,8 @@ this.AntiTracking = {
// get blocked with user interaction present
if (userInteractionTest) {
this._createUserInteractionTask(name, callbackTracking, callbackNonTracking,
runInPrivateWindow, iframeSandbox, extraPrefs);
runInPrivateWindow, iframeSandbox,
expectedBlockingNotifications, extraPrefs);
this._createCleanupTask(cleanupFunction);
}
}
@ -596,10 +597,65 @@ this.AntiTracking = {
let cookieBlocked = 0;
let listener = {
onSecurityChange(webProgress, request, oldState, state,
status, contentBlockingLogJSON) {
contentBlockingLogJSON) {
if (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) {
++cookieBlocked;
}
let contentBlockingLog = {};
try {
contentBlockingLog = JSON.parse(contentBlockingLogJSON);
} catch (e) {
}
// If this is the first cookie to be blocked, our state should have
// just changed, otherwise it should have previously contained the
// STATE_COOKIES_BLOCKED_TRACKER bit too.
if (options.expectedBlockingNotifications && cookieBlocked &&
!options.allowList) {
if (cookieBlocked == 1) {
is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
"When blocking the first cookie, old state should not have had the " +
"STATE_COOKIES_BLOCKED_TRACKER bit");
}
let trackerOriginCount = 0;
for (let trackerOrigin in contentBlockingLog) {
++trackerOriginCount;
is(trackerOrigin, TEST_3RD_PARTY_DOMAIN, "Correct tracker origin must be reported");
let originLog = contentBlockingLog[trackerOrigin];
if (originLog.length == 1) {
is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[0][1], true,
"Correct blocking status reported");
ok(originLog[0][2] >= 1,
"Correct repeat count reported");
} else {
// This branch is needed here because of the tests that use the storage
// access API to gain storage access.
is(originLog.length, 2, "Correct origin log length");
is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[0][1], true,
"Correct blocking status reported");
ok(originLog[0][2] >= 1,
"Correct repeat count reported");
is(originLog[1][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[1][1], false,
"Correct blocking status reported");
is(originLog[1][2], 1,
"Correct repeat count reported");
}
}
is(trackerOriginCount, 1, "Should only report one tracker origin");
}
if (!options.expectedBlockingNotifications) {
is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
"When not blocking, old state should not have had the " +
"STATE_COOKIES_BLOCKED_TRACKER bit");
is(Object.keys(contentBlockingLog).length, 0,
"Content blocking log JSON must be empty");
}
},
};
win.gBrowser.addProgressListener(listener);
@ -703,6 +759,61 @@ this.AntiTracking = {
await AntiTracking._setupTest(win, BEHAVIOR_REJECT_TRACKER, true, true, true, extraPrefs);
let cookieBlocked = 0;
let listener = {
onSecurityChange(webProgress, request, oldState, state,
contentBlockingLogJSON) {
if (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) {
++cookieBlocked;
}
let contentBlockingLog = {};
try {
contentBlockingLog = JSON.parse(contentBlockingLogJSON);
} catch (e) {
}
// If this is the first cookie to be blocked, our state should have
// just changed, otherwise it should have previously contained the
// STATE_COOKIES_BLOCKED_TRACKER bit too.
if (cookieBlocked) {
if (cookieBlocked == 1) {
is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
"When blocking the first cookie, old state should not have had the " +
"STATE_COOKIES_BLOCKED_TRACKER bit");
}
for (let trackerOrigin in contentBlockingLog) {
is(trackerOrigin, TEST_3RD_PARTY_DOMAIN, "Correct tracker origin must be reported");
let originLog = contentBlockingLog[trackerOrigin];
if (originLog.length == 1) {
is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[0][1], true,
"Correct blocking status reported");
ok(originLog[0][2] >= 1,
"Correct repeat count reported");
} else {
is(originLog.length, 2, "Correct origin log length");
is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[0][1], true,
"Correct blocking status reported");
ok(originLog[0][2] >= 1,
"Correct repeat count reported");
is(originLog[1][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[1][1], false,
"Correct blocking status reported");
is(originLog[1][2], 1,
"Correct repeat count reported");
}
}
// Can't assert the number of tracker origins because we may get 0
// for web progress navigations coming from the window opening...
}
},
};
win.gBrowser.addProgressListener(listener);
info("Creating a new tab");
let tab = BrowserTestUtils.addTab(win.gBrowser, TEST_TOP_PAGE);
win.gBrowser.selectedTab = tab;
@ -758,6 +869,8 @@ this.AntiTracking = {
});
});
win.gBrowser.removeProgressListener(listener);
info("Removing the tab");
BrowserTestUtils.removeTab(tab);
@ -768,7 +881,8 @@ this.AntiTracking = {
},
_createUserInteractionTask(name, blockingCallback, nonBlockingCallback,
runInPrivateWindow, iframeSandbox, extraPrefs) {
runInPrivateWindow, iframeSandbox,
expectedBlockingNotifications, extraPrefs) {
add_task(async function() {
info("Starting user-interaction test " + name);
@ -780,6 +894,61 @@ this.AntiTracking = {
await AntiTracking._setupTest(win, BEHAVIOR_REJECT_TRACKER, true, true, true, extraPrefs);
let cookieBlocked = 0;
let listener = {
onSecurityChange(webProgress, request, oldState, state,
contentBlockingLogJSON) {
if (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) {
++cookieBlocked;
}
let contentBlockingLog = {};
try {
contentBlockingLog = JSON.parse(contentBlockingLogJSON);
} catch (e) {
}
// If this is the first cookie to be blocked, our state should have
// just changed, otherwise it should have previously contained the
// STATE_COOKIES_BLOCKED_TRACKER bit too.
if (expectedBlockingNotifications && cookieBlocked) {
if (cookieBlocked == 1) {
is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
"When blocking the first cookie, old state should not have had the " +
"STATE_COOKIES_BLOCKED_TRACKER bit");
}
for (let trackerOrigin in contentBlockingLog) {
is(trackerOrigin, TEST_3RD_PARTY_DOMAIN, "Correct tracker origin must be reported");
let originLog = contentBlockingLog[trackerOrigin];
if (originLog.length == 1) {
is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[0][1], true,
"Correct blocking status reported");
ok(originLog[0][2] >= 1,
"Correct repeat count reported");
} else {
is(originLog.length, 2, "Correct origin log length");
is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[0][1], true,
"Correct blocking status reported");
ok(originLog[0][2] >= 1,
"Correct repeat count reported");
is(originLog[1][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
"Correct blocking type reported");
is(originLog[1][1], false,
"Correct blocking status reported");
ok(originLog[1][2] >= 1,
"Correct repeat count reported");
}
}
// Can't assert the number of tracker origins because we may get 0
// for web progress navigations coming from the window opening...
}
},
};
win.gBrowser.addProgressListener(listener);
info("Creating a new tab");
let tab = BrowserTestUtils.addTab(win.gBrowser, TEST_TOP_PAGE);
win.gBrowser.selectedTab = tab;
@ -869,6 +1038,8 @@ this.AntiTracking = {
});
});
win.gBrowser.removeProgressListener(listener);
info("Removing the tab");
BrowserTestUtils.removeTab(tab);