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