Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | |
michael@0 | 3 | <!DOCTYPE html> |
michael@0 | 4 | <html> |
michael@0 | 5 | <head> |
michael@0 | 6 | <meta charset="utf-8"> |
michael@0 | 7 | <title>Test for XMLHttpRequest with system privileges</title> |
michael@0 | 8 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body onload="runTests();"> |
michael@0 | 12 | <p id="display"> |
michael@0 | 13 | </p> |
michael@0 | 14 | <div id="content" style="display: none"> |
michael@0 | 15 | |
michael@0 | 16 | </div> |
michael@0 | 17 | <pre id="test"> |
michael@0 | 18 | <script class="testbody" type="application/javascript;version=1.8"> |
michael@0 | 19 | |
michael@0 | 20 | function runTests() { |
michael@0 | 21 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 22 | |
michael@0 | 23 | let validParameters = [ |
michael@0 | 24 | undefined, |
michael@0 | 25 | null, |
michael@0 | 26 | {}, |
michael@0 | 27 | {mozSystem: ""}, |
michael@0 | 28 | {mozSystem: 0}, |
michael@0 | 29 | {mozAnon: 1}, |
michael@0 | 30 | {mozAnon: []}, |
michael@0 | 31 | {get mozAnon() { return true; }}, |
michael@0 | 32 | 0, |
michael@0 | 33 | 7, |
michael@0 | 34 | Math.PI, |
michael@0 | 35 | "string", |
michael@0 | 36 | true, |
michael@0 | 37 | false, |
michael@0 | 38 | ]; |
michael@0 | 39 | |
michael@0 | 40 | let invalidParameters = [ |
michael@0 | 41 | {get mozSystem() { throw "Bla"; } }, |
michael@0 | 42 | ]; |
michael@0 | 43 | |
michael@0 | 44 | let havePrivileges = false; |
michael@0 | 45 | |
michael@0 | 46 | function testValidParameter(value) { |
michael@0 | 47 | let xhr; |
michael@0 | 48 | try { |
michael@0 | 49 | xhr = new XMLHttpRequest(value); |
michael@0 | 50 | } catch (ex) { |
michael@0 | 51 | ok(false, "Got unexpected exception: " + ex); |
michael@0 | 52 | return; |
michael@0 | 53 | } |
michael@0 | 54 | ok(xhr instanceof XMLHttpRequest, "passed " + JSON.stringify(value)); |
michael@0 | 55 | |
michael@0 | 56 | // If the page doesnt have privileges to create a system XHR, |
michael@0 | 57 | // this flag will always be false no matter what is passed. |
michael@0 | 58 | let expectedAnon = Boolean(value && value.mozAnon); |
michael@0 | 59 | let expectedSystem = false; |
michael@0 | 60 | if (havePrivileges) { |
michael@0 | 61 | expectedSystem = Boolean(value && value.mozSystem); |
michael@0 | 62 | } |
michael@0 | 63 | is(xhr.mozAnon, expectedAnon, "testing mozAnon"); |
michael@0 | 64 | is(xhr.mozSystem, expectedSystem, "testing mozSystem"); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function testInvalidParameter(value) { |
michael@0 | 68 | let expectedError; |
michael@0 | 69 | try { |
michael@0 | 70 | new XMLHttpRequest(value); |
michael@0 | 71 | ok(false, "invalid parameter did not cause exception: " + |
michael@0 | 72 | JSON.stringify(value)); |
michael@0 | 73 | } catch (ex) { |
michael@0 | 74 | expectedError = ex; |
michael@0 | 75 | } |
michael@0 | 76 | ok(expectedError, "invalid parameter raised exception as expected: " + |
michael@0 | 77 | JSON.stringify(expectedError)) |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | // Run the tests once without API privileges... |
michael@0 | 81 | validParameters.forEach(testValidParameter); |
michael@0 | 82 | invalidParameters.forEach(testInvalidParameter); |
michael@0 | 83 | |
michael@0 | 84 | // ...and once with privileges. |
michael@0 | 85 | havePrivileges = true; |
michael@0 | 86 | SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], function() { |
michael@0 | 87 | validParameters.forEach(testValidParameter); |
michael@0 | 88 | invalidParameters.forEach(testInvalidParameter); |
michael@0 | 89 | |
michael@0 | 90 | SimpleTest.finish(); |
michael@0 | 91 | }); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | </script> |
michael@0 | 95 | </pre> |
michael@0 | 96 | </body> |
michael@0 | 97 | </html> |