tor-browser

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

head.js (4114B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 const { AppConstants } = ChromeUtils.importESModule(
      7  "resource://gre/modules/AppConstants.sys.mjs"
      8 );
      9 
     10 const { InstallerPrefs } = ChromeUtils.importESModule(
     11  "resource:///modules/InstallerPrefs.sys.mjs"
     12 );
     13 
     14 let gRegistryKeyPath = "";
     15 
     16 function startModule(prefsList) {
     17  // Construct an InstallerPrefs object and simulate a profile-after-change
     18  // event on it, so that it performs its full startup procedure.
     19  const prefsModule = new InstallerPrefs(prefsList);
     20  prefsModule.observe(null, "profile-after-change", "");
     21 
     22  gRegistryKeyPath = prefsModule._registryKeyPath;
     23 
     24  registerCleanupFunction(() => cleanupReflectedPrefs(prefsList));
     25 }
     26 
     27 function getRegistryKey() {
     28  const key = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
     29    Ci.nsIWindowsRegKey
     30  );
     31  key.open(
     32    key.ROOT_KEY_CURRENT_USER,
     33    gRegistryKeyPath,
     34    key.ACCESS_READ | key.WOW64_64
     35  );
     36  return key;
     37 }
     38 
     39 function verifyReflectedPrefs(prefsList) {
     40  let key;
     41  try {
     42    key = getRegistryKey();
     43  } catch (ex) {
     44    Assert.ok(false, `Failed to open registry key: ${ex}`);
     45    return;
     46  }
     47 
     48  for (const pref of prefsList) {
     49    if (pref.startsWith("installer.")) {
     50      if (Services.prefs.getPrefType(pref) != Services.prefs.PREF_BOOL) {
     51        Assert.ok(
     52          !key.hasValue(pref),
     53          `Pref ${pref} should not be in the registry because its type is not bool`
     54        );
     55      } else if (Services.prefs.getBoolPref(pref, false)) {
     56        Assert.ok(key.hasValue(pref), `Pref ${pref} should be in the registry`);
     57        Assert.equal(
     58          key.getValueType(pref),
     59          key.TYPE_INT,
     60          `Pref ${pref} should be type DWORD`
     61        );
     62        Assert.equal(
     63          key.readIntValue(pref),
     64          1,
     65          `Pref ${pref} should have value 1`
     66        );
     67      } else {
     68        Assert.ok(
     69          !key.hasValue(pref),
     70          `Pref ${pref} should not be in the registry because it is false`
     71        );
     72      }
     73    } else {
     74      Assert.ok(
     75        !key.hasValue(pref),
     76        `Pref ${pref} should not be in the registry because its name is invalid`
     77      );
     78    }
     79  }
     80 
     81  key.close();
     82 }
     83 
     84 function cleanupReflectedPrefs(prefsList) {
     85  // Clear out the prefs themselves.
     86  prefsList.forEach(pref => Services.prefs.clearUserPref(pref));
     87 
     88  // Get the registry key path without the path hash at the end,
     89  // then delete the subkey with the path hash.
     90  const app = AppConstants.MOZ_APP_NAME;
     91  const vendor = Services.appinfo.vendor || "Mozilla";
     92  const xreDirProvider = Cc["@mozilla.org/xre/directory-provider;1"].getService(
     93    Ci.nsIXREDirProvider
     94  );
     95 
     96  const path = `Software\\${vendor}\\${app}\\Installer`;
     97 
     98  const key = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
     99    Ci.nsIWindowsRegKey
    100  );
    101  try {
    102    key.open(
    103      key.ROOT_KEY_CURRENT_USER,
    104      path,
    105      key.ACCESS_READ | key.ACCESS_WRITE | key.WOW64_64
    106    );
    107    const installHash = xreDirProvider.getInstallHash();
    108    key.removeChild(installHash);
    109  } catch (ex) {
    110    // Nothing left to clean up.
    111    return;
    112  }
    113 
    114  // If the Installer key is now empty, we need to clean it up also, because
    115  // that would mean that this test created it.
    116  if (key.childCount == 0) {
    117    // Unfortunately we can't delete the actual open key, so we'll have to
    118    // open its parent and delete the one we're after as a child.
    119    key.close();
    120    const parentKey = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
    121      Ci.nsIWindowsRegKey
    122    );
    123    try {
    124      parentKey.open(
    125        parentKey.ROOT_KEY_CURRENT_USER,
    126        `Software\\${vendor}\\${app}`,
    127        parentKey.ACCESS_READ | parentKey.ACCESS_WRITE | parentKey.WOW64_64
    128      );
    129      parentKey.removeChild("Installer");
    130      parentKey.close();
    131    } catch (ex) {
    132      // Nothing we can do, and this isn't worth failing the test over.
    133    }
    134  } else {
    135    key.close();
    136  }
    137 }