tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_XHR_system.html (2707B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Test for XMLHttpRequest with system privileges</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body onload="runTests();">
     10 <p id="display">
     11 </p>
     12 <div id="content" style="display: none">
     13 
     14 </div>
     15 <pre id="test">
     16 <script class="testbody" type="application/javascript">
     17 
     18 let tests = [];
     19 
     20 const PROTECTED_URL = "file:///etc/passwd";
     21 const REDIRECT_URL = window.location.protocol + "//" + window.location.host + "/tests/dom/xhr/tests/file_XHR_system_redirect.html";
     22 const CROSSSITE_URL = "http://example.com/tests/dom/xhr/tests/test_XHR_system.html";
     23 
     24 tests.push(function test_cross_origin() {
     25  // System XHR can load cross-origin resources.
     26 
     27  is(window.location.hostname, "mochi.test", "correct origin");
     28 
     29  let xhr = new XMLHttpRequest({mozSystem: true});
     30  is(xhr.mozSystem, true, ".mozSystem == true");
     31  xhr.open("GET", CROSSSITE_URL);
     32  xhr.onload = function onload() {
     33    is(xhr.status, 200, "correct HTTP status");
     34    ok(xhr.responseText != null, "HTTP response non-null");
     35    ok(xhr.responseText.length, "HTTP response not empty");
     36    runNextTest();
     37  };
     38  xhr.onerror = function onerror(event) {
     39    ok(false, "Got an error event: " + event);
     40    runNextTest();
     41  }
     42  xhr.send();
     43 });
     44 
     45 tests.push(function test_file_uri() {
     46  // System XHR is not permitted to access file:/// URIs.
     47 
     48  let xhr = new XMLHttpRequest({mozSystem: true});
     49  is(xhr.mozSystem, true, ".mozSystem == true");
     50  xhr.open("GET", PROTECTED_URL);
     51  xhr.onload = function() {
     52    ok(false, "Should not have loaded");
     53    runNextTest();
     54  }
     55  xhr.onerror = function(event) {
     56    ok(true, "Got an error event: " + event);
     57    is(xhr.status, 0, "HTTP status is 0");
     58    runNextTest();
     59  }
     60  xhr.send();
     61 });
     62 
     63 tests.push(function test_redirect_to_file_uri() {
     64  // System XHR won't load file:/// URIs even if an HTTP resource redirects there.
     65 
     66  let xhr = new XMLHttpRequest({mozSystem: true});
     67  is(xhr.mozSystem, true, ".mozSystem == true");
     68  xhr.open("GET", REDIRECT_URL);
     69  xhr.onload = function onload() {
     70    ok(false, "Should not have loaded");
     71    runNextTest();
     72  };
     73  xhr.onerror = function onerror(event) {
     74    ok(true, "Got an error event: " + event);
     75    is(xhr.status, 0, "HTTP status is 0");
     76    runNextTest();
     77  }
     78  xhr.send();
     79 });
     80 
     81 
     82 function runNextTest() {
     83  if (!tests.length) {
     84    SimpleTest.finish();
     85    return;
     86  }
     87  tests.shift()();
     88 }
     89 
     90 function runTests() {
     91  SimpleTest.waitForExplicitFinish();
     92 
     93  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runNextTest);
     94 }
     95 
     96 </script>
     97 </pre>
     98 </body>
     99 </html>