tor-browser

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

browser_connection.js (4970B)


      1 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
      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 function test() {
      7  waitForExplicitFinish();
      8 
      9  // network.proxy.type needs to be backed up and restored because mochitest
     10  // changes this setting from the default
     11  let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type");
     12  registerCleanupFunction(function () {
     13    Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType);
     14    Services.prefs.clearUserPref("network.proxy.no_proxies_on");
     15    // On accepting the dialog, we also write TRR values, so we need to clear
     16    // them. They are tested separately in browser_privacy_dnsoverhttps.js.
     17    Services.prefs.clearUserPref("network.trr.mode");
     18    Services.prefs.clearUserPref("network.trr.uri");
     19  });
     20 
     21  let connectionURL =
     22    "chrome://browser/content/preferences/dialogs/connection.xhtml";
     23 
     24  /*
     25  The connection dialog alone won't save onaccept since it uses type="child",
     26  so it has to be opened as a sub dialog of the main pref tab.
     27  Open the main tab here.
     28  */
     29  open_preferences(async function tabOpened() {
     30    is(
     31      gBrowser.currentURI.spec,
     32      "about:preferences",
     33      "about:preferences loaded"
     34    );
     35    let dialog = await openAndLoadSubDialog(connectionURL);
     36    let dialogElement = dialog.document.getElementById("ConnectionsDialog");
     37    let dialogClosingPromise = BrowserTestUtils.waitForEvent(
     38      dialogElement,
     39      "dialogclosing"
     40    );
     41 
     42    ok(dialog, "connection window opened");
     43    runConnectionTests(dialog);
     44    dialogElement.acceptDialog();
     45 
     46    let dialogClosingEvent = await dialogClosingPromise;
     47    ok(dialogClosingEvent, "connection window closed");
     48    // runConnectionTests will have changed this pref - make sure it was
     49    // sanitized correctly when the dialog was accepted
     50    is(
     51      Services.prefs.getCharPref("network.proxy.no_proxies_on"),
     52      ".a.com,.b.com,.c.com",
     53      "no_proxies_on pref has correct value"
     54    );
     55    gBrowser.removeCurrentTab();
     56    finish();
     57  });
     58 }
     59 
     60 // run a bunch of tests on the window containing connection.xul
     61 function runConnectionTests(win) {
     62  let doc = win.document;
     63  let networkProxyNone = doc.getElementById("networkProxyNone");
     64  let networkProxyNonePref = win.Preferences.get("network.proxy.no_proxies_on");
     65  let networkProxyTypePref = win.Preferences.get("network.proxy.type");
     66 
     67  // make sure the networkProxyNone textbox is formatted properly
     68  is(networkProxyNone.localName, "textarea", "networkProxyNone is a textarea");
     69  is(
     70    networkProxyNone.getAttribute("rows"),
     71    "2",
     72    "networkProxyNone textbox has two rows"
     73  );
     74 
     75  // make sure manual proxy controls are disabled when the window is opened
     76  let networkProxyHTTP = doc.getElementById("networkProxyHTTP");
     77  is(networkProxyHTTP.disabled, true, "networkProxyHTTP textbox is disabled");
     78 
     79  // check if sanitizing the given input for the no_proxies_on pref results in
     80  // expected string
     81  function testSanitize(input, expected, errorMessage) {
     82    networkProxyNonePref.value = input;
     83    win.gConnectionsDialog.sanitizeNoProxiesPref();
     84    is(networkProxyNonePref.value, expected, errorMessage);
     85  }
     86 
     87  // change this pref so proxy exceptions are actually configurable
     88  networkProxyTypePref.value = 1;
     89  is(networkProxyNone.disabled, false, "networkProxyNone textbox is enabled");
     90 
     91  testSanitize(".a.com", ".a.com", "sanitize doesn't mess up single filter");
     92  testSanitize(
     93    ".a.com, .b.com, .c.com",
     94    ".a.com, .b.com, .c.com",
     95    "sanitize doesn't mess up multiple comma/space sep filters"
     96  );
     97  testSanitize(
     98    ".a.com\n.b.com",
     99    ".a.com,.b.com",
    100    "sanitize turns line break into comma"
    101  );
    102  testSanitize(
    103    ".a.com,\n.b.com",
    104    ".a.com,.b.com",
    105    "sanitize doesn't add duplicate comma after comma"
    106  );
    107  testSanitize(
    108    ".a.com\n,.b.com",
    109    ".a.com,.b.com",
    110    "sanitize doesn't add duplicate comma before comma"
    111  );
    112  testSanitize(
    113    ".a.com,\n,.b.com",
    114    ".a.com,,.b.com",
    115    "sanitize doesn't add duplicate comma surrounded by commas"
    116  );
    117  testSanitize(
    118    ".a.com, \n.b.com",
    119    ".a.com, .b.com",
    120    "sanitize doesn't add comma after comma/space"
    121  );
    122  testSanitize(
    123    ".a.com\n .b.com",
    124    ".a.com, .b.com",
    125    "sanitize adds comma before space"
    126  );
    127  testSanitize(
    128    ".a.com\n\n\n;;\n;\n.b.com",
    129    ".a.com,.b.com",
    130    "sanitize only adds one comma per substring of bad chars"
    131  );
    132  testSanitize(
    133    ".a.com,,.b.com",
    134    ".a.com,,.b.com",
    135    "duplicate commas from user are untouched"
    136  );
    137  testSanitize(
    138    ".a.com\n.b.com\n.c.com,\n.d.com,\n.e.com",
    139    ".a.com,.b.com,.c.com,.d.com,.e.com",
    140    "sanitize replaces things globally"
    141  );
    142 
    143  // will check that this was sanitized properly after window closes
    144  networkProxyNonePref.value = ".a.com;.b.com\n.c.com";
    145 }