|
3 | 3 | const assert = require('node:assert') |
4 | 4 | const { test, after } = require('node:test') |
5 | 5 | const { WebSocketServer } = require('ws') |
6 | | -const { WebSocket } = require('../..') |
| 6 | +const { WebSocket, Agent } = require('../..') |
7 | 7 | const diagnosticsChannel = require('node:diagnostics_channel') |
8 | 8 |
|
9 | 9 | test('Fragmented frame with a ping frame in the middle of it', () => { |
@@ -40,3 +40,184 @@ test('Fragmented frame with a ping frame in the middle of it', () => { |
40 | 40 | }) |
41 | 41 | }) |
42 | 42 | }) |
| 43 | + |
| 44 | +test('Too many fragments (uncompressed)', (t, done) => { |
| 45 | + function maybeDone () { |
| 46 | + if (++maybeDone.callCount === 2) { |
| 47 | + agent.close() |
| 48 | + server.close(done) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + maybeDone.callCount = 0 |
| 53 | + |
| 54 | + const agent = new Agent({ |
| 55 | + webSocket: { |
| 56 | + maxFragments: 3 |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + const server = new WebSocketServer({ port: 0 }, () => { |
| 61 | + const { port } = server.address() |
| 62 | + const client = new WebSocket(`ws://127.0.0.1:${port}`, { |
| 63 | + dispatcher: agent |
| 64 | + }) |
| 65 | + |
| 66 | + client.addEventListener('error', () => { |
| 67 | + assert.ok(true) |
| 68 | + }) |
| 69 | + |
| 70 | + client.addEventListener('close', (event) => { |
| 71 | + assert.strictEqual(event.code, 1006) |
| 72 | + maybeDone() |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + server.on('connection', (ws) => { |
| 77 | + ws.on('close', (code, reason) => { |
| 78 | + assert.strictEqual(code, 1008) |
| 79 | + assert.strictEqual(reason.toString(), 'Too many message fragments') |
| 80 | + maybeDone() |
| 81 | + }) |
| 82 | + |
| 83 | + const fragment = Buffer.from('a') |
| 84 | + const options = { fin: false } |
| 85 | + |
| 86 | + ws.send(fragment, options) |
| 87 | + ws.send(fragment, options) |
| 88 | + ws.send(fragment, options) |
| 89 | + ws.send(fragment, options) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +test('Too many fragments (compressed)', (t, done) => { |
| 94 | + function maybeDone () { |
| 95 | + if (++maybeDone.callCount === 2) { |
| 96 | + agent.close() |
| 97 | + server.close(done) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + maybeDone.callCount = 0 |
| 102 | + |
| 103 | + const agent = new Agent({ |
| 104 | + webSocket: { |
| 105 | + maxFragments: 3 |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + const server = new WebSocketServer({ |
| 110 | + perMessageDeflate: { threshold: 0 }, |
| 111 | + port: 0 |
| 112 | + }, () => { |
| 113 | + const { port } = server.address() |
| 114 | + const client = new WebSocket(`ws://127.0.0.1:${port}`, { |
| 115 | + dispatcher: agent |
| 116 | + }) |
| 117 | + |
| 118 | + client.addEventListener('error', () => { |
| 119 | + assert.ok(true) |
| 120 | + }) |
| 121 | + |
| 122 | + client.addEventListener('close', (event) => { |
| 123 | + assert.strictEqual(event.code, 1006) |
| 124 | + maybeDone() |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + server.on('connection', (ws) => { |
| 129 | + ws.on('close', (code, reason) => { |
| 130 | + assert.strictEqual(code, 1008) |
| 131 | + assert.strictEqual(reason.toString(), 'Too many message fragments') |
| 132 | + maybeDone() |
| 133 | + }) |
| 134 | + |
| 135 | + const fragment = Buffer.from('a') |
| 136 | + const options = { fin: false } |
| 137 | + |
| 138 | + ws.send(fragment, options) |
| 139 | + ws.send(fragment, options) |
| 140 | + ws.send(fragment, options) |
| 141 | + ws.send(fragment, options) |
| 142 | + }) |
| 143 | +}) |
| 144 | + |
| 145 | +test('Empty first fragment followed by non-empty continuation delivers the message', () => { |
| 146 | + // RFC 6455 §5.4 allows zero-byte fragments. A conforming server that opens |
| 147 | + // a fragmented message with an empty frame must be honored: the parser must |
| 148 | + // recognize the in-progress fragmented message when the continuation arrives. |
| 149 | + const server = new WebSocketServer({ port: 0 }) |
| 150 | + |
| 151 | + server.on('connection', (ws) => { |
| 152 | + ws.send('', { fin: false }) |
| 153 | + ws.send('hello', { fin: true }) |
| 154 | + }) |
| 155 | + |
| 156 | + after(() => { |
| 157 | + for (const client of server.clients) { |
| 158 | + client.close() |
| 159 | + } |
| 160 | + |
| 161 | + server.close() |
| 162 | + }) |
| 163 | + |
| 164 | + const ws = new WebSocket(`ws://localhost:${server.address().port}`) |
| 165 | + |
| 166 | + return new Promise((resolve) => { |
| 167 | + ws.addEventListener('message', ({ data }) => { |
| 168 | + assert.strictEqual(data, 'hello') |
| 169 | + |
| 170 | + ws.close() |
| 171 | + resolve() |
| 172 | + }) |
| 173 | + }) |
| 174 | +}) |
| 175 | + |
| 176 | +test('Too many empty fragments triggers close 1008', (t, done) => { |
| 177 | + function maybeDone () { |
| 178 | + if (++maybeDone.callCount === 2) { |
| 179 | + agent.close() |
| 180 | + server.close(done) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + maybeDone.callCount = 0 |
| 185 | + |
| 186 | + const agent = new Agent({ |
| 187 | + webSocket: { |
| 188 | + maxFragments: 3 |
| 189 | + } |
| 190 | + }) |
| 191 | + |
| 192 | + const server = new WebSocketServer({ port: 0 }, () => { |
| 193 | + const { port } = server.address() |
| 194 | + const client = new WebSocket(`ws://127.0.0.1:${port}`, { |
| 195 | + dispatcher: agent |
| 196 | + }) |
| 197 | + |
| 198 | + client.addEventListener('error', () => { |
| 199 | + assert.ok(true) |
| 200 | + }) |
| 201 | + |
| 202 | + client.addEventListener('close', (event) => { |
| 203 | + assert.strictEqual(event.code, 1006) |
| 204 | + maybeDone() |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + server.on('connection', (ws) => { |
| 209 | + ws.on('close', (code, reason) => { |
| 210 | + assert.strictEqual(code, 1008) |
| 211 | + assert.strictEqual(reason.toString(), 'Too many message fragments') |
| 212 | + maybeDone() |
| 213 | + }) |
| 214 | + |
| 215 | + const fragment = '' |
| 216 | + const options = { fin: false } |
| 217 | + |
| 218 | + ws.send(fragment, options) // Text frame fin=0, len=0 |
| 219 | + ws.send(fragment, options) // Continuation fin=0, len=0 |
| 220 | + ws.send(fragment, options) // Continuation fin=0, len=0 |
| 221 | + ws.send(fragment, options) // Continuation fin=0, len=0 |
| 222 | + }) |
| 223 | +}) |
0 commit comments