summaryrefslogtreecommitdiffstats
path: root/uriloader/exthandler/tests/mochitest/test_invalidCharFileExtension.xhtml
blob: d7c796589fadf654a8a365ee5a393420d89f9a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test for Handling of unsafe bidi chars</title>
  <script src="/tests/SimpleTest/SimpleTest.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">
var tests = [
  ["test.png:large", "test.png"],
  ["test.png/large", "test.png"],
  [":test.png::large:", "_test.png"],
];

add_task(async function() {
  function promiseMessage(messageName) {
    return new Promise(resolve => {
      chromeScript.addMessageListener(messageName, function listener(data) {
        chromeScript.removeMessageListener(messageName, listener);
        resolve(data);
      });
    });
  }

  let url = SimpleTest.getTestFileURL("HelperAppLauncherDialog_chromeScript.js");
  let chromeScript = SpecialPowers.loadChromeScript(url);

  function wrongAPICallListener(msg) {
    ok(
      false,
      `Called ${msg} when always ask pref was set to ${
        Services.prefs.getBoolPref(
          "browser.download.always_ask_before_handling_new_types"
        )
      }, which shouldn't happen.`
    );
  }
  chromeScript.addMessageListener("wrongAPICall", wrongAPICallListener);

  for (let prefVal of [false, true]) {
    await SpecialPowers.pushPrefEnv({
      set: [["browser.download.always_ask_before_handling_new_types", prefVal]]
    });
    // Run all the tests.
    for (let [name, expected] of tests) {
      let promiseName = promiseMessage("suggestedFileName");
      document.getElementById("test").src =
        "invalidCharFileExtension.sjs?name=" + encodeURIComponent(name);
      is((await promiseName), expected, "got the expected sanitized name");
    }
  }

  // Clean up.
  let promise = promiseMessage("unregistered");
  chromeScript.sendAsyncMessage("unregister");
  await promise;

  chromeScript.removeMessageListener("wrongAPICall", wrongAPICallListener);
  chromeScript.destroy();
});
</script>
</body>
</html>