content/base/test/test_XHR_system.html
author Margaret Leibovic <margaret.leibovic@gmail.com>
Mon, 25 Aug 2014 15:28:10 -0700
changeset 216776 8d7b3bfaf3ab9bb3c32afb51d9714246d7c854f4
parent 179568 cbd3c79115680cb58beea0f0be7bc68a107fd7ce
permissions -rw-r--r--
Bug 996753 - Telemetry probes for settings pages. r=liuche, a=sledru

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for XMLHttpRequest with system privileges</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="runTests();">
<p id="display">
</p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">

let tests = [];

const PROTECTED_URL = "file:///etc/passwd";
const REDIRECT_URL = window.location.protocol + "//" + window.location.host + "/tests/content/base/test/file_XHR_system_redirect.html";
const CROSSSITE_URL = "http://example.com/tests/content/base/test/test_XHR_system.html";

tests.push(function test_cross_origin() {
  // System XHR can load cross-origin resources.

  is(window.location.hostname, "mochi.test", "correct origin");

  let xhr = new XMLHttpRequest({mozSystem: true});
  is(xhr.mozSystem, true, ".mozSystem == true");
  xhr.open("GET", CROSSSITE_URL);
  xhr.onload = function onload() {
    is(xhr.status, 200, "correct HTTP status");
    ok(xhr.responseText != null, "HTTP response non-null");
    ok(xhr.responseText.length, "HTTP response not empty");
    runNextTest();
  };
  xhr.onerror = function onerror(event) {
    ok(false, "Got an error event: " + event);
    runNextTest();
  }
  xhr.send();
});

tests.push(function test_file_uri() {
  // System XHR is not permitted to access file:/// URIs.

  let xhr = new XMLHttpRequest({mozSystem: true});
  is(xhr.mozSystem, true, ".mozSystem == true");
  xhr.open("GET", PROTECTED_URL);
  let error;
  try {
    xhr.send();
  } catch (ex) {
    error = ex;
  }
  ok(!!error, "got exception");
  is(error.name, "NS_ERROR_DOM_BAD_URI");
  is(error.message, "Access to restricted URI denied");

  runNextTest();
});

tests.push(function test_redirect_to_file_uri() {
  // System XHR won't load file:/// URIs even if an HTTP resource redirects there.

  let xhr = new XMLHttpRequest({mozSystem: true});
  is(xhr.mozSystem, true, ".mozSystem == true");
  xhr.open("GET", REDIRECT_URL);
  xhr.onload = function onload() {
    ok(false, "Should not have loaded");
    runNextTest();
  };
  xhr.onerror = function onerror(event) {
    ok(true, "Got an error event: " + event);
    is(xhr.status, 0, "HTTP status is 0");
    runNextTest();
  }
  xhr.send();
});


function runNextTest() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }
  tests.shift()();
}

function runTests() {
  SimpleTest.waitForExplicitFinish();

  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runNextTest);
}

</script>
</pre>
</body>
</html>