fune/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
Dan Banner 436f06fda4 Bug 1386684 - Enable ESLint for toolkit/components/url-classifier (automatic fixes). r=hchang
MozReview-Commit-ID: F0Z8dRaYOku

--HG--
extra : rebase_source : 3ebc9ab6ea9f1741d8a986c9217575644411e2a1
2017-08-02 16:12:07 +01:00

196 lines
5.9 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test the URI Classifier</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="classifierHelper.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var firstLoad = true;
// Add some URLs to the malware database.
// Note that we intentionally don't touch test-phish-simple, and will
// use the URL registered by addMozEntries(). Otherwise, classifierHelper.resetDatabase()
// will reset this table, and waitForInit() can't find the known phishing
// URL in test-phish-simple, breaking the tests following this one.
var testData = [
{ url: "malware.example.com/",
db: "test-malware-simple"
},
{ url: "unwanted.example.com/",
db: "test-unwanted-simple"
},
{ url: "tracking.example.com/",
db: "test-track-simple"
},
{ url: "blocked.example.com/",
db: "test-block-simple"
},
{ url: "harmful.example.com/",
db: "test-harmful-simple"
},
];
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const Cr = SpecialPowers.Cr;
var testURLs = [
{ url: "http://example.com",
trackingProtection: false,
table: "",
result: Cr.NS_OK
},
{ url: "http://example.com",
trackingProtection: true,
table: "",
result: Cr.NS_OK
},
{ url: "http://malware.example.com",
trackingProtection: false,
table: "test-malware-simple",
result: Cr.NS_ERROR_MALWARE_URI,
},
{ url: "http://malware.example.com",
trackingProtection: true,
table: "test-malware-simple",
result: Cr.NS_ERROR_MALWARE_URI,
},
{ url: "http://unwanted.example.com",
trackingProtection: false,
table: "test-unwanted-simple",
result: Cr.NS_ERROR_UNWANTED_URI
},
{ url: "http://unwanted.example.com",
trackingProtection: true,
table: "test-unwanted-simple",
result: Cr.NS_ERROR_UNWANTED_URI
},
{ url: "http://itisatrap.org/firefox/its-a-trap.html",
trackingProtection: false,
table: "test-phish-simple",
result: Cr.NS_ERROR_PHISHING_URI
},
{ url: "http://itisatrap.org/firefox/its-a-trap.html",
trackingProtection: true,
table: "test-phish-simple",
result: Cr.NS_ERROR_PHISHING_URI
},
{ url: "http://harmful.example.com",
trackingProtection: false,
table: "test-harmful-simple",
result: Cr.NS_ERROR_HARMFUL_URI
},
{ url: "http://harmful.example.com",
trackingProtection: true,
table: "test-harmful-simple",
result: Cr.NS_ERROR_HARMFUL_URI
},
{ url: "http://tracking.example.com",
trackingProtection: false,
table: "test-track-simple",
result: Cr.NS_OK
},
{ url: "http://tracking.example.com",
trackingProtection: true,
table: "test-track-simple",
result: Cr.NS_ERROR_TRACKING_URI
},
{ url: "http://blocked.example.com",
trackingProtection: false,
table: "test-block-simple",
result: Cr.NS_ERROR_BLOCKED_URI
},
{ url: "http://blocked.example.com",
trackingProtection: true,
table: "test-block-simple",
result: Cr.NS_ERROR_BLOCKED_URI
}
];
function loadTestFrame() {
document.getElementById("testFrame").src = "classifierFrame.html";
}
// Expected finish() call is in "classifierFrame.html".
SimpleTest.waitForExplicitFinish();
function updateSuccess() {
return SpecialPowers.pushPrefEnv(
{"set": [["browser.safebrowsing.malware.enabled", true]]});
}
function updateError(errorCode) {
ok(false, "Couldn't update classifier. Error code: " + errorCode);
// Abort test.
SimpleTest.finish();
}
function testService() {
return new Promise(resolve => {
let service = Cc["@mozilla.org/url-classifier/dbservice;1"].
getService(Ci.nsIURIClassifier);
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
let ios = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
function runNextTest() {
if (!testURLs.length) {
resolve();
return;
}
let test = testURLs.shift();
let tables = "test-malware-simple,test-unwanted-simple,test-phish-simple,test-track-simple,test-block-simple,test-harmful-simple";
let uri = ios.newURI(test.url);
let prin = ssm.createCodebasePrincipal(uri, {});
is(service.classifyLocal(uri, tables), test.table,
`Successful synchronous classification of ${test.url} with TP=${test.trackingProtection}`);
let result = SpecialPowers.doUrlClassify(prin, null, test.trackingProtection, function(errorCode) {
is(errorCode, test.result,
`Successful asynchronous classification of ${test.url} with TP=${test.trackingProtection}`);
// Same as classifyLocal except for the 'async' call.
SpecialPowers.doUrlClassifyLocal(uri, tables, function(errorCode, tables) {
is(tables, test.table,
`Successful asynchronous local classification of ${test.url} with TP=${test.trackingProtection}`);
runNextTest();
});
});
}
runNextTest(resolve);
});
}
SpecialPowers.pushPrefEnv(
{"set": [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple,test-harmful-simple"],
["urlclassifier.phishTable", "test-phish-simple"],
["urlclassifier.downloadBlockTable", "test-block-simple"],
["urlclassifier.trackingTable", "test-track-simple"],
["browser.safebrowsing.debug", true],
["privacy.trackingprotection.annotate_channels", true]]},
function() {
classifierHelper.waitForInit()
.then(() => classifierHelper.addUrlToDB(testData))
.then(updateSuccess)
.catch(err => {
updateError(err);
})
.then(testService)
.then(loadTestFrame);
});
</script>
</pre>
<iframe id="testFrame" onload=""></iframe>
</body>
</html>