Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / test / unit / test_http3_server_not_existing.js
blobcdaebc141209a3a4c083a941cee91da2730ca162
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 "use strict";
6 let httpsUri;
8 registerCleanupFunction(async () => {
9   Services.prefs.clearUserPref("network.http.http3.enable");
10   Services.prefs.clearUserPref("network.dns.localDomains");
11   Services.prefs.clearUserPref("network.dns.disableIPv6");
12   Services.prefs.clearUserPref(
13     "network.http.http3.alt-svc-mapping-for-testing"
14   );
15   Services.prefs.clearUserPref("network.http.http3.backup_timer_delay");
16   dump("cleanup done\n");
17 });
19 function makeChan() {
20   let chan = NetUtil.newChannel({
21     uri: httpsUri,
22     loadUsingSystemPrincipal: true,
23   }).QueryInterface(Ci.nsIHttpChannel);
24   chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
25   return chan;
28 function altsvcSetupPromise(chan, listener) {
29   return new Promise(resolve => {
30     function finish(result) {
31       resolve(result);
32     }
33     listener.finish = finish;
34     chan.asyncOpen(listener);
35   });
38 add_task(async function test_fatal_error() {
39   let h2Port = Services.env.get("MOZHTTP2_PORT");
40   Assert.notEqual(h2Port, null);
42   Services.prefs.setBoolPref("network.http.http3.enable", true);
43   Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
44   Services.prefs.setBoolPref("network.dns.disableIPv6", true);
45   // Set AltSvc to point to not existing HTTP3 server on port 443
46   Services.prefs.setCharPref(
47     "network.http.http3.alt-svc-mapping-for-testing",
48     "foo.example.com;h3-29=:443"
49   );
50   Services.prefs.setIntPref("network.http.http3.backup_timer_delay", 0);
52   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
53     Ci.nsIX509CertDB
54   );
55   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
57   httpsUri = "https://foo.example.com:" + h2Port + "/";
58 });
60 add_task(async function test_fatal_stream_error() {
61   let result = 1;
62   // We need to loop here because we need to wait for AltSvc storage to
63   // to be started.
64   // We also do not have a way to verify that HTTP3 has been tried, because
65   // the fallback is automatic, so try a couple of times.
66   do {
67     // We need to close HTTP2 connections, otherwise our connection pooling
68     // will dispatch the request over already existing HTTP2 connection.
69     Services.obs.notifyObservers(null, "net:prune-all-connections");
70     let chan = makeChan();
71     let listener = new CheckOnlyHttp2Listener();
72     await altsvcSetupPromise(chan, listener);
73     result++;
74   } while (result < 5);
75 });
77 let CheckOnlyHttp2Listener = function () {};
79 CheckOnlyHttp2Listener.prototype = {
80   onStartRequest: function testOnStartRequest() {},
82   onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
83     read_stream(stream, cnt);
84   },
86   onStopRequest: function testOnStopRequest(request, status) {
87     Assert.equal(status, Cr.NS_OK);
88     let httpVersion = "";
89     try {
90       httpVersion = request.protocolVersion;
91     } catch (e) {}
92     Assert.equal(httpVersion, "h2");
93     this.finish();
94   },
97 add_task(async function test_no_http3_after_error() {
98   let chan = makeChan();
99   let listener = new CheckOnlyHttp2Listener();
100   await altsvcSetupPromise(chan, listener);
103 // also after all connections are closed.
104 add_task(async function test_no_http3_after_error2() {
105   Services.obs.notifyObservers(null, "net:prune-all-connections");
106   let chan = makeChan();
107   let listener = new CheckOnlyHttp2Listener();
108   await altsvcSetupPromise(chan, listener);