|
| 1 | +const uWS = require('uWebSockets.js'); |
| 2 | +const WebSocket = require('ws'); |
| 3 | + |
| 4 | +const expectedCloseReasons = { |
| 5 | + invalidFrame: 'Received invalid WebSocket frame', |
| 6 | + tcpFinBeforeClose: 'Received TCP FIN before WebSocket close frame', |
| 7 | +}; |
| 8 | + |
| 9 | +let closeTestsPassed = true; |
| 10 | + |
| 11 | +// Create a minimal uWebSockets.js server |
| 12 | +const port = 9001; |
| 13 | + |
| 14 | +const server = uWS.App().ws('/*', { |
| 15 | + open: (ws) => { |
| 16 | + console.log('A WebSocket connected!'); |
| 17 | + }, |
| 18 | + message: (ws, message, isBinary) => { |
| 19 | + console.log('Received message:', message); |
| 20 | + }, |
| 21 | + close: (ws, code, message) => { |
| 22 | + const reason = Buffer.from(message).toString(); |
| 23 | + console.log('WebSocket closed with code:', code, 'and reason:', reason); |
| 24 | + |
| 25 | + if (code === 1006) { |
| 26 | + if (reason === expectedCloseReasons.invalidFrame) { |
| 27 | + console.log('Test passed: Invalid WebSocket frame'); |
| 28 | + } else if (reason === expectedCloseReasons.tcpFinBeforeClose) { |
| 29 | + console.log('Test passed: TCP FIN before WebSocket close frame'); |
| 30 | + } else { |
| 31 | + console.error('Test failed: Unexpected close reason:', reason); |
| 32 | + closeTestsPassed = false; |
| 33 | + } |
| 34 | + } else { |
| 35 | + console.error('Test failed: Unexpected close code:', code); |
| 36 | + closeTestsPassed = false; |
| 37 | + } |
| 38 | + } |
| 39 | +}).listen(port, (token) => { |
| 40 | + if (token) { |
| 41 | + console.log('Listening to port', port); |
| 42 | + |
| 43 | + const triggerClosure = (malformedData, useEnd) => { |
| 44 | + const client = new WebSocket(`ws://localhost:${port}`); |
| 45 | + |
| 46 | + client.on('open', () => { |
| 47 | + console.log('Client connected to server'); |
| 48 | + |
| 49 | + if (useEnd) { |
| 50 | + // Trigger "Received TCP FIN before WebSocket close frame" |
| 51 | + client._socket.end(); |
| 52 | + } else { |
| 53 | + // Trigger "Received invalid WebSocket frame" |
| 54 | + client._socket.write(malformedData); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + client.on('close', (code, reason) => { |
| 59 | + console.log('Client WebSocket closed with code:', code, 'and reason:', reason); |
| 60 | + }); |
| 61 | + |
| 62 | + client.on('error', (error) => { |
| 63 | + console.log('Client encountered error:', error); |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + // Test 1: Trigger invalid WebSocket frame |
| 68 | + const malformedData = Buffer.from([0xFF, 0x80, 0x00, 0x00, 0x00, 0x01]); |
| 69 | + triggerClosure(malformedData, false); |
| 70 | + |
| 71 | + // Wait for a short period before triggering the next test to ensure the server handles sequentially |
| 72 | + setTimeout(() => { |
| 73 | + // Test 2: Trigger TCP FIN before WebSocket close frame |
| 74 | + triggerClosure(null, true); |
| 75 | + }, 1000); // Adjust the delay as necessary |
| 76 | + |
| 77 | + // Allow some time for tests to run before exiting |
| 78 | + setTimeout(() => { |
| 79 | + if (closeTestsPassed) { |
| 80 | + console.log('All tests passed.'); |
| 81 | + process.exit(0); |
| 82 | + } else { |
| 83 | + console.error('Some tests failed.'); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | + }, 3000); // Adjust the delay as necessary |
| 87 | + } else { |
| 88 | + console.log('Failed to listen to port', port); |
| 89 | + process.exit(1); |
| 90 | + } |
| 91 | +}); |
0 commit comments