Bug 1262339 - P2. Classify appcache manifest testcase. r?francois draft
authordimi <dlee@mozilla.com>
Fri, 08 Apr 2016 17:30:21 +0800
changeset 348861 f45fe0b19fa177446634663bcfab95609d99abc8
parent 348860 1c9d556c4f7ee544a96c20b78ec177a75646d78c
child 517960 be8a2c155e274f8a90e9e11d292a75a5c0ae5cef
push id14946
push userdlee@mozilla.com
push dateFri, 08 Apr 2016 09:32:01 +0000
reviewersfrancois
bugs1262339
milestone48.0a1
Bug 1262339 - P2. Classify appcache manifest testcase. r?francois MozReview-Commit-ID: IFGq0l5nNuw
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
@@ -13,13 +13,16 @@ support-files =
   import.css
   raptor.jpg
   track.html
   unwantedWorker.js
   vp9.webm
   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]
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://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>