tor-browser

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

browser_nohttps_download.js (2542B)


      1 "use strict";
      2 
      3 // Create a uri for an https site
      4 const testPath = getRootDirectory(gTestPath).replace(
      5  "chrome://mochitests/content",
      6  "https://example.com"
      7 );
      8 const TEST_URI = testPath + "file_nohttps_download.html";
      9 const EXPECTED_DOWNLOAD_URL =
     10  "example.com/browser/dom/security/test/https-first/file_nohttps_download.sjs";
     11 
     12 function promisePanelOpened() {
     13  if (DownloadsPanel.panel && DownloadsPanel.panel.state == "open") {
     14    return Promise.resolve();
     15  }
     16  return BrowserTestUtils.waitForEvent(DownloadsPanel.panel, "popupshown");
     17 }
     18 
     19 // Test description:
     20 // 1. Open https://example.com/...
     21 // 2. Start download - location of download is http
     22 // 3. https-first upgrades to https
     23 // 4. https returns 404 => downgrade to http again
     24 // 5. Complete download of text file
     25 add_task(async function test_nohttps_download() {
     26  await SpecialPowers.pushPrefEnv({
     27    set: [["dom.security.https_first", true]],
     28  });
     29 
     30  // remove all previous downloads
     31  let downloadsList = await Downloads.getList(Downloads.PUBLIC);
     32  await downloadsList.removeFinished();
     33 
     34  let downloadsPanelPromise = promisePanelOpened();
     35  let downloadsPromise = Downloads.getList(Downloads.PUBLIC);
     36  BrowserTestUtils.startLoadingURIString(gBrowser, TEST_URI);
     37  // wait for downloadsPanel to open before continuing with test
     38  await downloadsPanelPromise;
     39  let downloadList = await downloadsPromise;
     40  is(DownloadsPanel.isPanelShowing, true, "DownloadsPanel should be open.");
     41  is(downloadList._downloads.length, 1, "Entry should be in downloads list.");
     42  let [download] = downloadList._downloads;
     43  // wait for download to finish (with success or error)
     44  await download.unblock();
     45  await download.start();
     46  is(download.contentType, "text/plain", "File contentType should be correct.");
     47  // ensure https-first did upgrade the scheme.
     48  is(
     49    download.source.url,
     50    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     51    "http://" + EXPECTED_DOWNLOAD_URL,
     52    "Scheme should be http."
     53  );
     54  // ensure that downloaded is complete
     55  is(download.target.size, 15, "Download size is correct");
     56  //clean up
     57  info("cleaning up downloads");
     58  try {
     59    if (Services.appinfo.OS === "WINNT") {
     60      // We need to make the file writable to delete it on Windows.
     61      await IOUtils.setPermissions(download.target.path, 0o600);
     62    }
     63    await IOUtils.remove(download.target.path);
     64  } catch (error) {
     65    info("The file " + download.target.path + " is not removed, " + error);
     66  }
     67 
     68  await downloadList.remove(download);
     69  await download.finalize();
     70 });