tor-browser

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

test_websocket_500k.js (5983B)


      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 /* import-globals-from head_cache.js */
      8 /* import-globals-from head_cookies.js */
      9 /* import-globals-from head_channels.js */
     10 
     11 ChromeUtils.defineESModuleGetters(this, {
     12  ObjectUtils: "resource://gre/modules/ObjectUtils.sys.mjs",
     13 });
     14 
     15 const {
     16  WebSocketConnection,
     17  NodeWebSocketHttp2Server,
     18  NodeWebSocketServer,
     19  NodeHTTPProxyServer,
     20  NodeHTTPSProxyServer,
     21  NodeHTTP2ProxyServer,
     22 } = ChromeUtils.importESModule("resource://testing-common/NodeServer.sys.mjs");
     23 
     24 add_setup(async function () {
     25  Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
     26 
     27  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     28    Ci.nsIX509CertDB
     29  );
     30  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     31  addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
     32 });
     33 
     34 registerCleanupFunction(async () => {
     35  Services.prefs.clearUserPref("network.dns.localDomains");
     36 });
     37 
     38 async function channelOpenPromise(url, msg) {
     39  let conn = new WebSocketConnection();
     40  await conn.open(url);
     41  conn.send(msg);
     42  let res = await conn.receiveMessages();
     43  conn.close();
     44  let { status } = await conn.finished();
     45  return [status, res];
     46 }
     47 
     48 async function sendDataAndCheck(url) {
     49  let data = "a".repeat(500000);
     50  let [status, res] = await channelOpenPromise(url, data);
     51  Assert.equal(status, Cr.NS_OK);
     52  // Use "ObjectUtils.deepEqual" directly to avoid printing data.
     53  Assert.ok(ObjectUtils.deepEqual(res, [data]));
     54 }
     55 
     56 add_task(async function test_h2_websocket_500k() {
     57  Services.prefs.setBoolPref("network.http.http2.websockets", true);
     58  let wss = new NodeWebSocketHttp2Server();
     59  await wss.start();
     60  registerCleanupFunction(async () => wss.stop());
     61 
     62  Assert.notEqual(wss.port(), null);
     63  await wss.registerMessageHandler((data, ws) => {
     64    ws.send(data);
     65  });
     66  let url = `wss://foo.example.com:${wss.port()}`;
     67  await sendDataAndCheck(url);
     68 });
     69 
     70 // h1.1 direct
     71 add_task(async function test_h1_websocket_direct() {
     72  let wss = new NodeWebSocketServer();
     73  await wss.start();
     74  registerCleanupFunction(async () => wss.stop());
     75  Assert.notEqual(wss.port(), null);
     76  await wss.registerMessageHandler((data, ws) => {
     77    ws.send(data);
     78  });
     79  let url = `wss://localhost:${wss.port()}`;
     80  await sendDataAndCheck(url);
     81 });
     82 
     83 // ws h1.1 with insecure h1.1 proxy
     84 add_task(async function test_h1_ws_with_h1_insecure_proxy() {
     85  Services.prefs.setBoolPref("network.http.http2.websockets", false);
     86  let proxy = new NodeHTTPProxyServer();
     87  await proxy.start();
     88 
     89  let wss = new NodeWebSocketServer();
     90  await wss.start();
     91 
     92  registerCleanupFunction(async () => {
     93    await wss.stop();
     94    await proxy.stop();
     95  });
     96 
     97  Assert.notEqual(wss.port(), null);
     98 
     99  await wss.registerMessageHandler((data, ws) => {
    100    ws.send(data);
    101  });
    102  let url = `wss://localhost:${wss.port()}`;
    103  await sendDataAndCheck(url);
    104 });
    105 
    106 // h1 server with secure h1.1 proxy
    107 add_task(async function test_h1_ws_with_secure_h1_proxy() {
    108  let proxy = new NodeHTTPSProxyServer();
    109  await proxy.start();
    110 
    111  let wss = new NodeWebSocketServer();
    112  await wss.start();
    113  registerCleanupFunction(async () => {
    114    await wss.stop();
    115    await proxy.stop();
    116  });
    117 
    118  Assert.notEqual(wss.port(), null);
    119  await wss.registerMessageHandler((data, ws) => {
    120    ws.send(data);
    121  });
    122 
    123  let url = `wss://localhost:${wss.port()}`;
    124  await sendDataAndCheck(url);
    125 
    126  await proxy.stop();
    127 });
    128 
    129 // ws h1.1 with h2 proxy
    130 add_task(async function test_h1_ws_with_h2_proxy() {
    131  Services.prefs.setBoolPref("network.http.http2.websockets", false);
    132 
    133  let proxy = new NodeHTTP2ProxyServer();
    134  await proxy.start();
    135 
    136  let wss = new NodeWebSocketServer();
    137  await wss.start();
    138 
    139  registerCleanupFunction(async () => {
    140    await wss.stop();
    141    await proxy.stop();
    142  });
    143 
    144  Assert.notEqual(wss.port(), null);
    145  await wss.registerMessageHandler((data, ws) => {
    146    ws.send(data);
    147  });
    148 
    149  let url = `wss://localhost:${wss.port()}`;
    150  await sendDataAndCheck(url);
    151 
    152  await proxy.stop();
    153 });
    154 
    155 // ws h2 with insecure h1.1 proxy
    156 add_task(async function test_h2_ws_with_h1_insecure_proxy() {
    157  Services.prefs.setBoolPref("network.http.http2.websockets", true);
    158 
    159  let proxy = new NodeHTTPProxyServer();
    160  await proxy.start();
    161 
    162  let wss = new NodeWebSocketHttp2Server();
    163  await wss.start();
    164 
    165  registerCleanupFunction(async () => {
    166    await wss.stop();
    167    await proxy.stop();
    168  });
    169 
    170  Assert.notEqual(wss.port(), null);
    171  await wss.registerMessageHandler((data, ws) => {
    172    ws.send(data);
    173  });
    174 
    175  let url = `wss://localhost:${wss.port()}`;
    176  await sendDataAndCheck(url);
    177 
    178  await proxy.stop();
    179 });
    180 
    181 add_task(async function test_h2_ws_with_h1_secure_proxy() {
    182  Services.prefs.setBoolPref("network.http.http2.websockets", true);
    183 
    184  let proxy = new NodeHTTPSProxyServer();
    185  await proxy.start();
    186 
    187  let wss = new NodeWebSocketHttp2Server();
    188  await wss.start();
    189 
    190  registerCleanupFunction(async () => {
    191    await wss.stop();
    192    await proxy.stop();
    193  });
    194 
    195  Assert.notEqual(wss.port(), null);
    196  await wss.registerMessageHandler((data, ws) => {
    197    ws.send(data);
    198  });
    199 
    200  let url = `wss://localhost:${wss.port()}`;
    201  await sendDataAndCheck(url);
    202 
    203  await proxy.stop();
    204 });
    205 
    206 // ws h2 with secure h2 proxy
    207 add_task(async function test_h2_ws_with_h2_proxy() {
    208  Services.prefs.setBoolPref("network.http.http2.websockets", true);
    209 
    210  let proxy = new NodeHTTP2ProxyServer();
    211  await proxy.start(); // start and register proxy "filter"
    212 
    213  let wss = new NodeWebSocketHttp2Server();
    214  await wss.start(); // init port
    215 
    216  registerCleanupFunction(async () => {
    217    await wss.stop();
    218    await proxy.stop();
    219  });
    220 
    221  Assert.notEqual(wss.port(), null);
    222  await wss.registerMessageHandler((data, ws) => {
    223    ws.send(data);
    224  });
    225 
    226  let url = `wss://localhost:${wss.port()}`;
    227  await sendDataAndCheck(url);
    228 
    229  await proxy.stop();
    230 });