]> git.proxmox.com Git - mirror_novnc.git/blame - tests/test.websock.js
Add eslint and fix reported issues
[mirror_novnc.git] / tests / test.websock.js
CommitLineData
2cccf753
SR
1var expect = chai.expect;
2
dfae3209
SR
3import Websock from '../core/websock.js';
4import FakeWebSocket from './fake.websocket.js';
5
0aaf59c2 6import sinon from '../vendor/sinon.js';
dfae3209 7
2cccf753
SR
8describe('Websock', function() {
9 "use strict";
10
11 describe('Queue methods', function () {
12 var sock;
38781d93 13 var RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
2cccf753
SR
14
15 beforeEach(function () {
16 sock = new Websock();
38781d93
SR
17 // skip init
18 sock._allocate_buffers();
19 sock._rQ.set(RQ_TEMPLATE);
20 sock._rQlen = RQ_TEMPLATE.length;
2cccf753
SR
21 });
22 describe('rQlen', function () {
23 it('should return the length of the receive queue', function () {
24 sock.set_rQi(0);
25
26 expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length);
27 });
28
29 it("should return the proper length if we read some from the receive queue", function () {
30 sock.set_rQi(1);
31
32 expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length - 1);
33 });
34 });
35
36 describe('rQpeek8', function () {
37 it('should peek at the next byte without poping it off the queue', function () {
38 var bef_len = sock.rQlen();
39 var peek = sock.rQpeek8();
40 expect(sock.rQpeek8()).to.equal(peek);
41 expect(sock.rQlen()).to.equal(bef_len);
42 });
43 });
44
45 describe('rQshift8', function () {
46 it('should pop a single byte from the receive queue', function () {
47 var peek = sock.rQpeek8();
48 var bef_len = sock.rQlen();
49 expect(sock.rQshift8()).to.equal(peek);
50 expect(sock.rQlen()).to.equal(bef_len - 1);
51 });
52 });
53
2cccf753
SR
54 describe('rQshift16', function () {
55 it('should pop two bytes from the receive queue and return a single number', function () {
56 var bef_len = sock.rQlen();
57 var expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1];
58 expect(sock.rQshift16()).to.equal(expected);
59 expect(sock.rQlen()).to.equal(bef_len - 2);
60 });
61 });
62
63 describe('rQshift32', function () {
64 it('should pop four bytes from the receive queue and return a single number', function () {
65 var bef_len = sock.rQlen();
66 var expected = (RQ_TEMPLATE[0] << 24) +
67 (RQ_TEMPLATE[1] << 16) +
68 (RQ_TEMPLATE[2] << 8) +
69 RQ_TEMPLATE[3];
70 expect(sock.rQshift32()).to.equal(expected);
71 expect(sock.rQlen()).to.equal(bef_len - 4);
72 });
73 });
74
75 describe('rQshiftStr', function () {
76 it('should shift the given number of bytes off of the receive queue and return a string', function () {
77 var bef_len = sock.rQlen();
78 var bef_rQi = sock.get_rQi();
79 var shifted = sock.rQshiftStr(3);
80 expect(shifted).to.be.a('string');
38781d93 81 expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3))));
2cccf753
SR
82 expect(sock.rQlen()).to.equal(bef_len - 3);
83 });
84
85 it('should shift the entire rest of the queue off if no length is given', function () {
86 sock.rQshiftStr();
87 expect(sock.rQlen()).to.equal(0);
88 });
89 });
90
91 describe('rQshiftBytes', function () {
92 it('should shift the given number of bytes of the receive queue and return an array', function () {
93 var bef_len = sock.rQlen();
94 var bef_rQi = sock.get_rQi();
95 var shifted = sock.rQshiftBytes(3);
38781d93
SR
96 expect(shifted).to.be.an.instanceof(Uint8Array);
97 expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3));
2cccf753
SR
98 expect(sock.rQlen()).to.equal(bef_len - 3);
99 });
100
101 it('should shift the entire rest of the queue off if no length is given', function () {
102 sock.rQshiftBytes();
103 expect(sock.rQlen()).to.equal(0);
104 });
105 });
106
107 describe('rQslice', function () {
108 beforeEach(function () {
109 sock.set_rQi(0);
110 });
111
112 it('should not modify the receive queue', function () {
113 var bef_len = sock.rQlen();
114 sock.rQslice(0, 2);
115 expect(sock.rQlen()).to.equal(bef_len);
116 });
117
118 it('should return an array containing the given slice of the receive queue', function () {
119 var sl = sock.rQslice(0, 2);
38781d93
SR
120 expect(sl).to.be.an.instanceof(Uint8Array);
121 expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
2cccf753
SR
122 });
123
124 it('should use the rest of the receive queue if no end is given', function () {
125 var sl = sock.rQslice(1);
126 expect(sl).to.have.length(RQ_TEMPLATE.length - 1);
38781d93 127 expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1));
2cccf753
SR
128 });
129
130 it('should take the current rQi in to account', function () {
131 sock.set_rQi(1);
38781d93 132 expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
2cccf753
SR
133 });
134 });
135
136 describe('rQwait', function () {
137 beforeEach(function () {
138 sock.set_rQi(0);
139 });
140
141 it('should return true if there are not enough bytes in the receive queue', function () {
142 expect(sock.rQwait('hi', RQ_TEMPLATE.length + 1)).to.be.true;
143 });
144
145 it('should return false if there are enough bytes in the receive queue', function () {
146 expect(sock.rQwait('hi', RQ_TEMPLATE.length)).to.be.false;
147 });
148
149 it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
150 sock.set_rQi(5);
151 expect(sock.rQwait('hi', RQ_TEMPLATE.length, 4)).to.be.true;
152 expect(sock.get_rQi()).to.equal(1);
153 });
154
155 it('should raise an error if we try to go back more than possible', function () {
156 sock.set_rQi(5);
157 expect(function () { sock.rQwait('hi', RQ_TEMPLATE.length, 6); }).to.throw(Error);
158 });
159
160 it('should not reduce rQi if there are enough bytes', function () {
161 sock.set_rQi(5);
162 sock.rQwait('hi', 1, 6);
163 expect(sock.get_rQi()).to.equal(5);
164 });
165 });
166
167 describe('flush', function () {
168 beforeEach(function () {
169 sock._websocket = {
170 send: sinon.spy()
171 };
172 });
173
c4482d2d 174 it('should actually send on the websocket', function () {
2cccf753 175 sock._websocket.bufferedAmount = 8;
cf0623ff 176 sock._websocket.readyState = WebSocket.OPEN
9ff86fb7
SR
177 sock._sQ = new Uint8Array([1, 2, 3]);
178 sock._sQlen = 3;
2cccf753
SR
179 var encoded = sock._encode_message();
180
181 sock.flush();
182 expect(sock._websocket.send).to.have.been.calledOnce;
183 expect(sock._websocket.send).to.have.been.calledWith(encoded);
184 });
185
2cccf753 186 it('should not call send if we do not have anything queued up', function () {
9ff86fb7 187 sock._sQlen = 0;
2cccf753
SR
188 sock._websocket.bufferedAmount = 8;
189
190 sock.flush();
191
192 expect(sock._websocket.send).not.to.have.been.called;
193 });
2cccf753
SR
194 });
195
196 describe('send', function () {
197 beforeEach(function () {
198 sock.flush = sinon.spy();
199 });
200
201 it('should add to the send queue', function () {
202 sock.send([1, 2, 3]);
203 var sq = sock.get_sQ();
9ff86fb7 204 expect(new Uint8Array(sq.buffer, sock._sQlen - 3, 3)).to.array.equal(new Uint8Array([1, 2, 3]));
2cccf753
SR
205 });
206
207 it('should call flush', function () {
208 sock.send([1, 2, 3]);
209 expect(sock.flush).to.have.been.calledOnce;
210 });
211 });
212
213 describe('send_string', function () {
214 beforeEach(function () {
215 sock.send = sinon.spy();
216 });
217
218 it('should call send after converting the string to an array', function () {
219 sock.send_string("\x01\x02\x03");
220 expect(sock.send).to.have.been.calledWith([1, 2, 3]);
221 });
222 });
223 });
224
225 describe('lifecycle methods', function () {
226 var old_WS;
227 before(function () {
228 old_WS = WebSocket;
229 });
230
231 var sock;
232 beforeEach(function () {
8727f598
JD
233 sock = new Websock();
234 // eslint-disable-next-line no-global-assign
235 WebSocket = sinon.spy();
236 WebSocket.OPEN = old_WS.OPEN;
237 WebSocket.CONNECTING = old_WS.CONNECTING;
238 WebSocket.CLOSING = old_WS.CLOSING;
239 WebSocket.CLOSED = old_WS.CLOSED;
240
241 WebSocket.prototype.binaryType = 'arraybuffer';
2cccf753
SR
242 });
243
244 describe('opening', function () {
245 it('should pick the correct protocols if none are given' , function () {
246
247 });
248
249 it('should open the actual websocket', function () {
38781d93
SR
250 sock.open('ws://localhost:8675', 'binary');
251 expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary');
2cccf753
SR
252 });
253
2cccf753
SR
254 // it('should initialize the event handlers')?
255 });
256
257 describe('closing', function () {
258 beforeEach(function () {
259 sock.open('ws://');
260 sock._websocket.close = sinon.spy();
261 });
262
263 it('should close the actual websocket if it is open', function () {
264 sock._websocket.readyState = WebSocket.OPEN;
265 sock.close();
266 expect(sock._websocket.close).to.have.been.calledOnce;
267 });
268
269 it('should close the actual websocket if it is connecting', function () {
270 sock._websocket.readyState = WebSocket.CONNECTING;
271 sock.close();
272 expect(sock._websocket.close).to.have.been.calledOnce;
273 });
274
275 it('should not try to close the actual websocket if closing', function () {
276 sock._websocket.readyState = WebSocket.CLOSING;
277 sock.close();
278 expect(sock._websocket.close).not.to.have.been.called;
279 });
280
281 it('should not try to close the actual websocket if closed', function () {
282 sock._websocket.readyState = WebSocket.CLOSED;
283 sock.close();
284 expect(sock._websocket.close).not.to.have.been.called;
285 });
286
287 it('should reset onmessage to not call _recv_message', function () {
288 sinon.spy(sock, '_recv_message');
289 sock.close();
290 sock._websocket.onmessage(null);
291 try {
292 expect(sock._recv_message).not.to.have.been.called;
293 } finally {
294 sock._recv_message.restore();
295 }
296 });
297 });
298
299 describe('event handlers', function () {
300 beforeEach(function () {
301 sock._recv_message = sinon.spy();
302 sock.on('open', sinon.spy());
303 sock.on('close', sinon.spy());
304 sock.on('error', sinon.spy());
305 sock.open('ws://');
306 });
307
308 it('should call _recv_message on a message', function () {
309 sock._websocket.onmessage(null);
310 expect(sock._recv_message).to.have.been.calledOnce;
311 });
312
2cccf753
SR
313 it('should call the open event handler on opening', function () {
314 sock._websocket.onopen();
315 expect(sock._eventHandlers.open).to.have.been.calledOnce;
316 });
317
318 it('should call the close event handler on closing', function () {
319 sock._websocket.onclose();
320 expect(sock._eventHandlers.close).to.have.been.calledOnce;
321 });
322
323 it('should call the error event handler on error', function () {
324 sock._websocket.onerror();
325 expect(sock._eventHandlers.error).to.have.been.calledOnce;
326 });
327 });
328
329 after(function () {
8727f598 330 // eslint-disable-next-line no-global-assign
2cccf753
SR
331 WebSocket = old_WS;
332 });
333 });
334
335 describe('WebSocket Receiving', function () {
336 var sock;
337 beforeEach(function () {
338 sock = new Websock();
38781d93 339 sock._allocate_buffers();
2cccf753
SR
340 });
341
342 it('should support adding binary Uint8Array data to the receive queue', function () {
343 var msg = { data: new Uint8Array([1, 2, 3]) };
344 sock._mode = 'binary';
345 sock._recv_message(msg);
346 expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
347 });
348
349 it('should call the message event handler if present', function () {
350 sock._eventHandlers.message = sinon.spy();
38781d93
SR
351 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
352 sock._mode = 'binary';
2cccf753
SR
353 sock._recv_message(msg);
354 expect(sock._eventHandlers.message).to.have.been.calledOnce;
355 });
356
357 it('should not call the message event handler if there is nothing in the receive queue', function () {
358 sock._eventHandlers.message = sinon.spy();
38781d93
SR
359 var msg = { data: new Uint8Array([]).buffer };
360 sock._mode = 'binary';
2cccf753
SR
361 sock._recv_message(msg);
362 expect(sock._eventHandlers.message).not.to.have.been.called;
363 });
364
365 it('should compact the receive queue', function () {
366 // NB(sross): while this is an internal implementation detail, it's important to
367 // test, otherwise the receive queue could become very large very quickly
38781d93
SR
368 sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
369 sock._rQlen = 6;
2cccf753
SR
370 sock.set_rQi(6);
371 sock._rQmax = 3;
38781d93
SR
372 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
373 sock._mode = 'binary';
2cccf753 374 sock._recv_message(msg);
38781d93 375 expect(sock._rQlen).to.equal(3);
2cccf753
SR
376 expect(sock.get_rQi()).to.equal(0);
377 });
378
40037b6a
SR
379 it('should automatically resize the receive queue if the incoming message is too large', function () {
380 sock._rQ = new Uint8Array(20);
381 sock._rQlen = 0;
382 sock.set_rQi(0);
383 sock._rQbufferSize = 20;
384 sock._rQmax = 2;
385 var msg = { data: new Uint8Array(30).buffer };
386 sock._mode = 'binary';
387 sock._recv_message(msg);
388 expect(sock._rQlen).to.equal(30);
389 expect(sock.get_rQi()).to.equal(0);
390 expect(sock._rQ.length).to.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
391 });
2cccf753
SR
392 });
393
394 describe('Data encoding', function () {
395 before(function () { FakeWebSocket.replace(); });
396 after(function () { FakeWebSocket.restore(); });
397
398 describe('as binary data', function () {
399 var sock;
400 beforeEach(function () {
401 sock = new Websock();
402 sock.open('ws://', 'binary');
403 sock._websocket._open();
404 });
405
9ff86fb7
SR
406 it('should only send the send queue up to the send queue length', function () {
407 sock._sQ = new Uint8Array([1, 2, 3, 4, 5]);
408 sock._sQlen = 3;
409 var res = sock._encode_message();
410 expect(res).to.array.equal(new Uint8Array([1, 2, 3]));
2cccf753
SR
411 });
412
413 it('should properly pass the encoded data off to the actual WebSocket', function () {
414 sock.send([1, 2, 3]);
9ff86fb7 415 expect(sock._websocket._get_sent_data()).to.array.equal(new Uint8Array([1, 2, 3]));
2cccf753
SR
416 });
417 });
2cccf753
SR
418 });
419});