summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_dns_retry.js
blob: 2ac313165d22d842d9f6ec7f2fe2372c38f01fa2 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
trr_test_setup();
let httpServerIPv4 = new HttpServer();
let httpServerIPv6 = new HttpServer();
let trrServer;
let testpath = "/simple";
let httpbody = "0123456789";
let CC_IPV4 = "example_cc_ipv4.com";
let CC_IPV6 = "example_cc_ipv6.com";
Services.prefs.clearUserPref("network.dns.native-is-localhost");

XPCOMUtils.defineLazyGetter(this, "URL_CC_IPV4", function () {
  return `http://${CC_IPV4}:${httpServerIPv4.identity.primaryPort}${testpath}`;
});
XPCOMUtils.defineLazyGetter(this, "URL_CC_IPV6", function () {
  return `http://${CC_IPV6}:${httpServerIPv6.identity.primaryPort}${testpath}`;
});
XPCOMUtils.defineLazyGetter(this, "URL6a", function () {
  return `http://example6a.com:${httpServerIPv6.identity.primaryPort}${testpath}`;
});
XPCOMUtils.defineLazyGetter(this, "URL6b", function () {
  return `http://example6b.com:${httpServerIPv6.identity.primaryPort}${testpath}`;
});

const ncs = Cc[
  "@mozilla.org/network/network-connectivity-service;1"
].getService(Ci.nsINetworkConnectivityService);
const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

registerCleanupFunction(async () => {
  Services.prefs.clearUserPref("network.http.speculative-parallel-limit");
  Services.prefs.clearUserPref("network.captive-portal-service.testMode");
  Services.prefs.clearUserPref("network.connectivity-service.IPv6.url");
  Services.prefs.clearUserPref("network.connectivity-service.IPv4.url");
  Services.prefs.clearUserPref("network.dns.localDomains");

  trr_clear_prefs();
  await httpServerIPv4.stop();
  await httpServerIPv6.stop();
  await trrServer.stop();
});

function makeChan(url) {
  let chan = NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  chan.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
  chan.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
  chan.setTRRMode(Ci.nsIRequest.TRR_DEFAULT_MODE);
  return chan;
}

function serverHandler(metadata, response) {
  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}

add_task(async function test_setup() {
  httpServerIPv4.registerPathHandler(testpath, serverHandler);
  httpServerIPv4.start(-1);
  httpServerIPv6.registerPathHandler(testpath, serverHandler);
  httpServerIPv6.start_ipv6(-1);
  Services.prefs.setCharPref(
    "network.dns.localDomains",
    `foo.example.com, ${CC_IPV4}, ${CC_IPV6}`
  );

  trrServer = new TRRServer();
  await trrServer.start();

  if (mozinfo.socketprocess_networking) {
    Services.dns; // Needed to trigger socket process.
    await TestUtils.waitForCondition(() => Services.io.socketProcessLaunched);
  }

  Services.prefs.setIntPref("network.trr.mode", 3);
  Services.prefs.setCharPref(
    "network.trr.uri",
    `https://foo.example.com:${trrServer.port}/dns-query`
  );

  await registerDoHAnswers(true, true);
});

async function registerDoHAnswers(ipv4, ipv6) {
  let hosts = ["example6a.com", "example6b.com"];
  for (const host of hosts) {
    let ipv4answers = [];
    if (ipv4) {
      ipv4answers = [
        {
          name: host,
          ttl: 55,
          type: "A",
          flush: false,
          data: "127.0.0.1",
        },
      ];
    }
    await trrServer.registerDoHAnswers(host, "A", {
      answers: ipv4answers,
    });

    let ipv6answers = [];
    if (ipv6) {
      ipv6answers = [
        {
          name: host,
          ttl: 55,
          type: "AAAA",
          flush: false,
          data: "::1",
        },
      ];
    }

    await trrServer.registerDoHAnswers(host, "AAAA", {
      answers: ipv6answers,
    });
  }

  Services.dns.clearCache(true);
}

let StatusCounter = function () {
  this._statusCount = {};
};
StatusCounter.prototype = {
  QueryInterface: ChromeUtils.generateQI([
    "nsIInterfaceRequestor",
    "nsIProgressEventSink",
  ]),

  getInterface(iid) {
    return this.QueryInterface(iid);
  },

  onProgress(request, progress, progressMax) {},
  onStatus(request, status, statusArg) {
    this._statusCount[status] = 1 + (this._statusCount[status] || 0);
  },
};

let HttpListener = function (finish, succeeded) {
  this.finish = finish;
  this.succeeded = succeeded;
};

HttpListener.prototype = {
  onStartRequest: function testOnStartRequest(request) {},

  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
    read_stream(stream, cnt);
  },

  onStopRequest: function testOnStopRequest(request, status) {
    equal(this.succeeded, status == Cr.NS_OK);
    this.finish();
  },
};

function promiseObserverNotification(aTopic, matchFunc) {
  return new Promise((resolve, reject) => {
    Services.obs.addObserver(function observe(subject, topic, data) {
      let matches = typeof matchFunc != "function" || matchFunc(subject, data);
      if (!matches) {
        return;
      }
      Services.obs.removeObserver(observe, topic);
      resolve({ subject, data });
    }, aTopic);
  });
}

async function make_request(uri, check_events, succeeded) {
  let chan = makeChan(uri);
  let statusCounter = new StatusCounter();
  chan.notificationCallbacks = statusCounter;
  await new Promise(resolve =>
    chan.asyncOpen(new HttpListener(resolve, succeeded))
  );

  if (check_events) {
    equal(
      statusCounter._statusCount[0x4b000b] || 0,
      1,
      "Expecting only one instance of NS_NET_STATUS_RESOLVED_HOST"
    );
    equal(
      statusCounter._statusCount[0x4b0007] || 0,
      1,
      "Expecting only one instance of NS_NET_STATUS_CONNECTING_TO"
    );
  }
}

async function setup_connectivity(ipv6, ipv4) {
  Services.prefs.setBoolPref("network.captive-portal-service.testMode", true);

  if (ipv6) {
    Services.prefs.setCharPref(
      "network.connectivity-service.IPv6.url",
      URL_CC_IPV6 + testpath
    );
  } else {
    Services.prefs.setCharPref(
      "network.connectivity-service.IPv6.url",
      "http://donotexist.example.com"
    );
  }

  if (ipv4) {
    Services.prefs.setCharPref(
      "network.connectivity-service.IPv4.url",
      URL_CC_IPV4 + testpath
    );
  } else {
    Services.prefs.setCharPref(
      "network.connectivity-service.IPv4.url",
      "http://donotexist.example.com"
    );
  }

  let topic = "network:connectivity-service:ip-checks-complete";
  if (mozinfo.socketprocess_networking) {
    topic += "-from-socket-process";
  }
  let observerNotification = promiseObserverNotification(topic);
  ncs.recheckIPConnectivity();
  await observerNotification;

  if (!ipv6) {
    equal(
      ncs.IPv6,
      Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
      "Check IPv6 support"
    );
  } else {
    equal(ncs.IPv6, Ci.nsINetworkConnectivityService.OK, "Check IPv6 support");
  }

  if (!ipv4) {
    equal(
      ncs.IPv4,
      Ci.nsINetworkConnectivityService.NOT_AVAILABLE,
      "Check IPv4 support"
    );
  } else {
    equal(ncs.IPv4, Ci.nsINetworkConnectivityService.OK, "Check IPv4 support");
  }
}

// This test that we retry to connect using IPv4 when IPv6 connecivity is not
// present, but a ConnectionEntry have IPv6 prefered set.
// Speculative connections are disabled.
add_task(async function test_prefer_address_version_fail_trr3_1() {
  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 0);
  await registerDoHAnswers(true, true);

  // Make a request to setup the address version preference to a ConnectionEntry.
  await make_request(URL6a, true, true);

  // connect again using the address version preference from the ConnectionEntry.
  await make_request(URL6a, true, true);

  // Make IPv6 connectivity check fail
  await setup_connectivity(false, true);

  Services.dns.clearCache(true);

  // This will succeed as we query both DNS records
  await make_request(URL6a, true, true);

  // Now make the DNS server only return IPv4 records
  await registerDoHAnswers(true, false);
  // This will fail, because the server is not lisenting to IPv4 address as well,
  // We should still get NS_NET_STATUS_RESOLVED_HOST and
  // NS_NET_STATUS_CONNECTING_TO notification.
  await make_request(URL6a, true, false);

  // Make IPv6 connectivity check succeed again
  await setup_connectivity(true, true);
});

// This test that we retry to connect using IPv4 when IPv6 connecivity is not
// present, but a ConnectionEntry have IPv6 prefered set.
// Speculative connections are enabled.
add_task(async function test_prefer_address_version_fail_trr3_2() {
  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
  await registerDoHAnswers(true, true);

  // Make a request to setup the address version preference to a ConnectionEntry.
  await make_request(URL6b, false, true);

  // connect again using the address version preference from the ConnectionEntry.
  await make_request(URL6b, false, true);

  // Make IPv6 connectivity check fail
  await setup_connectivity(false, true);

  Services.dns.clearCache(true);

  // This will succeed as we query both DNS records
  await make_request(URL6b, false, true);

  // Now make the DNS server only return IPv4 records
  await registerDoHAnswers(true, false);
  // This will fail, because the server is not lisenting to IPv4 address as well,
  // We should still get NS_NET_STATUS_RESOLVED_HOST and
  // NS_NET_STATUS_CONNECTING_TO notification.
  await make_request(URL6b, true, false);
});