test_websocket_server_multiclient.js (4268B)
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_websocket.js */ 11 12 const { 13 NodeWebSocketServer, 14 NodeWebSocketHttp2Server, 15 WebSocketConnection, 16 NodeHTTPSProxyServer, 17 NodeHTTPProxyServer, 18 NodeHTTP2ProxyServer, 19 } = ChromeUtils.importESModule("resource://testing-common/NodeServer.sys.mjs"); 20 21 // These test should basically match the ones in test_websocket_server.js, 22 // but with multiple websocket clients making requests on the same server 23 24 const certOverrideService = Cc[ 25 "@mozilla.org/security/certoverride;1" 26 ].getService(Ci.nsICertOverrideService); 27 28 // setup 29 add_setup(async function setup() { 30 // turn off cert checking for these tests 31 certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData( 32 true 33 ); 34 }); 35 36 // append cleanup to cleanup queue 37 registerCleanupFunction(async () => { 38 certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData( 39 false 40 ); 41 Services.prefs.clearUserPref("network.http.http2.websockets"); 42 }); 43 44 async function spinup_and_check(proxy_kind, ws_kind) { 45 let ws_h2 = true; 46 if (ws_kind == NodeWebSocketServer) { 47 info("not h2 ws"); 48 ws_h2 = false; 49 } 50 Services.prefs.setBoolPref("network.http.http2.websockets", ws_h2); 51 52 let proxy; 53 if (proxy_kind) { 54 proxy = new proxy_kind(); 55 await proxy.start(); 56 registerCleanupFunction(async () => proxy.stop()); 57 } 58 59 let wss = new ws_kind(); 60 await wss.start(); 61 registerCleanupFunction(async () => wss.stop()); 62 63 Assert.notEqual(wss.port(), null); 64 await wss.registerMessageHandler((data, ws) => { 65 ws.send(data); 66 }); 67 let url = `wss://localhost:${wss.port()}`; 68 69 let conn1 = new WebSocketConnection(); 70 await conn1.open(url); 71 72 let conn2 = new WebSocketConnection(); 73 await conn2.open(url); 74 75 conn1.send("msg1"); 76 conn2.send("msg2"); 77 78 let mess2 = await conn2.receiveMessages(); 79 Assert.deepEqual(mess2, ["msg2"]); 80 81 conn1.send("msg1 again"); 82 let mess1 = []; 83 while (mess1.length < 2) { 84 // receive could return only the first or both replies. 85 mess1 = mess1.concat(await conn1.receiveMessages()); 86 } 87 Assert.deepEqual(mess1, ["msg1", "msg1 again"]); 88 89 conn1.close(); 90 conn2.close(); 91 Assert.deepEqual({ status: Cr.NS_OK }, await conn1.finished()); 92 Assert.deepEqual({ status: Cr.NS_OK }, await conn2.finished()); 93 await wss.stop(); 94 95 if (proxy_kind) { 96 await proxy.stop(); 97 } 98 } 99 100 // h1.1 direct 101 async function test_h1_websocket_direct() { 102 await spinup_and_check(null, NodeWebSocketServer); 103 } 104 105 // h2 direct 106 async function test_h2_websocket_direct() { 107 await spinup_and_check(null, NodeWebSocketHttp2Server); 108 } 109 110 // ws h1.1 with secure h1.1 proxy 111 async function test_h1_ws_with_secure_h1_proxy() { 112 await spinup_and_check(NodeHTTPSProxyServer, NodeWebSocketServer); 113 } 114 115 // ws h1.1 with insecure h1.1 proxy 116 async function test_h1_ws_with_insecure_h1_proxy() { 117 await spinup_and_check(NodeHTTPProxyServer, NodeWebSocketServer); 118 } 119 120 // ws h1.1 with h2 proxy 121 async function test_h1_ws_with_h2_proxy() { 122 await spinup_and_check(NodeHTTP2ProxyServer, NodeWebSocketServer); 123 } 124 125 // ws h2 with insecure h1.1 proxy 126 async function test_h2_ws_with_insecure_h1_proxy() { 127 await spinup_and_check(NodeHTTPProxyServer, NodeWebSocketHttp2Server); 128 } 129 130 // ws h2 with secure h1 proxy 131 async function test_h2_ws_with_secure_h1_proxy() { 132 await spinup_and_check(NodeHTTPSProxyServer, NodeWebSocketHttp2Server); 133 } 134 135 // ws h2 with secure h2 proxy 136 async function test_h2_ws_with_h2_proxy() { 137 await spinup_and_check(NodeHTTP2ProxyServer, NodeWebSocketHttp2Server); 138 } 139 140 add_task(test_h1_websocket_direct); 141 add_task(test_h2_websocket_direct); 142 add_task(test_h1_ws_with_secure_h1_proxy); 143 add_task(test_h1_ws_with_insecure_h1_proxy); 144 add_task(test_h1_ws_with_h2_proxy); 145 146 // any multi-client test with h2 websocket and any kind of proxy will fail/hang 147 add_task(test_h2_ws_with_insecure_h1_proxy); 148 add_task(test_h2_ws_with_secure_h1_proxy); 149 add_task(test_h2_ws_with_h2_proxy);