Bug 1216723 - Add tests for the new -forbid- Safe Browsing list type. r=gcp

This commit is contained in:
Francois Marier 2015-11-20 20:25:11 -08:00
parent 67506f65a6
commit f6fd256206
7 changed files with 149 additions and 10 deletions

View File

@ -134,6 +134,10 @@
document.getElementById(error + "_sitename").textContent = getHostString();
document.title = document.getElementById("errorTitleText_" + error)
.innerHTML;
// Inform the test harness that we're done loading the page
var event = new CustomEvent("AboutBlockedLoaded");
document.dispatchEvent(event);
}
]]></script>
<style type="text/css">

View File

@ -1,6 +1,7 @@
[DEFAULT]
support-files = head.js
[browser_forbidden.js]
[browser_bug400731.js]
skip-if = e10s
[browser_bug415846.js]

View File

@ -0,0 +1,40 @@
/* Ensure that pages in the forbidden list are blocked. */
const PREF_FORBIDDEN_ENABLED = "browser.safebrowsing.forbiddenURIs.enabled";
const BENIGN_PAGE = "http://example.com/";
const FORBIDDEN_PAGE = "http://www.itisatrap.org/firefox/forbidden.html";
var tabbrowser = null;
registerCleanupFunction(function() {
tabbrowser = null;
Services.prefs.clearUserPref(PREF_FORBIDDEN_ENABLED);
while (gBrowser.tabs.length > 1) {
gBrowser.removeCurrentTab();
}
});
function testBenignPage(window) {
info("Non-forbidden content must not be blocked");
var getmeout_button = window.document.getElementById("getMeOutButton");
var ignorewarning_button = window.document.getElementById("ignoreWarningButton");
ok(!getmeout_button, "GetMeOut button not present");
ok(!ignorewarning_button, "IgnoreWarning button not present");
}
function testForbiddenPage(window) {
info("Forbidden content must be blocked");
ok(true, "about:blocked was shown");
}
add_task(function* testNormalBrowsing() {
tabbrowser = gBrowser;
let tab = tabbrowser.selectedTab = tabbrowser.addTab();
info("Load a test page that's not forbidden");
yield promiseTabLoadEvent(tab, BENIGN_PAGE, "load");
testBenignPage(tab.ownerDocument.defaultView);
info("Load a test page that is forbidden");
yield promiseTabLoadEvent(tab, FORBIDDEN_PAGE, "AboutBlockedLoaded");
testForbiddenPage(tab.ownerDocument.defaultView);
});

View File

@ -1,5 +1,57 @@
// Force SafeBrowsing to be initialized for the tests
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/Promise.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
/**
* Waits for a load (or custom) event to finish in a given tab. If provided
* load an uri into the tab.
*
* @param tab
* The tab to load into.
* @param [optional] url
* The url to load, or the current url.
* @param [optional] event
* The load event type to wait for. Defaults to "load".
* @return {Promise} resolved when the event is handled.
* @resolves to the received event
* @rejects if a valid load event is not received within a meaningful interval
*/
function promiseTabLoadEvent(tab, url, eventType="load")
{
let deferred = Promise.defer();
info("Wait tab event: " + eventType);
function handle(event) {
if (event.originalTarget != tab.linkedBrowser.contentDocument ||
event.target.location.href == "about:blank" ||
(url && event.target.location.href != url)) {
info("Skipping spurious '" + eventType + "'' event" +
" for " + event.target.location.href);
return;
}
clearTimeout(timeout);
tab.linkedBrowser.removeEventListener(eventType, handle, true);
info("Tab event received: " + eventType);
deferred.resolve(event);
}
let timeout = setTimeout(() => {
tab.linkedBrowser.removeEventListener(eventType, handle, true);
deferred.reject(new Error("Timed out while waiting for a '" + eventType + "'' event"));
}, 30000);
tab.linkedBrowser.addEventListener(eventType, handle, true, true);
if (url) {
tab.linkedBrowser.loadURI(url);
}
return deferred.promise;
}
Services.prefs.setCharPref("urlclassifier.forbiddenTable", "test-forbid-simple");
Services.prefs.setCharPref("urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple");
Services.prefs.setCharPref("urlclassifier.phishTable", "test-phish-simple");
Services.prefs.setBoolPref("browser.safebrowsing.forbiddenURIs.enabled", true);
SafeBrowsing.init();

View File

@ -54,23 +54,26 @@ function cleanUp() {
delFile("safebrowsing/test-phish-simple.sbstore");
delFile("safebrowsing/test-malware-simple.sbstore");
delFile("safebrowsing/test-unwanted-simple.sbstore");
delFile("safebrowsing/test-forbid-simple.sbstore");
delFile("safebrowsing/test-track-simple.sbstore");
delFile("safebrowsing/test-trackwhite-simple.sbstore");
delFile("safebrowsing/test-phish-simple.cache");
delFile("safebrowsing/test-malware-simple.cache");
delFile("safebrowsing/test-unwanted-simple.cache");
delFile("safebrowsing/test-forbid-simple.cache");
delFile("safebrowsing/test-track-simple.cache");
delFile("safebrowsing/test-trackwhite-simple.cache");
delFile("safebrowsing/test-phish-simple.pset");
delFile("safebrowsing/test-malware-simple.pset");
delFile("safebrowsing/test-unwanted-simple.pset");
delFile("safebrowsing/test-forbid-simple.pset");
delFile("safebrowsing/test-track-simple.pset");
delFile("safebrowsing/test-trackwhite-simple.pset");
delFile("testLarge.pset");
delFile("testNoDelta.pset");
}
var allTables = "test-phish-simple,test-malware-simple,test-unwanted-simple,test-track-simple,test-trackwhite-simple";
var allTables = "test-phish-simple,test-malware-simple,test-unwanted-simple,test-forbid-simple,test-track-simple,test-trackwhite-simple";
var dbservice = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService);
var streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"]
@ -127,6 +130,10 @@ function buildUnwantedUpdate(chunks, hashSize) {
return buildUpdate({"test-unwanted-simple" : chunks}, hashSize);
}
function buildForbiddenUpdate(chunks, hashSize) {
return buildUpdate({"test-forbid-simple" : chunks}, hashSize);
}
function buildBareUpdate(chunks, hashSize) {
return buildUpdate({"" : chunks}, hashSize);
}
@ -254,6 +261,11 @@ unwantedUrlsExist: function(urls, cb)
this.checkUrls(urls, 'test-unwanted-simple', cb);
},
forbiddenUrlsExist: function(urls, cb)
{
this.checkUrls(urls, 'test-forbid-simple', cb);
},
subsDontExist: function(urls, cb)
{
// XXX: there's no interface for checking items in the subs table

View File

@ -48,13 +48,15 @@ var chunk6Urls = [
var chunk6 = chunk6Urls.join("\n");
// we are going to add chunks 1, 2, 4, 5, and 6 to phish-simple,
// chunk 2 to malware-simple and chunk 3 to unwanted-simple.
// chunk 2 to malware-simple, chunk 3 to unwanted-simple and
// chunk 4 to forbid-simple.
// Then we'll remove the urls in chunk3 from phish-simple, then
// expire chunk 1 and chunks 4-6 from phish-simple.
var phishExpected = {};
var phishUnexpected = {};
var malwareExpected = {};
var unwantedExpected = {};
var forbiddenExpected = {};
for (var i = 0; i < chunk2Urls.length; i++) {
phishExpected[chunk2Urls[i]] = true;
malwareExpected[chunk2Urls[i]] = true;
@ -69,6 +71,7 @@ for (var i = 0; i < chunk1Urls.length; i++) {
phishUnexpected[chunk1Urls[i]] = true;
}
for (var i = 0; i < chunk4Urls.length; i++) {
forbiddenExpected[chunk4Urls[i]] = true;
// chunk4 urls are expired
phishUnexpected[chunk4Urls[i]] = true;
}
@ -117,7 +120,7 @@ function tablesCallbackWithoutSub(tables)
// there's a leading \n here because splitting left an empty string
// after the trailing newline, which will sort first
do_check_eq(parts.join("\n"),
"\ntest-malware-simple;a:1\ntest-phish-simple;a:2\ntest-unwanted-simple;a:1");
"\ntest-forbid-simple;a:1\ntest-malware-simple;a:1\ntest-phish-simple;a:2\ntest-unwanted-simple;a:1");
checkNoHost();
}
@ -135,7 +138,7 @@ function tablesCallbackWithSub(tables)
// there's a leading \n here because splitting left an empty string
// after the trailing newline, which will sort first
do_check_eq(parts.join("\n"),
"\ntest-malware-simple;a:1\ntest-phish-simple;a:2:s:3\ntest-unwanted-simple;a:1");
"\ntest-forbid-simple;a:1\ntest-malware-simple;a:1\ntest-phish-simple;a:2:s:3\ntest-unwanted-simple;a:1");
// verify that expiring a sub chunk removes its name from the list
var data =
@ -194,6 +197,16 @@ function unwantedExists(result) {
}
}
function forbiddenExists(result) {
dumpn("forbiddenExists: " + result);
try {
do_check_true(result.indexOf("test-forbid-simple") != -1);
} finally {
checkDone();
}
}
function checkState()
{
numExpecting = 0;
@ -222,6 +235,12 @@ function checkState()
dbservice.lookup(principal, allTables, unwantedExists, true);
numExpecting++;
}
for (var key in forbiddenExpected) {
var principal = secMan.createCodebasePrincipal(iosvc.newURI("http://" + key, null, null), {});
dbservice.lookup(principal, allTables, forbiddenExists, true);
numExpecting++;
}
}
function testSubSuccess(result)
@ -268,10 +287,13 @@ function do_adds() {
chunk6 + "\n" +
"i:test-malware-simple\n" +
"a:1:32:" + chunk2.length + "\n" +
chunk2 + "\n" +
chunk2 + "\n" +
"i:test-unwanted-simple\n" +
"a:1:32:" + chunk3.length + "\n" +
chunk3 + "\n";
chunk3 + "\n" +
"i:test-forbid-simple\n" +
"a:1:32:" + chunk4.length + "\n" +
chunk4 + "\n";
doSimpleUpdate(data, testAddSuccess, testFailure);
}

View File

@ -132,6 +132,7 @@ function testMultipleTables() {
var add2Urls = [ "foo-multiple.com/b" ];
var add3Urls = [ "bar-multiple.com/d" ];
var add4Urls = [ "bar-multiple.com/e" ];
var add5Urls = [ "bar-multiple.com/f" ];
var update = "n:1000\n";
update += "i:test-phish-simple\n";
@ -159,11 +160,18 @@ function testMultipleTables() {
"urls" : add4Urls }]);
update += "u:data:," + encodeURIComponent(update4) + "\n";
update += "i:test-forbid-simple\n";
var update5 = buildBareUpdate(
[{ "chunkNum" : 5,
"urls" : add5Urls }]);
update += "u:data:," + encodeURIComponent(update5) + "\n";
var assertions = {
"tableData" : "test-malware-simple;a:3\ntest-phish-simple;a:1-2\ntest-unwanted-simple;a:4",
"tableData" : "test-forbid-simple;a:5\ntest-malware-simple;a:3\ntest-phish-simple;a:1-2\ntest-unwanted-simple;a:4",
"urlsExist" : add1Urls.concat(add2Urls),
"malwareUrlsExist" : add3Urls,
"unwantedUrlsExist" : add4Urls
"unwantedUrlsExist" : add4Urls,
"forbiddenUrlsExist" : add5Urls
};
doTest([update], assertions, false);