tor-browser

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

browser_identityIcon_img_url.js (4410B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 /**
      5 * Test Bug 1562881 - Ensuring the identity icon loads correct img in different
      6 *                    circumstances.
      7 */
      8 
      9 const kBaseURI = getRootDirectory(gTestPath).replace(
     10  "chrome://mochitests/content",
     11  "https://example.com"
     12 );
     13 
     14 const kBaseURILocalhost = getRootDirectory(gTestPath).replace(
     15  "chrome://mochitests/content",
     16  "http://127.0.0.1"
     17 );
     18 
     19 const TEST_CASES = [
     20  {
     21    type: "http",
     22    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     23    testURL: "http://example.com",
     24    img_url: `url("chrome://global/skin/icons/security-broken.svg")`,
     25  },
     26  {
     27    type: "https",
     28    testURL: "https://example.com",
     29    img_url: `url("chrome://global/skin/icons/security.svg")`,
     30  },
     31  {
     32    type: "non-chrome about page",
     33    testURL: "about:about",
     34    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
     35  },
     36  {
     37    type: "chrome about page",
     38    testURL: "about:preferences",
     39    img_url: `url("chrome://branding/content/icon${
     40      window.devicePixelRatio > 1 ? 32 : 16
     41    }.png")`,
     42  },
     43  {
     44    type: "file",
     45    testURL: "dummy_page.html",
     46    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
     47  },
     48  {
     49    type: "resource",
     50    testURL: "resource://gre/modules/AppConstants.sys.mjs",
     51    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
     52  },
     53  {
     54    type: "mixedPassiveContent",
     55    testURL: kBaseURI + "file_mixedPassiveContent.html",
     56    img_url: `url("chrome://global/skin/icons/security-warning.svg")`,
     57  },
     58  {
     59    type: "mixedActiveContent",
     60    testURL: kBaseURI + "file_csp_block_all_mixedcontent.html",
     61    img_url: `url("chrome://global/skin/icons/security.svg")`,
     62  },
     63  {
     64    type: "certificateError",
     65    testURL: "https://self-signed.example.com",
     66    img_url: `url("chrome://global/skin/icons/security-warning.svg")`,
     67  },
     68  {
     69    type: "localhost",
     70    testURL: "http://127.0.0.1",
     71    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
     72  },
     73  {
     74    type: "localhost + http frame",
     75    testURL: kBaseURILocalhost + "file_csp_block_all_mixedcontent.html",
     76    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
     77  },
     78  {
     79    type: "data URI",
     80    testURL: "data:text/html,<div>",
     81    img_url: `url("chrome://global/skin/icons/security-broken.svg")`,
     82  },
     83  {
     84    type: "view-source HTTP",
     85    testURL: "view-source:http://example.com/",
     86    img_url: `url("chrome://global/skin/icons/security-broken.svg")`,
     87  },
     88  {
     89    type: "view-source HTTPS",
     90    testURL: "view-source:https://example.com/",
     91    img_url: `url("chrome://global/skin/icons/security.svg")`,
     92  },
     93 ];
     94 
     95 add_task(async function test() {
     96  await SpecialPowers.pushPrefEnv({
     97    set: [
     98      // By default, proxies don't apply to 127.0.0.1. We need them to for this test, though:
     99      ["network.proxy.allow_hijacking_localhost", true],
    100      ["security.mixed_content.upgrade_display_content", false],
    101    ],
    102  });
    103 
    104  for (let testData of TEST_CASES) {
    105    info(`Testing for ${testData.type}`);
    106    // Open the page for testing.
    107    let testURL = testData.testURL;
    108 
    109    // Overwrite the url if it is testing the file url.
    110    if (testData.type === "file") {
    111      let dir = getChromeDir(getResolvedURI(gTestPath));
    112      dir.append(testURL);
    113      dir.normalize();
    114      testURL = Services.io.newFileURI(dir).spec;
    115    }
    116 
    117    let pageLoaded;
    118    let tab = await BrowserTestUtils.openNewForegroundTab(
    119      gBrowser,
    120      () => {
    121        gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, testURL);
    122        let browser = gBrowser.selectedBrowser;
    123        if (testData.type === "certificateError") {
    124          pageLoaded = BrowserTestUtils.waitForErrorPage(browser);
    125        } else {
    126          pageLoaded = BrowserTestUtils.browserLoaded(browser);
    127        }
    128      },
    129      false
    130    );
    131    await pageLoaded;
    132 
    133    let identityIcon = document.getElementById("identity-icon");
    134 
    135    // Get the image url from the identity icon.
    136    let identityIconImageURL = gBrowser.ownerGlobal
    137      .getComputedStyle(identityIcon)
    138      .getPropertyValue("list-style-image");
    139 
    140    is(
    141      identityIconImageURL,
    142      testData.img_url,
    143      "The identity icon has a correct image url."
    144    );
    145 
    146    BrowserTestUtils.removeTab(tab);
    147  }
    148 });