tor-browser

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

test_third_party.html (4836B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <html>
      6 <head>
      7  <title>Indexed Database Test</title>
      8 
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11 
     12  <script type="text/javascript">
     13    const BEHAVIOR_ACCEPT = 0;
     14    const BEHAVIOR_REJECTFOREIGN = 1;
     15    const BEHAVIOR_REJECT = 2;
     16    const BEHAVIOR_LIMITFOREIGN = 3;
     17 
     18    const testData = [
     19      { host: "http://" + window.location.host, cookieBehavior: BEHAVIOR_ACCEPT, expectedResultFrame1: true, expectedResultFrame2: true },
     20      { host: "http://example.com", cookieBehavior: BEHAVIOR_ACCEPT, expectedResultFrame1: true, expectedResultFrame2: true },
     21      { host: "http://sub1.test2.example.org:8000", cookieBehavior: BEHAVIOR_ACCEPT, expectedResultFrame1: true, expectedResultFrame2: true },
     22 
     23      { host: "http://" + window.location.host, cookieBehavior: BEHAVIOR_REJECT, expectedResultFrame1: false, expectedResultFrame2: false },
     24      { host: "http://example.com", cookieBehavior: BEHAVIOR_REJECT, expectedResultFrame1: false, expectedResultFrame2: false },
     25      { host: "http://sub1.test2.example.org:8000", cookieBehavior: BEHAVIOR_REJECT, expectedResultFrame1: false, expectedResultFrame2: false },
     26 
     27      { host: "http://" + window.location.host, cookieBehavior: BEHAVIOR_REJECTFOREIGN, expectedResultFrame1: true, expectedResultFrame2: true },
     28      { host: "http://example.com", cookieBehavior: BEHAVIOR_REJECTFOREIGN, expectedResultFrame1: false, expectedResultFrame2: false },
     29      { host: "http://sub1.test2.example.org:8000", cookieBehavior: BEHAVIOR_REJECTFOREIGN, expectedResultFrame1: false, expectedResultFrame2: false },
     30 
     31      { host: "http://" + window.location.host, cookieBehavior: BEHAVIOR_LIMITFOREIGN, expectedResultFrame1: true, expectedResultFrame2: true },
     32      { host: "http://example.com", cookieBehavior: BEHAVIOR_LIMITFOREIGN, expectedResultFrame1: false, expectedResultFrame2: false },
     33      { host: "http://sub1.test2.example.org:8000", cookieBehavior: BEHAVIOR_LIMITFOREIGN, expectedResultFrame1: false, expectedResultFrame2: false },
     34    ];
     35 
     36    const iframe1Path =
     37      window.location.pathname.replace("test_third_party.html",
     38                                       "third_party_iframe1.html");
     39    const iframe2URL =
     40      "http://" + window.location.host +
     41      window.location.pathname.replace("test_third_party.html",
     42                                       "third_party_iframe2.html");
     43 
     44    let testIndex = 0;
     45    let openedWindow;
     46 
     47    // Cookie preference changes are only applied to top-level tabs/windows
     48    // when they are loaded. We need a window-proxy to continue the test.
     49    function openWindow() {
     50      SpecialPowers.pushPrefEnv({
     51        "set": [
     52          ["network.cookie.cookieBehavior", testData[testIndex].cookieBehavior],
     53        ],
     54      }, () => {
     55        openedWindow = window.open("third_party_window.html");
     56        openedWindow.onload = _ => {
     57          openedWindow.postMessage({
     58            source: "parent",
     59            href: iframe2URL,
     60            iframeUrl: testData[testIndex].host + iframe1Path,
     61          }, "*");
     62        };
     63      });
     64    }
     65 
     66    let testFrames = ["iframe1", "iframe2"];
     67    function messageListener(event) {
     68      let message = JSON.parse(event.data);
     69 
     70      // TODO: This is an ad-hoc solution to get a useful assertion message.
     71      // It would be desirable that the test framework provides the ability
     72      // to capture context information and provide it on assertion failures,
     73      // automatically stringified.
     74      let testContext = `testData[${testIndex}] == ${JSON.stringify(testData[testIndex])}`;
     75 
     76      let idx = testFrames.indexOf(message.source);
     77      if (idx != -1) {
     78        testFrames.splice(idx, 1);
     79        if (message.source == "iframe1") {
     80          is(message.result, testData[testIndex].expectedResultFrame1, `Good result for ${testContext} iframe1`);
     81        } else if (message.source == "iframe2") {
     82          is(message.result, testData[testIndex].expectedResultFrame2, `Good result for ${testContext} iframe2`);
     83        }
     84      } else {
     85        ok(false, 'Test has already received a message from ${message.source}');
     86      }
     87 
     88      if (testFrames.length) {
     89        return;
     90      }
     91 
     92      openedWindow.close();
     93 
     94      if (testIndex < testData.length - 1) {
     95        testFrames = ["iframe1", "iframe2"];
     96        testIndex++;
     97        openWindow();
     98        return;
     99      }
    100 
    101      SimpleTest.finish();
    102    }
    103 
    104    function runTest() {
    105      SimpleTest.waitForExplicitFinish();
    106 
    107      SpecialPowers.addPermission("indexedDB", true, document);
    108 
    109      window.addEventListener("message", messageListener);
    110      openWindow();
    111    }
    112  </script>
    113 
    114 </head>
    115 
    116 <body onload="runTest();">
    117 </body>
    118 
    119 </html>