caps/tests/mochitest/test_bug995943.xul
author Kris Maglione <maglione.k@gmail.com>
Mon, 29 Jan 2018 15:20:18 -0800
changeset 453844 34c999fa006bffe8705cf50c54708aa21a962e62
parent 453830 505fc52bd6080c95bf27458a5753340b326f0fa1
child 453889 c6e0fe339cb1e7c0c029e68be5332e52f53ab6b7
permissions -rw-r--r--
Bug 1431533: Part 5a - Auto-rewrite code to use ChromeUtils import methods. r=florian This was done using the following script: https://bitbucket.org/kmaglione/m-c-rewrites/src/37e3803c7a23385bab5ea077ba8692bcfe1608fc/processors/chromeutils-import.jsm MozReview-Commit-ID: 1Nc3XDu0wGl

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=995943
-->
<window title="Mozilla Bug 995943"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=995943"
     target="_blank">Mozilla Bug 995943</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  const Cu = Components.utils;
  const Cc = Components.classes;
  const Ci = Components.interfaces;
  ChromeUtils.import("resource://gre/modules/Services.jsm");
  function debug(msg) { info(msg); }

  /** Test for CAPS file:// URI prefs. **/
  SimpleTest.waitForExplicitFinish();
  SimpleTest.requestCompleteLog();
  if (navigator.userAgent.indexOf("Mac OS X 10.10") != -1)
    SimpleTest.expectAssertions(5, 11); // See bug 1067022, 1307988
  else if (Services.appinfo.OS == "WINNT")
    SimpleTest.expectAssertions(0, 1); // See bug 1067022
  else
    SimpleTest.expectAssertions(0, 2); // See bug 1305241

  var rootdir = Services.appinfo.OS == "WINNT" ? "file:///C:" : "file:///";

  function checkLoadFileURI(domain, shouldLoad) {
    debug("Invoking checkLoadFileURI with domain: " + domain + ", shouldLoad: " + shouldLoad);
    return new Promise(function(resolve, reject) {
      $('ifr').addEventListener('load', function l1() {
        debug("Invoked l1 for " + domain);
        $('ifr').removeEventListener('load', l1);
        function l2() {
          debug("Invoked l2 for " + domain);
          $('ifr').removeEventListener('load', l2);
          ok(shouldLoad, "Successfully loaded file:// URI for domain: " + domain);
          resolve();
        }
        $('ifr').addEventListener('load', l2);
        try {
          window[0].wrappedJSObject.location = rootdir;
          debug("Successfully navigated for " + domain);
        } catch (e) {
          ok(!shouldLoad && /denied|insecure/.test(e),
             "Prevented loading of file:// URI for domain: " + domain + " - " + e);
          $('ifr').removeEventListener('load', l2);
          resolve();
        }
      });
      let targetURI = domain + '/tests/js/xpconnect/tests/mochitest/file_empty.html';
      debug("Navigating iframe to " + targetURI);
      $('ifr').contentWindow.location = targetURI;
    });
  }

  function pushPrefs(prefs) {
    return SpecialPowers.pushPrefEnv({ set: prefs });
  }

  function popPrefs() {
    return new Promise(function(resolve) { SpecialPowers.popPrefEnv(resolve); });
  }

  var gGoCount = 0;
  function go() {
    debug("Invoking go for window with id: " + window.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID);
    is(++gGoCount, 1, "Should only call go once!");
    checkLoadFileURI('http://example.com', false).then(
      pushPrefs.bind(null, [['capability.policy.policynames', ' somepolicy '],
                            ['capability.policy.somepolicy.checkloaduri.enabled', 'AlLAcCeSs'],
                            ['capability.policy.somepolicy.sites', 'http://example.com']]))
    .then(checkLoadFileURI.bind(null, 'http://example.com', true))
    .then(popPrefs)
    .then(checkLoadFileURI.bind(null, 'http://example.com', false))
    .then(
      pushPrefs.bind(null, [['capability.policy.policynames', ',somepolicy, someotherpolicy, '],
                            ['capability.policy.somepolicy.checkloaduri.enabled', 'allaccess'],
                            ['capability.policy.someotherpolicy.checkloaduri.enabled', 'nope'],
                            ['capability.policy.somepolicy.sites', ' http://example.org   test1.example.com https://test2.example.com '],
                            ['capability.policy.someotherpolicy.sites', 'http://example.net ']]))
    .then(checkLoadFileURI.bind(null, 'http://example.org', true))
    .then(checkLoadFileURI.bind(null, 'http://test2.example.com', false))
    .then(checkLoadFileURI.bind(null, 'https://test2.example.com', true))
    .then(checkLoadFileURI.bind(null, 'http://sub1.test2.example.com', false))
    .then(checkLoadFileURI.bind(null, 'https://sub1.test2.example.com', true))
    .then(checkLoadFileURI.bind(null, 'http://example.net', false))
    .then(checkLoadFileURI.bind(null, 'http://test1.example.com', true))
    .then(checkLoadFileURI.bind(null, 'https://test1.example.com', true))
    .then(checkLoadFileURI.bind(null, 'http://sub1.test1.example.com', true))
    .then(checkLoadFileURI.bind(null, 'https://sub1.test1.example.com', true))
    .then(pushPrefs.bind(null, [['capability.policy.someotherpolicy.checkloaduri.enabled', 'allAccess']]))
    .then(checkLoadFileURI.bind(null, 'http://example.net', true))
    .then(popPrefs)
    .then(popPrefs)
    .then(checkLoadFileURI.bind(null, 'http://example.net', false))
    .then(SimpleTest.finish.bind(SimpleTest));

  }
  addLoadEvent(go);

  ]]>
  </script>
  <iframe id="ifr" type="content" />
</window>