tor-browser

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

browser_CORS-console-warnings.js (2650B)


      1 /*
      2 * Description of the test:
      3 *   Ensure that CORS warnings are printed to the web console.
      4 *
      5 *   This test uses the same tests as the plain mochitest, but needs access to
      6 *   the console.
      7 */
      8 "use strict";
      9 
     10 function console_observer(subject, topic, data) {
     11  var message = subject.wrappedJSObject.arguments[0];
     12  ok(false, message);
     13 }
     14 
     15 var webconsole = null;
     16 var messages_seen = 0;
     17 var expected_messages = 50;
     18 
     19 function on_new_message(msgObj) {
     20  let text = msgObj.message;
     21 
     22  if (text.match("Cross-Origin Request Blocked:")) {
     23    ok(true, "message is: " + text);
     24    messages_seen++;
     25  }
     26 }
     27 
     28 async function do_cleanup() {
     29  Services.console.unregisterListener(on_new_message);
     30  await unsetCookiePref();
     31 }
     32 
     33 /**
     34 * Set e10s related preferences in the test environment.
     35 *
     36 * @return {Promise} promise that resolves when preferences are set.
     37 */
     38 function setCookiePref() {
     39  return new Promise(resolve =>
     40    // accept all cookies so that the CORS requests will send the right cookies
     41    SpecialPowers.pushPrefEnv(
     42      {
     43        set: [["network.cookie.cookieBehavior", 0]],
     44      },
     45      resolve
     46    )
     47  );
     48 }
     49 
     50 /**
     51 * Unset e10s related preferences in the test environment.
     52 *
     53 * @return {Promise} promise that resolves when preferences are unset.
     54 */
     55 function unsetCookiePref() {
     56  return new Promise(resolve => {
     57    SpecialPowers.popPrefEnv(resolve);
     58  });
     59 }
     60 
     61 //jscs:disable
     62 add_task(async function () {
     63  //jscs:enable
     64  // A longer timeout is necessary for this test than the plain mochitests
     65  // due to opening a new tab with the web console.
     66  requestLongerTimeout(4);
     67  registerCleanupFunction(do_cleanup);
     68  await setCookiePref();
     69  Services.console.registerListener(on_new_message);
     70 
     71  let test_uri =
     72    "http://mochi.test:8888/browser/dom/security/test/cors/file_cors_logging_test.html";
     73 
     74  let tab = await BrowserTestUtils.openNewForegroundTab(
     75    gBrowser,
     76    "about:blank"
     77  );
     78 
     79  BrowserTestUtils.startLoadingURIString(gBrowser, test_uri);
     80 
     81  await BrowserTestUtils.waitForLocationChange(
     82    gBrowser,
     83    test_uri + "#finished"
     84  );
     85 
     86  // Different OS combinations
     87  Assert.greater(messages_seen, 0, "Saw " + messages_seen + " messages.");
     88 
     89  messages_seen = 0;
     90  let test_two_uri =
     91    "http://mochi.test:8888/browser/dom/security/test/cors/file_bug1456721.html";
     92  BrowserTestUtils.startLoadingURIString(gBrowser, test_two_uri);
     93 
     94  await BrowserTestUtils.waitForLocationChange(
     95    gBrowser,
     96    test_two_uri + "#finishedTestTwo"
     97  );
     98  await BrowserTestUtils.waitForCondition(() => messages_seen > 0);
     99 
    100  Assert.greater(messages_seen, 0, "Saw " + messages_seen + " messages.");
    101 
    102  BrowserTestUtils.removeTab(tab);
    103 });