Bug 1262339 - P2. Testcase. r=francois draft
authorDimi Lee <dlee@mozilla.com>
Wed, 06 Apr 2016 17:01:18 +0800
changeset 348001 3682f747a2645346b0291b27abf476486b9f612d
parent 348000 7a4d03ae882d793fbdf6f9f971002d08e6e7f908
child 517759 9d558e380ea55982c937fa196d9ec861426ad8fa
push id14724
push userdlee@mozilla.com
push dateWed, 06 Apr 2016 09:01:41 +0000
reviewersfrancois
bugs1262339
milestone48.0a1
Bug 1262339 - P2. Testcase. r=francois MozReview-Commit-ID: 9SyUbrAZraw
toolkit/components/url-classifier/tests/mochitest/appcache.html
toolkit/components/url-classifier/tests/mochitest/cacheManifest.sjs
toolkit/components/url-classifier/tests/mochitest/mochitest.ini
toolkit/components/url-classifier/tests/mochitest/test_classify_manifest.html
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/tests/mochitest/appcache.html
@@ -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>
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/tests/mochitest/cacheManifest.sjs
@@ -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");
+}
--- a/toolkit/components/url-classifier/tests/mochitest/mochitest.ini
+++ b/toolkit/components/url-classifier/tests/mochitest/mochitest.ini
@@ -12,12 +12,15 @@ support-files =
   evilWorker.js
   import.css
   raptor.jpg
   track.html
   unwantedWorker.js
   vp9.webm
   whitelistFrame.html
   workerFrame.html
+  cacheManifest.sjs
+  appcache.html
 
 [test_classifier.html]
 skip-if = (os == 'linux' && debug) #Bug 1199778
 [test_classifier_worker.html]
+[test_classify_manifest.html]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classify_manifest.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://mochi.test:8888/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 = "mochi.test/" + 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>