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