tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

postMessage_MessagePorts_xsite.sub.window.js (2198B)


      1 // META: script=/common/get-host-info.sub.js
      2 
      3 async_test(function(t) {
      4    var host = get_host_info();
      5    var noteSameSiteURL = host.HTTP_NOTSAMESITE_ORIGIN + "/webmessaging/support/ChildWindowPostMessage.htm";
      6    var TOTALPORTS = 100;
      7    var LocalPorts = [];
      8    var RemotePorts = [];
      9    var PassedResult = 0;
     10    var sum = 0;
     11    let iframe = document.createElement('iframe');
     12    iframe.src = noteSameSiteURL;
     13    document.body.appendChild(iframe);
     14    var TARGET = document.querySelector("iframe").contentWindow;
     15    iframe.onload = t.step_func(function() {
     16        assert_own_property(window, "MessageChannel", "window");
     17 
     18        var channels = [];
     19 
     20        for (var i=0; i<TOTALPORTS; i++)
     21        {
     22            channels[i] = new MessageChannel();
     23            LocalPorts[i] = channels[i].port1;
     24            LocalPorts[i].foo = i;
     25            RemotePorts[i] = channels[i].port2;
     26 
     27            LocalPorts[i].onmessage = t.step_func(function(e)
     28            {
     29                assert_equals(e.target.foo, e.data);
     30 
     31                PassedResult++;
     32                sum += e.data;
     33 
     34                if (PassedResult == TOTALPORTS)
     35                {
     36                    assert_equals(sum, 4950);
     37                    t.done();
     38                }
     39            });
     40        }
     41        // Sending in two batches, to test the two postMessage variants.
     42        var firstBatch = RemotePorts.slice(0, 50);
     43        var secondBatch = RemotePorts.slice(50, 100);
     44        TARGET.postMessage("ports", "*", firstBatch);
     45        TARGET.postMessage("ports", {targetOrigin: '*', transfer: secondBatch});
     46    });
     47    var counter = 0;
     48    window.onmessage = function(e)
     49    {
     50        if (e.data === "ports")
     51        {
     52            if (counter == 0) {
     53                for (var i=0; i<51; i++)
     54                {
     55                    LocalPorts[i].postMessage(LocalPorts[i].foo);
     56                }
     57                counter = 1;
     58            } else {
     59                for (var i=51; i<100; i++)
     60                {
     61                    LocalPorts[i].postMessage(LocalPorts[i].foo);
     62                }
     63            }
     64        }
     65    }
     66 }, "Test Description: postMessage to cross-site iframe with MessagePort array containing 100 ports.");