tor-browser

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

test_postMessage_origin.xhtml (13365B)


      1 <!DOCTYPE html>
      2 <html xmlns="http://www.w3.org/1999/xhtml">
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=417075
      5 -->
      6 <head>
      7  <title>postMessage from about:blank, data URLs</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=417075">Mozilla Bug 417075</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none"></div>
     15 
     16 <iframe src="http://mochi.test:8888/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml"
     17        id="sameDomain"></iframe>
     18 <iframe src="http://example.com/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml"
     19        id="otherDomain"></iframe>
     20 <iframe src="http://example.org:8000/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml"
     21        id="otherDomainPort"></iframe>
     22 <iframe src="ftp://mochi.test:27534/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml"
     23        id="localNoExist"></iframe>
     24 
     25 <iframe src="http://sub1.παράδειγμα.δοκιμή/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml"
     26        id="idnKidWhitelist"></iframe>
     27 
     28 <iframe src="http://sub1.exaмple.test/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml"
     29        id="idnKidNoWhitelist"></iframe>
     30 
     31 
     32 <pre id="test">
     33 <script class="testbody" type="application/javascript"><![CDATA[
     34 /** Test for Bug 417075 */
     35 
     36 SimpleTest.waitForExplicitFinish();
     37 
     38 function errorCheck(i, called, error, actual) {
     39  ok(!called, "receiver should not have been called for test #" + i);
     40  is(actual, error, "wrong error thrown in test #" + i);
     41 }
     42 
     43 // Sends a "PING" message to the given child frame content window and returns
     44 // a promise which resolves when we receive a corresponding "PONG" message back.
     45 function ping(win) {
     46  if (win.parent !== window) {
     47    throw new Error("Can only ping windows which are child frames of this window");
     48  }
     49  return new Promise(resolve => {
     50    window.addEventListener("message", function callback(event) {
     51      if (event.source === win && event.data === "PONG") {
     52        window.removeEventListener("message", callback);
     53        resolve();
     54      }
     55    });
     56    win.postMessage("PING", "*");
     57  });
     58 }
     59 
     60 var tests =
     61  [
     62   // 0
     63   {
     64     args: ["NOT-RECEIVED", ""],
     65     source: "sameDomain",
     66     name: "SyntaxError",
     67     code: DOMException.SYNTAX_ERR
     68   },
     69   {
     70     args: ["NOT-RECEIVED", "null"],
     71     source: "sameDomain",
     72     name: "SyntaxError",
     73     code: DOMException.SYNTAX_ERR
     74   },
     75   {
     76     args: ["NOT-RECEIVED", "a"],
     77     source: "sameDomain",
     78     name: "SyntaxError",
     79     code: DOMException.SYNTAX_ERR
     80   },
     81   {
     82     args: ["NOT-RECEIVED", "http :"],
     83     source: "sameDomain",
     84     name: "SyntaxError",
     85     code: DOMException.SYNTAX_ERR
     86   },
     87   {
     88     args: ["NOT-RECEIVED", "http: //"],
     89     source: "sameDomain",
     90     name: "SyntaxError",
     91     code: DOMException.SYNTAX_ERR,
     92 
     93     hasThrowsNoExceptionBug: true
     94   },
     95   // 5
     96   {
     97     args: ["NOT-RECEIVED", "http ://"],
     98     source: "sameDomain",
     99     name: "SyntaxError",
    100     code: DOMException.SYNTAX_ERR
    101   },
    102   {
    103     args: ["TODO", " http://localhost:8888"],
    104     source: "sameDomain",
    105     name: "SyntaxError",
    106     code: DOMException.SYNTAX_ERR,
    107 
    108     returnOrigin: "http://mochi.test:8888",
    109     hasThrowsNoExceptionBug: true
    110   },
    111   {
    112     args: ["NOT-RECEIVED", "hä"],
    113     source: "sameDomain",
    114     name: "SyntaxError",
    115     code: DOMException.SYNTAX_ERR
    116   },
    117   {
    118     args: ["NOT-RECEIVED", "http://lo\0k.com"],
    119     source: "sameDomain",
    120     name: "SyntaxError",
    121     code: DOMException.SYNTAX_ERR
    122   },
    123   {
    124     args: ["NOT-RECEIVED", "http: //localhost:8888"],
    125     source: "sameDomain",
    126     name: "SyntaxError",
    127     code: DOMException.SYNTAX_ERR,
    128 
    129     hasThrowsNoExceptionBug: true
    130   },
    131   // 10
    132   {
    133     args: ["NOT-RECEIVED", "http://localhost :8888"],
    134     source: "sameDomain",
    135     name: "SyntaxError",
    136     code: DOMException.SYNTAX_ERR
    137   },
    138   {
    139     args: ["NOT-RECEIVED", "http:// localhost:8888"],
    140     source: "sameDomain",
    141     name: "SyntaxError",
    142     code: DOMException.SYNTAX_ERR,
    143 
    144     hasThrowsNoExceptionBug: true
    145   },
    146   {
    147     args: ["TODO", "http://\nlocalhost:8888"],
    148     source: "sameDomain",
    149     name: "SyntaxError",
    150     code: DOMException.SYNTAX_ERR,
    151 
    152     returnOrigin: "http://mochi.test:8888",
    153     hasThrowsNoExceptionBug: true
    154   },
    155   {
    156     args: ["TODO", "http://localhost:8888\0"],
    157     source: "sameDomain",
    158     name: "SyntaxError",
    159     code: DOMException.SYNTAX_ERR,
    160 
    161     returnOrigin: "http://mochi.test:8888",
    162     hasThrowsNoExceptionBug: true
    163   },
    164   {
    165     args: ["TODO", "http://localhost:8888\n"],
    166     source: "sameDomain",
    167     name: "SyntaxError",
    168     code: DOMException.SYNTAX_ERR,
    169 
    170     returnOrigin: "http://mochi.test:8888",
    171     hasThrowsNoExceptionBug: true
    172   },
    173   // 15
    174   {
    175     args: ["PASS", "*"],
    176     source: "sameDomain",
    177     returnOrigin: "http://mochi.test:8888"
    178   },
    179   {
    180     args: ["PASS", "http://mochi.test:8888"],
    181     source: "sameDomain",
    182     returnOrigin: "http://mochi.test:8888"
    183   },
    184   {
    185     args: ["PASS", "http://example.com"],
    186     source: "otherDomain",
    187     returnOrigin: "http://example.com"
    188   },
    189   {
    190     args: ["PASS", "http://example.com/"],
    191     source: "otherDomain",
    192     returnOrigin: "http://example.com"
    193   },
    194   {
    195     args: ["PASS", "http://example.com:80"],
    196     source: "otherDomain",
    197     returnOrigin: "http://example.com"
    198   },
    199   // 20
    200   {
    201     args: ["PASS", "http://example.com:80/"],
    202     source: "otherDomain",
    203     returnOrigin: "http://example.com"
    204   },
    205   {
    206     args: ["PASS", "http://example.com:80/foobar"],
    207     source: "otherDomain",
    208     returnOrigin: "http://example.com"
    209   },
    210   {
    211     args: ["PASS", "http://example.com/foobar"],
    212     source: "otherDomain",
    213     returnOrigin: "http://example.com"
    214   },
    215   {
    216     args: ["PASS", "http://example.com:8000"],
    217     source: "otherDomain",
    218     expectNoCallback: true
    219   },
    220   {
    221     args: ["PASS", "http://example.com:8000/"],
    222     source: "otherDomain",
    223     expectNoCallback: true
    224   },
    225   // 25
    226   {
    227     args: ["PASS", "http://example.org:8000"],
    228     source: "otherDomainPort",
    229     returnOrigin: "http://example.org:8000"
    230   },
    231   {
    232     args: ["PASS", "http://example.org:8000/"],
    233     source: "otherDomainPort",
    234     returnOrigin: "http://example.org:8000"
    235   },
    236   {
    237     args: ["PASS", "http://example.org:8000/tests/dom/test/mochitest/whatwg/postMessage_origin_helper.xhtml"],
    238     source: "otherDomainPort",
    239     returnOrigin: "http://example.org:8000"
    240   },
    241   {
    242     args: ["PASS", "http://example.org:8000/tests/dom/test/mochitest/whatwg/this_file_does_not_exist.xhtml"],
    243     source: "otherDomainPort",
    244     returnOrigin: "http://example.org:8000"
    245   },
    246   {
    247     args: ["PASS", "http://example.org"],
    248     source: "otherDomainPort",
    249     expectNoCallback: true
    250   },
    251   // 30
    252   {
    253     args: ["PASS", "http://example.org:80"],
    254     source: "otherDomainPort",
    255     expectNoCallback: true
    256   },
    257   {
    258     args: ["PASS", "http://example.org/"],
    259     source: "otherDomainPort",
    260     expectNoCallback: true
    261   },
    262   {
    263     args: ["PASS", "http://example.org"],
    264     source: "otherDomain",
    265     expectNoCallback: true
    266   },
    267   {
    268     args: ["PASS", "ftp://mochi.test:8888"],
    269     source: "sameDomain",
    270     expectNoCallback: true
    271   },
    272   {
    273     args: ["PASS", "http://mochi.test:8888"],
    274     source: "sameDomain",
    275     returnOrigin: "http://mochi.test:8888"
    276   },
    277   // 35
    278   {
    279     args: ["PASS", "http://mochi.test:27534"],
    280     source: "sameDomain",
    281     expectNoCallback: true
    282   },
    283   {
    284     args: ["PASS", "http://sub1.παράδειγμα.δοκιμή"],
    285     source: "idnKidWhitelist",
    286     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    287   },
    288   {
    289     args: ["PASS", "http://sub1.παράδειγμα.δοκιμή:80"],
    290     source: "idnKidWhitelist",
    291     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    292   },
    293   {
    294     args: ["PASS", "http://sub1.παράδειγμα.δοκιμή:80/"],
    295     source: "idnKidWhitelist",
    296     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    297   },
    298   {
    299     args: ["PASS", "http://sub1.παράδειγμα.δοκιμή:80/foobar"],
    300     source: "idnKidWhitelist",
    301     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    302   },
    303   // 40
    304   {
    305     args: ["PASS", "http://sub1.παράδειγμα.δοκιμή/foobar"],
    306     source: "idnKidWhitelist",
    307     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    308   },
    309   {
    310     args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"],
    311     source: "idnKidWhitelist",
    312     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    313   },
    314   {
    315     args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp:80"],
    316     source: "idnKidWhitelist",
    317     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    318   },
    319   {
    320     args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp:80/"],
    321     source: "idnKidWhitelist",
    322     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    323   },
    324   {
    325     args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp:80/foo"],
    326     source: "idnKidWhitelist",
    327     returnOrigin: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"
    328   },
    329   // 45
    330   {
    331     args: ["PASS", "http://sub1.exaмple.test"],
    332     source: "idnKidNoWhitelist",
    333     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    334   },
    335   {
    336     args: ["PASS", "http://sub1.exaмple.test:80"],
    337     source: "idnKidNoWhitelist",
    338     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    339   },
    340   {
    341     args: ["PASS", "http://sub1.exaмple.test:80/"],
    342     source: "idnKidNoWhitelist",
    343     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    344   },
    345   {
    346     args: ["PASS", "http://sub1.exaмple.test/"],
    347     source: "idnKidNoWhitelist",
    348     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    349   },
    350   {
    351     args: ["PASS", "http://sub1.exaмple.test/foobar"],
    352     source: "idnKidNoWhitelist",
    353     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    354   },
    355   // 50
    356   {
    357     args: ["PASS", "http://sub1.xn--exaple-kqf.test"],
    358     source: "idnKidNoWhitelist",
    359     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    360   },
    361   {
    362     args: ["PASS", "http://sub1.xn--exaple-kqf.test:80"],
    363     source: "idnKidNoWhitelist",
    364     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    365   },
    366   {
    367     args: ["PASS", "http://sub1.xn--exaple-kqf.test:80/"],
    368     source: "idnKidNoWhitelist",
    369     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    370   },
    371   {
    372     args: ["PASS", "http://sub1.xn--exaple-kqf.test/"],
    373     source: "idnKidNoWhitelist",
    374     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    375   },
    376   {
    377     args: ["PASS", "http://sub1.xn--exaple-kqf.test/foobar"],
    378     source: "idnKidNoWhitelist",
    379     returnOrigin: "http://sub1.xn--exaple-kqf.test",
    380   },
    381  ];
    382 
    383 async function allTests() {
    384  let target, called, test, i;
    385 
    386  let listener = evt => {
    387    // Ignore "PONG" messages, which are used to ensure synchronization
    388    // between scripts in parent and child frames.
    389    if (evt.data === "PONG") {
    390      return;
    391    }
    392 
    393    is(test, tests[i],
    394       "i and test are out of sync!  async is hard, let's go shopping");
    395 
    396    var originCheck = test.hasWrongReturnOriginBug ? todo_is : is;
    397    originCheck(evt.origin, test.returnOrigin, "wrong origin for #" + i);
    398    if (test.args[0] == "TODO")
    399      todo_is(evt.data, "PASS", "wrong data");
    400    else
    401      is(evt.data, "PASS", "wrong data");
    402    is(evt.lastEventId, "",
    403       "postMessage creates events with empty lastEventId");
    404    ok(evt.source === target, "wrong source");
    405    called = true;
    406  };
    407  window.addEventListener("message", listener);
    408 
    409  for ([i, test] of tests.entries()) {
    410    called = false;
    411    target = $(test.source).contentWindow;
    412    try {
    413      target.postMessage.apply(target, test.args);
    414    } catch (e) {
    415      // Since an exception was thrown, we know at this point that we're not
    416      // waiting on anything else in the queue of script to run, and we can just
    417      // continue immediately.
    418      errorCheck(i, called, e.name, test.name);
    419      errorCheck(i, called, e.code, test.code);
    420      continue;
    421    }
    422 
    423    // Perform a postMessage round-trip to ensure the target frame has
    424    // processed our message and we've had time to receive its reply.
    425    await ping(target);
    426 
    427    if (test.hasThrowsNoExceptionBug) {
    428      todo(false, `should throw on test #${i}`);
    429    } else {
    430      is(!test.expectNoCallback, called, `should have been called #${i}`);
    431    }
    432  }
    433 
    434  window.removeEventListener("message", listener);
    435 }
    436 
    437 async function oddballTests() {
    438  for (let i = 0; i < 2; i++) {
    439    let evt = await new Promise(resolve => {
    440      window.addEventListener("message", function listener(evt) {
    441        resolve(evt);
    442        window.removeEventListener("message", listener);
    443      });
    444      window.postMessage("PASS", "http://mochi.test:8888");
    445    });
    446 
    447    is(evt.origin, "http://mochi.test:8888", "wrong sender");
    448    is(evt.data, "PASS", "wrong data");
    449    is(evt.lastEventId, "",
    450       "postMessage creates events with empty lastEventId");
    451    ok(evt.source === window, "wrong source");
    452  }
    453 }
    454 
    455 async function run() {
    456  await oddballTests();
    457  await allTests();
    458  SimpleTest.finish();
    459 }
    460 
    461 window.addEventListener("load", run);
    462 ]]></script>
    463 </pre>
    464 </body>
    465 </html>