Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_trr_noPrefetch.js
blobca3188a3700bb5afda154db83658813bc90b89d9
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 = null;
8 add_setup(async function setup() {
9   if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
10     return;
11   }
13   trr_test_setup();
14   Services.prefs.setBoolPref("network.dns.disablePrefetch", true);
15   registerCleanupFunction(async () => {
16     Services.prefs.clearUserPref("network.dns.disablePrefetch");
17     trr_clear_prefs();
18   });
20   trrServer = new TRRServer();
21   registerCleanupFunction(async () => {
22     await trrServer.stop();
23   });
24   await trrServer.start();
26   Services.prefs.setCharPref(
27     "network.trr.uri",
28     `https://foo.example.com:${trrServer.port()}/dns-query`
29   );
30   Services.prefs.setIntPref("network.trr.mode", Ci.nsIDNSService.MODE_TRRONLY);
32   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
33     Ci.nsIX509CertDB
34   );
35   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
37   // We need to define both A and AAAA responses, otherwise
38   // we might race and pick up the skip reason for the other request.
39   await trrServer.registerDoHAnswers(`myfoo.test`, "A", {
40     answers: [],
41   });
42   await trrServer.registerDoHAnswers(`myfoo.test`, "AAAA", {
43     answers: [],
44   });
46   // myfoo2.test will return sever error as it's not defined
48   // return nxdomain for this one
49   await trrServer.registerDoHAnswers(`myfoo3.test`, "A", {
50     flags: 0x03,
51     answers: [],
52   });
53   await trrServer.registerDoHAnswers(`myfoo3.test`, "AAAA", {
54     flags: 0x03,
55     answers: [],
56   });
58   await trrServer.registerDoHAnswers(`alt1.example.com`, "A", {
59     answers: [
60       {
61         name: "alt1.example.com",
62         ttl: 55,
63         type: "A",
64         flush: false,
65         data: "127.0.0.1",
66       },
67     ],
68   });
69 });
71 add_task(async function test_failure() {
72   let req = await new Promise(resolve => {
73     let chan = NetUtil.newChannel({
74       uri: `http://myfoo.test/`,
75       loadUsingSystemPrincipal: true,
76     }).QueryInterface(Ci.nsIHttpChannel);
77     chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
78   });
80   equal(req.status, Cr.NS_ERROR_UNKNOWN_HOST);
81   equal(
82     req.QueryInterface(Ci.nsIHttpChannelInternal).effectiveTRRMode,
83     Ci.nsIRequest.TRR_ONLY_MODE
84   );
85   equal(
86     req.QueryInterface(Ci.nsIHttpChannelInternal).trrSkipReason,
87     Ci.nsITRRSkipReason.TRR_NO_ANSWERS
88   );
90   req = await new Promise(resolve => {
91     let chan = NetUtil.newChannel({
92       uri: `http://myfoo2.test/`,
93       loadUsingSystemPrincipal: true,
94     }).QueryInterface(Ci.nsIHttpChannel);
95     chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
96   });
98   equal(req.status, Cr.NS_ERROR_UNKNOWN_HOST);
99   equal(
100     req.QueryInterface(Ci.nsIHttpChannelInternal).effectiveTRRMode,
101     Ci.nsIRequest.TRR_ONLY_MODE
102   );
103   equal(
104     req.QueryInterface(Ci.nsIHttpChannelInternal).trrSkipReason,
105     Ci.nsITRRSkipReason.TRR_RCODE_FAIL
106   );
108   req = await new Promise(resolve => {
109     let chan = NetUtil.newChannel({
110       uri: `http://myfoo3.test/`,
111       loadUsingSystemPrincipal: true,
112     }).QueryInterface(Ci.nsIHttpChannel);
113     chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
114   });
116   equal(req.status, Cr.NS_ERROR_UNKNOWN_HOST);
117   equal(
118     req.QueryInterface(Ci.nsIHttpChannelInternal).effectiveTRRMode,
119     Ci.nsIRequest.TRR_ONLY_MODE
120   );
121   equal(
122     req.QueryInterface(Ci.nsIHttpChannelInternal).trrSkipReason,
123     Ci.nsITRRSkipReason.TRR_NXDOMAIN
124   );
127 add_task(async function test_success() {
128   let httpServer = new NodeHTTP2Server();
129   await httpServer.start();
130   await httpServer.registerPathHandler("/", (req, resp) => {
131     resp.writeHead(200);
132     resp.end("done");
133   });
134   registerCleanupFunction(async () => {
135     await httpServer.stop();
136   });
138   let req = await new Promise(resolve => {
139     let chan = NetUtil.newChannel({
140       uri: `https://alt1.example.com:${httpServer.port()}/`,
141       loadUsingSystemPrincipal: true,
142     }).QueryInterface(Ci.nsIHttpChannel);
143     chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
144   });
146   equal(req.status, Cr.NS_OK);
147   equal(
148     req.QueryInterface(Ci.nsIHttpChannelInternal).effectiveTRRMode,
149     Ci.nsIRequest.TRR_ONLY_MODE
150   );
151   equal(
152     req.QueryInterface(Ci.nsIHttpChannelInternal).trrSkipReason,
153     Ci.nsITRRSkipReason.TRR_OK
154   );
156   // Another request to check connection reuse
157   req = await new Promise(resolve => {
158     let chan = NetUtil.newChannel({
159       uri: `https://alt1.example.com:${httpServer.port()}/second`,
160       loadUsingSystemPrincipal: true,
161     }).QueryInterface(Ci.nsIHttpChannel);
162     chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL));
163   });
165   equal(req.status, Cr.NS_OK);
166   equal(
167     req.QueryInterface(Ci.nsIHttpChannelInternal).effectiveTRRMode,
168     Ci.nsIRequest.TRR_ONLY_MODE
169   );
170   equal(
171     req.QueryInterface(Ci.nsIHttpChannelInternal).trrSkipReason,
172     Ci.nsITRRSkipReason.TRR_OK
173   );