Backed out 2 changesets (bug 1827651) for causing TestAUSHelper related bustages...
[gecko.git] / netwerk / test / unit / test_https_rr_ech_prefs.js
blobf70c672dde2e8dba82695f06ab9ecc05e0fe1b76
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 let trrServer;
9 function setup() {
10   trr_test_setup();
12   Services.prefs.setBoolPref("network.dns.upgrade_with_https_rr", true);
13   Services.prefs.setBoolPref("network.dns.use_https_rr_as_altsvc", true);
14   Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
17 setup();
18 registerCleanupFunction(async () => {
19   trr_clear_prefs();
20   Services.prefs.clearUserPref("network.dns.upgrade_with_https_rr");
21   Services.prefs.clearUserPref("network.dns.use_https_rr_as_altsvc");
22   Services.prefs.clearUserPref("network.dns.echconfig.enabled");
23   Services.prefs.clearUserPref("network.dns.http3_echconfig.enabled");
24   Services.prefs.clearUserPref("network.http.http3.enable");
25   Services.prefs.clearUserPref("network.http.http2.enabled");
26   if (trrServer) {
27     await trrServer.stop();
28   }
29 });
31 function checkResult(inRecord, noHttp2, noHttp3, result) {
32   if (!result) {
33     Assert.throws(
34       () => {
35         inRecord
36           .QueryInterface(Ci.nsIDNSHTTPSSVCRecord)
37           .GetServiceModeRecord(noHttp2, noHttp3);
38       },
39       /NS_ERROR_NOT_AVAILABLE/,
40       "Should get an error"
41     );
42     return;
43   }
45   let record = inRecord
46     .QueryInterface(Ci.nsIDNSHTTPSSVCRecord)
47     .GetServiceModeRecord(noHttp2, noHttp3);
48   Assert.equal(record.priority, result.expectedPriority);
49   Assert.equal(record.name, result.expectedName);
50   Assert.equal(record.selectedAlpn, result.expectedAlpn);
53 // Test configuration:
54 //   There are two records: one has a echConfig and the other doesn't.
55 // We want to test if the record with echConfig is preferred.
56 add_task(async function testEchConfigEnabled() {
57   trrServer = new TRRServer();
58   await trrServer.start();
60   Services.prefs.setIntPref("network.trr.mode", 3);
61   Services.prefs.setCharPref(
62     "network.trr.uri",
63     `https://foo.example.com:${trrServer.port()}/dns-query`
64   );
65   Services.prefs.setBoolPref("network.dns.echconfig.enabled", false);
67   await trrServer.registerDoHAnswers("test.bar.com", "HTTPS", {
68     answers: [
69       {
70         name: "test.bar.com",
71         ttl: 55,
72         type: "HTTPS",
73         flush: false,
74         data: {
75           priority: 1,
76           name: "test.bar_1.com",
77           values: [{ key: "alpn", value: ["h3-29"] }],
78         },
79       },
80       {
81         name: "test.bar.com",
82         ttl: 55,
83         type: "HTTPS",
84         flush: false,
85         data: {
86           priority: 2,
87           name: "test.bar_2.com",
88           values: [
89             { key: "alpn", value: ["h2"] },
90             { key: "echconfig", value: "456..." },
91           ],
92         },
93       },
94     ],
95   });
97   let { inRecord } = await new TRRDNSListener("test.bar.com", {
98     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
99   });
101   checkResult(inRecord, false, false, {
102     expectedPriority: 1,
103     expectedName: "test.bar_1.com",
104     expectedAlpn: "h3-29",
105   });
106   checkResult(inRecord, false, true, {
107     expectedPriority: 2,
108     expectedName: "test.bar_2.com",
109     expectedAlpn: "h2",
110   });
111   checkResult(inRecord, true, false, {
112     expectedPriority: 1,
113     expectedName: "test.bar_1.com",
114     expectedAlpn: "h3-29",
115   });
116   checkResult(inRecord, true, true);
118   Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
119   Services.dns.clearCache(true);
121   ({ inRecord } = await new TRRDNSListener("test.bar.com", {
122     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
123   }));
125   checkResult(inRecord, false, false, {
126     expectedPriority: 2,
127     expectedName: "test.bar_2.com",
128     expectedAlpn: "h2",
129   });
130   checkResult(inRecord, false, true, {
131     expectedPriority: 2,
132     expectedName: "test.bar_2.com",
133     expectedAlpn: "h2",
134   });
135   checkResult(inRecord, true, false, {
136     expectedPriority: 1,
137     expectedName: "test.bar_1.com",
138     expectedAlpn: "h3-29",
139   });
140   checkResult(inRecord, true, true);
142   await trrServer.stop();
143   trrServer = null;
146 // Test configuration:
147 //   There are two records: both have echConfigs, and only one supports h3.
148 // This test is about testing which record should we get when
149 // network.dns.http3_echconfig.enabled is true and false.
150 // When network.dns.http3_echconfig.enabled is false, we should try to
151 // connect with h2 and echConfig.
152 add_task(async function testTwoRecordsHaveEchConfig() {
153   Services.dns.clearCache(true);
155   trrServer = new TRRServer();
156   await trrServer.start();
158   Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
159   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", false);
160   Services.prefs.setIntPref("network.trr.mode", 3);
161   Services.prefs.setCharPref(
162     "network.trr.uri",
163     `https://foo.example.com:${trrServer.port()}/dns-query`
164   );
166   await trrServer.registerDoHAnswers("test.foo.com", "HTTPS", {
167     answers: [
168       {
169         name: "test.foo.com",
170         ttl: 55,
171         type: "HTTPS",
172         flush: false,
173         data: {
174           priority: 1,
175           name: "test.foo_h3.com",
176           values: [
177             { key: "alpn", value: ["h3"] },
178             { key: "echconfig", value: "456..." },
179           ],
180         },
181       },
182       {
183         name: "test.foo.com",
184         ttl: 55,
185         type: "HTTPS",
186         flush: false,
187         data: {
188           priority: 2,
189           name: "test.foo_h2.com",
190           values: [
191             { key: "alpn", value: ["h2"] },
192             { key: "echconfig", value: "456..." },
193           ],
194         },
195       },
196     ],
197   });
199   let { inRecord } = await new TRRDNSListener("test.foo.com", {
200     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
201   });
203   checkResult(inRecord, false, false, {
204     expectedPriority: 2,
205     expectedName: "test.foo_h2.com",
206     expectedAlpn: "h2",
207   });
208   checkResult(inRecord, false, true, {
209     expectedPriority: 2,
210     expectedName: "test.foo_h2.com",
211     expectedAlpn: "h2",
212   });
213   checkResult(inRecord, true, false, {
214     expectedPriority: 1,
215     expectedName: "test.foo_h3.com",
216     expectedAlpn: "h3",
217   });
218   checkResult(inRecord, true, true);
220   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", true);
221   Services.dns.clearCache(true);
222   ({ inRecord } = await new TRRDNSListener("test.foo.com", {
223     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
224   }));
226   checkResult(inRecord, false, false, {
227     expectedPriority: 1,
228     expectedName: "test.foo_h3.com",
229     expectedAlpn: "h3",
230   });
231   checkResult(inRecord, false, true, {
232     expectedPriority: 2,
233     expectedName: "test.foo_h2.com",
234     expectedAlpn: "h2",
235   });
236   checkResult(inRecord, true, false, {
237     expectedPriority: 1,
238     expectedName: "test.foo_h3.com",
239     expectedAlpn: "h3",
240   });
241   checkResult(inRecord, true, true);
243   await trrServer.stop();
244   trrServer = null;
247 // Test configuration:
248 //   There are two records: both have echConfigs, and one supports h3 and h2.
249 // When network.dns.http3_echconfig.enabled is false, we should use the record
250 // that supports h3 and h2 (the alpn is h2).
251 add_task(async function testTwoRecordsHaveEchConfig1() {
252   Services.dns.clearCache(true);
254   trrServer = new TRRServer();
255   await trrServer.start();
257   Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
258   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", false);
259   Services.prefs.setIntPref("network.trr.mode", 3);
260   Services.prefs.setCharPref(
261     "network.trr.uri",
262     `https://foo.example.com:${trrServer.port()}/dns-query`
263   );
265   await trrServer.registerDoHAnswers("test.foo.com", "HTTPS", {
266     answers: [
267       {
268         name: "test.foo.com",
269         ttl: 55,
270         type: "HTTPS",
271         flush: false,
272         data: {
273           priority: 1,
274           name: "test.foo_h3.com",
275           values: [
276             { key: "alpn", value: ["h3", "h2"] },
277             { key: "echconfig", value: "456..." },
278           ],
279         },
280       },
281       {
282         name: "test.foo.com",
283         ttl: 55,
284         type: "HTTPS",
285         flush: false,
286         data: {
287           priority: 2,
288           name: "test.foo_h2.com",
289           values: [
290             { key: "alpn", value: ["h2", "http/1.1"] },
291             { key: "echconfig", value: "456..." },
292           ],
293         },
294       },
295     ],
296   });
298   let { inRecord } = await new TRRDNSListener("test.foo.com", {
299     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
300   });
302   checkResult(inRecord, false, false, {
303     expectedPriority: 1,
304     expectedName: "test.foo_h3.com",
305     expectedAlpn: "h2",
306   });
307   checkResult(inRecord, false, true, {
308     expectedPriority: 1,
309     expectedName: "test.foo_h3.com",
310     expectedAlpn: "h2",
311   });
312   checkResult(inRecord, true, false, {
313     expectedPriority: 2,
314     expectedName: "test.foo_h2.com",
315     expectedAlpn: "http/1.1",
316   });
317   checkResult(inRecord, true, true, {
318     expectedPriority: 2,
319     expectedName: "test.foo_h2.com",
320     expectedAlpn: "http/1.1",
321   });
323   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", true);
324   Services.dns.clearCache(true);
325   ({ inRecord } = await new TRRDNSListener("test.foo.com", {
326     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
327   }));
329   checkResult(inRecord, false, false, {
330     expectedPriority: 1,
331     expectedName: "test.foo_h3.com",
332     expectedAlpn: "h3",
333   });
334   checkResult(inRecord, false, true, {
335     expectedPriority: 1,
336     expectedName: "test.foo_h3.com",
337     expectedAlpn: "h2",
338   });
339   checkResult(inRecord, true, false, {
340     expectedPriority: 1,
341     expectedName: "test.foo_h3.com",
342     expectedAlpn: "h3",
343   });
344   checkResult(inRecord, true, true, {
345     expectedPriority: 2,
346     expectedName: "test.foo_h2.com",
347     expectedAlpn: "http/1.1",
348   });
350   await trrServer.stop();
351   trrServer = null;
354 // Test configuration:
355 //   There are two records: only one support h3 and only one has echConfig.
356 // This test is about never usng the record without echConfig.
357 add_task(async function testOneRecordsHasEchConfig() {
358   Services.dns.clearCache(true);
360   trrServer = new TRRServer();
361   await trrServer.start();
363   Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
364   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", false);
365   Services.prefs.setIntPref("network.trr.mode", 3);
366   Services.prefs.setCharPref(
367     "network.trr.uri",
368     `https://foo.example.com:${trrServer.port()}/dns-query`
369   );
371   await trrServer.registerDoHAnswers("test.foo.com", "HTTPS", {
372     answers: [
373       {
374         name: "test.foo.com",
375         ttl: 55,
376         type: "HTTPS",
377         flush: false,
378         data: {
379           priority: 1,
380           name: "test.foo_h3.com",
381           values: [
382             { key: "alpn", value: ["h3"] },
383             { key: "echconfig", value: "456..." },
384           ],
385         },
386       },
387       {
388         name: "test.foo.com",
389         ttl: 55,
390         type: "HTTPS",
391         flush: false,
392         data: {
393           priority: 2,
394           name: "test.foo_h2.com",
395           values: [{ key: "alpn", value: ["h2"] }],
396         },
397       },
398     ],
399   });
401   let { inRecord } = await new TRRDNSListener("test.foo.com", {
402     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
403   });
405   checkResult(inRecord, false, false, {
406     expectedPriority: 1,
407     expectedName: "test.foo_h3.com",
408     expectedAlpn: "h3",
409   });
410   checkResult(inRecord, false, true, {
411     expectedPriority: 2,
412     expectedName: "test.foo_h2.com",
413     expectedAlpn: "h2",
414   });
415   checkResult(inRecord, true, false, {
416     expectedPriority: 1,
417     expectedName: "test.foo_h3.com",
418     expectedAlpn: "h3",
419   });
420   checkResult(inRecord, true, true);
422   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", true);
423   Services.dns.clearCache(true);
424   ({ inRecord } = await new TRRDNSListener("test.foo.com", {
425     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
426   }));
428   checkResult(inRecord, false, false, {
429     expectedPriority: 1,
430     expectedName: "test.foo_h3.com",
431     expectedAlpn: "h3",
432   });
433   checkResult(inRecord, false, true, {
434     expectedPriority: 2,
435     expectedName: "test.foo_h2.com",
436     expectedAlpn: "h2",
437   });
438   checkResult(inRecord, true, false, {
439     expectedPriority: 1,
440     expectedName: "test.foo_h3.com",
441     expectedAlpn: "h3",
442   });
443   checkResult(inRecord, true, true);
445   await trrServer.stop();
446   trrServer = null;
449 // Test the case that "network.http.http3.enable" and
450 // "network.http.http2.enabled" are true/false.
451 add_task(async function testHttp3AndHttp2Pref() {
452   Services.dns.clearCache(true);
454   trrServer = new TRRServer();
455   await trrServer.start();
457   Services.prefs.setBoolPref("network.http.http3.enable", false);
458   Services.prefs.setBoolPref("network.dns.echconfig.enabled", false);
459   Services.prefs.setBoolPref("network.dns.http3_echconfig.enabled", false);
460   Services.prefs.setIntPref("network.trr.mode", 3);
461   Services.prefs.setCharPref(
462     "network.trr.uri",
463     `https://foo.example.com:${trrServer.port()}/dns-query`
464   );
466   await trrServer.registerDoHAnswers("test.foo.com", "HTTPS", {
467     answers: [
468       {
469         name: "test.foo.com",
470         ttl: 55,
471         type: "HTTPS",
472         flush: false,
473         data: {
474           priority: 1,
475           name: "test.foo_h3.com",
476           values: [
477             { key: "alpn", value: ["h3", "h2"] },
478             { key: "echconfig", value: "456..." },
479           ],
480         },
481       },
482       {
483         name: "test.foo.com",
484         ttl: 55,
485         type: "HTTPS",
486         flush: false,
487         data: {
488           priority: 2,
489           name: "test.foo_h2.com",
490           values: [
491             { key: "alpn", value: ["h2"] },
492             { key: "echconfig", value: "456..." },
493           ],
494         },
495       },
496     ],
497   });
499   let { inRecord } = await new TRRDNSListener("test.foo.com", {
500     type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
501   });
503   checkResult(inRecord, false, false, {
504     expectedPriority: 1,
505     expectedName: "test.foo_h3.com",
506     expectedAlpn: "h2",
507   });
508   checkResult(inRecord, false, true, {
509     expectedPriority: 1,
510     expectedName: "test.foo_h3.com",
511     expectedAlpn: "h2",
512   });
513   checkResult(inRecord, true, false);
514   checkResult(inRecord, true, true);
516   Services.prefs.setBoolPref("network.http.http2.enabled", false);
517   checkResult(inRecord, false, false);
519   Services.prefs.setBoolPref("network.http.http3.enable", true);
520   checkResult(inRecord, false, false, {
521     expectedPriority: 1,
522     expectedName: "test.foo_h3.com",
523     expectedAlpn: "h3",
524   });
525   checkResult(inRecord, false, true);
526   checkResult(inRecord, true, false, {
527     expectedPriority: 1,
528     expectedName: "test.foo_h3.com",
529     expectedAlpn: "h3",
530   });
531   checkResult(inRecord, true, true);
533   await trrServer.stop();
534   trrServer = null;