content/base/test/test_messagemanager_assertpermission.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4   <meta charset="utf-8">
     5   <title>Test for the nsIProcessChecker part of Message Managers</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">
    14 </div>
    15 <pre id="test">
    16 <script class="testbody" type="application/javascript;version=1.8">
    18 const APP_URL = "http://example.org";
    19 const APP_MANIFEST = "http://example.org/manifest.webapp";
    20 const CHILD_PROCESS_SHUTDOWN_MESSAGE = "child-process-shutdown";
    22 let ppmm = SpecialPowers.Cc["@mozilla.org/parentprocessmessagemanager;1"]
    23                         .getService(SpecialPowers.Ci.nsIMessageBroadcaster);
    24 let cpmm = SpecialPowers.Cc["@mozilla.org/childprocessmessagemanager;1"]
    25                         .getService(SpecialPowers.Ci.nsISyncMessageSender);
    26 let gAppsService = SpecialPowers.Cc["@mozilla.org/AppsService;1"]
    27                      .getService(SpecialPowers.Ci.nsIAppsService);
    29 function setUp() {
    30   SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
    31   SpecialPowers.setBoolPref("dom.ipc.browser_frames.oop_by_default", true);
    32   SpecialPowers.addPermission("browser", true, window.document);
    33   SpecialPowers.addPermission("embed-apps", true, window.document);
    35   let appId = gAppsService.getAppLocalIdByManifestURL(APP_MANIFEST);
    36   SpecialPowers.addPermission("foobar", true, { url: APP_URL,
    37                                                 appId: appId,
    38                                                 isInBrowserElement: false });
    39   runNextTest();
    40 }
    42 /**
    43  * Load the example.org app in an <iframe mozbrowser mozapp>
    44  */
    45 function loadApp(callback) {
    46   let iframe = document.createElement("iframe");
    47   iframe.setAttribute("mozapp", APP_MANIFEST);
    48   SpecialPowers.wrap(iframe).mozbrowser = true;
    49   iframe.src = APP_URL;
    50   document.getElementById("content").appendChild(iframe);
    52   iframe.addEventListener("mozbrowserloadend", function onloadend() {
    53     iframe.removeEventListener("mozbrowserloadend", onloadend);
    54     callback(iframe);
    55   });
    56 }
    58 /**
    59  * Prepare the child process for an intentional crash. This is to keep
    60  * the leak automation tools happy.
    61  *
    62  * This also allows us to acquire the process message manaager that
    63  * corresponds to the process by sending a message to a frame script
    64  * in the content process and having it reply to us via the child
    65  * process message manager.
    66  */
    67 function prepareProcess(frameMM, callback) {
    68   let frameScript = 'data:,\
    69     privateNoteIntentionalCrash();\
    70     var cpmm = Components.classes["@mozilla.org/childprocessmessagemanager;1"]\
    71                          .getService(Components.interfaces.nsISyncMessageSender);\
    72     addMessageListener("TestChild:Ohai", function receiveMessage(msg) {\
    73       cpmm.sendAsyncMessage("TestChild:Ohai");\
    74     });';
    75   frameMM.loadFrameScript(frameScript, false);
    76   frameMM.sendAsyncMessage("TestChild:Ohai");
    77   ppmm.addMessageListener("TestChild:Ohai", function receiveMessage(msg) {
    78     ppmm.removeMessageListener("TestChild:Ohai", receiveMessage);
    79     msg = SpecialPowers.wrap(msg);
    80     callback(msg.target);
    81   });
    82 }
    84 /**
    85  * Expects an OOP frame's process to shut down and report three
    86  * events/messages: an error event on the browser element, and a
    87  * 'child-process-shutdown' message on both the frame and process
    88  * message managers.
    89  */
    90 function expectFrameProcessShutdown(iframe, frameMM, processMM, callback) {
    91   let msgCount = 0;
    92   function countMessage() {
    93     msgCount += 1;
    94     if (msgCount == 3) {
    95       ok(true, "Observed all three expected events.");
    96       callback();
    97     }
    98   };
   100   iframe.addEventListener("mozbrowsererror", function onerror(event) {
   101     iframe.removeEventListener("mozbrowsererror", onerror);
   102     is(event.detail.type, "fatal", "Observed expected event.");
   103     countMessage();
   104   });
   106   processMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
   107     processMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
   108     ok(true, "Received 'child-process-shutdown' message from process message manager.");
   109     countMessage();
   110   });
   112   frameMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
   113     frameMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
   114     ok(true, "Received 'child-process-shutdown' message from frame message manager.");
   115     countMessage();
   116   });
   117 }
   119 function testSameProcess() {
   120   // Assert permissions on the in-process child process message manager.
   121   // It always has all permissions, including ones that were never
   122   // assigned to anybody.
   124   cpmm.sendAsyncMessage("TestPermission:InProcess");
   125   ppmm.addMessageListener("TestPermission:InProcess", function receiveMessage(msg) {
   126     ppmm.removeMessageListener("TestPermission:InProcess", receiveMessage);
   127     msg = SpecialPowers.wrap(msg);
   129     ok(msg.target.assertPermission("frobnaz"), "in-process cpmm always has all capabilities");
   130     runNextTest();
   131   });
   132 }
   134 function testFrameMessageManager() {
   135   // Assert permissions on the frame message manager.
   137   loadApp(function (iframe) {
   138     let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
   139     prepareProcess(frameMM, function (processMM) {
   140       ok(frameMM.assertPermission("foobar"),
   141          "Frame mm has assigned permission.");
   142       ok(!frameMM.assertPermission("frobnaz"),
   143          "Frame mm doesn't have non-existing permission.");
   144       expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
   145         iframe.parentNode.removeChild(iframe);
   146         runNextTest();
   147       });
   148     });
   149   });
   150 }
   152 function testChildProcessMessageManager() {
   153   // Assert permissions on the child process message manager.
   155   loadApp(function (iframe) {
   156     let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
   157     prepareProcess(frameMM, function (processMM) {
   158       ok(processMM.assertPermission("foobar"),
   159          "Process mm has assigned permission.");
   160       ok(!processMM.assertPermission("frobnaz"),
   161          "Process mm doesn't have non-existing permission.");
   162       expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
   163         iframe.parentNode.removeChild(iframe);
   164         runNextTest();
   165       });
   166     });
   167   });
   168 }
   170 function tearDown() {
   171   SpecialPowers.clearUserPref("dom.mozBrowserFramesEnabled");
   172   SpecialPowers.clearUserPref("dom.ipc.browser_frames.oop_by_default");
   173   SimpleTest.finish();
   174 }
   176 let _tests = [
   177   setUp,
   178   testSameProcess,
   179   testFrameMessageManager,
   180   testChildProcessMessageManager,
   181   tearDown
   182 ]
   183 function runNextTest() {
   184   SimpleTest.executeSoon(_tests.shift());
   185 }
   187 function runTests() {
   188   SimpleTest.waitForExplicitFinish();
   189   runNextTest();
   190 }
   192 </script>
   193 </pre>
   194 </body>
   195 </html>

mercurial