Bug 1264169 - P1. test_classifier.html doesn't remove url added to malware database when finish. r=francois
MozReview-Commit-ID: 5iuMuVk5f8d
--- a/toolkit/components/url-classifier/tests/mochitest/classifierCommon.js
+++ b/toolkit/components/url-classifier/tests/mochitest/classifierCommon.js
@@ -14,17 +14,17 @@ function doUpdate(update) {
throw Cr.NS_ERROR_NO_INTERFACE;
},
updateUrlRequested: function(url) { },
streamFinished: function(status) { },
updateError: function(errorCode) {
sendAsyncMessage("updateError", { errorCode });
},
updateSuccess: function(requestedTimeout) {
- sendAsyncMessage("loadTestFrame");
+ sendAsyncMessage("updateSuccess");
}
};
let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
.getService(Ci.nsIUrlClassifierDBService);
dbService.beginUpdate(listener, "test-malware-simple,test-unwanted-simple", "");
dbService.beginStream("", "");
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
@@ -0,0 +1,100 @@
+if (typeof(classifierHelper) == "undefined") {
+ var classifierHelper = {};
+}
+
+const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
+var classifierCommonScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
+
+// addUrlToDB & removeUrlFromDB are asynchronous, queue the task to ensure
+// the callback follow correct order.
+classifierHelper._updates = [];
+
+// Keep urls added to database, those urls should be automatically
+// removed after test complete.
+classifierHelper._updatesToCleanup = [];
+
+// Pass { url: ..., db: ... } to add url to database,
+// onsuccess/onerror will be called when update complete.
+classifierHelper.addUrlToDB = function(updateData, onsuccess, onerror) {
+ var testUpdate = "";
+ for (var update of updateData) {
+ classifierHelper._updatesToCleanup.push(update);
+ testUpdate +=
+ "n:1000\ni:" + update.db + "\nad:1\n" +
+ "a:524:32:" + update.url.length + "\n" +
+ update.url;
+ }
+
+ classifierHelper._update(testUpdate, onsuccess, onerror);
+}
+
+// Pass { url: ..., db: ... } to remove url from database,
+// onsuccess/onerror will be called when update complete.
+classifierHelper.removeUrlFromDB = function(updateData, onsuccess, onerror) {
+ var testUpdate = "";
+ for (var update of updateData) {
+ testUpdate +=
+ "n:1000\ni:" + update.db + "\nsd:1\n" +
+ "s:524:32:" + (4 + update.url.length) + "\n" +
+ "524:" + update.url;
+ }
+
+ classifierHelper._updatesToCleanup =
+ classifierHelper._updatesToCleanup.filter((v) => {
+ return updateData.indexOf(v) == -1;
+ });
+
+ classifierHelper._update(testUpdate, onsuccess, onerror);
+};
+
+classifierHelper._update = function(testUpdate, onsuccess, onerror) {
+ // Queue the task if there is still an on-going update
+ classifierHelper._updates.push({"data": testUpdate,
+ "onsuccess": onsuccess,
+ "onerror": onerror});
+ if (classifierHelper._updates.length != 1) {
+ return;
+ }
+
+ classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+};
+
+classifierHelper._updateSuccess = function() {
+ var update = classifierHelper._updates.shift();
+ update.onsuccess();
+
+ if (classifierHelper._updates.length) {
+ var testUpdate = classifierHelper._updates[0].data;
+ classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+ }
+};
+
+classifierHelper._updateError = function(errorCode) {
+ var update = classifierHelper._updates.shift();
+ update.onerror(errorCode);
+
+ if (classifierHelper._updates.length) {
+ var testUpdate = classifierHelper._updates[0].data;
+ classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+ }
+};
+
+classifierHelper._setup = function() {
+ classifierCommonScript.addMessageListener("updateSuccess", classifierHelper._updateSuccess);
+ classifierCommonScript.addMessageListener("updateError", classifierHelper._updateError);
+
+ // cleanup will be called at end of each testcase to remove all the urls added to database.
+ SimpleTest.registerCleanupFunction(classifierHelper._cleanup);
+};
+
+classifierHelper._cleanup = function() {
+ if (!classifierHelper._updatesToCleanup) {
+ return Promise.resolve();
+ }
+
+ return new Promise(function(resolve, reject) {
+ classifierHelper.removeUrlFromDB(classifierHelper._updatesToCleanup, resolve, reject);
+ });
+};
+
+classifierHelper._setup();
--- a/toolkit/components/url-classifier/tests/mochitest/mochitest.ini
+++ b/toolkit/components/url-classifier/tests/mochitest/mochitest.ini
@@ -1,14 +1,15 @@
[DEFAULT]
skip-if = buildapp == 'b2g'
support-files =
classifiedAnnotatedPBFrame.html
classifierCommon.js
classifierFrame.html
+ classifierHelper.js
cleanWorker.js
good.js
evil.css
evil.js
evil.js^headers^
evilWorker.js
import.css
raptor.jpg
--- a/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
@@ -1,64 +1,60 @@
<!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.
-var testData = "malware.example.com/";
-var testUpdate =
- "n:1000\ni:test-malware-simple\nad:1\n" +
- "a:524:32:" + testData.length + "\n" +
- testData;
-
-testData = "unwanted.example.com/";
-testUpdate +=
- "n:1000\ni:test-unwanted-simple\nad:1\n" +
- "a:524:32:" + testData.length + "\n" +
- testData;
+var testData = [
+ { url: "malware.example.com/",
+ db: "test-malware-simple"
+ },
+ { url: "unwanted.example.com/",
+ db: "test-unwanted-simple"
+ }
+];
function loadTestFrame() {
document.getElementById("testFrame").src = "classifierFrame.html";
}
-const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
-let classifierCommonScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
-
// Expected finish() call is in "classifierFrame.html".
SimpleTest.waitForExplicitFinish();
-classifierCommonScript.addMessageListener("loadTestFrame", () => {
+function updateSuccess() {
SpecialPowers.pushPrefEnv(
{"set" : [["browser.safebrowsing.malware.enabled", true]]},
loadTestFrame);
-});
-classifierCommonScript.addMessageListener("updateError", ({ errorCode }) => {
+}
+
+function updateError(errorCode) {
ok(false, "Couldn't update classifier. Error code: " + errorCode);
// Abort test.
SimpleTest.finish();
-});
+}
SpecialPowers.pushPrefEnv(
{"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
["urlclassifier.phishTable", "test-phish-simple"]]},
function() {
- classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+ classifierHelper.addUrlToDB(testData, updateSuccess, updateError);
});
</script>
</pre>
<iframe id="testFrame" onload=""></iframe>
</body>
</html>
--- a/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html
@@ -1,35 +1,33 @@
<!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">
// Add some URLs to the malware database.
-var testData = "example.com/tests/toolkit/components/url-classifier/tests/mochitest/evilWorker.js";
-var testUpdate =
- "n:1000\ni:test-malware-simple\nad:550\n" +
- "a:550:32:" + testData.length + "\n" +
- testData;
-
-testData = "example.com/tests/toolkit/components/url-classifier/tests/mochitest/unwantedWorker.js";
-testUpdate +=
- "n:1000\ni:test-unwanted-simple\nad:550\n" +
- "a:550:32:" + testData.length + "\n" +
- testData;
+var testData = [
+ { url: "example.com/tests/toolkit/components/url-classifier/tests/mochitest/evilWorker.js",
+ db: "test-malware-simple"
+ },
+ { url: "example.com/tests/toolkit/components/url-classifier/tests/mochitest/unwantedWorker.js",
+ db: "test-unwanted-simple"
+ }
+];
function loadTestFrame() {
document.getElementById("testFrame").src =
"http://example.com/tests/toolkit/components/url-classifier/tests/mochitest/workerFrame.html";
}
function onmessage(event)
{
@@ -37,35 +35,33 @@ function onmessage(event)
if (pieces[0] == "finish") {
SimpleTest.finish();
return;
}
is(pieces[0], "success", pieces[1]);
}
-const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
-let classifierCommonScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
-
-classifierCommonScript.addMessageListener("loadTestFrame", () => {
+function updateSuccess() {
SpecialPowers.pushPrefEnv(
{"set" : [["browser.safebrowsing.malware.enabled", true]]},
loadTestFrame);
-});
-classifierCommonScript.addMessageListener("updateError", ({ errorCode }) => {
+}
+
+function updateError(errorCode) {
ok(false, "Couldn't update classifier. Error code: " + errorCode);
// Abort test.
SimpleTest.finish();
-});
+};
SpecialPowers.pushPrefEnv(
{"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
["urlclassifier.phishTable", "test-phish-simple"]]},
function() {
- classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+ classifierHelper.addUrlToDB(testData, updateSuccess, updateError);
});
window.addEventListener("message", onmessage, false);
SimpleTest.waitForExplicitFinish();
</script>