toolkit/components/social/test/browser/browser_frameworker_sandbox.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // This file tests features made available to the frameworker via the sandbox.
     6 // For other frameworker tests, see browser_frameworker.js
     8 function makeWorkerUrl(runner) {
     9   let prefix =  "http://example.com/browser/toolkit/components/social/test/browser/echo.sjs?";
    10   if (typeof runner == "function") {
    11     runner = "var run=" + runner.toSource() + ";run();";
    12   }
    13   return prefix + encodeURI(runner);
    14 }
    16 var getFrameWorkerHandle;
    17 function test() {
    18   waitForExplicitFinish();
    20   let scope = {};
    21   Cu.import("resource://gre/modules/FrameWorker.jsm", scope);
    22   getFrameWorkerHandle = scope.getFrameWorkerHandle;
    24   runTests(tests);
    25 }
    27 let tests = {
    28   testArrayUsingBuffer: function(cbnext) {
    29     let run = function() {
    30       onconnect = function(e) {
    31         let port = e.ports[0];
    32         port.onmessage = function(e) {
    33           if (e.data.topic == "go") {
    34             let buffer = new ArrayBuffer(10);
    35             // this one has always worked in the past, but worth checking anyway...
    36             if (new Uint8Array(buffer).length != 10) {
    37               port.postMessage({topic: "result", reason: "first length was not 10"});
    38               return;
    39             }
    40             let reader = new FileReader();
    41             reader.onload = function(event) {
    42               if (new Uint8Array(buffer).length != 10) {
    43                 port.postMessage({topic: "result", reason: "length in onload handler was not 10"});
    44                 return;
    45               }
    46               // all seems good!
    47               port.postMessage({topic: "result", reason: "ok"});
    48             }
    49             let blob = new Blob([buffer], {type: "binary"});
    50             reader.readAsArrayBuffer(blob);
    51           }
    52         }
    53       }
    54     }
    55     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testArray");
    56     worker.port.onmessage = function(e) {
    57       if (e.data.topic == "result") {
    58         is(e.data.reason, "ok", "check the array worked");
    59         worker.terminate();
    60         cbnext();
    61       }
    62     }
    63     worker.port.postMessage({topic: "go"});
    64   },
    66   testArrayUsingReader: function(cbnext) {
    67     let run = function() {
    68       onconnect = function(e) {
    69         let port = e.ports[0];
    70         port.onmessage = function(e) {
    71           if (e.data.topic == "go") {
    72             let buffer = new ArrayBuffer(10);
    73             let reader = new FileReader();
    74             reader.onload = function(event) {
    75               try {
    76                 if (new Uint8Array(reader.result).length != 10) {
    77                   port.postMessage({topic: "result", reason: "length in onload handler was not 10"});
    78                   return;
    79                 }
    80                 // all seems good!
    81                 port.postMessage({topic: "result", reason: "ok"});
    82               } catch (ex) {
    83                 port.postMessage({topic: "result", reason: ex.toString()});
    84               }
    85             }
    86             let blob = new Blob([buffer], {type: "binary"});
    87             reader.readAsArrayBuffer(blob);
    88           }
    89         }
    90       }
    91     }
    92     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testArray");
    93     worker.port.onmessage = function(e) {
    94       if (e.data.topic == "result") {
    95         is(e.data.reason, "ok", "check the array worked");
    96         worker.terminate();
    97         cbnext();
    98       }
    99     }
   100     worker.port.postMessage({topic: "go"});
   101   },
   103   testXHR: function(cbnext) {
   104     // NOTE: this url MUST be in the same origin as worker_xhr.js fetches from!
   105     let url = "https://example.com/browser/toolkit/components/social/test/browser/worker_xhr.js";
   106     let worker = getFrameWorkerHandle(url, undefined, "testXHR");
   107     worker.port.onmessage = function(e) {
   108       if (e.data.topic == "done") {
   109         is(e.data.result, "ok", "check the xhr test worked");
   110         worker.terminate();
   111         cbnext();
   112       }
   113     }
   114   },
   116   testLocalStorage: function(cbnext) {
   117     let run = function() {
   118       onconnect = function(e) {
   119         let port = e.ports[0];
   120         try {
   121           localStorage.setItem("foo", "1");
   122         } catch(e) {
   123           port.postMessage({topic: "done", result: "FAILED to set localStorage, " + e.toString() });
   124           return;
   125         }
   127         var ok;
   128         try {
   129           ok = localStorage["foo"] == 1;
   130         } catch (e) {
   131           port.postMessage({topic: "done", result: "FAILED to read localStorage, " + e.toString() });
   132           return;
   133         }
   134         port.postMessage({topic: "done", result: "ok"});
   135       }
   136     }
   137     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testLocalStorage", null, true);
   138     worker.port.onmessage = function(e) {
   139       if (e.data.topic == "done") {
   140         is(e.data.result, "ok", "check the localStorage test worked");
   141         worker.terminate();
   142         cbnext();
   143       }
   144     }
   145   },
   147   testNoLocalStorage: function(cbnext) {
   148     let run = function() {
   149       onconnect = function(e) {
   150         let port = e.ports[0];
   151         try {
   152           localStorage.setItem("foo", "1");
   153         } catch(e) {
   154           port.postMessage({topic: "done", result: "ok"});
   155           return;
   156         }
   158         port.postMessage({topic: "done", result: "FAILED because localStorage was exposed" });
   159       }
   160     }
   161     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testNoLocalStorage");
   162     worker.port.onmessage = function(e) {
   163       if (e.data.topic == "done") {
   164         is(e.data.result, "ok", "check that retrieving localStorage fails by default");
   165         worker.terminate();
   166         cbnext();
   167       }
   168     }
   169   },
   171   testBase64: function (cbnext) {
   172     let run = function() {
   173       onconnect = function(e) {
   174         let port = e.ports[0];
   175         var ok = false;
   176         try {
   177           ok = btoa("1234") == "MTIzNA==";
   178         } catch(e) {
   179           port.postMessage({topic: "done", result: "FAILED to call btoa, " + e.toString() });
   180           return;
   181         }
   182         if (!ok) {
   183           port.postMessage({topic: "done", result: "FAILED calling btoa"});
   184           return;
   185         }
   187         try {
   188           ok = atob("NDMyMQ==") == "4321";
   189         } catch (e) {
   190           port.postMessage({topic: "done", result: "FAILED to call atob, " + e.toString() });
   191           return;
   192         }
   193         if (!ok) {
   194           port.postMessage({topic: "done", result: "FAILED calling atob"});
   195           return;
   196         }
   198         port.postMessage({topic: "done", result: "ok"});
   199       }
   200     }
   201     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testBase64");
   202     worker.port.onmessage = function(e) {
   203       if (e.data.topic == "done") {
   204         is(e.data.result, "ok", "check the atob/btoa test worked");
   205         worker.terminate();
   206         cbnext();
   207       }
   208     }
   209   },
   211   testTimeouts: function (cbnext) {
   212     let run = function() {
   213       onconnect = function(e) {
   214         let port = e.ports[0];
   216         var timeout;
   217         try {
   218           timeout = setTimeout(function () {
   219             port.postMessage({topic: "done", result: "FAILED cancelled timeout was called"});
   220           }, 100);
   221         } catch (ex) {
   222           port.postMessage({topic: "done", result: "FAILED calling setTimeout: " + ex});
   223           return;
   224         }
   226         try {
   227           clearTimeout(timeout);
   228         } catch (ex) {
   229           port.postMessage({topic: "done", result: "FAILED calling clearTimeout: " + ex});
   230           return;
   231         }
   233         var counter = 0;
   234         try {
   235           timeout = setInterval(function () {
   236             if (++counter == 2) {
   237               clearInterval(timeout);
   238               setTimeout(function () {
   239                 port.postMessage({topic: "done", result: "ok"});
   240                 return;
   241               }, 0);
   242             }
   243           }, 100);
   244         } catch (ex) {
   245           port.postMessage({topic: "done", result: "FAILED calling setInterval: " + ex});
   246           return;
   247         }
   248       }
   249     }
   250     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testTimeouts");
   251     worker.port.onmessage = function(e) {
   252       if (e.data.topic == "done") {
   253         is(e.data.result, "ok", "check that timeouts worked");
   254         worker.terminate();
   255         cbnext();
   256       }
   257     }
   258   },
   260   testWebSocket: function (cbnext) {
   261     let run = function() {
   262       onconnect = function(e) {
   263         let port = e.ports[0];
   265         try {
   266           var exampleSocket = new WebSocket("ws://mochi.test:8888/socketserver");
   267         } catch (e) {
   268           port.postMessage({topic: "done", result: "FAILED calling WebSocket constructor: " + e});
   269           return;
   270         }
   272         port.postMessage({topic: "done", result: "ok"});
   273       }
   274     }
   275     let worker = getFrameWorkerHandle(makeWorkerUrl(run), undefined, "testWebSocket");
   276     worker.port.onmessage = function(e) {
   277       if (e.data.topic == "done") {
   278         is(e.data.result, "ok", "check that websockets worked");
   279         worker.terminate();
   280         cbnext();
   281       }
   282     }
   283   },
   285   testEventSource: function(cbnext) {
   286     let worker = getFrameWorkerHandle("https://example.com/browser/toolkit/components/social/test/browser/worker_eventsource.js", undefined, "testEventSource");
   287     worker.port.onmessage = function(e) {
   288       let m = e.data;
   289       if (m.topic == "eventSourceTest") {
   290         if (m.result.ok != undefined)
   291           ok(m.result.ok, e.data.result.msg);
   292         if (m.result.is != undefined)
   293           is(m.result.is, m.result.match, m.result.msg);
   294         if (m.result.info != undefined)
   295           info(m.result.info);
   296       } else if (e.data.topic == "pong") {
   297         worker.terminate();
   298         cbnext();
   299       }
   300     }
   301     worker.port.postMessage({topic: "ping"})
   302   },
   304   testIndexedDB: function(cbnext) {
   305     let worker = getFrameWorkerHandle("https://example.com/browser/toolkit/components/social/test/browser/worker_social.js", undefined, "testIndexedDB");
   306     worker.port.onmessage = function(e) {
   307       let m = e.data;
   308       if (m.topic == "social.indexeddb-result") {
   309         is(m.data.result, "ok", "created indexeddb");
   310         worker.terminate();
   311         cbnext();
   312       }
   313     }
   314     worker.port.postMessage({topic: "test-indexeddb-create"})
   315   },
   317   testSubworker: function(cbnext) {
   318     // the main "frameworker"...
   319     let mainworker = function() {
   320       onconnect = function(e) {
   321         let port = e.ports[0];
   322         port.onmessage = function(e) {
   323           if (e.data.topic == "go") {
   324             let suburl = e.data.data;
   325             let worker = new Worker(suburl);
   326             worker.onmessage = function(sube) {
   327               port.postMessage({topic: "sub-message", data: sube.data});
   328             }
   329           }
   330         }
   331       }
   332     }
   334     // The "subworker" that is actually a real, bona-fide worker.
   335     let subworker = function() {
   336       postMessage("hello");
   337     }
   338     let worker = getFrameWorkerHandle(makeWorkerUrl(mainworker), undefined, "testSubWorker");
   339     worker.port.onmessage = function(e) {
   340       if (e.data.topic == "sub-message" && e.data.data == "hello") {
   341         worker.terminate();
   342         cbnext();
   343       }
   344     }
   345     worker.port.postMessage({topic: "go", data: makeWorkerUrl(subworker)});
   346   }
   347 }

mercurial