Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: http3 OR http2
 - Manifest: toolkit/components/url-classifier/tests/mochitest/mochitest.toml
 
<!DOCTYPE HTML>
<html>
<head>
  <title>Test the URI Classifier</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>
<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 = [
  { url: "malware.mochi.test/",
    db: "test-malware-simple",
  },
  { url: "unwanted.mochi.test/",
    db: "test-unwanted-simple",
  },
  { url: "blocked.mochi.test/",
    db: "test-block-simple",
  },
  { url: "harmful.mochi.test/",
    db: "test-harmful-simple",
  },
  { url: "phish.mochi.test/firefox/its-a-trap.html",
    db: "test-phish-simple",
  },
];
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const Cr = SpecialPowers.Cr;
var testURLs = [
    table: "",
    result: Cr.NS_OK,
  },
    table: "test-malware-simple",
    result: Cr.NS_ERROR_MALWARE_URI,
  },
    table: "test-unwanted-simple",
    result: Cr.NS_ERROR_UNWANTED_URI,
  },
    table: "test-phish-simple",
    result: Cr.NS_ERROR_PHISHING_URI,
  },
    table: "test-harmful-simple",
    result: Cr.NS_ERROR_HARMFUL_URI,
  },
    table: "test-block-simple",
    result: Cr.NS_ERROR_BLOCKED_URI,
  },
];
function loadTestFrame() {
  document.getElementById("testFrame").src = "classifierFrame.html";
}
// Expected finish() call is in "classifierFrame.html".
SimpleTest.waitForExplicitFinish();
function updateSuccess() {
  return SpecialPowers.pushPrefEnv(
    {"set": [["browser.safebrowsing.malware.enabled", true]]});
}
function updateError(errorCode) {
  ok(false, "Couldn't update classifier. Error code: " + errorCode);
  // Abort test.
  SimpleTest.finish();
}
function testService() {
  return new Promise(resolve => {
    function runNextTest() {
      if (!testURLs.length) {
        resolve();
        return;
      }
      let test = testURLs.shift();
      let tables = "test-malware-simple,test-unwanted-simple,test-phish-simple,test-block-simple,test-harmful-simple";
      let uri = SpecialPowers.Services.io.newURI(test.url);
      let prin = SpecialPowers.Services.scriptSecurityManager.createContentPrincipal(uri, {});
      SpecialPowers.doUrlClassify(prin, function(errorCode) {
        is(errorCode, test.result,
           `Successful asynchronous classification of ${test.url}`);
        SpecialPowers.doUrlClassifyLocal(uri, tables, function(results) {
          if (!results.length) {
            is(test.table, "",
               `Successful asynchronous local classification of ${test.url}`);
          } else {
            let result = results[0].QueryInterface(Ci.nsIUrlClassifierFeatureResult);
            is(result.list, test.table,
               `Successful asynchronous local classification of ${test.url}`);
          }
          runNextTest();
        });
      });
    }
    runNextTest(resolve);
  });
}
SpecialPowers.pushPrefEnv(
  {"set": [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple,test-harmful-simple"],
           ["urlclassifier.blockedTable", "test-block-simple"],
           ["urlclassifier.phishTable", "test-phish-simple"],
           ["browser.safebrowsing.phishing.enabled", true],
           ["browser.safebrowsing.malware.enabled", true],
           ["browser.safebrowsing.blockedURIs.enabled", true],
           ["browser.safebrowsing.debug", true]]},
  function() {
    classifierHelper.waitForInit()
      .then(() => classifierHelper.addUrlToDB(testData))
      .then(updateSuccess)
      .catch(err => {
        updateError(err);
      })
      .then(testService)
      .then(loadTestFrame);
  });
</script>
</pre>
<iframe id="testFrame" onload=""></iframe>
</body>
</html>