tor-browser

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

test_dns_offline.js (2733B)


      1 "use strict";
      2 
      3 var ioService = Services.io;
      4 var prefs = Services.prefs;
      5 var mainThread = Services.tm.currentThread;
      6 
      7 var listener1 = {
      8  onLookupComplete(inRequest, inRecord, inStatus) {
      9    Assert.equal(inStatus, Cr.NS_ERROR_OFFLINE);
     10    test2();
     11    do_test_finished();
     12  },
     13 };
     14 
     15 var listener2 = {
     16  onLookupComplete(inRequest, inRecord, inStatus) {
     17    Assert.equal(inStatus, Cr.NS_OK);
     18    inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
     19    var answer = inRecord.getNextAddrAsString();
     20    Assert.ok(answer == "127.0.0.1" || answer == "::1");
     21    test3();
     22    do_test_finished();
     23  },
     24 };
     25 
     26 var listener3 = {
     27  onLookupComplete(inRequest, inRecord, inStatus) {
     28    Assert.equal(inStatus, Cr.NS_OK);
     29    inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
     30    var answer = inRecord.getNextAddrAsString();
     31    Assert.ok(answer == "127.0.0.1" || answer == "::1");
     32    cleanup();
     33    do_test_finished();
     34  },
     35 };
     36 
     37 const defaultOriginAttributes = {};
     38 
     39 function run_test() {
     40  do_test_pending();
     41  prefs.setBoolPref("network.dns.offline-localhost", false);
     42  // We always resolve localhost as it's hardcoded without the following pref:
     43  prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
     44  prefs.setBoolPref(
     45    "network.proxy.testing_localhost_is_secure_when_hijacked",
     46    false
     47  );
     48  ioService.offline = true;
     49  try {
     50    Services.dns.asyncResolve(
     51      "localhost",
     52      Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     53      0,
     54      null, // resolverInfo
     55      listener1,
     56      mainThread,
     57      defaultOriginAttributes
     58    );
     59  } catch (e) {
     60    Assert.equal(e.result, Cr.NS_ERROR_OFFLINE);
     61    test2();
     62    do_test_finished();
     63  }
     64 }
     65 
     66 function test2() {
     67  do_test_pending();
     68  prefs.setBoolPref("network.dns.offline-localhost", true);
     69  ioService.offline = false;
     70  ioService.offline = true;
     71  // we need to let the main thread run and apply the changes
     72  do_timeout(0, test2Continued);
     73 }
     74 
     75 function test2Continued() {
     76  Services.dns.asyncResolve(
     77    "localhost",
     78    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     79    0,
     80    null, // resolverInfo
     81    listener2,
     82    mainThread,
     83    defaultOriginAttributes
     84  );
     85 }
     86 
     87 function test3() {
     88  do_test_pending();
     89  ioService.offline = false;
     90  // we need to let the main thread run and apply the changes
     91  do_timeout(0, test3Continued);
     92 }
     93 
     94 function test3Continued() {
     95  Services.dns.asyncResolve(
     96    "localhost",
     97    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     98    0,
     99    null, // resolverInfo
    100    listener3,
    101    mainThread,
    102    defaultOriginAttributes
    103  );
    104 }
    105 
    106 function cleanup() {
    107  prefs.clearUserPref("network.dns.offline-localhost");
    108  prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
    109  prefs.clearUserPref(
    110    "network.proxy.testing_localhost_is_secure_when_hijacked"
    111  );
    112 }