tor-browser

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

browser_https_only_section.js (4878B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Bug 1671122 - Fixed bug where second click on HTTPS-Only Mode enable-checkbox disables it again.
      5 // https://bugzilla.mozilla.org/bug/1671122
      6 "use strict";
      7 
      8 const HTTPS_ONLY_ENABLED = "enabled";
      9 const HTTPS_ONLY_PBM_ONLY = "privateOnly";
     10 const HTTPS_ONLY_DISABLED = "disabled";
     11 
     12 add_task(async function httpsOnlyRadioGroupIsWorking() {
     13  // Make sure HTTPS-Only mode is only enabled for PBM
     14 
     15  registerCleanupFunction(async function () {
     16    Services.prefs.clearUserPref("dom.security.https_only_mode");
     17    Services.prefs.clearUserPref("dom.security.https_only_mode_pbm");
     18  });
     19 
     20  await SpecialPowers.setBoolPref("dom.security.https_only_mode", false);
     21  await SpecialPowers.setBoolPref("dom.security.https_only_mode_pbm", true);
     22 
     23  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
     24 
     25  const doc = gBrowser.selectedBrowser.contentDocument;
     26  const radioGroup = doc.getElementById("httpsOnlyRadioGroup");
     27  const enableAllRadio = doc.getElementById("httpsOnlyRadioEnabled");
     28  const enablePbmRadio = doc.getElementById("httpsOnlyRadioEnabledPBM");
     29  const disableRadio = doc.getElementById("httpsOnlyRadioDisabled");
     30 
     31  // Check if UI
     32  check(radioGroup, HTTPS_ONLY_PBM_ONLY);
     33 
     34  // Check if UI updated on pref-change
     35  await SpecialPowers.setBoolPref("dom.security.https_only_mode_pbm", false);
     36  check(radioGroup, HTTPS_ONLY_DISABLED);
     37 
     38  // Check if prefs change if clicked on radio button
     39  let changePromise = BrowserTestUtils.waitForEvent(radioGroup, "change");
     40  enableAllRadio.click();
     41  await changePromise;
     42  check(radioGroup, HTTPS_ONLY_ENABLED);
     43 
     44  // Check if prefs stay the same if clicked on same
     45  // radio button again (see bug 1671122)
     46  enableAllRadio.click();
     47  await TestUtils.waitForTick(); // There shouldn't be a change so we cannot
     48  // await it. This is just waiting a bit instead.
     49  check(radioGroup, HTTPS_ONLY_ENABLED);
     50 
     51  // Check if prefs are set correctly for PBM-only mode.
     52  changePromise = BrowserTestUtils.waitForEvent(radioGroup, "change");
     53  enablePbmRadio.click();
     54  await changePromise;
     55  check(radioGroup, HTTPS_ONLY_PBM_ONLY);
     56 
     57  // Check if prefs are set correctly when disabled again.
     58  changePromise = BrowserTestUtils.waitForEvent(radioGroup, "change");
     59  disableRadio.click();
     60  await changePromise;
     61  check(radioGroup, HTTPS_ONLY_DISABLED);
     62 
     63  BrowserTestUtils.removeTab(gBrowser.selectedTab);
     64 });
     65 
     66 function check(radioGroupElement, expectedValue) {
     67  is(
     68    radioGroupElement.value,
     69    expectedValue,
     70    "Radio Group value should match expected value"
     71  );
     72  is(
     73    SpecialPowers.getBoolPref("dom.security.https_only_mode"),
     74    expectedValue === HTTPS_ONLY_ENABLED,
     75    "HTTPS-Only pref should match expected value."
     76  );
     77  is(
     78    SpecialPowers.getBoolPref("dom.security.https_only_mode_pbm"),
     79    expectedValue === HTTPS_ONLY_PBM_ONLY,
     80    "HTTPS-Only PBM pref should match expected value."
     81  );
     82 }
     83 
     84 add_task(async function httpsOnlyCorrectLabels() {
     85  registerCleanupFunction(async function () {
     86    Services.prefs.clearUserPref("dom.security.https_first");
     87  });
     88 
     89  function ensureL10nIds(httpsFirstEnabled) {
     90    return SpecialPowers.spawn(
     91      gBrowser.selectedBrowser,
     92      [httpsFirstEnabled],
     93      _httpsFirstEnabled => {
     94        function ensureL10nId(elementId, l10nId) {
     95          const element = content.document.getElementById(elementId);
     96          ok(element, `${elementId} should be on the settings page`);
     97          is(
     98            content.document.l10n.getAttributes(element).id,
     99            l10nId,
    100            `${elementId} should have the correct data-l10n-id attribute`
    101          );
    102        }
    103 
    104        ensureL10nId("httpsOnlyRadioEnabled", "httpsonly-radio-enabled");
    105        ensureL10nId("httpsOnlyRadioEnabledPBM", "httpsonly-radio-enabled-pbm");
    106        ensureL10nId("httpsOnlyRadioDisabled", "httpsonly-radio-disabled3");
    107      }
    108    );
    109  }
    110 
    111  // Load the page with HTTPS-First disabled and then enable it while on the
    112  // page
    113  await SpecialPowers.setBoolPref("dom.security.https_first", false);
    114  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
    115  await ensureL10nIds(false);
    116  await SpecialPowers.setBoolPref("dom.security.https_first", true);
    117  await ensureL10nIds(true);
    118  BrowserTestUtils.removeTab(gBrowser.selectedTab);
    119 
    120  // Load the page with HTTPS-First enabled and then disable it while on the
    121  // page
    122  await SpecialPowers.setBoolPref("dom.security.https_first", true);
    123  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
    124  await ensureL10nIds(true);
    125  await SpecialPowers.setBoolPref("dom.security.https_first", false);
    126  await ensureL10nIds(false);
    127  BrowserTestUtils.removeTab(gBrowser.selectedTab);
    128 
    129  SpecialPowers.clearUserPref("dom.security.https_only_mode_ever_enabled_pbm");
    130 });