content/base/test/test_messagemanager_assertpermission.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_messagemanager_assertpermission.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test for the nsIProcessChecker part of Message Managers</title>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body onload="runTests();">
    1.13 +<p id="display">
    1.14 +</p>
    1.15 +<div id="content" style="display: none">
    1.16 +  
    1.17 +</div>
    1.18 +<pre id="test">
    1.19 +<script class="testbody" type="application/javascript;version=1.8">
    1.20 +
    1.21 +const APP_URL = "http://example.org";
    1.22 +const APP_MANIFEST = "http://example.org/manifest.webapp";
    1.23 +const CHILD_PROCESS_SHUTDOWN_MESSAGE = "child-process-shutdown";
    1.24 +
    1.25 +let ppmm = SpecialPowers.Cc["@mozilla.org/parentprocessmessagemanager;1"]
    1.26 +                        .getService(SpecialPowers.Ci.nsIMessageBroadcaster);
    1.27 +let cpmm = SpecialPowers.Cc["@mozilla.org/childprocessmessagemanager;1"]
    1.28 +                        .getService(SpecialPowers.Ci.nsISyncMessageSender);
    1.29 +let gAppsService = SpecialPowers.Cc["@mozilla.org/AppsService;1"]
    1.30 +                     .getService(SpecialPowers.Ci.nsIAppsService);
    1.31 +
    1.32 +function setUp() {
    1.33 +  SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
    1.34 +  SpecialPowers.setBoolPref("dom.ipc.browser_frames.oop_by_default", true);
    1.35 +  SpecialPowers.addPermission("browser", true, window.document);
    1.36 +  SpecialPowers.addPermission("embed-apps", true, window.document);
    1.37 +
    1.38 +  let appId = gAppsService.getAppLocalIdByManifestURL(APP_MANIFEST);
    1.39 +  SpecialPowers.addPermission("foobar", true, { url: APP_URL,
    1.40 +                                                appId: appId,
    1.41 +                                                isInBrowserElement: false });
    1.42 +  runNextTest();
    1.43 +}
    1.44 +
    1.45 +/**
    1.46 + * Load the example.org app in an <iframe mozbrowser mozapp>
    1.47 + */
    1.48 +function loadApp(callback) {
    1.49 +  let iframe = document.createElement("iframe");
    1.50 +  iframe.setAttribute("mozapp", APP_MANIFEST);
    1.51 +  SpecialPowers.wrap(iframe).mozbrowser = true;
    1.52 +  iframe.src = APP_URL;
    1.53 +  document.getElementById("content").appendChild(iframe);
    1.54 +
    1.55 +  iframe.addEventListener("mozbrowserloadend", function onloadend() {
    1.56 +    iframe.removeEventListener("mozbrowserloadend", onloadend);
    1.57 +    callback(iframe);
    1.58 +  });
    1.59 +}
    1.60 +
    1.61 +/**
    1.62 + * Prepare the child process for an intentional crash. This is to keep
    1.63 + * the leak automation tools happy.
    1.64 + *
    1.65 + * This also allows us to acquire the process message manaager that
    1.66 + * corresponds to the process by sending a message to a frame script
    1.67 + * in the content process and having it reply to us via the child
    1.68 + * process message manager.
    1.69 + */
    1.70 +function prepareProcess(frameMM, callback) {
    1.71 +  let frameScript = 'data:,\
    1.72 +    privateNoteIntentionalCrash();\
    1.73 +    var cpmm = Components.classes["@mozilla.org/childprocessmessagemanager;1"]\
    1.74 +                         .getService(Components.interfaces.nsISyncMessageSender);\
    1.75 +    addMessageListener("TestChild:Ohai", function receiveMessage(msg) {\
    1.76 +      cpmm.sendAsyncMessage("TestChild:Ohai");\
    1.77 +    });';
    1.78 +  frameMM.loadFrameScript(frameScript, false);
    1.79 +  frameMM.sendAsyncMessage("TestChild:Ohai");
    1.80 +  ppmm.addMessageListener("TestChild:Ohai", function receiveMessage(msg) {
    1.81 +    ppmm.removeMessageListener("TestChild:Ohai", receiveMessage);
    1.82 +    msg = SpecialPowers.wrap(msg);
    1.83 +    callback(msg.target);
    1.84 +  });
    1.85 +}
    1.86 +
    1.87 +/**
    1.88 + * Expects an OOP frame's process to shut down and report three
    1.89 + * events/messages: an error event on the browser element, and a
    1.90 + * 'child-process-shutdown' message on both the frame and process
    1.91 + * message managers.
    1.92 + */
    1.93 +function expectFrameProcessShutdown(iframe, frameMM, processMM, callback) {
    1.94 +  let msgCount = 0;
    1.95 +  function countMessage() {
    1.96 +    msgCount += 1;
    1.97 +    if (msgCount == 3) {
    1.98 +      ok(true, "Observed all three expected events.");
    1.99 +      callback();
   1.100 +    }
   1.101 +  };
   1.102 +
   1.103 +  iframe.addEventListener("mozbrowsererror", function onerror(event) {
   1.104 +    iframe.removeEventListener("mozbrowsererror", onerror);
   1.105 +    is(event.detail.type, "fatal", "Observed expected event.");
   1.106 +    countMessage();
   1.107 +  });
   1.108 +
   1.109 +  processMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
   1.110 +    processMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
   1.111 +    ok(true, "Received 'child-process-shutdown' message from process message manager.");
   1.112 +    countMessage();
   1.113 +  });
   1.114 +
   1.115 +  frameMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
   1.116 +    frameMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
   1.117 +    ok(true, "Received 'child-process-shutdown' message from frame message manager.");
   1.118 +    countMessage();
   1.119 +  });
   1.120 +}
   1.121 +
   1.122 +function testSameProcess() {
   1.123 +  // Assert permissions on the in-process child process message manager.
   1.124 +  // It always has all permissions, including ones that were never
   1.125 +  // assigned to anybody.
   1.126 +
   1.127 +  cpmm.sendAsyncMessage("TestPermission:InProcess");
   1.128 +  ppmm.addMessageListener("TestPermission:InProcess", function receiveMessage(msg) {
   1.129 +    ppmm.removeMessageListener("TestPermission:InProcess", receiveMessage);
   1.130 +    msg = SpecialPowers.wrap(msg);
   1.131 +
   1.132 +    ok(msg.target.assertPermission("frobnaz"), "in-process cpmm always has all capabilities");
   1.133 +    runNextTest();
   1.134 +  });
   1.135 +}
   1.136 +
   1.137 +function testFrameMessageManager() {
   1.138 +  // Assert permissions on the frame message manager.
   1.139 +
   1.140 +  loadApp(function (iframe) {
   1.141 +    let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
   1.142 +    prepareProcess(frameMM, function (processMM) {
   1.143 +      ok(frameMM.assertPermission("foobar"),
   1.144 +         "Frame mm has assigned permission.");
   1.145 +      ok(!frameMM.assertPermission("frobnaz"),
   1.146 +         "Frame mm doesn't have non-existing permission.");
   1.147 +      expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
   1.148 +        iframe.parentNode.removeChild(iframe);
   1.149 +        runNextTest();
   1.150 +      });
   1.151 +    });
   1.152 +  });
   1.153 +}
   1.154 +
   1.155 +function testChildProcessMessageManager() {
   1.156 +  // Assert permissions on the child process message manager.
   1.157 +
   1.158 +  loadApp(function (iframe) {
   1.159 +    let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
   1.160 +    prepareProcess(frameMM, function (processMM) {
   1.161 +      ok(processMM.assertPermission("foobar"),
   1.162 +         "Process mm has assigned permission.");
   1.163 +      ok(!processMM.assertPermission("frobnaz"),
   1.164 +         "Process mm doesn't have non-existing permission.");
   1.165 +      expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
   1.166 +        iframe.parentNode.removeChild(iframe);
   1.167 +        runNextTest();
   1.168 +      });
   1.169 +    });
   1.170 +  });
   1.171 +}
   1.172 +
   1.173 +function tearDown() {
   1.174 +  SpecialPowers.clearUserPref("dom.mozBrowserFramesEnabled");
   1.175 +  SpecialPowers.clearUserPref("dom.ipc.browser_frames.oop_by_default");
   1.176 +  SimpleTest.finish();
   1.177 +}
   1.178 +
   1.179 +let _tests = [
   1.180 +  setUp,
   1.181 +  testSameProcess,
   1.182 +  testFrameMessageManager,
   1.183 +  testChildProcessMessageManager,
   1.184 +  tearDown
   1.185 +]
   1.186 +function runNextTest() {
   1.187 +  SimpleTest.executeSoon(_tests.shift());
   1.188 +}
   1.189 +
   1.190 +function runTests() {
   1.191 +  SimpleTest.waitForExplicitFinish();
   1.192 +  runNextTest();
   1.193 +}
   1.194 +
   1.195 +</script>
   1.196 +</pre>
   1.197 +</body>
   1.198 +</html>

mercurial