content/base/test/test_XHR_system.html

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:a755fe244bea
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Test for XMLHttpRequest with system privileges</title>
6 <script type="text/javascript" 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;version=1.8">
17
18 let tests = [];
19
20 const PROTECTED_URL = "file:///etc/passwd";
21 const REDIRECT_URL = window.location.protocol + "//" + window.location.host + "/tests/content/base/test/file_XHR_system_redirect.html";
22 const CROSSSITE_URL = "http://example.com/tests/content/base/test/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 let error;
52 try {
53 xhr.send();
54 } catch (ex) {
55 error = ex;
56 }
57 ok(!!error, "got exception");
58 is(error.name, "NS_ERROR_DOM_BAD_URI");
59 is(error.message, "Access to restricted URI denied");
60
61 runNextTest();
62 });
63
64 tests.push(function test_redirect_to_file_uri() {
65 // System XHR won't load file:/// URIs even if an HTTP resource redirects there.
66
67 let xhr = new XMLHttpRequest({mozSystem: true});
68 is(xhr.mozSystem, true, ".mozSystem == true");
69 xhr.open("GET", REDIRECT_URL);
70 xhr.onload = function onload() {
71 ok(false, "Should not have loaded");
72 runNextTest();
73 };
74 xhr.onerror = function onerror(event) {
75 ok(true, "Got an error event: " + event);
76 is(xhr.status, 0, "HTTP status is 0");
77 runNextTest();
78 }
79 xhr.send();
80 });
81
82
83 function runNextTest() {
84 if (!tests.length) {
85 SimpleTest.finish();
86 return;
87 }
88 tests.shift()();
89 }
90
91 function runTests() {
92 SimpleTest.waitForExplicitFinish();
93
94 SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runNextTest);
95 }
96
97 </script>
98 </pre>
99 </body>
100 </html>

mercurial