Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: http3 OR http2
- Manifest: toolkit/components/url-classifier/tests/mochitest/mochitest.toml
<!DOCTYPE HTML>
<html>
<head>
<script 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>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript">
var Cc = SpecialPowers.Cc;
var Ci = SpecialPowers.Ci;
var Cr = SpecialPowers.Cr;
var inputDatas = [
{ url: "malware.mochi.test/",
db: "mochi-block-simple",
},
{ url: "malware1.mochi.test/",
db: "mochi1-block-simple",
},
{ url: "malware1.mochi.test/",
db: "mochi1-malware-simple",
provider: "mozilla",
},
{ url: "malware2.mochi.test/",
db: "mochi2-unwanted-simple",
provider: "mozilla",
},
{ url: "malware2.mochi.test/",
db: "mochi2-malware-simple",
provider: "mozilla",
},
{ url: "malware3.mochi.test/",
db: "mochig3-malware-simple",
provider: "google",
},
{ url: "malware3.mochi.test/",
db: "mochim3-malware-simple",
provider: "mozilla",
},
];
function hash(str) {
let hasher = Cc["@mozilla.org/security/hash;1"]
.createInstance(Ci.nsICryptoHash);
let data = new TextEncoder().encode(str);
hasher.init(hasher.SHA256);
hasher.update(data, data.length);
return hasher.finish(false);
}
var testDatas = [
// Match empty provider
expect: { error: Cr.NS_ERROR_BLOCKED_URI,
table: "mochi-block-simple",
provider: "",
fullhash: (function() {
return hash("malware.mochi.test/");
})(),
},
},
// Match multiple tables, only one has valid provider
expect: { error: Cr.NS_ERROR_MALWARE_URI,
table: "mochi1-malware-simple",
provider: "mozilla",
fullhash: (function() {
return hash("malware1.mochi.test/");
})(),
},
},
// Match multiple tables, handle order
expect: { error: Cr.NS_ERROR_MALWARE_URI,
table: "mochi2-malware-simple",
provider: "mozilla",
fullhash: (function() {
return hash("malware2.mochi.test/");
})(),
},
},
// Match multiple tables, handle order
expect: { error: Cr.NS_ERROR_MALWARE_URI,
table: "mochig3-malware-simple",
provider: "google",
fullhash: (function() {
return hash("malware3.mochi.test/");
})(),
},
},
];
SimpleTest.waitForExplicitFinish();
function setupTestData(datas) {
let prefValues = {};
for (let data of datas) {
if (!data.provider) {
continue;
}
let providerPref = "browser.safebrowsing.provider." + data.provider + ".lists";
let prefValue;
if (!prefValues[providerPref]) {
prefValue = data.db;
} else {
prefValue = prefValues[providerPref] + "," + data.db;
}
prefValues[providerPref] = prefValue;
}
// Convert map to array
let prefArray = [];
for (var pref in prefValues) {
prefArray.push([pref, prefValues[pref]]);
}
let activeTablePref = "urlclassifier.malwareTable";
let activeTable = SpecialPowers.getCharPref(activeTablePref);
for (let data of datas) {
activeTable += "," + data.db;
}
prefArray.push([activeTablePref, activeTable]);
return SpecialPowers.pushPrefEnv({set: prefArray});
}
function runTest() {
return new Promise(resolve => {
function runNextTest() {
if (!testDatas.length) {
resolve();
return;
}
let test = testDatas.shift();
let uri = SpecialPowers.Services.io.newURI(test.url);
let prin = SpecialPowers.Services.scriptSecurityManager.createContentPrincipal(uri, {});
SpecialPowers.doUrlClassify(prin, function(errorCode, table, provider, fullhash) {
is(errorCode, test.expect.error, `Test url ${test.url} correct error`);
is(table, test.expect.table, `Test url ${test.url} correct table`);
is(provider, test.expect.provider, `Test url ${test.url} correct provider`);
is(fullhash, btoa(test.expect.fullhash), `Test url ${test.url} correct full hash`);
runNextTest();
});
}
runNextTest();
});
}
SpecialPowers.pushPrefEnv(
{"set": [["browser.safebrowsing.malware.enabled", true]]},
function() {
classifierHelper.waitForInit()
.then(() => setupTestData(inputDatas))
.then(() => classifierHelper.addUrlToDB(inputDatas))
.then(runTest)
.then(function() {
SimpleTest.finish();
}).catch(function(e) {
ok(false, "Some tests failed with error " + e);
SimpleTest.finish();
});
});
</script>
</pre>
</body>
</html>