content/base/test/test_XHR_system.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial