tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_websocket_with_h3_active.js (2670B)


      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";
      5 
      6 registerCleanupFunction(async () => {
      7  http3_clear_prefs();
      8 });
      9 
     10 let wssUri;
     11 let httpsUri;
     12 
     13 add_task(async function pre_setup() {
     14  let h2Port = Services.env.get("MOZHTTP2_PORT");
     15  Assert.notEqual(h2Port, null);
     16  Assert.notEqual(h2Port, "");
     17 
     18  wssUri = "wss://foo.example.com:" + h2Port + "/websocket";
     19  httpsUri = "https://foo.example.com:" + h2Port + "/";
     20  Services.prefs.setBoolPref("network.http.http3.support_version1", true);
     21 });
     22 
     23 add_task(async function setup() {
     24  await http3_setup_tests("h3", true);
     25 });
     26 
     27 WebSocketListener.prototype = {
     28  onAcknowledge() {},
     29  onBinaryMessageAvailable() {},
     30  onMessageAvailable() {},
     31  onServerClose() {},
     32  onStart() {
     33    this.finish();
     34  },
     35  onStop() {},
     36 };
     37 
     38 function makeH2Chan() {
     39  let chan = NetUtil.newChannel({
     40    uri: httpsUri,
     41    loadUsingSystemPrincipal: true,
     42  }).QueryInterface(Ci.nsIHttpChannel);
     43  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
     44  return chan;
     45 }
     46 
     47 add_task(async function open_wss_when_h3_is_active() {
     48  // Make an active connection using HTTP/3
     49  let chanHttp1 = makeH2Chan(httpsUri);
     50  await new Promise(resolve => {
     51    chanHttp1.asyncOpen(
     52      new ChannelListener(request => {
     53        let httpVersion = "";
     54        try {
     55          httpVersion = request.protocolVersion;
     56        } catch (e) {}
     57        Assert.equal(httpVersion, "h3");
     58        resolve();
     59      })
     60    );
     61  });
     62 
     63  // Now try to connect ot a WebSocket on the same port -> this should not loop
     64  // see bug 1717360.
     65  let chan = Cc["@mozilla.org/network/protocol;1?name=wss"].createInstance(
     66    Ci.nsIWebSocketChannel
     67  );
     68  chan.initLoadInfo(
     69    null, // aLoadingNode
     70    Services.scriptSecurityManager.getSystemPrincipal(),
     71    null, // aTriggeringPrincipal
     72    Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
     73    Ci.nsIContentPolicy.TYPE_WEBSOCKET
     74  );
     75 
     76  var uri = Services.io.newURI(wssUri);
     77  var wsListener = new WebSocketListener();
     78  await new Promise(resolve => {
     79    wsListener.finish = resolve;
     80    chan.asyncOpen(uri, wssUri, {}, 0, wsListener, null);
     81  });
     82 
     83  // Try to use https protocol, it should sttill use HTTP/3
     84  let chanHttp2 = makeH2Chan(httpsUri);
     85  await new Promise(resolve => {
     86    chanHttp2.asyncOpen(
     87      new ChannelListener(request => {
     88        let httpVersion = "";
     89        try {
     90          httpVersion = request.protocolVersion;
     91        } catch (e) {}
     92        Assert.equal(httpVersion, "h3");
     93        resolve();
     94      })
     95    );
     96  });
     97 });