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