summaryrefslogtreecommitdiffstats
path: root/docshell/test/unit/test_URIFixup_check_host.js
blob: 8d75fcc0321ba44ccadc2bccf822c44550606d59 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const lazy = {};

// Ensure DNS lookups don't hit the server
XPCOMUtils.defineLazyServiceGetter(
  lazy,
  "gDNSOverride",
  "@mozilla.org/network/native-dns-override;1",
  "nsINativeDNSResolverOverride"
);

add_task(async function setup() {
  Services.prefs.setStringPref("browser.fixup.alternate.prefix", "www.");
  Services.prefs.setStringPref("browser.fixup.alternate.suffix", ".com");
  Services.prefs.setStringPref("browser.fixup.alternate.protocol", "https");
  Services.prefs.setBoolPref(
    "browser.urlbar.dnsResolveFullyQualifiedNames",
    true
  );
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("browser.fixup.alternate.prefix");
    Services.prefs.clearUserPref("browser.fixup.alternate.suffix");
    Services.prefs.clearUserPref("browser.fixup.alternate.protocol");
    Services.prefs.clearUserPref(
      "browser.urlbar.dnsResolveFullyQualifiedNames"
    );
  });
});

// TODO: Export Listener from test_dns_override instead of duping
class Listener {
  constructor() {
    this.promise = new Promise(resolve => {
      this.resolve = resolve;
    });
  }

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

  async firstAddress() {
    let all = await this.addresses();
    if (all.length) {
      return all[0];
    }
    return undefined;
  }

  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);
  }
}

const FAKE_IP = "::1";
const FAKE_INTRANET_IP = "::2";
const ORIGIN_ATTRIBUTE = {};

add_task(async function test_uri_with_force_fixup() {
  let listener = new Listener();
  let { fixedURI } = Services.uriFixup.forceHttpFixup("http://www.example.com");

  lazy.gDNSOverride.addIPOverride(fixedURI.displayHost, FAKE_IP);

  Services.uriFixup.checkHost(fixedURI, listener, ORIGIN_ATTRIBUTE);
  Assert.equal(
    await listener.firstAddress(),
    FAKE_IP,
    "Should've received fake IP"
  );

  lazy.gDNSOverride.clearHostOverride(fixedURI.displayHost);
  Services.dns.clearCache(false);
});

add_task(async function test_uri_with_get_fixup() {
  let listener = new Listener();
  let uri = Services.io.newURI("http://www.example.com");

  lazy.gDNSOverride.addIPOverride(uri.displayHost, FAKE_IP);

  Services.uriFixup.checkHost(uri, listener, ORIGIN_ATTRIBUTE);
  Assert.equal(
    await listener.firstAddress(),
    FAKE_IP,
    "Should've received fake IP"
  );

  lazy.gDNSOverride.clearHostOverride(uri.displayHost);
  Services.dns.clearCache(false);
});

add_task(async function test_intranet_like_uri() {
  let listener = new Listener();
  let uri = Services.io.newURI("http://someintranet");

  lazy.gDNSOverride.addIPOverride(uri.displayHost, FAKE_IP);
  // Hosts without periods should end with a period to make them FQN
  lazy.gDNSOverride.addIPOverride(uri.displayHost + ".", FAKE_INTRANET_IP);

  Services.uriFixup.checkHost(uri, listener, ORIGIN_ATTRIBUTE);
  Assert.deepEqual(
    await listener.addresses(),
    FAKE_INTRANET_IP,
    "Should've received fake intranet IP"
  );

  lazy.gDNSOverride.clearHostOverride(uri.displayHost);
  lazy.gDNSOverride.clearHostOverride(uri.displayHost + ".");
  Services.dns.clearCache(false);
});

add_task(async function test_intranet_like_uri_without_fixup() {
  let listener = new Listener();
  let uri = Services.io.newURI("http://someintranet");
  Services.prefs.setBoolPref(
    "browser.urlbar.dnsResolveFullyQualifiedNames",
    false
  );

  lazy.gDNSOverride.addIPOverride(uri.displayHost, FAKE_IP);
  // Hosts without periods should end with a period to make them FQN
  lazy.gDNSOverride.addIPOverride(uri.displayHost + ".", FAKE_INTRANET_IP);

  Services.uriFixup.checkHost(uri, listener, ORIGIN_ATTRIBUTE);
  Assert.deepEqual(
    await listener.addresses(),
    FAKE_IP,
    "Should've received non-fixed up IP"
  );

  lazy.gDNSOverride.clearHostOverride(uri.displayHost);
  lazy.gDNSOverride.clearHostOverride(uri.displayHost + ".");
  Services.dns.clearCache(false);
});

add_task(async function test_ip_address() {
  let listener = new Listener();
  let uri = Services.io.newURI("http://192.168.0.1");

  // Testing IP address being passed into the method
  // requires observing if the asyncResolve method gets called
  let didResolve = false;
  let topic = "uri-fixup-check-dns";
  let observer = {
    observe(aSubject, aTopicInner) {
      if (aTopicInner == topic) {
        didResolve = true;
      }
    },
  };
  Services.obs.addObserver(observer, topic);
  lazy.gDNSOverride.addIPOverride(uri.displayHost, FAKE_IP);

  Services.uriFixup.checkHost(uri, listener, ORIGIN_ATTRIBUTE);
  Assert.equal(
    didResolve,
    false,
    "Passing an IP address should not conduct a host lookup"
  );

  lazy.gDNSOverride.clearHostOverride(uri.displayHost);
  Services.dns.clearCache(false);
  Services.obs.removeObserver(observer, topic);
});