tor-browser

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

head.js (3626B)


      1 ChromeUtils.defineESModuleGetters(this, {
      2  FormHistory: "resource://gre/modules/FormHistory.sys.mjs",
      3  SearchTestUtils: "resource://testing-common/SearchTestUtils.sys.mjs",
      4 });
      5 
      6 SearchTestUtils.init(this);
      7 
      8 function getCertChainAsString(certBase64Array) {
      9  let certChain = "";
     10  for (let cert of certBase64Array) {
     11    certChain += getPEMString(cert);
     12  }
     13  return certChain;
     14 }
     15 
     16 function getPEMString(derb64) {
     17  // Wrap the Base64 string into lines of 64 characters,
     18  // with CRLF line breaks (as specified in RFC 1421).
     19  var wrapped = derb64.replace(/(\S{64}(?!$))/g, "$1\r\n");
     20  return (
     21    "-----BEGIN CERTIFICATE-----\r\n" +
     22    wrapped +
     23    "\r\n-----END CERTIFICATE-----\r\n"
     24  );
     25 }
     26 
     27 async function injectErrorPageFrame(tab, src, sandboxed) {
     28  let loadedPromise = BrowserTestUtils.browserLoaded(
     29    tab.linkedBrowser,
     30    true,
     31    null,
     32    true
     33  );
     34 
     35  await SpecialPowers.spawn(
     36    tab.linkedBrowser,
     37    [src, sandboxed],
     38    async function (frameSrc, frameSandboxed) {
     39      let iframe = content.document.createElement("iframe");
     40      iframe.src = frameSrc;
     41      if (frameSandboxed) {
     42        iframe.setAttribute("sandbox", "allow-scripts");
     43      }
     44      content.document.body.appendChild(iframe);
     45    }
     46  );
     47 
     48  await loadedPromise;
     49 }
     50 
     51 async function openErrorPage(src, useFrame, sandboxed) {
     52  let dummyPage =
     53    getRootDirectory(gTestPath).replace(
     54      "chrome://mochitests/content",
     55      "https://example.com"
     56    ) + "dummy_page.html";
     57 
     58  let tab;
     59  if (useFrame) {
     60    info("Loading cert error page in an iframe");
     61    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, dummyPage);
     62    await injectErrorPageFrame(tab, src, sandboxed);
     63  } else {
     64    let certErrorLoaded;
     65    tab = await BrowserTestUtils.openNewForegroundTab(
     66      gBrowser,
     67      () => {
     68        gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, src);
     69        let browser = gBrowser.selectedBrowser;
     70        certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser);
     71      },
     72      false
     73    );
     74    info("Loading and waiting for the cert error");
     75    await certErrorLoaded;
     76  }
     77 
     78  return tab;
     79 }
     80 
     81 function waitForCondition(condition, nextTest, errorMsg, retryTimes) {
     82  retryTimes = typeof retryTimes !== "undefined" ? retryTimes : 30;
     83  var tries = 0;
     84  var interval = setInterval(function () {
     85    if (tries >= retryTimes) {
     86      ok(false, errorMsg);
     87      moveOn();
     88    }
     89    var conditionPassed;
     90    try {
     91      conditionPassed = condition();
     92    } catch (e) {
     93      ok(false, e + "\n" + e.stack);
     94      conditionPassed = false;
     95    }
     96    if (conditionPassed) {
     97      moveOn();
     98    }
     99    tries++;
    100  }, 100);
    101  var moveOn = function () {
    102    clearInterval(interval);
    103    nextTest();
    104  };
    105 }
    106 
    107 async function waitForBookmarksToolbarVisibility({
    108  win = window,
    109  visible,
    110  message,
    111 }) {
    112  let result = await TestUtils.waitForCondition(
    113    () => {
    114      let toolbar = win.document.getElementById("PersonalToolbar");
    115      return toolbar && (visible ? !toolbar.collapsed : toolbar.collapsed);
    116    },
    117    message ||
    118      "waiting for toolbar to become " + (visible ? "visible" : "hidden")
    119  );
    120  ok(result, message);
    121  return result;
    122 }
    123 
    124 function isBookmarksToolbarVisible(win = window) {
    125  let toolbar = win.document.getElementById("PersonalToolbar");
    126  return !toolbar.collapsed;
    127 }
    128 
    129 const setSecurityCertErrorsFeltPrivacyToTrue = async () =>
    130  await SpecialPowers.pushPrefEnv({
    131    set: [["security.certerrors.felt-privacy-v1", true]],
    132  });
    133 const setSecurityCertErrorsFeltPrivacyToFalse = async () =>
    134  await SpecialPowers.pushPrefEnv({
    135    set: [["security.certerrors.felt-privacy-v1", false]],
    136  });