summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_dns_onion.js
blob: b7824fed3f51822fbc9d47bbb31d26c3cfe29f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict";

var dns = Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService);
var threadManager = Cc["@mozilla.org/thread-manager;1"].getService(
  Ci.nsIThreadManager
);
var mainThread = threadManager.currentThread;

var onionPref;
var localdomainPref;
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(
  Ci.nsIPrefBranch
);

// check that we don't lookup .onion
var listenerBlock = {
  onLookupComplete(inRequest, inRecord, inStatus) {
    Assert.ok(!Components.isSuccessCode(inStatus));
    do_test_dontBlock();
  },
  QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
};

// check that we do lookup .onion (via pref)
var listenerDontBlock = {
  onLookupComplete(inRequest, inRecord, inStatus) {
    inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
    var answer = inRecord.getNextAddrAsString();
    Assert.ok(answer == "127.0.0.1" || answer == "::1");
    all_done();
  },
  QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
};

const defaultOriginAttributes = {};

function do_test_dontBlock() {
  prefs.setBoolPref("network.dns.blockDotOnion", false);
  dns.asyncResolve(
    "private.onion",
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    0,
    null, // resolverInfo
    listenerDontBlock,
    mainThread,
    defaultOriginAttributes
  );
}

function do_test_block() {
  prefs.setBoolPref("network.dns.blockDotOnion", true);
  try {
    dns.asyncResolve(
      "private.onion",
      Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
      0,
      null, // resolverInfo
      listenerBlock,
      mainThread,
      defaultOriginAttributes
    );
  } catch (e) {
    // it is ok for this negative test to fail fast
    Assert.ok(true);
    do_test_dontBlock();
  }
}

function all_done() {
  // reset locally modified prefs
  prefs.setCharPref("network.dns.localDomains", localdomainPref);
  prefs.setBoolPref("network.dns.blockDotOnion", onionPref);
  do_test_finished();
}

function run_test() {
  onionPref = prefs.getBoolPref("network.dns.blockDotOnion");
  localdomainPref = prefs.getCharPref("network.dns.localDomains");
  prefs.setCharPref("network.dns.localDomains", "private.onion");
  do_test_block();
  do_test_pending();
}