tor-browser

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

browser_mixed_content_with_navigation.js (4738B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* Any copyright is dedicated to the Public Domain.
      4 * http://creativecommons.org/publicdomain/zero/1.0/ */
      5 
      6 // Tests that the site identity indicator is properly updated when loading from
      7 // the BF cache. This is achieved by loading a page, navigating to another page,
      8 // and then going "back" to the first page, as well as the reverse (loading to
      9 // the other page, navigating to the page we're interested in, going back, and
     10 // then going forward again).
     11 
     12 const kBaseURI = getRootDirectory(gTestPath).replace(
     13  "chrome://mochitests/content",
     14  "https://example.com"
     15 );
     16 const kSecureURI = kBaseURI + "dummy_page.html";
     17 
     18 const kTestcases = [
     19  {
     20    uri: kBaseURI + "file_mixedPassiveContent.html",
     21    expectErrorPage: false,
     22    expectedIdentityMode: "mixedDisplayContent",
     23  },
     24  {
     25    uri: kBaseURI + "file_mixedActiveContent_1.html",
     26    expectErrorPage: false,
     27    expectedIdentityMode: "mixedActiveBlocked",
     28  },
     29  {
     30    uri: "https://expired.example.com",
     31    expectErrorPage: true,
     32    expectedIdentityMode: "certErrorPage",
     33  },
     34 ];
     35 
     36 add_task(async function () {
     37  for (let testcase of kTestcases) {
     38    await run_testcase(testcase);
     39  }
     40 });
     41 
     42 async function run_testcase(testcase) {
     43  await SpecialPowers.pushPrefEnv({
     44    set: [["security.mixed_content.upgrade_display_content", false]],
     45  });
     46  // Test the forward and back case.
     47  // Start by loading an unrelated URI so that this generalizes well when the
     48  // testcase would otherwise first navigate to an error page, which doesn't
     49  // seem to work with withNewTab.
     50  await BrowserTestUtils.withNewTab("about:blank", async browser => {
     51    // Navigate to the test URI.
     52    BrowserTestUtils.startLoadingURIString(browser, testcase.uri);
     53    if (!testcase.expectErrorPage) {
     54      await BrowserTestUtils.browserLoaded(browser, false, testcase.uri);
     55    } else {
     56      await BrowserTestUtils.waitForErrorPage(browser);
     57    }
     58    let identityMode = window.document.getElementById("identity-box").classList;
     59    ok(
     60      identityMode.contains(testcase.expectedIdentityMode),
     61      `identity should be ${testcase.expectedIdentityMode}`
     62    );
     63 
     64    // Navigate to a URI that should be secure.
     65    BrowserTestUtils.startLoadingURIString(browser, kSecureURI);
     66    await BrowserTestUtils.browserLoaded(browser, false, kSecureURI);
     67    let secureIdentityMode =
     68      window.document.getElementById("identity-box").className;
     69    is(secureIdentityMode, "verifiedDomain", "identity should be secure now");
     70 
     71    // Go back to the test page.
     72    browser.webNavigation.goBack();
     73    if (!testcase.expectErrorPage) {
     74      await BrowserTestUtils.browserStopped(browser, testcase.uri);
     75    } else {
     76      await BrowserTestUtils.waitForErrorPage(browser);
     77    }
     78    let identityModeAgain =
     79      window.document.getElementById("identity-box").classList;
     80    ok(
     81      identityModeAgain.contains(testcase.expectedIdentityMode),
     82      `identity should again be ${testcase.expectedIdentityMode}`
     83    );
     84  });
     85 
     86  // Test the back and forward case.
     87  // Start on a secure page.
     88  await BrowserTestUtils.withNewTab(kSecureURI, async browser => {
     89    let secureIdentityMode =
     90      window.document.getElementById("identity-box").className;
     91    is(secureIdentityMode, "verifiedDomain", "identity should start as secure");
     92 
     93    // Navigate to the test URI.
     94    BrowserTestUtils.startLoadingURIString(browser, testcase.uri);
     95    if (!testcase.expectErrorPage) {
     96      await BrowserTestUtils.browserLoaded(browser, false, testcase.uri);
     97    } else {
     98      await BrowserTestUtils.waitForErrorPage(browser);
     99    }
    100    let identityMode = window.document.getElementById("identity-box").classList;
    101    ok(
    102      identityMode.contains(testcase.expectedIdentityMode),
    103      `identity should be ${testcase.expectedIdentityMode}`
    104    );
    105 
    106    // Go back to the secure page.
    107    browser.webNavigation.goBack();
    108    await BrowserTestUtils.browserStopped(browser, kSecureURI);
    109    let secureIdentityModeAgain =
    110      window.document.getElementById("identity-box").className;
    111    is(
    112      secureIdentityModeAgain,
    113      "verifiedDomain",
    114      "identity should be secure again"
    115    );
    116 
    117    // Go forward again to the test URI.
    118    browser.webNavigation.goForward();
    119    if (!testcase.expectErrorPage) {
    120      await BrowserTestUtils.browserStopped(browser, testcase.uri);
    121    } else {
    122      await BrowserTestUtils.waitForErrorPage(browser);
    123    }
    124    let identityModeAgain =
    125      window.document.getElementById("identity-box").classList;
    126    ok(
    127      identityModeAgain.contains(testcase.expectedIdentityMode),
    128      `identity should again be ${testcase.expectedIdentityMode}`
    129    );
    130  });
    131 }