tor-browser

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

sharedworker-partitioning.tentative.https.window.js (4045B)


      1 // META: script=/common/utils.js
      2 
      3 const sw_url = location.pathname.replace(/[^/]*$/, '') +
      4      "./resources/sharedworker-partitioning-helper.js";
      5 
      6 promise_test(async t => {
      7  // Create 4 iframes (two normal and two credentialless ones) and create
      8  // a shared worker with the same url in all of them.
      9  //
     10  // Creating the same shared worker again with the same url is a
     11  // no-op. However, credentialless iframes get partitioned shared workers,
     12  // so we should have a total of 2 shared workers at the end (one for
     13  // the normal iframes and one for the credentialless ones).
     14  let iframes = await Promise.all([
     15    { name: "normal", credentialless: false},
     16    { name: "normal_control", credentialless: false},
     17    { name: "credentialless", credentialless: true},
     18    { name: "credentialless_control", credentialless: true},
     19  ].map(async ({name, credentialless}) => {
     20 
     21    let iframe = await new Promise(resolve => {
     22      let iframe = document.createElement('iframe');
     23      iframe.onload = () => resolve(iframe);
     24      iframe.src = '/common/blank.html';
     25      if (credentialless) iframe.credentialless = true;
     26      document.body.append(iframe);
     27    });
     28 
     29    let sw = new iframe.contentWindow.SharedWorker(sw_url);
     30    return { iframe: iframe, name: name, sw: sw };
     31  }));
     32 
     33  // Ping each worker telling him which iframe it belongs to.
     34  await Promise.all(iframes.map(iframe => {
     35    iframe.sw.port.postMessage({ action: 'record', from: iframe.name});
     36    return new Promise(resolve => {
     37      iframe.sw.port.onmessage = event => {
     38        if (event.data.ack === iframe.name) resolve();
     39      }
     40    });
     41  }));
     42 
     43  // Ping each (iframe, sharedworker) asking for which messages it got.
     44  let msgs = await Promise.all(iframes.map(iframe => {
     45    iframe.sw.port.postMessage({ action: 'retrieve', from: iframe.name });
     46    return new Promise(resolve => {
     47      iframe.sw.port.onmessage = event => {
     48        if (event.data.ack === iframe.name) resolve(event.data.messages);
     49      }
     50    });
     51  }));
     52 
     53  // The "normal" iframe sharedworker belongs to the "normal" and the
     54  // "normal_control" iframes.
     55  assert_true(!!msgs[0]["normal"] &&
     56              !!msgs[0]["normal_control"] &&
     57              !msgs[0]["credentialless"] &&
     58              !msgs[0]["credentialless_control"],
     59              'The "normal" iframe\'s sharedworker should return ' +
     60              '{"normal": true, "normal_control": true}, ' +
     61              'but instead returned ' + JSON.stringify(msgs[0]));
     62 
     63  // The "normal_control" iframe shares the same sharedworker as the "normal"
     64  // iframe.
     65  assert_true(!!msgs[1]["normal"] &&
     66              !!msgs[1]["normal_control"] &&
     67              !msgs[1]["credentialless"] &&
     68              !msgs[1]["credentialless_control"],
     69              'The "normal_control" iframe\'s sharedworker should return ' +
     70              '{"normal": true, "normal_control": true}, ' +
     71              'but instead returned ' + JSON.stringify(msgs[1]));
     72 
     73  // The "credentialless" iframe sharedworker belongs to the "credentialless" and the
     74  // "credentialless_control" iframes.
     75  assert_true(!msgs[2]["normal"] &&
     76              !msgs[2]["normal_control"] &&
     77              !!msgs[2]["credentialless"] &&
     78              !!msgs[2]["credentialless_control"],
     79              'The "credentialless" iframe\'s sharedworker should return ' +
     80              '{"credentialless": true, "credentialless_control": true}, ' +
     81              'but instead returned ' + JSON.stringify(msgs[2]));
     82 
     83  // The "credentialless_control" iframe shares the same sharedworker as
     84  // the "credentialless" iframe.
     85  assert_true(!msgs[3]["normal"] &&
     86              !msgs[3]["normal_control"] &&
     87              !!msgs[3]["credentialless"] &&
     88              !!msgs[3]["credentialless_control"],
     89              'The "credentialless_control" iframe\'s sharedworker should return ' +
     90              '{"credentialless": true, "credentialless_control": true}, ' +
     91              'but instead returned ' + JSON.stringify(msgs[3]));
     92 
     93 }, "credentialless iframes get partitioned shared workers.");