tor-browser

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

test_messagemanager_send_principal.html (3720B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test for Principal in MessageManager</title>
      5  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
      7 </head>
      8 <body>
      9 
     10  <script type="application/javascript">
     11    "use strict";
     12 
     13    SimpleTest.waitForExplicitFinish();
     14 
     15    const childFrameURL =
     16      "data:text/html,<!DOCTYPE HTML><html><body></body></html>";
     17 
     18    function childFrameScript() {
     19      "use strict";
     20 
     21 
     22      addMessageListener("test:content", function(message) {
     23        sendAsyncMessage("test:result", "is nsIPrincipal: " +
     24                         (message.data instanceof Ci.nsIPrincipal ? "OK" : "KO"));
     25 
     26        sendAsyncMessage("test:result", "principal.origin: " +
     27                         ("origin" in message.data ? "OK" : "KO"));
     28      });
     29 
     30      addMessageListener("test:system", function(message) {
     31        sendAsyncMessage("test:result", "isSystemPrincipal: " +
     32                         (message.data.isSystemPrincipal ? "OK" : "KO"));
     33      });
     34 
     35      addMessageListener("test:ep", function(message) {
     36        sendAsyncMessage("test:result", "expanded principal: " +
     37                         (message.data.isExpandedPrincipal ? "OK" : "KO"));
     38        sendAsyncMessage("test:result", "correct origin: " +
     39                         (message.data.origin == "[Expanded Principal [http://bar.example.com, http://foo.example.com]]" ? "OK" : "KO"));
     40      });
     41 
     42      addMessageListener("test:null", function(message) {
     43        sendAsyncMessage("test:result", "is nsIPrincipal: " +
     44                         (message.data instanceof Ci.nsIPrincipal ? "OK" : "KO"));
     45 
     46        sendAsyncMessage("test:result", "isNullPrincipal: " +
     47                         (message.data.isNullPrincipal ? "OK" : "KO"));
     48        sendAsyncMessage("test:result", "DONE");
     49      });
     50    }
     51 
     52    function runTests() {
     53      ok("Browser prefs set.");
     54 
     55      let iframe = document.createXULElement("browser");
     56      iframe.setAttribute("type", "content");
     57      iframe.setAttribute("forcemessagemanager", "true");
     58      iframe.id = "iframe";
     59      iframe.src = childFrameURL;
     60 
     61      let sb = new Cu.Sandbox(['http://foo.example.com', 'http://bar.example.com']);
     62      let ep = Cu.getObjectPrincipal(sb);
     63 
     64      iframe.addEventListener("load", function() {
     65        ok(true, "Got iframe load event.");
     66 
     67        let mm = iframe.messageManager;
     68        mm.addMessageListener("test:result", function(message) {
     69          // We need to wrap to access message.json, and unwrap to do the
     70          // identity check.
     71          var msg = SpecialPowers.unwrap(SpecialPowers.wrap(message).data);
     72          if (/OK$/.exec(msg)) {
     73            ok(true, msg);
     74          } else if(/KO$/.exec(msg)) {
     75            ok(true, false);
     76          } else if (/DONE/.exec(msg)) {
     77            SimpleTest.finish();
     78          }
     79        });
     80        mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")();",
     81                           false);
     82 
     83        mm.sendAsyncMessage("test:content", window.document.nodePrincipal);
     84 
     85        let system = Services.scriptSecurityManager.getSystemPrincipal();
     86        mm.sendAsyncMessage("test:system", system);
     87 
     88        mm.sendAsyncMessage("test:ep", ep);
     89 
     90        let nullP = Services.scriptSecurityManager.createNullPrincipal({});
     91        mm.sendAsyncMessage("test:null", nullP);
     92      });
     93 
     94      document.body.appendChild(iframe);
     95    }
     96 
     97    addEventListener("load", function() {
     98      info("Got load event.");
     99 
    100      SpecialPowers.pushPrefEnv({
    101        "set": [
    102          ["browser.pagethumbnails.capturing_disabled", true]
    103        ]
    104      }, runTests);
    105    });
    106  </script>
    107 </body>
    108 </html>