tor-browser

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

browser_primaryPassword.js (8459B)


      1 const { OSKeyStoreTestUtils } = ChromeUtils.importESModule(
      2  "resource://testing-common/OSKeyStoreTestUtils.sys.mjs"
      3 );
      4 const { OSKeyStore } = ChromeUtils.importESModule(
      5  "resource://gre/modules/OSKeyStore.sys.mjs"
      6 );
      7 
      8 add_setup(async function () {
      9  await SpecialPowers.pushPrefEnv({
     10    set: [["toolkit.osKeyStore.unofficialBuildOnlyLogin", ""]],
     11  });
     12 });
     13 
     14 add_task(async function () {
     15  await SpecialPowers.pushPrefEnv({
     16    set: [["browser.settings-redesign.enabled", false]],
     17  });
     18  let prefs = await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
     19    leaveOpen: true,
     20  });
     21  is(prefs.selectedPane, "panePrivacy", "Privacy pane was selected");
     22 
     23  let doc = gBrowser.contentDocument;
     24  // Fake the subdialog and LoginHelper
     25  let win = doc.defaultView;
     26  let dialogURL = "";
     27  let dialogOpened = false;
     28  ChromeUtils.defineLazyGetter(win, "gSubDialog", () => ({
     29    open(aDialogURL, { closingCallback: aCallback }) {
     30      dialogOpened = true;
     31      dialogURL = aDialogURL;
     32      primaryPasswordSet = primaryPasswordNextState;
     33      aCallback();
     34    },
     35  }));
     36 
     37  let primaryPasswordSet = false;
     38  win.LoginHelper = {
     39    isPrimaryPasswordSet() {
     40      return primaryPasswordSet;
     41    },
     42    getOSAuthEnabled() {
     43      return true; // Since enabled by default.
     44    },
     45  };
     46 
     47  let checkbox = doc.querySelector("#useMasterPassword");
     48  checkbox.scrollIntoView();
     49  ok(
     50    !checkbox.checked,
     51    "primary password checkbox should be unchecked by default"
     52  );
     53  let button = doc.getElementById("changeMasterPassword");
     54  ok(button.disabled, "primary password button should be disabled by default");
     55 
     56  let primaryPasswordNextState = false;
     57  if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin() && OSKeyStore.canReauth()) {
     58    let osAuthDialogShown = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(false);
     59    checkbox.click();
     60    info("waiting for os auth dialog to appear and get canceled");
     61    await osAuthDialogShown;
     62    await TestUtils.waitForCondition(
     63      () => !checkbox.checked,
     64      "wait for checkbox to get unchecked"
     65    );
     66    ok(!dialogOpened, "the dialog should not have opened");
     67    ok(
     68      !dialogURL,
     69      "the changemp dialog should not have been opened when the os auth dialog is canceled"
     70    );
     71    ok(
     72      !checkbox.checked,
     73      "primary password checkbox should be unchecked after canceling os auth dialog"
     74    );
     75    ok(button.disabled, "button should be disabled after canceling os auth");
     76  }
     77 
     78  primaryPasswordNextState = true;
     79  if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin() && OSKeyStore.canReauth()) {
     80    let osAuthDialogShown = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(true);
     81    checkbox.click();
     82    info("waiting for os auth dialog to appear");
     83    await osAuthDialogShown;
     84    info("waiting for dialogURL to get set");
     85    await TestUtils.waitForCondition(
     86      () => dialogURL,
     87      "wait for open to get called asynchronously"
     88    );
     89    is(
     90      dialogURL,
     91      "chrome://mozapps/content/preferences/changemp.xhtml",
     92      "clicking on the checkbox should open the primary password dialog"
     93    );
     94  } else {
     95    primaryPasswordSet = true;
     96    doc.defaultView.gPrivacyPane._initMasterPasswordUI();
     97    await TestUtils.waitForCondition(
     98      () => !button.disabled,
     99      "waiting for primary password button to get enabled"
    100    );
    101  }
    102  ok(!button.disabled, "primary password button should now be enabled");
    103  ok(checkbox.checked, "primary password checkbox should be checked now");
    104 
    105  dialogURL = "";
    106  button.doCommand();
    107  await TestUtils.waitForCondition(
    108    () => dialogURL,
    109    "wait for open to get called asynchronously"
    110  );
    111  is(
    112    dialogURL,
    113    "chrome://mozapps/content/preferences/changemp.xhtml",
    114    "clicking on the button should open the primary password dialog"
    115  );
    116  ok(!button.disabled, "primary password button should still be enabled");
    117  ok(checkbox.checked, "primary password checkbox should be checked still");
    118 
    119  // Confirm that we won't automatically respond to the dialog,
    120  // since we don't expect a dialog here, we want the test to fail if one appears.
    121  is(
    122    Services.prefs.getStringPref(
    123      "toolkit.osKeyStore.unofficialBuildOnlyLogin",
    124      ""
    125    ),
    126    "",
    127    "Pref should be set to an empty string"
    128  );
    129 
    130  primaryPasswordNextState = false;
    131  dialogURL = "";
    132  checkbox.click();
    133  is(
    134    dialogURL,
    135    "chrome://mozapps/content/preferences/removemp.xhtml",
    136    "clicking on the checkbox to uncheck primary password should show the removal dialog"
    137  );
    138  ok(button.disabled, "primary password button should now be disabled");
    139  ok(!checkbox.checked, "primary password checkbox should now be unchecked");
    140 
    141  BrowserTestUtils.removeTab(gBrowser.selectedTab);
    142 });
    143 
    144 add_task(async function () {
    145  await SpecialPowers.pushPrefEnv({
    146    set: [["browser.settings-redesign.enabled", true]],
    147  });
    148  let prefs = await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
    149    leaveOpen: true,
    150  });
    151  is(prefs.selectedPane, "panePrivacy", "Privacy pane was selected");
    152 
    153  let doc = gBrowser.contentDocument;
    154  // Fake the subdialog and LoginHelper
    155  let win = doc.defaultView;
    156  let dialogURL = "";
    157  let dialogOpened = false;
    158  ChromeUtils.defineLazyGetter(win, "gSubDialog", () => ({
    159    open(aDialogURL, { closingCallback: aCallback }) {
    160      dialogOpened = true;
    161      dialogURL = aDialogURL;
    162      primaryPasswordSet = primaryPasswordNextState;
    163      aCallback();
    164    },
    165  }));
    166 
    167  let primaryPasswordSet = false;
    168  win.LoginHelper = {
    169    isPrimaryPasswordSet() {
    170      return primaryPasswordSet;
    171    },
    172    getOSAuthEnabled() {
    173      return true; // Since enabled by default.
    174    },
    175  };
    176 
    177  let primaryPasswordNotSet = doc.querySelector("#primaryPasswordNotSet");
    178  primaryPasswordNotSet.scrollIntoView();
    179  ok(
    180    primaryPasswordNotSet,
    181    "'Primary password not set' control should be shown by default"
    182  );
    183  let button = doc.getElementById("addPrimaryPassword");
    184 
    185  let primaryPasswordNextState = false;
    186  if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin() && OSKeyStore.canReauth()) {
    187    let osAuthDialogShown = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(false);
    188    button.click();
    189    info("waiting for os auth dialog to appear and get canceled");
    190    await osAuthDialogShown;
    191    ok(!dialogOpened, "the dialog should not have opened");
    192    ok(
    193      !dialogURL,
    194      "the changemp dialog should not have been opened when the os auth dialog is canceled"
    195    );
    196  }
    197 
    198  let primaryPasswordSetCtrl = doc.querySelector("#primaryPasswordSet");
    199  primaryPasswordNextState = true;
    200  if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin() && OSKeyStore.canReauth()) {
    201    let osAuthDialogShown = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(true);
    202    button.click();
    203    info("waiting for os auth dialog to appear");
    204    await osAuthDialogShown;
    205    info("waiting for dialogURL to get set");
    206    await TestUtils.waitForCondition(
    207      () => dialogURL,
    208      "wait for open to get called asynchronously"
    209    );
    210    is(
    211      dialogURL,
    212      "chrome://mozapps/content/preferences/changemp.xhtml",
    213      "clicking on the checkbox should open the primary password dialog"
    214    );
    215  } else {
    216    primaryPasswordSet = true;
    217    doc.defaultView.gPrivacyPane._initMasterPasswordUI();
    218    await TestUtils.waitForCondition(
    219      () => !button.disabled,
    220      "waiting for primary password button to get enabled"
    221    );
    222  }
    223  ok(
    224    primaryPasswordSetCtrl,
    225    "'primary password set control' should be visible now"
    226  );
    227 
    228  dialogURL = "";
    229  button.click();
    230  await TestUtils.waitForCondition(
    231    () => dialogURL,
    232    "wait for open to get called asynchronously"
    233  );
    234  is(
    235    dialogURL,
    236    "chrome://mozapps/content/preferences/changemp.xhtml",
    237    "clicking on the button should open the primary password dialog"
    238  );
    239 
    240  // Confirm that we won't automatically respond to the dialog,
    241  // since we don't expect a dialog here, we want the test to fail if one appears.
    242  is(
    243    Services.prefs.getStringPref(
    244      "toolkit.osKeyStore.unofficialBuildOnlyLogin",
    245      ""
    246    ),
    247    "",
    248    "Pref should be set to an empty string"
    249  );
    250 
    251  let removePrimaryPasswordButton = doc.querySelector(
    252    "#turnOffPrimaryPassword"
    253  );
    254  primaryPasswordNextState = false;
    255  dialogURL = "";
    256  removePrimaryPasswordButton.click();
    257  is(
    258    dialogURL,
    259    "chrome://mozapps/content/preferences/removemp.xhtml",
    260    "clicking on the checkbox to uncheck primary password should show the removal dialog"
    261  );
    262 
    263  BrowserTestUtils.removeTab(gBrowser.selectedTab);
    264 });