uriloader/exthandler/tests/mochitest/test_unsafeBidiChars.xhtml
author Syeda Asra Arshia Qadri <asra.qadri@gmail.com>
Thu, 14 Mar 2019 11:19:44 +0000
changeset 521883 9e322c3c5db7941730308e3022222a8904081b99
parent 521860 14c61ee043c8e0507c05e13b8c82a0610df660fa
child 528457 c9c0c6f2eed54a187e124942e53c3660b4cf17d8
permissions -rw-r--r--
Bug 1532941 - Enable ESLint for uriloader (automatic changes). r=Standard8,qdot Differential Revision: https://phabricator.services.mozilla.com/D22308

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test for Handling of unsafe bidi chars</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/AddTask.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<iframe id="test"></iframe>
<script type="text/javascript">
<![CDATA[

var unsafeBidiChars = [
  "\xe2\x80\xaa", // LRE
  "\xe2\x80\xab", // RLE
  "\xe2\x80\xac", // PDF
  "\xe2\x80\xad", // LRO
  "\xe2\x80\xae",  // RLO
];

var tests = [
  "{1}.test",
  "{1}File.test",
  "Fi{1}le.test",
  "File{1}.test",
  "File.{1}test",
  "File.te{1}st",
  "File.test{1}",
  "File.{1}",
];

function replace(name, x) {
  return name.replace(/\{1\}/, x);
}

function sanitize(name) {
  return replace(name, "_");
}

const INSECURE_REGISTER_PREF = "dom.registerProtocolHandler.insecure.enabled";

add_task(async function() {
  SpecialPowers.setBoolPref(INSECURE_REGISTER_PREF, false);
  let url = SimpleTest.getTestFileURL("unsafeBidi_chromeScript.js");
  let chromeScript = SpecialPowers.loadChromeScript(url);

  for (let test of tests) {
    for (let char of unsafeBidiChars) {
      let promiseName = new Promise(function(resolve) {
        chromeScript.addMessageListener("suggestedFileName",
                                        function listener(data) {
          chromeScript.removeMessageListener("suggestedFileName", listener);
          resolve(data);
        });
      });
      let name = replace(test, char);
      let expected = sanitize(test);
      document.getElementById("test").src =
        "unsafeBidiFileName.sjs?name=" + encodeURIComponent(name);
      is((await promiseName), expected, "got the expected sanitized name");
    }
  }

  let promise = new Promise(function(resolve) {
    chromeScript.addMessageListener("unregistered", function listener() {
      chromeScript.removeMessageListener("unregistered", listener);
      resolve();
    });
  });
  chromeScript.sendAsyncMessage("unregister");
  await promise;

  SpecialPowers.clearUserPref(INSECURE_REGISTER_PREF);

  chromeScript.destroy();
});

]]>
</script>
</body>
</html>