Bug 1888590 - Mark some subtests on trusted-types-event-handlers.html as failing...
[gecko.git] / caps / tests / mochitest / test_addonMayLoad.html
blob74dc8134b485abb328e97e0c6e1128d85502ee62
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=1180921
5 -->
6 <head>
7 <meta charset="utf-8">
8 <title>Test for Bug 1180921</title>
9 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
11 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
12 <script type="application/javascript">
14 /** Test for Bug 1180921 */
15 let module = Cu.getGlobalForObject(Services);
16 let ssm = Services.scriptSecurityManager;
18 function StubPolicy(id, subdomain) {
19 /* globals MatchPatternSet */
20 return new module.WebExtensionPolicy({
21 id,
22 mozExtensionHostname: id,
23 baseURL: `file:///{id}`,
25 allowedOrigins: new MatchPatternSet([`*://${subdomain}.example.org/*`]),
26 localizeCallback() {},
27 });
30 /* globals WebExtensionPolicy */
31 let policyA = StubPolicy("addona", "test1");
32 let policyB = StubPolicy("addonb", "test2");
33 policyA.active = true;
34 policyB.active = true;
36 SimpleTest.waitForExplicitFinish();
37 SimpleTest.registerCleanupFunction(function() {
38 policyA.active = false;
39 policyB.active = false;
40 });
42 function tryLoad(sb, uri) {
43 let p = new Promise(function(resolve, reject) {
44 Cu.exportFunction(resolve, sb, { defineAs: "finish" });
45 Cu.exportFunction(reject, sb, { defineAs: "error" });
46 sb.eval("try { (function () { " +
47 " var xhr = new XMLHttpRequest();" +
48 " xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { finish(xhr.status == 200); } };" +
49 " xhr.open('GET', '" + uri + "', true);" +
50 " xhr.send();" +
51 "})() } catch (e) { error(e); }");
52 });
53 return p;
56 let addonA = new Cu.Sandbox(ssm.createContentPrincipal(Services.io.newURI("moz-extension://addonA/"), {}),
57 {wantGlobalProperties: ["XMLHttpRequest"]});
58 let addonB = new Cu.Sandbox(ssm.createContentPrincipal(Services.io.newURI("moz-extension://addonB/"), {}),
59 {wantGlobalProperties: ["XMLHttpRequest"]});
61 function uriForDomain(d) { return d + "/tests/caps/tests/mochitest/file_data.txt"; }
63 tryLoad(addonA, uriForDomain("http://test4.example.org"))
64 .then(function(success) {
65 ok(!success, "cross-origin load should fail for addon A");
66 return tryLoad(addonA, uriForDomain("http://test1.example.org"));
67 }).then(function(success) {
68 ok(success, "allowlisted cross-origin load of test1 should succeed for addon A");
69 return tryLoad(addonB, uriForDomain("http://test1.example.org"));
70 }).then(function(success) {
71 ok(!success, "non-allowlisted cross-origin load of test1 should fail for addon B");
72 return tryLoad(addonB, uriForDomain("http://test2.example.org"));
73 }).then(function(success) {
74 ok(success, "allowlisted cross-origin load of test2 should succeed for addon B");
75 return tryLoad(addonA, uriForDomain("http://test2.example.org"));
76 }).then(function(success) {
77 ok(!success, "non-allowlisted cross-origin load of test2 should fail for addon A");
78 SimpleTest.finish();
79 }, function(e) {
80 ok(false, "Rejected promise chain: " + e);
81 SimpleTest.finish();
82 });
84 </script>
85 </head>
86 <body>
87 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1180921">Mozilla Bug 1180921</a>
88 <p id="display"></p>
89 <div id="content" style="display: none">
91 </div>
92 <pre id="test">
93 </pre>
94 </body>
95 </html>