Bug 1262339 - P2. Classify appcache manifest testcase. r=francois

MozReview-Commit-ID: IFGq0l5nNuw

--HG--
extra : transplant_source : %F4_%E0%B1%9F%A1wDf4f%3B%CF%AB%95%60%9D%99%AB%C8
This commit is contained in:
dimi 2016-04-08 17:30:21 +08:00
Родитель a1c9a8528e
Коммит 234215baa8
4 изменённых файлов: 189 добавлений и 0 удалений

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

@ -0,0 +1,25 @@
<html manifest="cacheManifest.sjs">
<head>
<title>classify manifest test</title>
<script type="text/javascript">
function success() {
window.opener.postMessage("success", "*");
}
function fail() {
window.opener.postMessage("fail", "*");
}
// Both onupdateready and oncached are treated as successful download.
applicationCache.onupdateready = success;
applicationCache.oncached = success;
applicationCache.onerror = fail;
</script>
</head>
<body>
<h1></h1>
</body>
</html>

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

@ -0,0 +1,22 @@
var manifest =
"CACHE MANIFEST\n"
function handleRequest(request, response)
{
var query = {};
request.queryString.split('&').forEach(function (val) {
var [name, value] = val.split('=');
query[name] = unescape(value);
});
if (query["update"]) {
setState("update", "#" + Date.now() + "\n");
}
var update = getState("update");
response.setStatusLine(request.httpVersion, 200, "Ok");
response.setHeader("Content-Type", "text/cache-manifest");
response.setHeader("Cache-Control", "no-cache");
response.write(manifest);
response.write(update ? update : "#default\n");
}

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

@ -18,8 +18,11 @@ support-files =
whitelistFrame.html
workerFrame.html
ping.sjs
cacheManifest.sjs
appcache.html
[test_classifier.html]
skip-if = (os == 'linux' && debug) #Bug 1199778
[test_classifier_worker.html]
[test_classify_manifest.html]
[test_classify_ping.html]

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

@ -0,0 +1,139 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1262339 - Appcache manifest doesn't use URL classifier.</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.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="text/javascript">
const PREF = "browser.safebrowsing.malware.enabled";
const appcache_url = "http://example.com/tests/toolkit/components/url-classifier/tests/mochitest/appcache.html";
const manifest_path = "tests/toolkit/components/url-classifier/tests/mochitest/cacheManifest.sjs"
var win;
function testLoadNonBlacklistManifest() {
updateManifest(function() {
SpecialPowers.setBoolPref(PREF, true);
win = window.open(appcache_url);
});
return new Promise(function(resolve, reject) {
window.onmessage = function(e) {
is(e.data, "success", "Manifest not in blacklist should be loaded");
win.close();
resolve();
}
});
}
function testLoadBlacklistManifestSafebrowsingOff() {
updateManifest(function() {
SpecialPowers.setBoolPref(PREF, false);
// url should be added to malware table while running this testcase
win = window.open(appcache_url);
});
return new Promise(function(resolve, reject) {
window.onmessage = function(e) {
is(e.data, "success", "Manifest in blacklist should be loaded when safebrowsing is off");
win.close();
resolve();
}
});
}
function testLoadBlacklistManifestSafebrowsingOn() {
updateManifest(function() {
SpecialPowers.setBoolPref(PREF, true);
// url should be added to malware table while running this testcase
win = window.open(appcache_url);
});
return new Promise(function(resolve, reject) {
window.onmessage = function(e) {
is(e.data, "fail", "Manifest in blacklist shouldn't be loaded when safebrowsing is on");
win.close();
resolve();
}
});
}
// Call this before each testcase starts to ensure that manifest will be downloaded again.
function updateManifest(callback) {
var req = new XMLHttpRequest();
req.open("GET", "http://mochi.test:8888/" + manifest_path + "?update=true");
req.onreadystatechange = function () {
if (req.readyState === XMLHttpRequest.DONE) {
callback();
}
};
req.send();
}
function cleanup() {
SpecialPowers.clearUserPref(PREF);
}
function addManifestToBlackList() {
return new Promise(function(resolve, reject) {
// Add some URLs to the malware database.
var testData = "example.com/" + manifest_path;
var testUpdate =
"n:1000\ni:test-malware-simple\nad:1\n" +
"a:524:32:" + testData.length + "\n" +
testData;
const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
let classifierCommonScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
classifierCommonScript.addMessageListener("loadTestFrame", () => {
resolve();
});
classifierCommonScript.addMessageListener("updateError", ({ errorCode }) => {
ok(false, "Couldn't update classifier. Error code: " + errorCode);
// Abort test.
SimpleTest.finish();
});
classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
});
}
function runTest() {
Promise.resolve()
.then(testLoadNonBlacklistManifest)
.then(addManifestToBlackList)
.then(testLoadBlacklistManifestSafebrowsingOff)
.then(testLoadBlacklistManifestSafebrowsingOn)
.then(function() {
SimpleTest.finish();
}).catch(function(e) {
ok(false, "Some test failed with error " + e);
SimpleTest.finish();
});
}
SimpleTest.waitForExplicitFinish();
SimpleTest.registerCleanupFunction(cleanup);
SpecialPowers.pushPrefEnv({"set": [
["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
]}, runTest);
</script>
</pre>
</body>
</html>