mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-13 06:38:48 +02:00
This excludes dom/, otherwise the file size is too large for phabricator to handle. This is an autogenerated commit to handle scripts loading mochitest harness files, in the simple case where the script src is on the same line as the tag. This was generated with https://bug1544322.bmoattachments.org/attachment.cgi?id=9058170 using the `--part 2` argument. Differential Revision: https://phabricator.services.mozilla.com/D27456 --HG-- extra : moz-landing-system : lando
187 lines
5.2 KiB
HTML
187 lines
5.2 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<title>Test the URI Classifier Matched Info (bug 1288633) </title>
|
|
<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.example.com/",
|
|
db: "mochi-block-simple",
|
|
},
|
|
{ url: "malware1.example.com/",
|
|
db: "mochi1-block-simple",
|
|
},
|
|
{ url: "malware1.example.com/",
|
|
db: "mochi1-malware-simple",
|
|
provider: "mozilla",
|
|
},
|
|
{ url: "malware2.example.com/",
|
|
db: "mochi2-unwanted-simple",
|
|
provider: "mozilla",
|
|
},
|
|
{ url: "malware2.example.com/",
|
|
db: "mochi2-malware-simple",
|
|
provider: "mozilla",
|
|
},
|
|
{ url: "malware3.example.com/",
|
|
db: "mochig3-malware-simple",
|
|
provider: "google",
|
|
},
|
|
{ url: "malware3.example.com/",
|
|
db: "mochim3-malware-simple",
|
|
provider: "mozilla",
|
|
},
|
|
];
|
|
|
|
function hash(str) {
|
|
function bytesFromString(str1) {
|
|
let converter =
|
|
Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
converter.charset = "UTF-8";
|
|
return converter.convertToByteArray(str1);
|
|
}
|
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
let data = bytesFromString(str);
|
|
hasher.init(hasher.SHA256);
|
|
hasher.update(data, data.length);
|
|
|
|
return hasher.finish(false);
|
|
}
|
|
|
|
var testDatas = [
|
|
// Match empty provider
|
|
{ url: "http://malware.example.com",
|
|
expect: { error: Cr.NS_ERROR_BLOCKED_URI,
|
|
table: "mochi-block-simple",
|
|
provider: "",
|
|
fullhash: (function() {
|
|
return hash("malware.example.com/");
|
|
})(),
|
|
},
|
|
},
|
|
// Match multiple tables, only one has valid provider
|
|
{ url: "http://malware1.example.com",
|
|
expect: { error: Cr.NS_ERROR_MALWARE_URI,
|
|
table: "mochi1-malware-simple",
|
|
provider: "mozilla",
|
|
fullhash: (function() {
|
|
return hash("malware1.example.com/");
|
|
})(),
|
|
},
|
|
},
|
|
// Match multiple tables, handle order
|
|
{ url: "http://malware2.example.com",
|
|
expect: { error: Cr.NS_ERROR_MALWARE_URI,
|
|
table: "mochi2-malware-simple",
|
|
provider: "mozilla",
|
|
fullhash: (function() {
|
|
return hash("malware2.example.com/");
|
|
})(),
|
|
},
|
|
},
|
|
// Match multiple tables, handle order
|
|
{ url: "http://malware3.example.com",
|
|
expect: { error: Cr.NS_ERROR_MALWARE_URI,
|
|
table: "mochig3-malware-simple",
|
|
provider: "google",
|
|
fullhash: (function() {
|
|
return hash("malware3.example.com/");
|
|
})(),
|
|
},
|
|
},
|
|
|
|
];
|
|
|
|
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.createCodebasePrincipal(uri, {});
|
|
SpecialPowers.doUrlClassify(prin, null, 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>
|