summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_dns_disabled.js
blob: cfffd5530feb6b3c3e9f7c111dd48f47547c3b31 (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
83
84
85
86
87
88
"use strict";

const override = Cc["@mozilla.org/network/native-dns-override;1"].getService(
  Ci.nsINativeDNSResolverOverride
);
const mainThread = Services.tm.currentThread;

function makeListenerBlock(next) {
  return {
    onLookupComplete(inRequest, inRecord, inStatus) {
      Assert.ok(!Components.isSuccessCode(inStatus));
      next();
    },
  };
}

function makeListenerDontBlock(next, expectedAnswer) {
  return {
    onLookupComplete(inRequest, inRecord, inStatus) {
      Assert.equal(inStatus, Cr.NS_OK);
      inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
      var answer = inRecord.getNextAddrAsString();
      if (expectedAnswer) {
        Assert.equal(answer, expectedAnswer);
      } else {
        Assert.ok(answer == "127.0.0.1" || answer == "::1");
      }
      next();
    },
  };
}

function do_test({ dnsDisabled, mustBlock, testDomain, expectedAnswer }) {
  return new Promise(resolve => {
    Services.prefs.setBoolPref("network.dns.disabled", dnsDisabled);
    try {
      Services.dns.asyncResolve(
        testDomain,
        Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
        0,
        null, // resolverInfo
        mustBlock
          ? makeListenerBlock(resolve)
          : makeListenerDontBlock(resolve, expectedAnswer),
        mainThread,
        {} // Default origin attributes
      );
    } catch (e) {
      Assert.ok(mustBlock === true);
      resolve();
    }
  });
}

function setup() {
  override.addIPOverride("foo.bar", "127.0.0.1");
  registerCleanupFunction(function () {
    override.clearOverrides();
    Services.prefs.clearUserPref("network.dns.disabled");
  });
}
setup();

// IP literals should be resolved even if dns is disabled
add_task(async function testIPLiteral() {
  return do_test({
    dnsDisabled: true,
    mustBlock: false,
    testDomain: "0x01010101",
    expectedAnswer: "1.1.1.1",
  });
});

add_task(async function testBlocked() {
  return do_test({
    dnsDisabled: true,
    mustBlock: true,
    testDomain: "foo.bar",
  });
});

add_task(async function testNotBlocked() {
  return do_test({
    dnsDisabled: false,
    mustBlock: false,
    testDomain: "foo.bar",
  });
});