summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_dns_override_for_localhost.js
blob: ecd708ee53531c2b8f1adb7dec7e3721a1b776ad (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
89
90
91
92
"use strict";

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

class Listener {
  constructor() {
    this.promise = new Promise(resolve => {
      this.resolve = resolve;
    });
  }

  onLookupComplete(inRequest, inRecord, inStatus) {
    this.resolve([inRequest, inRecord, inStatus]);
  }

  async addresses() {
    let [, inRecord] = await this.promise;
    let addresses = [];
    if (!inRecord) {
      return addresses; // returns []
    }
    inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
    while (inRecord.hasMore()) {
      addresses.push(inRecord.getNextAddrAsString());
    }
    return addresses;
  }

  then() {
    return this.promise.then.apply(this.promise, arguments);
  }
}
Listener.prototype.QueryInterface = ChromeUtils.generateQI(["nsIDNSListener"]);

const DOMAINS = [
  "localhost",
  "localhost.",
  "vhost.localhost",
  "vhost.localhost.",
];
DOMAINS.forEach(domain => {
  add_task(async function test_() {
    let listener1 = new Listener();
    const overrides = ["1.2.3.4", "5.6.7.8"];
    overrides.forEach(ip_address => {
      override.addIPOverride(domain, ip_address);
    });

    // Verify that loopback host names are not overridden.
    Services.dns.asyncResolve(
      domain,
      Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
      0,
      null,
      listener1,
      mainThread,
      defaultOriginAttributes
    );
    Assert.deepEqual(
      await listener1.addresses(),
      ["127.0.0.1", "::1"],
      `${domain} is not overridden`
    );

    // Verify that if localhost hijacking is enabled, the overrides
    // registered above are taken into account.
    Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
    let listener2 = new Listener();
    Services.dns.asyncResolve(
      domain,
      Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
      0,
      null,
      listener2,
      mainThread,
      defaultOriginAttributes
    );
    Assert.deepEqual(
      await listener2.addresses(),
      overrides,
      `${domain} is overridden`
    );
    Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");

    Services.dns.clearCache(false);
    override.clearOverrides();
  });
});