tor-browser

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

test_browsingcontext_currenturi.html (4368B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      5  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
      6 </head>
      7 <body>
      8 
      9 <iframe id="tls1frame" src="https://tls1.example.com/"></iframe>
     10 
     11 <script>
     12 "use strict";
     13 
     14 add_task(async function test_frame() {
     15  let win = SpecialPowers.wrap(window);
     16  info(`id=${win.browsingContext.id}`);
     17  let [docURI, curURI] = await SpecialPowers.spawnChrome([win.browsingContext.id], async id => {
     18    let bc = BrowsingContext.get(id);
     19    return [
     20      bc.currentWindowGlobal.documentURI.spec,
     21      bc.currentURI.spec,
     22    ];
     23  });
     24  info(`docURI=${docURI}, curURI=${curURI}`);
     25  is(window.location.href, curURI, "curURI has the expected value");
     26  is(window.location.href, docURI, "documentURI has the expected value");
     27 });
     28 
     29 add_task(async function test_tls1_frame() {
     30  let expframe = SpecialPowers.wrap(document.getElementById("tls1frame"));
     31  let [docURI, curURI] = await SpecialPowers.spawnChrome(
     32    [expframe.browsingContext.id], async id => {
     33      const { TestUtils } = ChromeUtils.importESModule(
     34        "resource://testing-common/TestUtils.sys.mjs"
     35      );
     36 
     37      let bc = BrowsingContext.get(id);
     38 
     39      // awkwardly wait for the current window global to update to the error page.
     40      // would be nice to do just about anything else here...
     41      await TestUtils.waitForCondition(
     42        () =>
     43          bc.currentURI && bc.currentURI.spec != "about:blank" &&
     44          bc.currentWindowGlobal && bc.currentWindowGlobal.documentURI.spec != "about:blank",
     45        "waiting for current window global to be non-initial");
     46 
     47      info(`currentWindowGlobal has updated in the parent!`);
     48      return [
     49        bc.currentWindowGlobal.documentURI.spec,
     50        bc.currentURI.spec,
     51      ];
     52    });
     53 
     54  info(`docURI=${docURI}, curURI=${curURI}`);
     55  is(curURI, "https://tls1.example.com/", "curURI has expected value");
     56  ok(docURI.startsWith("about:neterror"), "documentURI starts with about:neterror");
     57 });
     58 
     59 let BROADCAST_ONLOAD_URL =
     60    new URL("file_broadcast_currenturi_onload.html", location.href);
     61 
     62 async function broadcastLoadTest(baseURI, callback) {
     63  // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
     64  // Acquire storage access permission here so that the BroadcastChannel used to
     65  // communicate with the opened windows works in xorigin tests. Otherwise,
     66  // the iframe containing this page is isolated from first-party storage access,
     67  // which isolates BroadcastChannel communication.
     68  if (isXOrigin) {
     69    await SpecialPowers.pushPrefEnv({
     70      set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
     71    });
     72    SpecialPowers.wrap(document).notifyUserGestureActivation();
     73    await SpecialPowers.addPermission(
     74      "storageAccessAPI",
     75      true,
     76      window.location.href
     77     );
     78    await SpecialPowers.wrap(document).requestStorageAccess();
     79  }
     80  let loaded = new Promise(resolve => {
     81    let chan = new BroadcastChannel("test_broadcast_onload");
     82    chan.onmessage = event => {
     83      resolve(event.data);
     84    };
     85  });
     86  let srcURL = new URL(BROADCAST_ONLOAD_URL.pathname, baseURI);
     87  callback(srcURL.href);
     88 
     89  let results = await loaded;
     90  for (let { location, curURI, docURI } of results) {
     91    info(`location=${location}, docURI=${docURI}, curURI=${curURI}`);
     92    is(location, curURI, "curURI has expected value");
     93    is(location, docURI, "documentURI has expected value");
     94  }
     95 }
     96 
     97 async function normalFrameLoadTest(base) {
     98  await broadcastLoadTest(base, src => {
     99    let frame = document.createElement("iframe");
    100    frame.src = src;
    101    document.body.appendChild(frame);
    102  });
    103 }
    104 
    105 async function normalPopupLoadTest(base, flags = "") {
    106  await broadcastLoadTest(base, src => {
    107    window.open(src, null, flags);
    108  });
    109 }
    110 
    111 add_task(async function test_sameorigin_frame() {
    112  await normalFrameLoadTest(location.href);
    113 })
    114 
    115 add_task(async function test_crossorigin_frame() {
    116  await normalFrameLoadTest("https://example.com");
    117 });
    118 
    119 add_task(async function test_sameorigin_popup() {
    120  await normalPopupLoadTest(location.href);
    121  await normalPopupLoadTest(location.href, "noopener");
    122 });
    123 
    124 add_task(async function test_crossorigin_popup() {
    125  await normalPopupLoadTest("https://example.com");
    126  await normalPopupLoadTest("https://example.com", "noopener");
    127 });
    128 
    129 </script>
    130 </body>
    131 </html>