Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This file tests message ports and semantics of the frameworker which aren't |
michael@0 | 6 | // directly related to the sandbox. See also browser_frameworker_sandbox.js. |
michael@0 | 7 | |
michael@0 | 8 | function makeWorkerUrl(runner) { |
michael@0 | 9 | let prefix = "http://example.com/browser/toolkit/components/social/test/browser/echo.sjs?"; |
michael@0 | 10 | if (typeof runner == "function") { |
michael@0 | 11 | runner = "var run=" + runner.toSource() + ";run();"; |
michael@0 | 12 | } |
michael@0 | 13 | return prefix + encodeURI(runner); |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | var getFrameWorkerHandle; |
michael@0 | 17 | function test() { |
michael@0 | 18 | waitForExplicitFinish(); |
michael@0 | 19 | |
michael@0 | 20 | let scope = {}; |
michael@0 | 21 | Cu.import("resource://gre/modules/FrameWorker.jsm", scope); |
michael@0 | 22 | getFrameWorkerHandle = scope.getFrameWorkerHandle; |
michael@0 | 23 | |
michael@0 | 24 | runTests(tests); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | let tests = { |
michael@0 | 28 | testSimple: function(cbnext) { |
michael@0 | 29 | let run = function() { |
michael@0 | 30 | onconnect = function(e) { |
michael@0 | 31 | let port = e.ports[0]; |
michael@0 | 32 | port.onmessage = function(e) { |
michael@0 | 33 | if (e.data.topic == "ping") { |
michael@0 | 34 | port.postMessage({topic: "pong"}); |
michael@0 | 35 | } |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSimple"); |
michael@0 | 41 | |
michael@0 | 42 | worker.port.onmessage = function(e) { |
michael@0 | 43 | if (e.data.topic == "pong") { |
michael@0 | 44 | worker.terminate(); |
michael@0 | 45 | cbnext(); |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | worker.port.postMessage({topic: "ping"}) |
michael@0 | 49 | }, |
michael@0 | 50 | |
michael@0 | 51 | // when the client closes early but the worker tries to send anyway... |
michael@0 | 52 | // XXX - disabled due to bug 919878 - we close the frameworker before the |
michael@0 | 53 | // remote browser has completed initializing, leading to failures. Given |
michael@0 | 54 | // this can realistically only happen in this synthesized test environment, |
michael@0 | 55 | // disabling just this test seems OK for now. |
michael@0 | 56 | /*** |
michael@0 | 57 | testEarlyClose: function(cbnext) { |
michael@0 | 58 | let run = function() { |
michael@0 | 59 | onconnect = function(e) { |
michael@0 | 60 | let port = e.ports[0]; |
michael@0 | 61 | port.postMessage({topic: "oh hai"}); |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testEarlyClose"); |
michael@0 | 66 | worker.port.close(); |
michael@0 | 67 | worker.terminate(); |
michael@0 | 68 | cbnext(); |
michael@0 | 69 | }, |
michael@0 | 70 | ***/ |
michael@0 | 71 | |
michael@0 | 72 | // Check we do get a social.port-closing message as the port is closed. |
michael@0 | 73 | testPortClosingMessage: function(cbnext) { |
michael@0 | 74 | // We use 2 ports - we close the first and report success via the second. |
michael@0 | 75 | let run = function() { |
michael@0 | 76 | let firstPort, secondPort; |
michael@0 | 77 | onconnect = function(e) { |
michael@0 | 78 | let port = e.ports[0]; |
michael@0 | 79 | if (firstPort === undefined) { |
michael@0 | 80 | firstPort = port; |
michael@0 | 81 | port.onmessage = function(e) { |
michael@0 | 82 | if (e.data.topic == "social.port-closing") { |
michael@0 | 83 | secondPort.postMessage({topic: "got-closing"}); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | } else { |
michael@0 | 87 | secondPort = port; |
michael@0 | 88 | // now both ports are connected we can trigger the client side |
michael@0 | 89 | // closing the first. |
michael@0 | 90 | secondPort.postMessage({topic: "connected"}); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | let workerurl = makeWorkerUrl(run); |
michael@0 | 95 | let worker1 = getFrameWorkerHandle(workerurl, undefined, "testPortClosingMessage worker1"); |
michael@0 | 96 | let worker2 = getFrameWorkerHandle(workerurl, undefined, "testPortClosingMessage worker2"); |
michael@0 | 97 | worker2.port.onmessage = function(e) { |
michael@0 | 98 | if (e.data.topic == "connected") { |
michael@0 | 99 | // both ports connected, so close the first. |
michael@0 | 100 | worker1.port.close(); |
michael@0 | 101 | } else if (e.data.topic == "got-closing") { |
michael@0 | 102 | worker2.terminate(); |
michael@0 | 103 | cbnext(); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | // Tests that prototypes added to core objects work with data sent over |
michael@0 | 109 | // the message ports. |
michael@0 | 110 | testPrototypes: function(cbnext) { |
michael@0 | 111 | let run = function() { |
michael@0 | 112 | // Modify the Array prototype... |
michael@0 | 113 | Array.prototype.customfunction = function() {}; |
michael@0 | 114 | onconnect = function(e) { |
michael@0 | 115 | let port = e.ports[0]; |
michael@0 | 116 | port.onmessage = function(e) { |
michael@0 | 117 | // Check the data we get via the port has the prototype modification |
michael@0 | 118 | if (e.data.topic == "hello" && e.data.data.customfunction) { |
michael@0 | 119 | port.postMessage({topic: "hello", data: [1,2,3]}); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | // hrmph - this kinda sucks as it is really just testing the actual |
michael@0 | 125 | // implementation rather than the end result, but is OK for now. |
michael@0 | 126 | // Really we are just testing that JSON.parse in the client window |
michael@0 | 127 | // is called. |
michael@0 | 128 | let fakeWindow = { |
michael@0 | 129 | JSON: { |
michael@0 | 130 | parse: function(s) { |
michael@0 | 131 | let data = JSON.parse(s); |
michael@0 | 132 | data.data.somextrafunction = function() {}; |
michael@0 | 133 | return data; |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), fakeWindow, "testPrototypes"); |
michael@0 | 138 | worker.port.onmessage = function(e) { |
michael@0 | 139 | if (e.data.topic == "hello") { |
michael@0 | 140 | ok(e.data.data.somextrafunction, "have someextrafunction") |
michael@0 | 141 | worker.terminate(); |
michael@0 | 142 | cbnext(); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | worker.port.postMessage({topic: "hello", data: [1,2,3]}); |
michael@0 | 146 | }, |
michael@0 | 147 | |
michael@0 | 148 | testSameOriginImport: function(cbnext) { |
michael@0 | 149 | let run = function() { |
michael@0 | 150 | onconnect = function(e) { |
michael@0 | 151 | let port = e.ports[0]; |
michael@0 | 152 | port.onmessage = function(e) { |
michael@0 | 153 | if (e.data.topic == "ping") { |
michael@0 | 154 | try { |
michael@0 | 155 | importScripts("http://mochi.test:8888/error"); |
michael@0 | 156 | } catch(ex) { |
michael@0 | 157 | port.postMessage({topic: "pong", data: ex}); |
michael@0 | 158 | return; |
michael@0 | 159 | } |
michael@0 | 160 | port.postMessage({topic: "pong", data: null}); |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSameOriginImport"); |
michael@0 | 167 | worker.port.onmessage = function(e) { |
michael@0 | 168 | if (e.data.topic == "pong") { |
michael@0 | 169 | isnot(e.data.data, null, "check same-origin applied to importScripts"); |
michael@0 | 170 | worker.terminate(); |
michael@0 | 171 | cbnext(); |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | worker.port.postMessage({topic: "ping"}) |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | testRelativeImport: function(cbnext) { |
michael@0 | 178 | let url = "https://example.com/browser/toolkit/components/social/test/browser/worker_relative.js"; |
michael@0 | 179 | let worker = getFrameWorkerHandle(url, undefined, "testSameOriginImport"); |
michael@0 | 180 | worker.port.onmessage = function(e) { |
michael@0 | 181 | if (e.data.topic == "done") { |
michael@0 | 182 | is(e.data.result, "ok", "check relative url in importScripts"); |
michael@0 | 183 | worker.terminate(); |
michael@0 | 184 | cbnext(); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | }, |
michael@0 | 188 | |
michael@0 | 189 | testNavigator: function(cbnext) { |
michael@0 | 190 | let run = function() { |
michael@0 | 191 | let port; |
michael@0 | 192 | ononline = function() { |
michael@0 | 193 | port.postMessage({topic: "ononline", data: navigator.onLine}); |
michael@0 | 194 | } |
michael@0 | 195 | onoffline = function() { |
michael@0 | 196 | port.postMessage({topic: "onoffline", data: navigator.onLine}); |
michael@0 | 197 | } |
michael@0 | 198 | onconnect = function(e) { |
michael@0 | 199 | port = e.ports[0]; |
michael@0 | 200 | port.postMessage({topic: "ready", |
michael@0 | 201 | data: { |
michael@0 | 202 | appName: navigator.appName, |
michael@0 | 203 | appVersion: navigator.appVersion, |
michael@0 | 204 | platform: navigator.platform, |
michael@0 | 205 | userAgent: navigator.userAgent, |
michael@0 | 206 | } |
michael@0 | 207 | }); |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | let ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService2); |
michael@0 | 211 | let oldManage = ioService.manageOfflineStatus; |
michael@0 | 212 | let oldOffline = ioService.offline; |
michael@0 | 213 | |
michael@0 | 214 | ioService.manageOfflineStatus = false; |
michael@0 | 215 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testNavigator"); |
michael@0 | 216 | let expected_topic = "onoffline"; |
michael@0 | 217 | let expected_data = false; |
michael@0 | 218 | worker.port.onmessage = function(e) { |
michael@0 | 219 | is(e.data.topic, "ready"); |
michael@0 | 220 | for each (let attr in ['appName', 'appVersion', 'platform', 'userAgent']) { |
michael@0 | 221 | // each attribute must be a string with length > 0. |
michael@0 | 222 | is(typeof e.data.data[attr], "string"); |
michael@0 | 223 | ok(e.data.data[attr].length > 0); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | worker.port.onmessage = function(e) { |
michael@0 | 227 | // a handler specifically for the offline notification. |
michael@0 | 228 | is(e.data.topic, "onoffline"); |
michael@0 | 229 | is(e.data.data, false); |
michael@0 | 230 | |
michael@0 | 231 | // add another handler specifically for the 'online' case. |
michael@0 | 232 | worker.port.onmessage = function(e) { |
michael@0 | 233 | is(e.data.topic, "ononline"); |
michael@0 | 234 | is(e.data.data, true); |
michael@0 | 235 | // all good! |
michael@0 | 236 | ioService.manageOfflineStatus = oldManage; |
michael@0 | 237 | ioService.offline = oldOffline; |
michael@0 | 238 | worker.terminate(); |
michael@0 | 239 | cbnext(); |
michael@0 | 240 | } |
michael@0 | 241 | ioService.offline = false; |
michael@0 | 242 | } |
michael@0 | 243 | ioService.offline = true; |
michael@0 | 244 | } |
michael@0 | 245 | }, |
michael@0 | 246 | |
michael@0 | 247 | testMissingWorker: function(cbnext) { |
michael@0 | 248 | // don't ever create this file! We want a 404. |
michael@0 | 249 | let url = "https://example.com/browser/toolkit/components/social/test/browser/worker_is_missing.js"; |
michael@0 | 250 | let worker = getFrameWorkerHandle(url, undefined, "testMissingWorker"); |
michael@0 | 251 | Services.obs.addObserver(function handleError(subj, topic, data) { |
michael@0 | 252 | Services.obs.removeObserver(handleError, "social:frameworker-error"); |
michael@0 | 253 | is(data, worker._worker.origin, "social:frameworker-error was handled"); |
michael@0 | 254 | worker.terminate(); |
michael@0 | 255 | cbnext(); |
michael@0 | 256 | }, 'social:frameworker-error', false); |
michael@0 | 257 | worker.port.onmessage = function(e) { |
michael@0 | 258 | ok(false, "social:frameworker-error was handled"); |
michael@0 | 259 | cbnext(); |
michael@0 | 260 | } |
michael@0 | 261 | }, |
michael@0 | 262 | |
michael@0 | 263 | testNoConnectWorker: function(cbnext) { |
michael@0 | 264 | let worker = getFrameWorkerHandle(makeWorkerUrl(function () {}), |
michael@0 | 265 | undefined, "testNoConnectWorker"); |
michael@0 | 266 | Services.obs.addObserver(function handleError(subj, topic, data) { |
michael@0 | 267 | Services.obs.removeObserver(handleError, "social:frameworker-error"); |
michael@0 | 268 | is(data, worker._worker.origin, "social:frameworker-error was handled"); |
michael@0 | 269 | worker.terminate(); |
michael@0 | 270 | cbnext(); |
michael@0 | 271 | }, 'social:frameworker-error', false); |
michael@0 | 272 | worker.port.onmessage = function(e) { |
michael@0 | 273 | ok(false, "social:frameworker-error was handled"); |
michael@0 | 274 | cbnext(); |
michael@0 | 275 | } |
michael@0 | 276 | }, |
michael@0 | 277 | |
michael@0 | 278 | testEmptyWorker: function(cbnext) { |
michael@0 | 279 | let worker = getFrameWorkerHandle(makeWorkerUrl(''), |
michael@0 | 280 | undefined, "testEmptyWorker"); |
michael@0 | 281 | Services.obs.addObserver(function handleError(subj, topic, data) { |
michael@0 | 282 | Services.obs.removeObserver(handleError, "social:frameworker-error"); |
michael@0 | 283 | is(data, worker._worker.origin, "social:frameworker-error was handled"); |
michael@0 | 284 | worker.terminate(); |
michael@0 | 285 | cbnext(); |
michael@0 | 286 | }, 'social:frameworker-error', false); |
michael@0 | 287 | worker.port.onmessage = function(e) { |
michael@0 | 288 | ok(false, "social:frameworker-error was handled"); |
michael@0 | 289 | cbnext(); |
michael@0 | 290 | } |
michael@0 | 291 | }, |
michael@0 | 292 | |
michael@0 | 293 | testWorkerConnectError: function(cbnext) { |
michael@0 | 294 | let run = function () { |
michael@0 | 295 | onconnect = function(e) { |
michael@0 | 296 | throw new Error("worker failure"); |
michael@0 | 297 | } |
michael@0 | 298 | } |
michael@0 | 299 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), |
michael@0 | 300 | undefined, "testWorkerConnectError"); |
michael@0 | 301 | Services.obs.addObserver(function handleError(subj, topic, data) { |
michael@0 | 302 | Services.obs.removeObserver(handleError, "social:frameworker-error"); |
michael@0 | 303 | is(data, worker._worker.origin, "social:frameworker-error was handled"); |
michael@0 | 304 | worker.terminate(); |
michael@0 | 305 | cbnext(); |
michael@0 | 306 | }, 'social:frameworker-error', false); |
michael@0 | 307 | worker.port.onmessage = function(e) { |
michael@0 | 308 | ok(false, "social:frameworker-error was handled"); |
michael@0 | 309 | cbnext(); |
michael@0 | 310 | } |
michael@0 | 311 | }, |
michael@0 | 312 | |
michael@0 | 313 | // This will create the worker, then send a message to the port, then close |
michael@0 | 314 | // the port - all before the worker has actually initialized. |
michael@0 | 315 | testCloseFirstSend: function(cbnext) { |
michael@0 | 316 | let run = function() { |
michael@0 | 317 | let numPings = 0, numCloses = 0; |
michael@0 | 318 | onconnect = function(e) { |
michael@0 | 319 | let port = e.ports[0]; |
michael@0 | 320 | port.onmessage = function(e) { |
michael@0 | 321 | if (e.data.topic == "ping") { |
michael@0 | 322 | numPings += 1; |
michael@0 | 323 | } else if (e.data.topic == "social.port-closing") { |
michael@0 | 324 | numCloses += 1; |
michael@0 | 325 | } else if (e.data.topic == "get-counts") { |
michael@0 | 326 | port.postMessage({topic: "result", |
michael@0 | 327 | result: {ping: numPings, close: numCloses}}); |
michael@0 | 328 | } |
michael@0 | 329 | } |
michael@0 | 330 | } |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose"); |
michael@0 | 334 | worker.port.postMessage({topic: "ping"}); |
michael@0 | 335 | worker.port.close(); |
michael@0 | 336 | let newPort = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose").port; |
michael@0 | 337 | newPort.onmessage = function(e) { |
michael@0 | 338 | if (e.data.topic == "result") { |
michael@0 | 339 | is(e.data.result.ping, 1, "the worker got the ping"); |
michael@0 | 340 | is(e.data.result.close, 1, "the worker got 1 close message"); |
michael@0 | 341 | worker.terminate(); |
michael@0 | 342 | cbnext(); |
michael@0 | 343 | } |
michael@0 | 344 | } |
michael@0 | 345 | newPort.postMessage({topic: "get-counts"}); |
michael@0 | 346 | }, |
michael@0 | 347 | |
michael@0 | 348 | // Like testCloseFirstSend, although in this test the worker has already |
michael@0 | 349 | // initialized (so the "connect pending ports" part of the worker isn't |
michael@0 | 350 | // what needs to handle this case.) |
michael@0 | 351 | testCloseAfterInit: function(cbnext) { |
michael@0 | 352 | let run = function() { |
michael@0 | 353 | let numPings = 0, numCloses = 0; |
michael@0 | 354 | onconnect = function(e) { |
michael@0 | 355 | let port = e.ports[0]; |
michael@0 | 356 | port.onmessage = function(e) { |
michael@0 | 357 | if (e.data.topic == "ping") { |
michael@0 | 358 | numPings += 1; |
michael@0 | 359 | } else if (e.data.topic == "social.port-closing") { |
michael@0 | 360 | numCloses += 1; |
michael@0 | 361 | } else if (e.data.topic == "get-counts") { |
michael@0 | 362 | port.postMessage({topic: "result", |
michael@0 | 363 | result: {ping: numPings, close: numCloses}}); |
michael@0 | 364 | } else if (e.data.topic == "get-ready") { |
michael@0 | 365 | port.postMessage({topic: "ready"}); |
michael@0 | 366 | } |
michael@0 | 367 | } |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose"); |
michael@0 | 372 | worker.port.onmessage = function(e) { |
michael@0 | 373 | if (e.data.topic == "ready") { |
michael@0 | 374 | let newPort = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testSendAndClose").port; |
michael@0 | 375 | newPort.postMessage({topic: "ping"}); |
michael@0 | 376 | newPort.close(); |
michael@0 | 377 | worker.port.postMessage({topic: "get-counts"}); |
michael@0 | 378 | } else if (e.data.topic == "result") { |
michael@0 | 379 | is(e.data.result.ping, 1, "the worker got the ping"); |
michael@0 | 380 | is(e.data.result.close, 1, "the worker got 1 close message"); |
michael@0 | 381 | worker.terminate(); |
michael@0 | 382 | cbnext(); |
michael@0 | 383 | } |
michael@0 | 384 | } |
michael@0 | 385 | worker.port.postMessage({topic: "get-ready"}); |
michael@0 | 386 | }, |
michael@0 | 387 | } |